]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
json entity saving: cout
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 2 Sep 2019 22:23:09 +0000 (18:23 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 2 Sep 2019 22:23:09 +0000 (18:23 -0400)
Makefile
lib/entityx/entityx/Entity.h
src/components/Component.hpp
src/components/Light.hpp
src/components/Name.hpp
src/components/Player.hpp
src/components/Position.hpp
src/components/Render.hpp
src/components/Script.hpp
src/components/Velocity.hpp
src/engine.cpp

index 2e7b55d76e155b1491c944c2081696c6a68e73af..50c0863e49ec2bf7ee1c2be846018df1aa8904d3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -38,10 +38,12 @@ DEPEXT = d
 LIBDIR = lib
 LIBS   = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -ldl -lluajit -lGLEW -lGL -lSDL2_image -lSOIL
 
-CXXFLAGS = -ggdb -std=c++17 -Wall -Wextra -Werror -pedantic
+CXXFLAGS = -ggdb -std=c++17 -Wall -Wextra -Werror -pedantic \
+                  -Wno-class-memaccess -Wno-implicit-fallthrough
 
 CXXINCS = -Isrc -I$(LIBDIR)/LuaJIT/src -I$(LIBDIR)/entityx \
-               -I$(LIBDIR)/LuaBridge/Source -I$(LIBDIR)/sol2/include -I$(LIBDIR)/soil
+                 -I$(LIBDIR)/LuaBridge/Source -I$(LIBDIR)/sol2/include -I$(LIBDIR)/soil \
+                 -I$(LIBDIR)/cereal/include
 
 CXXSRC := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
 CXXOBJ := $(patsubst $(SRCDIR)/%,$(OUTDIR)/%,$(CXXSRC:.$(SRCEXT)=.$(OBJEXT)))
index 11bacfa62f43a4765e5ba3ca11aaa60ac42bf3fc..193d581c95da5192cea76c32ef5cbf5b727f3f16 100644 (file)
@@ -34,8 +34,6 @@
 #include "entityx/Event.h"
 #include "entityx/help/NonCopyable.h"
 
-class Archive;
-
 namespace entityx {
 
 typedef std::uint32_t uint32_t;
@@ -244,8 +242,7 @@ struct BaseComponent {
   void operator delete([[maybe_unused]] void *p) { fail(); }
   void operator delete[]([[maybe_unused]] void *p) { fail(); }
 
-  virtual void save([[maybe_unused]] Archive& ar) {}
-  virtual void load([[maybe_unused]] Archive& ar) {}
+  virtual void internal_serialize(bool save, void *ar) = 0;
 
  protected:
   static void fail() {
@@ -753,13 +750,16 @@ class EntityManager : entityx::help::NonCopyable {
    * EDIT by tcsullivan
    * Iterates through all components of a given Entity.
    */
-  void entity_each_component(Entity entity, std::function<void(BaseComponent*)> f)
+  template<class Archive>
+  void entity_serialize(Entity entity, bool save, Archive& ar)
   {
-    auto mask = entity.component_mask();
+    auto mask = component_mask(entity.id());
     for (size_t i = 0; i < component_helpers_.size(); i++) {
       BaseComponentHelper *helper = component_helpers_[i];
-      if (helper && mask.test(i))
-        f(helper->get_component(entity));
+      if (helper && mask.test(i)) {
+        helper->get_component(entity)->internal_serialize(save,
+          static_cast<void*>(&ar));
+      }
     }
   }
 
index 5a062bd4406513d432093832ba6db8e5e674c9c5..2928366af1aff1407e7610f62374b00722f25fad 100644 (file)
 #ifndef COMPONENT_HPP_
 #define COMPONENT_HPP_
 
+#include <cereal/cereal.hpp>
+#include <cereal/archives/json.hpp>
+
+#include <entityx/entityx.h>
 #include <sol/sol.hpp>
 
 template<typename T>
-class Component 
+class Component : public entityx::Component<T>
 {
 public:
     virtual T FromLua(sol::object) = 0;
+
+    virtual void serialize(cereal::JSONOutputArchive& ar) = 0;
+    virtual void serialize(cereal::JSONInputArchive& ar) = 0;
+
+    void internal_serialize(bool save, void *ar) final {
+        if (save)
+            serialize(*reinterpret_cast<cereal::JSONOutputArchive*>(ar));
+        else
+            serialize(*reinterpret_cast<cereal::JSONInputArchive*>(ar));
+    }
 };
 
 #endif // COMPONENT_HPP_
index ee215a6379709786ae83b784eacdddef0692a1d4..2cb0e1bbb41601ee4ab4d62f5ebd97fa5eadf4e7 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "Component.hpp"
 
-struct Light : Component<Light>, entityx::Component<Light>
+struct Light : Component<Light>
 {
 public:
     float r, g, b;
@@ -49,6 +49,14 @@ public:
         }
         return *this;
     }
+
+    void serialize(cereal::JSONOutputArchive& ar) final {
+        ar(r, g, b, strength);
+    }
+
+    void serialize(cereal::JSONInputArchive& ar) final {
+        ar(r, g, b, strength);
+    }
 };
 
 #endif//COMPONENT_LIGHT_HPP_
index 94b7531ed23651da1859c89157615a09ac2b0c9e..b42ef4690069bf2479e0da4a0f536776a6bad961 100644 (file)
@@ -21,7 +21,7 @@
 #include "Component.hpp"
 #include <string>
 
-struct Name : Component<Name>, entityx::Component<Name>
+struct Name : Component<Name>
 {
 public:
     std::string name;
@@ -38,6 +38,14 @@ public:
 
         return *this;
     }
+
+    void serialize(cereal::JSONOutputArchive& ar) final {
+        ar(name);
+    }
+
+    void serialize(cereal::JSONInputArchive& ar) final {
+        ar(name);
+    }
 };
 
 #endif // COMPONENT_NAME_HPP_
index 5c1e870f934f6b6478614170116c4f4152fd787d..95b2a96416292b94a38d8d50d4a7124809bb60dc 100644 (file)
 
 #include "Component.hpp"
 
-struct Player : Component<Player>, entityx::Component<Player>
+struct Player : Component<Player>
 {
 public:
+    char _unused;
+
     Player FromLua([[maybe_unused]] sol::object ref)
     {
         return *this;
     }
+
+    void serialize([[maybe_unused]] cereal::JSONOutputArchive& ar) final {}
+    void serialize([[maybe_unused]] cereal::JSONInputArchive& ar) final {}
 };
 
 #endif // COMPONENT_PLAYER_HPP_
index c80199809c08dd8efb5cd761b54403d65b0c3552..6157265c4b1076ccaffef7a5fedc99aa2b7af851 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "Component.hpp"
 
-struct Position : Component<Position>, entityx::Component<Position>
+struct Position : Component<Position>
 {
 public:
     double x, y;
@@ -42,6 +42,14 @@ public:
         }
         return *this;
     }
+
+    void serialize(cereal::JSONOutputArchive& ar) final {
+        ar(x, y);
+    }
+
+    void serialize(cereal::JSONInputArchive& ar) final {
+        ar(x, y);
+    }
 };
 
 #endif // COMPONENT_POSITION_HPP_
index 3f1750fe786d462e574c6a3027dcedcbdedcfdf2..49a95881672d59a1d1bc8b7b599afb44d733c48f 100644 (file)
@@ -21,7 +21,7 @@
 #include "Component.hpp"
 #include "texture.hpp"
 
-struct Render : Component<Render>, entityx::Component<Render>
+struct Render : Component<Render>
 {
 public:
     Texture texture;
@@ -41,9 +41,9 @@ public:
             if (tab["visible"].get_type() == sol::type::boolean)
                 this->visible = tab["visible"];
             if (tab["texture"].get_type() == sol::type::string)
-                this->texture = Texture(static_cast<std::string>(tab["texture"]));
+                this->texture = Texture(tab.get<std::string>("texture"));
             if (tab["normal"].get_type() == sol::type::string)
-                this->normal = Texture(static_cast<std::string>(tab["normal"]));
+                this->normal = Texture(tab.get<std::string>("normal"));
             if (tab["flipx"].get_type() == sol::type::boolean)
                 this->flipX = tab["flipx"];
         } else {
@@ -53,6 +53,14 @@ public:
         }
         return *this;
     }
+
+    void serialize(cereal::JSONOutputArchive& ar) final {
+        ar(visible, flipX);
+    }
+
+    void serialize(cereal::JSONInputArchive& ar) final {
+        ar(visible, flipX);
+    }
 };
 
 #endif // COMPONENT_RENDER_HPP_
index b3c89f300735c838cc05b5cd4ec384f26e5f9284..178c933159800f96289cb98430a502539328e320 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "Component.hpp"
 
-struct Scripted : Component<Scripted>, entityx::Component<Scripted>
+struct Scripted : Component<Scripted>
 {
 public:
     sol::table caller;
@@ -60,6 +60,9 @@ public:
         if (caller["RenderIdle"] == sol::type::function)
             caller["RenderIdle"](caller);
     }
+
+    void serialize([[maybe_unused]] cereal::JSONOutputArchive& ar) final {}
+    void serialize([[maybe_unused]] cereal::JSONInputArchive& ar) final {}
 };
 
 #endif // COMPONENT_SCRIPT_HPP_
index 29c0e5cd5d92cbab0f3d3427b1159459127d0b85..7a757068a0eca114cfaef49d5fb7d143dd071d53 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "Component.hpp"
 
-struct Velocity : Component<Velocity>, entityx::Component<Velocity>
+struct Velocity : Component<Velocity>
 {
 public:
     double x, y;
@@ -43,6 +43,14 @@ public:
         }
         return *this;
     }
+
+    void serialize(cereal::JSONOutputArchive& ar) final {
+        ar(x, y);
+    }
+
+    void serialize(cereal::JSONInputArchive& ar) final {
+        ar(x, y);
+    }
 };
 
 #endif // COMPONENT_VELOCITY_HPP_
index 61d119a8ec94c6151cabbcdb12778da4ff8f4741..db31b9d46ff4302e21b576a7866bbe0c110fe9d9 100644 (file)
@@ -116,6 +116,16 @@ void Engine::run(void)
 
     // Done, bring logic thread back
     logicThread.join();
+
+    cereal::JSONOutputArchive archive (std::cout);
+    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();
+    }
 }
 
 bool Engine::shouldRun(void)