From f14dec7e936642727b3a8077da4fd357a790ae89 Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Mon, 3 Nov 2025 12:31:21 -0500 Subject: [PATCH] modify tilemap loading file structure --- src/TileMap.h | 72 ++++++++++++++++++++++++ src/{TilemapLoader.h => TileMapLoader.h} | 13 +++-- src/main.cpp | 72 ++---------------------- 3 files changed, 84 insertions(+), 73 deletions(-) create mode 100644 src/TileMap.h rename src/{TilemapLoader.h => TileMapLoader.h} (92%) diff --git a/src/TileMap.h b/src/TileMap.h new file mode 100644 index 0000000..dd8fdbb --- /dev/null +++ b/src/TileMap.h @@ -0,0 +1,72 @@ +#ifndef TILEMAP_H +#define TILEMAP_H + +#include +#include + +class TileMap : public sf::Drawable, public sf::Transformable { +public: + 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; + } + +private: + void draw(sf::RenderTarget& target, sf::RenderStates states) const override + { + // apply the transform + states.transform *= getTransform(); + + // 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; +}; + +#endif diff --git a/src/TilemapLoader.h b/src/TileMapLoader.h similarity index 92% rename from src/TilemapLoader.h rename to src/TileMapLoader.h index 9c04c3c..08b6b9b 100644 --- a/src/TilemapLoader.h +++ b/src/TileMapLoader.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef TILEMAPLOADER_H +#define TILEMAPLOADER_H #include #include @@ -9,16 +10,16 @@ using json = nlohmann::json; -struct TilemapData { +struct TileMapData { std::vector tiles; unsigned int width; unsigned int height; unsigned int tileSize; }; -class TilemapLoader { +class TileMapLoader { public: - static TilemapData loadFromJSON(const std::filesystem::path& jsonPath) { + static TileMapData 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()); @@ -27,7 +28,7 @@ public: json data = json::parse(file); file.close(); - TilemapData result; + TileMapData result; result.tileSize = data["tileSize"].get(); // Get the first layer's tiles @@ -76,3 +77,5 @@ public: return result; } }; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 766f5a8..4c587f1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,79 +3,15 @@ #include #include #include -#include "TilemapLoader.h" - -class TileMap : public sf::Drawable, public sf::Transformable { -public: - 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; - } - -private: - void draw(sf::RenderTarget& target, sf::RenderStates states) const override - { - // apply the transform - states.transform *= getTransform(); - - // 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; -}; +#include "TileMapLoader.h" +#include "TileMap.h" int main() { // Load tilemap data from JSON - TilemapData tilemapData; + TileMapData tilemapData; try { - tilemapData = TilemapLoader::loadFromJSON("util/pacmantiles.json"); + tilemapData = TileMapLoader::loadFromJSON("util/pacmantiles.json"); } catch (const std::exception& e) { std::cerr << "Error loading tilemap: " << e.what() << std::endl; return -1;