#include "tilemap.hpp" TileMap::TileMap(std::shared_ptr tileSet, std::vector> tiles) { // resize the vertex array to fit the level size vertices.setPrimitiveType(sf::Quads); unsigned int size = tiles.size(); vertices.resize(size * size * 4); // populate the vertex array, with one quad per tile for (unsigned int y = 0; y < size; ++y) for (unsigned int x = 0; x < size; ++x) { // get the current tile number int tileNumber = tiles[size - 1 - y][x]; // TODO // Follow: https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map // find its position in the tileset texture int tu = tileNumber % (tileset.getSize().y / tileSize.y); int tv = tileNumber / (tileset.getSize().y / tileSize.y); // get a pointer to the current tile's quad sf::Vertex *quad = &vertices[(y + x * width) * 4]; // define its 4 corners quad[0].position = sf::Vector2f(y * tileSize.y, x * tileSize.y); quad[1].position = sf::Vector2f((y + 1) * tileSize.y, x * tileSize.y); quad[2].position = sf::Vector2f((y + 1) * tileSize.y, (x + 1) * tileSize.y); quad[3].position = sf::Vector2f(y * tileSize.y, (x + 1) * tileSize.y); // define its 4 texture coordinates quad[0].texCoords = sf::Vector2f(tu * tileSize.y, tv * tileSize.y); quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.y, tv * tileSize.y); quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.y, (tv + 1) * tileSize.y); quad[3].texCoords = sf::Vector2f(tu * tileSize.y, (tv + 1) * tileSize.y); } } void TileMap::draw(sf::RenderWindow *window) { window->draw(*this); } void TileMap::draw(sf::RenderTarget &target, sf::RenderStates states) const { // apply the transform // TODO? NEEDED? // states.transform *= getTransform(); // apply the tileset texture states.texture = &tileMapTexture; // draw the vertex array target.draw(vertices, states); }