I used the example code from tutorial but added more functionality and changed things here and there.
void TileMap::load(const std::string& tileset, sf::Vector2u tileSize, std::vector<std::vector<int>>& tiles, unsigned int width, unsigned int height) {
if (!m_tileset.loadFromFile(tileset)) {
m_hasTexture = false;
std::cout << "Could not load tilemap texture" << std::endl;
}
else {
m_hasTexture = true;
// sets the values to be saved
m_tileSize = tileSize;
m_width = width;
m_height = height;
// resize the vertex array to fit the level size
m_vertices.clear(); //wasnt originally here
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(m_width * m_height * 4);
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < m_width; ++i) // 20
for (unsigned int j = 0; j < m_height; ++j) // 30
{
// get the current tile number
//int tileNumber = tiles[i + j * width][7]; // 0 25 50.... reads the vector from top to bot, then moves to column 2 then 3....
int tileNumber = tiles[j][i]; // 0 25 50.... reads the vector from top to bot, then moves to column 2 then 3....
if (tileNumber == 0) // so its skips when the tile is a 0
continue;
else
tileNumber--;
// find its position in the tileset texture
int tu = tileNumber % (m_tileset.getSize().x / m_tileSize.x); // X % (256 / 32) <=> X % 8 -> se X=33 -> 33%8 = 1 (note tileNumber--)
int tv = tileNumber / (m_tileset.getSize().x / m_tileSize.x); // -> 33/8 = 4 (pk e int)
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * m_width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(static_cast<float>(i * m_tileSize.x), static_cast<float>(j * m_tileSize.y));
quad[1].position = sf::Vector2f(static_cast<float>((i + 1) * m_tileSize.x), static_cast<float>(j * m_tileSize.y));
quad[2].position = sf::Vector2f(static_cast<float>((i + 1) * m_tileSize.x), static_cast<float>((j + 1) * m_tileSize.y));
quad[3].position = sf::Vector2f(static_cast<float>(i * m_tileSize.x), static_cast<float>((j + 1) * m_tileSize.y));
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(static_cast<float>(tu * m_tileSize.x), static_cast<float>(tv * m_tileSize.y));
quad[1].texCoords = sf::Vector2f(static_cast<float>((tu + 1) * m_tileSize.x), static_cast<float>(tv * m_tileSize.y));
quad[2].texCoords = sf::Vector2f(static_cast<float>((tu + 1) * m_tileSize.x), static_cast<float>((tv + 1) * m_tileSize.y));
quad[3].texCoords = sf::Vector2f(static_cast<float>(tu * m_tileSize.x), static_cast<float>((tv + 1) * m_tileSize.y));
}
}
}
void TileMap::simpleLoad(std::vector<std::vector<int>>& tiles) {
if (!m_hasTexture)
std::cout << "Couldn't load map, no texture set" << std::endl;
else if(m_tileSize.x <= 0 && m_tileSize.y <= 0 && m_width <= 0 && m_height <= 0)
std::cout << "Couldn't load map, sizes not set" << std::endl; // do i need this? the user cant make this error ocurr only i with code.
else {
// resize the vertex array to fit the level size
m_vertices.clear(); //wasnt originally here
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(m_width * m_height * 4);
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < m_width; ++i) // 20
for (unsigned int j = 0; j < m_height; ++j) // 30
{
// get the current tile number
//int tileNumber = tiles[i + j * width][7]; // 0 25 50.... reads the vector from top to bot, then moves to column 2 then 3....
int tileNumber = tiles[j][i]; // 0 25 50.... reads the vector from top to bot, then moves to column 2 then 3....
if (tileNumber == 0) // so its skips when the tile is a 0
continue;
else
tileNumber--;
// find its position in the tileset texture
int tu = tileNumber % (m_tileset.getSize().x / m_tileSize.x); // X % (256 / 32) <=> X % 8 -> se X=33 -> 33%8 = 1 (note tileNumber--)
int tv = tileNumber / (m_tileset.getSize().x / m_tileSize.x); // -> 33/8 = 4 (pk e int)
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * m_width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(static_cast<float>(i * m_tileSize.x), static_cast<float>(j * m_tileSize.y));
quad[1].position = sf::Vector2f(static_cast<float>((i + 1) * m_tileSize.x), static_cast<float>(j * m_tileSize.y));
quad[2].position = sf::Vector2f(static_cast<float>((i + 1) * m_tileSize.x), static_cast<float>((j + 1) * m_tileSize.y));
quad[3].position = sf::Vector2f(static_cast<float>(i * m_tileSize.x), static_cast<float>((j + 1) * m_tileSize.y));
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(static_cast<float>(tu * m_tileSize.x), static_cast<float>(tv * m_tileSize.y));
quad[1].texCoords = sf::Vector2f(static_cast<float>((tu + 1) * m_tileSize.x), static_cast<float>(tv * m_tileSize.y));
quad[2].texCoords = sf::Vector2f(static_cast<float>((tu + 1) * m_tileSize.x), static_cast<float>((tv + 1) * m_tileSize.y));
quad[3].texCoords = sf::Vector2f(static_cast<float>(tu * m_tileSize.x), static_cast<float>((tv + 1) * m_tileSize.y));
}
}
}
void TileMap::updateTile(int posX, int posY, unsigned int newValue, sf::Vector2u tileSize, int vecWidth) {
if (posX > 15) // because its global cords not view cords...
posX -= 15;
if (posY > 10)
posY -= 10;
sf::Vertex* quad = &m_vertices[(posX + posY * vecWidth) * 4];
if (newValue == 0) { //removes texture and position
quad[0].position = sf::Vector2f(0, 0);
quad[1].position = sf::Vector2f(0, 0);
quad[2].position = sf::Vector2f(0, 0);
quad[3].position = sf::Vector2f(0, 0);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(0, 0);
quad[1].texCoords = sf::Vector2f(0, 0);
quad[2].texCoords = sf::Vector2f(0, 0);
quad[3].texCoords = sf::Vector2f(0, 0);
}
else { // I never actually tested change from tile X to tile Y.... i should teste this...
int tu = newValue % (m_tileset.getSize().x / tileSize.x); // X % (256 / 32) <=> X % 8 -> se X=33 -> 33%8 = 1 (note tileNumber--)
int tv = newValue / (m_tileset.getSize().x / tileSize.x); // -> 33/8 = 4 (pk e int)
// define its 4 corners
quad[0].position = sf::Vector2f(static_cast<float>(posX * tileSize.x), static_cast<float>(posY * tileSize.y));
quad[1].position = sf::Vector2f(static_cast<float>((posX + 1) * tileSize.x), static_cast<float>(posY * tileSize.y));
quad[2].position = sf::Vector2f(static_cast<float>((posX + 1) * tileSize.x), static_cast<float>((posY + 1) * tileSize.y));
quad[3].position = sf::Vector2f(static_cast<float>(posX * tileSize.x), static_cast<float>((posY + 1) * tileSize.y));
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(static_cast<float>(tu * tileSize.x), static_cast<float>(tv * tileSize.y));
quad[1].texCoords = sf::Vector2f(static_cast<float>((tu + 1) * tileSize.x), static_cast<float>(tv * tileSize.y));
quad[2].texCoords = sf::Vector2f(static_cast<float>((tu + 1) * tileSize.x), static_cast<float>((tv + 1) * tileSize.y));
quad[3].texCoords = sf::Vector2f(static_cast<float>(tu * tileSize.x), static_cast<float>((tv + 1) * tileSize.y));
}
}
void TileMap::setFixedVars(const std::string& tileset, sf::Vector2u tileSize, unsigned int width, unsigned int height) {
if (!m_tileset.loadFromFile(tileset)) {
m_hasTexture = false;
std::cout << "Could not load tilemap texture" << std::endl;
}
else {
m_hasTexture = true;
}
m_tileSize = tileSize;
m_width = width;
m_height = height;
}
void TileMap::setTilemapTexture(std::string tileset) {
if (!m_tileset.loadFromFile(tileset)) {
m_hasTexture = false;
std::cout << "Could not load tilemap texture" << std::endl;
}
else {
m_hasTexture = true;
}
}
void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const {
// apply the transform
states.transform *= getTransform();
// apply the tileset texture
states.texture = &m_tileset;
// draw the vertex array
target.draw(m_vertices, states);
}