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:
♡ aria.: http://www.snorflake.com/u/Wcij.png before moving
♡ aria.: get_current_tile should return 1
♡ aria.: which it does
♡ aria.: http://www.snorflake.com/u/Ok8E.png
♡ aria.: after moving
♡ aria.: get_current_tile should return 0
♡ 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() ?