Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Miss__Aria

Pages: [1]
1
I'm trying to grab an integer from an array that presumes the map is at 0, 0. How can I transform this position so it is relative to what the map thinks is 0, 0.

That may have been confusing to read, it was confusing for me to write. I've been expanding on a tile_map and scratching my head at this one point.

Here's my source,
#pragma once
#include <SFML\Graphics.hpp>

class Tile_map : public sf::Drawable, public sf::Transformable
{
public:
  Tile_map(const std::string& tileset_path, sf::Vector2u tile_size, std::vector<int> tiles, unsigned int width, unsigned int height) :  tileset_path(tileset_path),   tile_size(tile_size),
                                                                                                                                        tiles(tiles),                 width(width),
                                                                                                                                        height(height) {}
  bool load();
  int get_current_tile(const sf::Vector2f& position);
private:
  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
  std::string tileset_path;
  sf::Vector2u tile_size;
  std::vector<int> tiles;
  unsigned int width, height;
  sf::VertexArray verticies;
  sf::Texture tileset;
};
 

#include "tilemap.h"

bool Tile_map::load()
{
  if (!tileset.loadFromFile(tileset_path))
    return false;

  verticies.setPrimitiveType(sf::Quads);
  //4 because each square has 4 points(vertexes)
  verticies.resize(width * height * 4);
  //this->setScale({ 2, 2 }); //Scales textures up from 16x16 to 32x32 so we can easily see them

  for (unsigned int i = 0; i < width; ++i) {
    for (unsigned int j = 0; j < height; ++j) {
      int tile_number = tiles[i + j * width] - 1; //-1 is for tiled, first sprite on sheet in tiled is 1 but when doing these calculations it's 0
      if (tile_number == -1) continue; // In tiled, 0 is transparent so 0 - 1 will hit this

      int tu = tile_number % (tileset.getSize().x / tile_size.x);
      int tv = tile_number / (tileset.getSize().x / tile_size.x);
      sf::Vertex* quad = &verticies[(i + j * width) * 4];

      quad[0].position = { (float)i * tile_size.x, (float)j * tile_size.y };
      quad[1].position = { (float)(i + 1) * tile_size.x, (float)j * tile_size.y };
      quad[2].position = { (float)(i + 1) * tile_size.x, (float)(j + 1) * tile_size.y };
      quad[3].position = { (float)i * tile_size.x, (float)(j + 1) * tile_size.y };

      quad[0].texCoords = { (float)tu * tile_size.x, (float)tv * tile_size.y };
      quad[1].texCoords = { (float)(tu + 1) * tile_size.x, (float)tv * tile_size.y };
      quad[2].texCoords = { (float)(tu + 1) * tile_size.x, (float)(tv + 1) * tile_size.y };
      quad[3].texCoords = { (float)tu * tile_size.x, (float)(tv + 1) * tile_size.y };
    }
  }

  return true;
}

int Tile_map::get_current_tile(const sf::Vector2f& position)
{
  auto newpos = position; //getTransform().transformPoint(position);
  return tiles[(newpos.x / tile_size.x) + (newpos.y / tile_size.y) * width] - 1;
}

void Tile_map::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
  states.transform *= getTransform();
  states.texture = &tileset;
  target.draw(verticies, states);
}

inside Tile_map::get_current_tile is where the issue is. Maybe a better explanation is the one I gave my friend:
&#9825; aria.: http://www.snorflake.com/u/Wcij.png before moving
&#9825; aria.: get_current_tile should return 1
&#9825; aria.: which it does
&#9825; aria.: http://www.snorflake.com/u/Ok8E.png
&#9825; aria.: after moving
&#9825; aria.: get_current_tile should return 0
&#9825; aria.: but it doesn't, because it's not relative to it's transformation; it returns 1.
 
that chat log is after this test case
Tile_map map{"characters.png", { 32, 32 }, level, 16, 8};
  map.load();
  printf("%d\n", map.get_current_tile({32 * 10, 32 * 6}));
  map.move({ 32 * 2, 32 * 2 });
  printf("%d\n", map.get_current_tile({ 32 * 10, 32 * 6 }));

EDIT: This paticular issue is easily fixed by keeping the map at 0, 0 and using views (http://www.sfml-dev.org/tutorials/2.3/graphics-view.php) but the main question still stands, how can I transform a vector with the opposite of the current getTransform() ?

2
Yet it should be? I'm compiling using babun, (http://babun.github.io/) which is a cygwin wrapper. Do you have any ideas on how to fix it? Here's my CMakeLists.txt.
Aria@Aria /d/projects/Treetop-paste/build$ cat ../CMakeLists.txt                                                                                                                                                                       2 &#8629;
# CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(The_Treetop)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -std=c++11")

include_directories(${PROJECT_BINARY_DIR})

set(EXECUTABLE_NAME "The_Treetop")
add_executable(${EXECUTABLE_NAME} src/main.cpp)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/sfmlsrc")
MESSAGE(STATUS "nice: " ${SFML_ROOT})

find_package(SFML 2 REQUIRED system window graphics network audio)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})

install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)

include(InstallRequiredSystemLibraries)
include(CPack)
 


Console output:
Aria@Aria /cygdrive/d/Projects/Treetop-paste/build$ make
[ 50%] Building CXX object CMakeFiles/The_Treetop.dir/src/main.cpp.o
In file included from /d/projects/Treetop-paste/sfmlsrc/include/SFML/System.hpp:32:0,
                 from /d/projects/Treetop-paste/sfmlsrc/include/SFML/Window.hpp:32,
                 from /d/projects/Treetop-paste/sfmlsrc/include/SFML/Graphics.hpp:32,
                 from /d/projects/Treetop-paste/src/main.cpp:2:
/d/projects/Treetop-paste/sfmlsrc/include/SFML/Config.hpp:92:10: error: #error This UNIX operating system is not supported by SFML library
         #error This UNIX operating system is not supported by SFML library
          ^
In file included from /d/projects/Treetop-paste/sfmlsrc/include/SFML/Window.hpp:42:0,
                 from /d/projects/Treetop-paste/sfmlsrc/include/SFML/Graphics.hpp:32,
                 from /d/projects/Treetop-paste/src/main.cpp:2:
/d/projects/Treetop-paste/sfmlsrc/include/SFML/Window/Window.hpp:106:34: error: expected ‘)’ before ‘handle’
     explicit Window(WindowHandle handle, const ContextSettings& settings = ContextSettings());
                                  ^
/d/projects/Treetop-paste/sfmlsrc/include/SFML/Window/Window.hpp:151:17: error: ‘WindowHandle’ has not been declared
     void create(WindowHandle handle, const ContextSettings& settings = ContextSettings());
                 ^
/d/projects/Treetop-paste/sfmlsrc/include/SFML/Window/Window.hpp:468:5: error: ‘WindowHandle’ does not name a type
     WindowHandle getSystemHandle() const;
     ^
In file included from /d/projects/Treetop-paste/sfmlsrc/include/SFML/Graphics.hpp:47:0,
                 from /d/projects/Treetop-paste/src/main.cpp:2:
/d/projects/Treetop-paste/sfmlsrc/include/SFML/Graphics/RenderWindow.hpp:94:40: error: expected ‘)’ before ‘handle’
     explicit RenderWindow(WindowHandle handle, const ContextSettings& settings = ContextSettings());
                                        ^
CMakeFiles/The_Treetop.dir/build.make:62: recipe for target 'CMakeFiles/The_Treetop.dir/src/main.cpp.o' failed
make[2]: *** [CMakeFiles/The_Treetop.dir/src/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/The_Treetop.dir/all' failed
make[1]: *** [CMakeFiles/The_Treetop.dir/all] Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2
 

Pages: [1]