]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
json save and load, made GameState class
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 3 Sep 2019 17:35:13 +0000 (13:35 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 3 Sep 2019 17:35:13 +0000 (13:35 -0400)
src/engine.cpp
src/gamestate.hpp [new file with mode: 0644]

index afd325dce5054f23b7da25305ad5d61c6c1a51c2..9c0b09b5d6222dafbc32cebcc562bdb4dc1b319d 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include "engine.hpp"
+#include "gamestate.hpp"
 #include "gamerun.hpp"
 #include "input.hpp"
 #include "player.hpp"
@@ -45,7 +46,9 @@ int Engine::init(void)
     systems.add<ScriptSystem>();
     systems.configure();
 
+    // Load game script and entity data
     systems.system<ScriptSystem>()->init();
+    GameState::load("save.json", entities);
 
     return 0;
 }
@@ -119,16 +122,8 @@ void Engine::run(void)
     // Done, bring logic thread back
     logicThread.join();
 
-    std::ofstream saveFile ("save.json");
-    cereal::JSONOutputArchive archive (saveFile);
-    std::string name ("entity");
-    int i = 0;
-    for (entityx::Entity e : entities.entities_for_debugging()) {
-        archive.setNextName((name + std::to_string(i++)).c_str());
-        archive.startNode();
-        entities.entity_serialize(e, true, archive);
-        archive.finishNode();
-    }
+    // Save the entities' data
+    GameState::save("save.json", entities);
 }
 
 bool Engine::shouldRun(void)
diff --git a/src/gamestate.hpp b/src/gamestate.hpp
new file mode 100644 (file)
index 0000000..196c6b7
--- /dev/null
@@ -0,0 +1,96 @@
+/**
+ * @file gamestate.hpp
+ * Provides functionality to load and save entity data to or from JSON files.
+ *
+ * Copyright (C) 2019 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GAMELOAD_HPP_
+#define GAMELOAD_HPP_
+
+#include <cereal/cereal.hpp>
+#include <cereal/archives/json.hpp>
+#include <entityx/entityx.h>
+
+#include <fstream>
+
+/**
+ * @class GameState
+ * Manages save files that contain entity data.
+ */
+class GameState
+{
+public:
+    /**
+     * Loads entity data from a JSON file into entities contained in the
+     * entity manager.
+     *
+     * The order of entities and the components they have must match between
+     * those in the manager and those in the file.
+     *
+     * @param file The file to load from
+     * @param entities The entity manager to load into
+     */
+    static void load(const std::string& file, entityx::EntityManager &entities)
+    {
+        if (std::ifstream saveFile (file); saveFile.good()) {
+            cereal::JSONInputArchive archive (saveFile);
+           serializeEntities(archive, false, entities);
+        }
+    }
+
+    /**
+     * Saves entity data from the entities in the provided manager into a JSON
+     * file,
+     *
+     * @param file The file to load from
+     * @param entities The entity manager to get entity data from
+     */
+    static void save(const std::string& file, entityx::EntityManager &entities)
+    {
+        if (std::ofstream saveFile (file); saveFile.good()) {
+            cereal::JSONOutputArchive archive (saveFile);
+           serializeEntities(archive, true, entities);
+        }
+    }
+
+private:
+    /**
+     * Calls the serialization funciton for each entity in the given entity
+     * manager.
+     * @param archive The archive to save/load with (cereal::JSON...)
+     * @param save True to save data, false to load
+     * @param entities The entity manager to iterate through
+     */
+    template<class Archive>
+    static void serializeEntities(Archive& archive,
+                                  bool save,
+                                  entityx::EntityManager& entities)
+    {
+        std::string name ("entity");
+        int i = 0;
+
+        for (auto entity : entities.entities_for_debugging()) {
+            archive.setNextName((name + std::to_string(i++)).c_str());
+            archive.startNode();
+            entities.entity_serialize(entity, save, archive);
+            archive.finishNode();
+        }
+    }
+};
+
+#endif // GAMELOAD_HPP_
+