Fixing loading problem

+ ImHex Pattern Code
This commit is contained in:
n0ffie 2025-03-18 21:55:08 +01:00
parent 72d1bf3bfb
commit 37a249dc62
9 changed files with 179 additions and 41 deletions

View file

@ -2,7 +2,9 @@
#include <fstream>
Map::Map() {}
bool operator==(uint32_t lhs, TileType rhs) {
return lhs == static_cast<uint32_t>(rhs);
}
bool operator&(uint8_t lhs, MapFlags rhs)
{
@ -19,17 +21,47 @@ auto operator<=>(int lhs, TileType rhs)
return lhs <=> static_cast<int>(rhs);
}
MapError Map::populate_vertex_array() {
if (!tiles) return {"Map: [Loading] Tilemap is not loaded", 2};
vertices.setPrimitiveType(sf::PrimitiveType::Triangles);
vertices.resize(map_size.x * map_size.y);
for (uint32_t x = 0; x < map_size.x; x++) {
for (uint32_t y = 0; y < map_size.y; ++y)
{
uint32_t tile = tiles[x + y * map_size.y];
/*
* -----
* --#++
* +++++
*
* + = other
* - = wall
* # = special
*/
}
}
return {"Map: [Loading] Success", 0};
}
Map::Map() {}
#include <iostream>
#include <cstring>
#include <filesystem>
MapError Map::load(const std::filesystem::path& path)
{
// opening the file and checking the magic number
std::ifstream file(path, std::ios::binary | std::ios::in);
char magic[4];
file.read(magic, 4);
uint32_t magic;
file.read(reinterpret_cast<char*>(&magic), 4);
if (std::memcmp(magic, "CYMF", 4) != 0) {
return {"Map: [Loading] Invalid magic number", 1};
if (std::memcmp(&magic, "CYMF", 4) != 0) {
return {"Map: [Loading] Invalid magic number: " + std::string(reinterpret_cast<const char *>(&magic), 4) + " (expected CYMF)", 1};
}
// reading the version
@ -53,7 +85,7 @@ MapError Map::load(const std::filesystem::path& path)
char* filepath_buffer = new char[filepath_buffer_size];
file.read(filepath_buffer, filepath_buffer_size);
tileAtlas.load(std::filesystem::path(filepath_buffer));
delete[] filepath_buffer;
}
@ -129,19 +161,22 @@ sf::Vector2u Map::getSize() const
return {static_cast<uint32_t>(map_size.y), static_cast<uint32_t>(map_size.z)};
}
void Map::draw(sf::RenderTarget& target, const sf::RenderStates states) const
void Map::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (!tiles) return;
for (uint32_t x = 0; x < map_size.y; x++) {
for (uint32_t y = 0; y < map_size.z; ++y)
{
target.draw(tileAtlas, states);
}
}
// apply the transform
states.transform *= getTransform();
// apply the tileset texture
states.texture = &texture;
// draw the vertex array
target.draw(vertices, states);
}
#include <iostream>
#ifdef _DEBUG
void Map::debug()
{
std::cout << "Map Debug" << std::endl;
@ -159,4 +194,5 @@ void Map::debug()
std::cout << "\t\t\tSpawn : " << spawn.x << ", " << spawn.y << std::endl;
}
std::cout << "\t\tPlayer Spawn : " << player_spawn.x << ", " << player_spawn.y << std::endl;
}
}
#endif