mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
add boilerplate map loading from json file
This commit is contained in:
+10
-2
@@ -10,8 +10,16 @@ FetchContent_Declare(SFML
|
|||||||
GIT_SHALLOW ON
|
GIT_SHALLOW ON
|
||||||
EXCLUDE_FROM_ALL
|
EXCLUDE_FROM_ALL
|
||||||
SYSTEM)
|
SYSTEM)
|
||||||
FetchContent_MakeAvailable(SFML)
|
|
||||||
|
FetchContent_Declare(json
|
||||||
|
GIT_REPOSITORY https://github.com/nlohmann/json.git
|
||||||
|
GIT_TAG v3.11.3
|
||||||
|
GIT_SHALLOW ON
|
||||||
|
EXCLUDE_FROM_ALL
|
||||||
|
SYSTEM)
|
||||||
|
|
||||||
|
FetchContent_MakeAvailable(SFML json)
|
||||||
|
|
||||||
add_executable(main src/main.cpp)
|
add_executable(main src/main.cpp)
|
||||||
target_compile_features(main PRIVATE cxx_std_17)
|
target_compile_features(main PRIVATE cxx_std_17)
|
||||||
target_link_libraries(main PRIVATE SFML::Graphics)
|
target_link_libraries(main PRIVATE SFML::Graphics nlohmann_json::nlohmann_json)
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.3 KiB |
@@ -0,0 +1,78 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
};
|
||||||
+15
-11
@@ -2,6 +2,8 @@
|
|||||||
#include <SFML/Graphics/CircleShape.hpp>
|
#include <SFML/Graphics/CircleShape.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include "TilemapLoader.h"
|
||||||
|
|
||||||
class TileMap : public sf::Drawable, public sf::Transformable {
|
class TileMap : public sf::Drawable, public sf::Transformable {
|
||||||
public:
|
public:
|
||||||
@@ -70,19 +72,21 @@ private:
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
constexpr std::array level = {
|
// Load tilemap data from JSON
|
||||||
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
TilemapData tilemapData;
|
||||||
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
|
try {
|
||||||
1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
|
tilemapData = TilemapLoader::loadFromJSON("util/pacmantiles.json");
|
||||||
0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
|
} catch (const std::exception& e) {
|
||||||
0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
|
std::cerr << "Error loading tilemap: " << e.what() << std::endl;
|
||||||
0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
|
return -1;
|
||||||
2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
|
}
|
||||||
0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
TileMap map;
|
TileMap map;
|
||||||
if (!map.load("assets/tileset.png", {32, 32}, level.data(), 16, 8))
|
if (!map.load("assets/maze.png",
|
||||||
|
{tilemapData.tileSize, tilemapData.tileSize},
|
||||||
|
tilemapData.tiles.data(),
|
||||||
|
tilemapData.width,
|
||||||
|
tilemapData.height))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen");
|
auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen");
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user