From 61e85132e699a57b8db7ab91905a61cf6ee4649b Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Fri, 7 Nov 2025 00:31:42 -0500 Subject: [PATCH] modify tilemap class --- CMakeLists.txt | 2 +- src/TileMap.cpp | 162 ++++++++++++++++++++++++++++++++++++++++++++++ src/TileMap.h | 169 ++++-------------------------------------------- src/main.cpp | 1 + 4 files changed, 176 insertions(+), 158 deletions(-) create mode 100644 src/TileMap.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e3c62a6..a0f0632 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,6 @@ FetchContent_Declare(json FetchContent_MakeAvailable(SFML json) -add_executable(main src/main.cpp) +add_executable(main src/main.cpp src/TileMap.cpp) target_compile_features(main PRIVATE cxx_std_17) target_link_libraries(main PRIVATE SFML::Graphics nlohmann_json::nlohmann_json) diff --git a/src/TileMap.cpp b/src/TileMap.cpp new file mode 100644 index 0000000..5530731 --- /dev/null +++ b/src/TileMap.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "TileMap.h" + +using json = nlohmann::json; + +void TileMap::loadFromJSON(const std::filesystem::path& jsonPath) { + std::ifstream file(jsonPath); + if (!file.is_open()) { + throw std::runtime_error("Failed to open JSON file: " + jsonPath.string()); + } + + json data = json::parse(file); + file.close(); + + tileSize = data["tileSize"].get(); + + // Get the first layer's tiles + if (data["layers"].empty() || data["layers"][0]["tiles"].empty()) { + throw std::runtime_error("No tiles found in JSON"); + } + + const auto& tilesArray = data["layers"][0]["tiles"]; + + // Find the bounds of the tilemap + int minX = std::numeric_limits::max(); + int minY = std::numeric_limits::max(); + int maxX = std::numeric_limits::min(); + int maxY = std::numeric_limits::min(); + + for (const auto& tile : tilesArray) { + int x = tile["x"].get(); + int y = tile["y"].get(); + minX = std::min(minX, x); + minY = std::min(minY, y); + maxX = std::max(maxX, x); + maxY = std::max(maxY, y); + } + + // Calculate grid dimensions + width = (maxX - minX) / tileSize + 1; + height = (maxY - minY) / tileSize + 1; + + // Initialize the grid with zeros (empty tiles) + tiles.resize(width * height, 0); + + // Fill in the tiles from the JSON + for (const auto& tile : tilesArray) { + int x = tile["x"].get(); + int y = tile["y"].get(); + int tileId = std::stoi(tile["id"].get()); + + // Convert pixel position to grid position + int gridX = (x - minX) / tileSize; + int gridY = (y - minY) / tileSize; + + // Store in the grid + tiles[gridX + gridY * width] = tileId; + } +} + +bool TileMap::load(const std::filesystem::path& tilesetPath, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height) +{ + // load the tileset texture + if (!tileset.loadFromFile(tilesetPath)) + return false; + + // resize the vertex array to fit the level size + vertices.setPrimitiveType(sf::PrimitiveType::Triangles); + vertices.resize(width * height * 6); + + // populate the vertex array, with two triangles per tile + for (unsigned int i = 0; i < width; ++i) + { + for (unsigned int j = 0; j < height; ++j) + { + // get the current tile number + const int tileNumber = tiles[i + j * width]; + + // find its position in the tileset texture + const int tu = tileNumber % (tileset.getSize().x / tileSize.x); + const int tv = tileNumber / (tileset.getSize().x / tileSize.x); + + // get a pointer to the triangles' vertices of the current tile + sf::Vertex* triangles = &vertices[(i + j * width) * 6]; + + // define the 6 corners of the two triangles + triangles[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y); + triangles[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y); + triangles[2].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y); + triangles[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y); + triangles[4].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y); + triangles[5].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y); + + // define the 6 matching texture coordinates + triangles[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); + triangles[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); + triangles[2].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); + triangles[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); + triangles[4].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); + triangles[5].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); + } + } + + return true; +} + +int TileMap::convert2DCoords(int row, int col) { + return col + (row * width); +} + +int TileMap::convert2DCoords(sf::Vector2i vec) { + return vec.y + (vec.x * width); +} + +int TileMap::getTileId(int tileX, int tileY) { + //tileX and tileY start from top left 0,0 + //tileX increases going right + //tileY increases going down + + int index = convert2DCoords(tileY, tileX); + + int tileId = tiles.at(index); + + return tileId; +} + +int TileMap::getTileId(sf::Vector2i vec) { + int index = convert2DCoords(vec); + + int tileId = tiles.at(index); + + return tileId; +} + +bool TileMap::isLegalTile(sf::Vector2i vec) { + int index = convert2DCoords(vec); + + int tileId = tiles.at(index); + + return tileId == 92; +} + +void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const { + // apply the transform + states.transform *= getTransform(); + + // apply the tileset texture + states.texture = &tileset; + + // draw the vertex array + target.draw(vertices, states); +} diff --git a/src/TileMap.h b/src/TileMap.h index 9548967..e2773c1 100644 --- a/src/TileMap.h +++ b/src/TileMap.h @@ -1,18 +1,10 @@ #ifndef TILEMAP_H #define TILEMAP_H -#include -#include -#include -#include -#include -#include -#include -#include - #include -using json = nlohmann::json; +#include +#include class TileMap : public sf::Drawable, public sf::Transformable { public: @@ -21,162 +13,25 @@ public: unsigned int height; unsigned int tileSize; - void loadFromJSON(const std::filesystem::path& jsonPath) { - std::ifstream file(jsonPath); - if (!file.is_open()) { - throw std::runtime_error("Failed to open JSON file: " + jsonPath.string()); - } + void loadFromJSON(const std::filesystem::path& jsonPath); - json data = json::parse(file); - file.close(); + bool load(const std::filesystem::path& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height); - //TileMapData result; - //result.tileSize = data["tileSize"].get(); + int convert2DCoords(int row, int col); - tileSize = data["tileSize"].get(); + int convert2DCoords(sf::Vector2i vec); - // Get the first layer's tiles - if (data["layers"].empty() || data["layers"][0]["tiles"].empty()) { - throw std::runtime_error("No tiles found in JSON"); - } + int getTileId(int tileX, int tileY); - const auto& tilesArray = data["layers"][0]["tiles"]; - - // Find the bounds of the tilemap - int minX = std::numeric_limits::max(); - int minY = std::numeric_limits::max(); - int maxX = std::numeric_limits::min(); - int maxY = std::numeric_limits::min(); - - for (const auto& tile : tilesArray) { - int x = tile["x"].get(); - int y = tile["y"].get(); - minX = std::min(minX, x); - minY = std::min(minY, y); - maxX = std::max(maxX, x); - maxY = std::max(maxY, y); - } - - // Calculate grid dimensions - width = (maxX - minX) / tileSize + 1; - height = (maxY - minY) / tileSize + 1; - - // Initialize the grid with zeros (empty tiles) - tiles.resize(width * height, 0); - - // Fill in the tiles from the JSON - for (const auto& tile : tilesArray) { - int x = tile["x"].get(); - int y = tile["y"].get(); - int tileId = std::stoi(tile["id"].get()); - - // Convert pixel position to grid position - int gridX = (x - minX) / tileSize; - int gridY = (y - minY) / tileSize; - - // Store in the grid - tiles[gridX + gridY * width] = tileId; - } - } - - bool load(const std::filesystem::path& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height) - { - // load the tileset texture - if (!m_tileset.loadFromFile(tileset)) - return false; - - // resize the vertex array to fit the level size - m_vertices.setPrimitiveType(sf::PrimitiveType::Triangles); - m_vertices.resize(width * height * 6); - - // populate the vertex array, with two triangles per tile - for (unsigned int i = 0; i < width; ++i) - { - for (unsigned int j = 0; j < height; ++j) - { - // get the current tile number - const int tileNumber = tiles[i + j * width]; - - // find its position in the tileset texture - const int tu = tileNumber % (m_tileset.getSize().x / tileSize.x); - const int tv = tileNumber / (m_tileset.getSize().x / tileSize.x); - - // get a pointer to the triangles' vertices of the current tile - sf::Vertex* triangles = &m_vertices[(i + j * width) * 6]; - - // define the 6 corners of the two triangles - triangles[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y); - triangles[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y); - triangles[2].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y); - triangles[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y); - triangles[4].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y); - triangles[5].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y); - - // define the 6 matching texture coordinates - triangles[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); - triangles[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); - triangles[2].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); - triangles[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); - triangles[4].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); - triangles[5].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); - } - } - - return true; - } - - int convert2DCoords(int row, int col) { - return col + (row * width); - } - - int convert2DCoords(sf::Vector2i vec) { - return vec.y + (vec.x * width); - } - - int getTileId(int tileX, int tileY) { - //tileX and tileY start from top left 0,0 - //tileX increases going right - //tileY increases going down - - int index = convert2DCoords(tileY, tileX); - - int tileId = tiles.at(index); - - return tileId; - } - - int getTileId(sf::Vector2i vec) { - int index = convert2DCoords(vec); - - int tileId = tiles.at(index); - - return tileId; - } - - bool isLegalTile(sf::Vector2i vec) { - int index = convert2DCoords(vec); - - int tileId = tiles.at(index); - - return tileId == 92; - } + int getTileId(sf::Vector2i vec); + bool isLegalTile(sf::Vector2i vec); private: - void draw(sf::RenderTarget& target, sf::RenderStates states) const override - { - // apply the transform - states.transform *= getTransform(); + void draw(sf::RenderTarget& target, sf::RenderStates states) const override; - // apply the tileset texture - states.texture = &m_tileset; - - // draw the vertex array - target.draw(m_vertices, states); - } - - sf::VertexArray m_vertices; - sf::Texture m_tileset; + sf::VertexArray vertices; + sf::Texture tileset; }; #endif diff --git a/src/main.cpp b/src/main.cpp index 42d7e9a..1c9b6ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include #include #include + #include "TileMap.h" #include "MazeCharacter.h"