Nimbin[12]?SDL & Graphics / mapcreator / commits / dbf10c3

mapcreator git · master

SDL3 2.5D game and engine using assets, with map editor

sdl3 c++ game engine map-editor cmake · first commit 2025-10-31 · last commit 2026-09-07 (1 month ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-spieleentwicklung/map_creator

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gz

Add save and load Vegetable

Christian Immanuel · 2025-12-14 16:48 · dbf10c3309f01da94d785d53d4d5fcc3193a219b

 saves/save.map                 | Bin 121448 -> 121272 bytes
 saves/save.tileset_collissions | Bin 194788 -> 214812 bytes
 saves/save.tileset_objects     | Bin 27600 -> 27624 bytes
 src/map/map.cpp                | 244 +++++++++++++++++++++++++----------------
 4 files changed, 148 insertions(+), 96 deletions(-)

diff --git a/saves/save.map b/saves/save.map
index 8769822..9fc56b7 100644
Binary files a/saves/save.map and b/saves/save.map differ
diff --git a/saves/save.tileset_collissions b/saves/save.tileset_collissions
index d17067c..1860638 100644
Binary files a/saves/save.tileset_collissions and b/saves/save.tileset_collissions differ
diff --git a/saves/save.tileset_objects b/saves/save.tileset_objects
index f5d40e8..56ad19a 100644
Binary files a/saves/save.tileset_objects and b/saves/save.tileset_objects differ
diff --git a/src/map/map.cpp b/src/map/map.cpp
index 427a98e..737af21 100644
--- a/src/map/map.cpp
+++ b/src/map/map.cpp
@@ -251,10 +251,6 @@ namespace Map {
             for (const auto& obj : map.objects) {
                 Object* o = Object::get_ObjectById(obj.object_id);
 
-                // Is Building?
-                bool is_building = (dynamic_cast<Building*>(o) != nullptr);
-                file.write(reinterpret_cast<const char*>(&is_building), sizeof(is_building));
-
                 // TilesetObject
                 file.write(reinterpret_cast<const char*>(&obj.position.x), sizeof(obj.position.x));
                 file.write(reinterpret_cast<const char*>(&obj.position.y), sizeof(obj.position.y));
@@ -275,33 +271,58 @@ namespace Map {
 
                 file.write(reinterpret_cast<const char*>(&o->z_index), sizeof(o->z_index));
 
-                if (is_building) {
+                if (o->category == ObjectCategory::BUILDING) {
                     Building* b = static_cast<Building*>(o);
 
                     auto type   = static_cast<int>(b->building_type);
                     auto status = static_cast<int>(b->building_status);
-                    float time  = b->building_time;
 
                     file.write(reinterpret_cast<const char*>(&type),   sizeof(type));
                     file.write(reinterpret_cast<const char*>(&status), sizeof(status));
-                    file.write(reinterpret_cast<const char*>(&time),   sizeof(time));
+                    file.write(reinterpret_cast<const char*>(&b->building_time),
+                            sizeof(b->building_time));
 
                     size_t cost_count = b->cost_fulfilled.size();
                     file.write(reinterpret_cast<const char*>(&cost_count), sizeof(cost_count));
                     for (auto& p : b->cost_fulfilled) {
                         auto r = static_cast<int>(p.first);
-                        file.write(reinterpret_cast<const char*>(&r),     sizeof(r));
-                        file.write(reinterpret_cast<const char*>(&p.second), sizeof(p.second));
+                        file.write(reinterpret_cast<const char*>(&r), sizeof(r));
+                        file.write(reinterpret_cast<const char*>(&p.second),
+                                sizeof(p.second));
                     }
 
                     size_t frac_count = b->fractional_buffer.size();
                     file.write(reinterpret_cast<const char*>(&frac_count), sizeof(frac_count));
                     for (auto& p : b->fractional_buffer) {
                         auto r = static_cast<int>(p.first);
-                        file.write(reinterpret_cast<const char*>(&r),       sizeof(r));
-                        file.write(reinterpret_cast<const char*>(&p.second), sizeof(p.second));
+                        file.write(reinterpret_cast<const char*>(&r), sizeof(r));
+                        file.write(reinterpret_cast<const char*>(&p.second),
+                                sizeof(p.second));
                     }
                 }
+                else if (o->category == ObjectCategory::FARM_CROP) {
+                    Vegetable* v = static_cast<Vegetable*>(o);
+
+                    auto type = static_cast<int>(v->type);
+                    file.write(reinterpret_cast<const char*>(&type), sizeof(type));
+
+                    file.write(reinterpret_cast<const char*>(&v->quantity),
+                            sizeof(v->quantity));
+                    file.write(reinterpret_cast<const char*>(&v->growth_stage),
+                            sizeof(v->growth_stage));
+                    file.write(reinterpret_cast<const char*>(&v->next_update_sec),
+                            sizeof(v->next_update_sec));
+                    file.write(reinterpret_cast<const char*>(&v->collecting_duration_max),
+                            sizeof(v->collecting_duration_max));
+
+                    file.write(reinterpret_cast<const char*>(&v->tileset_object_x),
+                            sizeof(v->tileset_object_x));
+                    file.write(reinterpret_cast<const char*>(&v->tileset_object_y),
+                            sizeof(v->tileset_object_y));
+
+                    auto slot = static_cast<int>(v->equipement_slot);
+                    file.write(reinterpret_cast<const char*>(&slot), sizeof(slot));
+                }
             }
         }
     }
@@ -356,21 +377,17 @@ namespace Map {
             for (auto& map_object : map.objects) {
                 TilesetObject obj;
 
-                // is building?
-                bool is_building = false;
-                file.read(reinterpret_cast<char*>(&is_building), sizeof(is_building));
-
                 // TilesetObject
                 file.read(reinterpret_cast<char*>(&obj.position.x), sizeof(obj.position.x));
                 file.read(reinterpret_cast<char*>(&obj.position.y), sizeof(obj.position.y));
                 file.read(reinterpret_cast<char*>(&obj.position.w), sizeof(obj.position.w));
                 file.read(reinterpret_cast<char*>(&obj.position.h), sizeof(obj.position.h));
 
-                file.read(reinterpret_cast<char*>(&obj.draw_from_this_tile), sizeof(obj.draw_from_this_tile));
+                file.read(reinterpret_cast<char*>(&obj.draw_from_this_tile),
+                        sizeof(obj.draw_from_this_tile));
 
-                int object_type = 0;
+                int object_type;
                 file.read(reinterpret_cast<char*>(&object_type), sizeof(object_type));
-                obj.object_type = static_cast<ObjectType>(object_type);
 
                 int cat_i;
                 file.read(reinterpret_cast<char*>(&cat_i), sizeof(cat_i));
@@ -379,112 +396,147 @@ namespace Map {
                 int tt;
                 file.read(reinterpret_cast<char*>(&tt), sizeof(tt));
                 TileType tileset_type = static_cast<TileType>(tt);
-                file.read(reinterpret_cast<char*>(&obj.tileset_info.set_id), sizeof(obj.tileset_info.set_id));
-                file.read(reinterpret_cast<char*>(&obj.tileset_info.object_id), sizeof(obj.tileset_info.object_id));
+
+                file.read(reinterpret_cast<char*>(&obj.tileset_info.set_id),
+                        sizeof(obj.tileset_info.set_id));
+                file.read(reinterpret_cast<char*>(&obj.tileset_info.object_id),
+                        sizeof(obj.tileset_info.object_id));
 
                 file.read(reinterpret_cast<char*>(&obj.z_index), sizeof(obj.z_index));
 
 
 
                 // SET DATA TO REAL OBJECT AND MAP OBJECT
-
                 SDL_Point wp = {x-obj.position.x, y-obj.position.y};
                 std::vector<Object*> os = Object::get_ObjectsByWorldPosition(wp);
                 Object* o = Object::get_ObjectByWorldPositionAndTilesetObjectID(wp, obj.tileset_info.object_id);
 
 
                 SDL_Point dim = {obj.position.w, obj.position.h};
-                if (!o && !is_building) {
-                    switch (category) {
-                        case ObjectCategory::STATIC: {
-                            o = new Static((ObjectType)object_type,
-                                            ObjectCategory::STATIC,
-                                            tileset_type,
-                                            obj.tileset_info.set_id,
-                                            obj.tileset_info.object_id,
-                                            wp,
-                                            dim,
-                                            obj.z_index);
-                            Object::objects.push_back(o);
-                            break; }
-                        case ObjectCategory::RESOURCE: {
-                            Resource_s r = Tile::get_ResourceFromTileSet(obj.tileset_info.set_id, obj.tileset_info.object_id);
-                            o = new Resource((ObjectType)object_type,
-                                              ObjectCategory::RESOURCE,
-                                              tileset_type,
-                                              obj.tileset_info.set_id,
-                                              obj.tileset_info.object_id,
-                                              wp,
-                                              dim,
-                                              obj.z_index,
-                                              r.resource,
-                                              r.quantity,
-                                              r.collecting_methode,
-                                              r.collecting_duration);
-                            Object::objects.push_back(o);
-                            break; }
-                        default: break;
+
+                switch (category) {
+
+                    case ObjectCategory::STATIC: {
+                        o = new Static(
+                                static_cast<ObjectType>(object_type),
+                                ObjectCategory::STATIC,
+                                tileset_type,
+                                obj.tileset_info.set_id,
+                                obj.tileset_info.object_id,
+                                wp,
+                                dim,
+                                obj.z_index);
+                        break;
                     }
 
-                } else if (is_building) {
-                    // BUILDING
-                    int bt_i;
-                    file.read(reinterpret_cast<char*>(&bt_i), sizeof(bt_i));
-                    BuildingType bt = static_cast<BuildingType>(bt_i);
+                    case ObjectCategory::RESOURCE: {
+                        Resource_s r = Tile::get_ResourceFromTileSet(
+                                obj.tileset_info.set_id,
+                                obj.tileset_info.object_id);
+
+                        o = new Resource(
+                                static_cast<ObjectType>(object_type),
+                                ObjectCategory::RESOURCE,
+                                tileset_type,
+                                obj.tileset_info.set_id,
+                                obj.tileset_info.object_id,
+                                wp,
+                                dim,
+                                obj.z_index,
+                                r.resource,
+                                r.quantity,
+                                r.collecting_methode,
+                                r.collecting_duration);
+                        break;
+                    }
 
-                    int status_i;
-                    file.read(reinterpret_cast<char*>(&status_i), sizeof(status_i));
-                    BuildingStatus status = static_cast<BuildingStatus>(status_i);
+                    case ObjectCategory::BUILDING: {
+                        int bt_i;
+                        file.read(reinterpret_cast<char*>(&bt_i), sizeof(bt_i));
+                        BuildingType bt = static_cast<BuildingType>(bt_i);
 
-                    float time;
-                    file.read(reinterpret_cast<char*>(&time), sizeof(time));
+                        int status_i;
+                        file.read(reinterpret_cast<char*>(&status_i), sizeof(status_i));
+                        BuildingStatus status = static_cast<BuildingStatus>(status_i);
 
-                    size_t cost_count;
-                    file.read(reinterpret_cast<char*>(&cost_count), sizeof(cost_count));
+                        float time;
+                        file.read(reinterpret_cast<char*>(&time), sizeof(time));
 
-                    std::unordered_map<ResourceType, int> cost_f;
+                        size_t cost_count;
+                        file.read(reinterpret_cast<char*>(&cost_count), sizeof(cost_count));
 
-                    for (size_t i=0; i<cost_count; ++i) {
-                        int r;
-                        int v;
-                        file.read(reinterpret_cast<char*>(&r), sizeof(r));
-                        file.read(reinterpret_cast<char*>(&v), sizeof(v));
-                        cost_f[static_cast<ResourceType>(r)] = v;
-                    }
+                        std::unordered_map<ResourceType, int> cost_f;
+                        for (size_t i = 0; i < cost_count; ++i) {
+                            int r, v;
+                            file.read(reinterpret_cast<char*>(&r), sizeof(r));
+                            file.read(reinterpret_cast<char*>(&v), sizeof(v));
+                            cost_f[static_cast<ResourceType>(r)] = v;
+                        }
 
-                    size_t frac_count;
-                    file.read(reinterpret_cast<char*>(&frac_count), sizeof(frac_count));
+                        size_t frac_count;
+                        file.read(reinterpret_cast<char*>(&frac_count), sizeof(frac_count));
 
-                    std::unordered_map<ResourceType, float> frac_b;
+                        std::unordered_map<ResourceType, float> frac_b;
+                        for (size_t i = 0; i < frac_count; ++i) {
+                            int r;
+                            float v;
+                            file.read(reinterpret_cast<char*>(&r), sizeof(r));
+                            file.read(reinterpret_cast<char*>(&v), sizeof(v));
+                            frac_b[static_cast<ResourceType>(r)] = v;
+                        }
 
-                    for (size_t i=0; i<frac_count; ++i) {
-                        int r;
-                        float v;
-                        file.read(reinterpret_cast<char*>(&r), sizeof(r));
-                        file.read(reinterpret_cast<char*>(&v), sizeof(v));
-                        frac_b[static_cast<ResourceType>(r)] = v;
+                        o = new Building(
+                                static_cast<ObjectType>(object_type),
+                                ObjectCategory::BUILDING,
+                                tileset_type,
+                                obj.tileset_info.set_id,
+                                obj.tileset_info.object_id,
+                                wp,
+                                dim,
+                                obj.z_index,
+                                bt);
+
+                        Building* b = static_cast<Building*>(o);
+                        b->building_status   = status;
+                        b->building_time     = time;
+                        b->cost_fulfilled    = std::move(cost_f);
+                        b->fractional_buffer = std::move(frac_b);
+                        break;
                     }
 
-                    o = new Building(
-                            static_cast<ObjectType>(object_type),
-                            ObjectCategory::BUILDING,
-                            tileset_type,
-                            obj.tileset_info.set_id,
-                            obj.tileset_info.object_id,
-                            wp,
-                            dim,
-                            obj.z_index,
-                            bt);
+                    case ObjectCategory::FARM_CROP: {
+                        int type_i;
+                        file.read(reinterpret_cast<char*>(&type_i), sizeof(type_i));
 
-                    Building* b = static_cast<Building*>(o);
-                    b->building_status = status;
-                    b->building_time   = time;
-                    b->cost_fulfilled  = std::move(cost_f);
-                    b->fractional_buffer = std::move(frac_b);
+                        Vegetable* v = new Vegetable(static_cast<VegetableData::Type>(type_i), wp);
+
+                        file.read(reinterpret_cast<char*>(&v->quantity), sizeof(v->quantity));
+                        file.read(reinterpret_cast<char*>(&v->growth_stage),
+                                sizeof(v->growth_stage));
+                        file.read(reinterpret_cast<char*>(&v->next_update_sec),
+                                sizeof(v->next_update_sec));
+                        file.read(reinterpret_cast<char*>(&v->collecting_duration_max),
+                                sizeof(v->collecting_duration_max));
+
+                        file.read(reinterpret_cast<char*>(&v->tileset_object_x),
+                                sizeof(v->tileset_object_x));
+                        file.read(reinterpret_cast<char*>(&v->tileset_object_y),
+                                sizeof(v->tileset_object_y));
 
-                    Object::objects.push_back(o);
+                        int slot;
+                        file.read(reinterpret_cast<char*>(&slot), sizeof(slot));
+                        v->equipement_slot = static_cast<EquipmentSlot>(slot);
+
+                        o = v;
+                        break;
+                    }
+
+                    default:
+                        break;
                 }
 
+                if (o) Object::objects.push_back(o);
+
                 /*
                 std::cout << "OP: " << obj.position.x << " " << obj.position.y << " ID " << o->id << std::endl;
                 std::cout << "WP " << wp.x << " " << wp.y << std::endl;