modify map loading

This commit is contained in:
2025-11-04 23:24:08 -05:00
parent 10cc7c0e22
commit 8602d93ede
3 changed files with 110 additions and 91 deletions
+95 -1
View File
@@ -1,11 +1,91 @@
#ifndef TILEMAP_H
#define TILEMAP_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include <nlohmann/json.hpp>
#include <fstream>
#include <vector>
#include <string>
#include <filesystem>
#include <algorithm>
#include <SFML/Graphics.hpp>
using json = nlohmann::json;
struct TileMapData {
std::vector<int> tiles;
unsigned int width;
unsigned int height;
unsigned int tileSize;
};
class TileMap : public sf::Drawable, public sf::Transformable {
public:
std::vector<int> tiles;
unsigned int width;
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());
}
json data = json::parse(file);
file.close();
//TileMapData result;
//result.tileSize = data["tileSize"].get<unsigned int>();
tileSize = data["tileSize"].get<unsigned int>();
// 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<int>::max();
int minY = std::numeric_limits<int>::max();
int maxX = std::numeric_limits<int>::min();
int maxY = std::numeric_limits<int>::min();
for (const auto& tile : tilesArray) {
int x = tile["x"].get<int>();
int y = tile["y"].get<int>();
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>();
int y = tile["y"].get<int>();
int tileId = std::stoi(tile["id"].get<std::string>());
// 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
@@ -52,6 +132,20 @@ public:
return true;
}
int convert2DCoords(int row, int col, int width) {
return col + (row * 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, 28);
return 0;
}
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override
{
-81
View File
@@ -1,81 +0,0 @@
#ifndef TILEMAPLOADER_H
#define TILEMAPLOADER_H
#include <nlohmann/json.hpp>
#include <fstream>
#include <vector>
#include <string>
#include <filesystem>
#include <algorithm>
using json = nlohmann::json;
struct TileMapData {
std::vector<int> tiles;
unsigned int width;
unsigned int height;
unsigned int tileSize;
};
class TileMapLoader {
public:
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());
}
json data = json::parse(file);
file.close();
TileMapData result;
result.tileSize = data["tileSize"].get<unsigned int>();
// 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<int>::max();
int minY = std::numeric_limits<int>::max();
int maxX = std::numeric_limits<int>::min();
int maxY = std::numeric_limits<int>::min();
for (const auto& tile : tilesArray) {
int x = tile["x"].get<int>();
int y = tile["y"].get<int>();
minX = std::min(minX, x);
minY = std::min(minY, y);
maxX = std::max(maxX, x);
maxY = std::max(maxY, y);
}
// Calculate grid dimensions
result.width = (maxX - minX) / result.tileSize + 1;
result.height = (maxY - minY) / result.tileSize + 1;
// Initialize the grid with zeros (empty tiles)
result.tiles.resize(result.width * result.height, 0);
// Fill in the tiles from the JSON
for (const auto& tile : tilesArray) {
int x = tile["x"].get<int>();
int y = tile["y"].get<int>();
int tileId = std::stoi(tile["id"].get<std::string>());
// Convert pixel position to grid position
int gridX = (x - minX) / result.tileSize;
int gridY = (y - minY) / result.tileSize;
// Store in the grid
result.tiles[gridX + gridY * result.width] = tileId;
}
return result;
}
};
#endif
+15 -9
View File
@@ -6,15 +6,15 @@
#include <SFML/Window/Keyboard.hpp>
#include <iostream>
#include <string>
#include "TileMapLoader.h"
//#include "TileMapLoader.h"
#include "TileMap.h"
int main()
{
// Load tilemap data from JSON
TileMapData tilemapData;
TileMap mazeMap;
try {
tilemapData = TileMapLoader::loadFromJSON("util/pacmantiles32.json");
mazeMap.loadFromJSON("util/pacmantiles32.json");
} catch (const std::exception& e) {
std::cerr << "Error loading tilemap: " << e.what() << std::endl;
return -1;
@@ -22,10 +22,10 @@ int main()
TileMap map;
if (!map.load("assets/maze32.png",
{tilemapData.tileSize, tilemapData.tileSize},
tilemapData.tiles.data(),
tilemapData.width,
tilemapData.height))
{mazeMap.tileSize, mazeMap.tileSize},
mazeMap.tiles.data(),
mazeMap.width,
mazeMap.height))
return -1;
auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen");
@@ -38,7 +38,7 @@ int main()
sf::CircleShape pacman(20.0f);
pacman.setFillColor(sf::Color(255, 255, 0));
pacman.move({32, 32});
pacman.move({(mazeMap.tileSize * 1.0f), (mazeMap.tileSize * 1.0f)});
int x = 0;
int y = 0;
@@ -49,6 +49,11 @@ int main()
sf::Text tileY(bitFont, "0");
tileY.move({1100, 100});
sf::Text tileId(bitFont, "");
tileId.move({1000, 150});
int tileInt = 0;
while (window.isOpen())
{
while (const std::optional event = window.pollEvent())
@@ -59,12 +64,13 @@ int main()
}
}
window.clear();
window.clear();
window.draw(map);
window.draw(currTile);
window.draw(tileX);
window.draw(tileY);
window.draw(tileId);
window.draw(pacman);