diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-02 18:23:09 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-02 18:23:09 -0400 |
commit | 6614ea639414caab546091841bf920fe6459cc9e (patch) | |
tree | 408df7aeb419ed6e096512d371f499ac6732cade | |
parent | 0466537f81106f679c0b291be26861862c43c13c (diff) |
json entity saving: cout
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | lib/entityx/entityx/Entity.h | 16 | ||||
-rw-r--r-- | src/components/Component.hpp | 16 | ||||
-rw-r--r-- | src/components/Light.hpp | 10 | ||||
-rw-r--r-- | src/components/Name.hpp | 10 | ||||
-rw-r--r-- | src/components/Player.hpp | 7 | ||||
-rw-r--r-- | src/components/Position.hpp | 10 | ||||
-rw-r--r-- | src/components/Render.hpp | 14 | ||||
-rw-r--r-- | src/components/Script.hpp | 5 | ||||
-rw-r--r-- | src/components/Velocity.hpp | 10 | ||||
-rw-r--r-- | src/engine.cpp | 10 |
11 files changed, 94 insertions, 20 deletions
@@ -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))) diff --git a/lib/entityx/entityx/Entity.h b/lib/entityx/entityx/Entity.h index 11bacfa..193d581 100644 --- a/lib/entityx/entityx/Entity.h +++ b/lib/entityx/entityx/Entity.h @@ -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)); + } } } diff --git a/src/components/Component.hpp b/src/components/Component.hpp index 5a062bd..2928366 100644 --- a/src/components/Component.hpp +++ b/src/components/Component.hpp @@ -18,13 +18,27 @@ #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_ diff --git a/src/components/Light.hpp b/src/components/Light.hpp index ee215a6..2cb0e1b 100644 --- a/src/components/Light.hpp +++ b/src/components/Light.hpp @@ -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_ diff --git a/src/components/Name.hpp b/src/components/Name.hpp index 94b7531..b42ef46 100644 --- a/src/components/Name.hpp +++ b/src/components/Name.hpp @@ -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_ diff --git a/src/components/Player.hpp b/src/components/Player.hpp index 5c1e870..95b2a96 100644 --- a/src/components/Player.hpp +++ b/src/components/Player.hpp @@ -23,13 +23,18 @@ #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_ diff --git a/src/components/Position.hpp b/src/components/Position.hpp index c801998..6157265 100644 --- a/src/components/Position.hpp +++ b/src/components/Position.hpp @@ -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_ diff --git a/src/components/Render.hpp b/src/components/Render.hpp index 3f1750f..49a9588 100644 --- a/src/components/Render.hpp +++ b/src/components/Render.hpp @@ -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_ diff --git a/src/components/Script.hpp b/src/components/Script.hpp index b3c89f3..178c933 100644 --- a/src/components/Script.hpp +++ b/src/components/Script.hpp @@ -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_ diff --git a/src/components/Velocity.hpp b/src/components/Velocity.hpp index 29c0e5c..7a75706 100644 --- a/src/components/Velocity.hpp +++ b/src/components/Velocity.hpp @@ -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_ diff --git a/src/engine.cpp b/src/engine.cpp index 61d119a..db31b9d 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -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) |