modify tilemap loading file structure

This commit is contained in:
2025-11-03 12:31:21 -05:00
parent 82e799dbfc
commit f14dec7e93
3 changed files with 84 additions and 73 deletions
+72
View File
@@ -0,0 +1,72 @@
#ifndef TILEMAP_H
#define TILEMAP_H
#include <SFML/Graphics.hpp>
#include <iostream>
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
+8 -5
View File
@@ -1,4 +1,5 @@
#pragma once
#ifndef TILEMAPLOADER_H
#define TILEMAPLOADER_H
#include <nlohmann/json.hpp>
#include <fstream>
@@ -9,16 +10,16 @@
using json = nlohmann::json;
struct TilemapData {
struct TileMapData {
std::vector<int> 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<unsigned int>();
// Get the first layer's tiles
@@ -76,3 +77,5 @@ public:
return result;
}
};
#endif
+4 -68
View File
@@ -3,79 +3,15 @@
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <iostream>
#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;