mapcreator git · master
SDL3 2.5D game and engine using assets, with map editor
C++ 99.3%git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gzidk
src/bird/bird.cpp | 1 - src/map/map.cpp | 125 +++++++++++++++++++++++++++++++----------------------- src/map/map.hpp | 1 + 3 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/bird/bird.cpp b/src/bird/bird.cpp index 6e4860d..8a9de8a 100644 --- a/src/bird/bird.cpp +++ b/src/bird/bird.cpp @@ -102,7 +102,6 @@ void Bird::Update(double delta_s, const SDL_FPoint& player_pos) { // Handle walking movement else if (state == BirdState::WALKING) { - std::cout << "DIST: " << dist << " SP " << speed << std::endl; if (dist > 0.4) { float move = speed * delta_s; if (move > dist) move = dist; diff --git a/src/map/map.cpp b/src/map/map.cpp index 31980fa..ded05f3 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -24,7 +24,7 @@ namespace Map { SDL_Point offset = {0, 0}; - SDL_Point size = {64, 40}; // map size in tiles... + SDL_Point size = {160, 100}; // map size in tiles... std::vector<TileMap> tile_map; @@ -207,6 +207,8 @@ namespace Map { return; } + out.write(reinterpret_cast<const char*>(&Map::size.x), sizeof(Map::size.x)); + out.write(reinterpret_cast<const char*>(&Map::size.y), sizeof(Map::size.y)); // Write the size first size_t size = tile_collission_map.size(); out.write(reinterpret_cast<const char*>(&size), sizeof(size)); @@ -220,6 +222,57 @@ namespace Map { std::cout << "Saved " << size << " collision cells to " << path << '\n'; } + void + load_TileCollision(const std::string &path) { + std::ifstream in(path, std::ios::binary); + if (!in) { + std::cerr << "Failed to open file for reading: " << path << '\n'; + return; + } + + + int map_tilesize_x; + int map_tilesize_y; + in.read(reinterpret_cast<char*>(&map_tilesize_x), sizeof(map_tilesize_x)); + in.read(reinterpret_cast<char*>(&map_tilesize_y), sizeof(map_tilesize_y)); + + std::cout << "WUU " << map_tilesize_x << " " << map_tilesize_y<< std::endl; + + // Read the size + size_t size = 0; + in.read(reinterpret_cast<char*>(&size), sizeof(size)); + + if (!in) { + std::cerr << "Failed to read size from file: " << path << '\n'; + return; + } + + // Resize and read data + std::vector<bool> temp(size); + const size_t w = static_cast<size_t>(Map::size.x); + const size_t h = static_cast<size_t>(Map::size.y); + const size_t m = Tile::tile_size/Tile::tile_collider_size; + + Map::tile_collission_map.assign(w * h * m * m, false); + for (size_t i = 0; i < size; ++i) { + uint8_t byte = 0; + in.read(reinterpret_cast<char*>(&byte), sizeof(byte)); + temp[i] = (byte != 0); + size_t x = i% static_cast<size_t>(map_tilesize_x); + size_t y = i/ static_cast<size_t>(map_tilesize_x); + Map::tile_collission_map[y*static_cast<size_t>(Map::size.x)+x] = temp[i]; + } + + + if (!in) { + std::cerr << "File corrupted or incomplete: " << path << '\n'; + return; + } + + //Map::tile_collission_map = std::move(temp); + std::cout << "Loaded " << size << " collision cells from " << path << '\n'; + } + void save_MapTiles(const std::vector<TileMap>& tile_map, const std::string& path) { std::ofstream file(path, std::ios::binary); @@ -228,8 +281,14 @@ namespace Map { size_t map_count = tile_map.size(); file.write(reinterpret_cast<const char*>(&map_count), sizeof(map_count)); + size_t id = 0; for (const auto& map : tile_map) { // --- background --- + + size_t map_tile_x = id % static_cast<size_t>(Map::size.x); + size_t map_tile_y = id / static_cast<size_t>(Map::size.x); + file.write(reinterpret_cast<const char*>(&map_tile_x), sizeof(map_tile_x)); + file.write(reinterpret_cast<const char*>(&map_tile_y), sizeof(map_tile_y)); file.write(reinterpret_cast<const char*>(&map.background.set_id), sizeof(map.background.set_id)); file.write(reinterpret_cast<const char*>(&map.background.object_id), sizeof(map.background.object_id)); file.write(reinterpret_cast<const char*>(&map.background.set_category), sizeof(map.background.set_category)); @@ -325,6 +384,7 @@ namespace Map { file.write(reinterpret_cast<const char*>(&slot), sizeof(slot)); } } + id++; } } @@ -337,12 +397,17 @@ namespace Map { size_t map_count = 0; file.read(reinterpret_cast<char*>(&map_count), sizeof(map_count)); - tile_map.resize(map_count); + tile_map.resize(static_cast<size_t>(Map::size.y*Map::size.x)); - size_t i = 0; - for (auto& map : tile_map) { - int x = i % static_cast<size_t>(Map::size.x); - int y = i / static_cast<size_t>(Map::size.x); + for (size_t id = 0; id < map_count; id++) { + //int x = i % static_cast<size_t>(Map::size.x); + //int y = i / static_cast<size_t>(Map::size.x); + size_t x; + size_t y; + + file.read(reinterpret_cast<char*>(&x), sizeof(x)); + file.read(reinterpret_cast<char*>(&y), sizeof(y)); + auto& map = tile_map[y*static_cast<size_t>(Map::size.x)+x]; // --- background --- file.read(reinterpret_cast<char*>(&map.background.set_id), sizeof(map.background.set_id)); file.read(reinterpret_cast<char*>(&map.background.object_id), sizeof(map.background.object_id)); @@ -408,7 +473,7 @@ namespace Map { // SET DATA TO REAL OBJECT AND MAP OBJECT - SDL_Point wp = {x-obj.position.x, y-obj.position.y}; + SDL_Point wp = {static_cast<int>(x)-obj.position.x, static_cast<int>(y)-obj.position.y}; std::vector<Object*> os = Object::get_ObjectsByWorldPosition(wp); Object* o = Object::get_ObjectByWorldPositionAndTilesetObjectID(wp, obj.tileset_info.object_id); @@ -545,7 +610,6 @@ namespace Map { */ map_object = {{obj.position.x, obj.position.y}, obj.draw_from_this_tile, o ? o->id : 0}; } - i++; } return tile_map; @@ -847,51 +911,6 @@ namespace Map { } - void - load_TileCollision(const std::string &path) { - std::ifstream in(path, std::ios::binary); - if (!in) { - std::cerr << "Failed to open file for reading: " << path << '\n'; - return; - } - - // Read the size - size_t size = 0; - in.read(reinterpret_cast<char*>(&size), sizeof(size)); - - if (!in) { - std::cerr << "Failed to read size from file: " << path << '\n'; - return; - } - - // Resize and read data - std::vector<bool> temp(size); - for (size_t i = 0; i < size; ++i) { - uint8_t byte = 0; - in.read(reinterpret_cast<char*>(&byte), sizeof(byte)); - temp[i] = (byte != 0); - } - - if (!in) { - std::cerr << "File corrupted or incomplete: " << path << '\n'; - return; - } - - Map::tile_collission_map = std::move(temp); - std::cout << "Loaded " << size << " collision cells from " << path << '\n'; - - - /* - for (TileMap& tm : Map::tile_map) { - for (TileObject& to : tm.objects) { - Object* o = Object::get_ObjectById(to.object_id); - o->collission_map = Tile::get_ObjectCollisionsFromTileset(o->tileset_id, to.object_id); - } - } - */ - - } - void set_Offset(Input& input) { // Set map map offset set diff --git a/src/map/map.hpp b/src/map/map.hpp index a6cf9e8..96b8443 100644 --- a/src/map/map.hpp +++ b/src/map/map.hpp @@ -21,6 +21,7 @@ typedef struct { Tile_s background; std::vector<Tile_s> overlays; std::vector<TileObject> objects; + SDL_Point position; } TileMap; namespace Map {