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)))
#include "entityx/Event.h"
#include "entityx/help/NonCopyable.h"
-class Archive;
-
namespace entityx {
typedef std::uint32_t uint32_t;
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() {
* 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));
+ }
}
}
#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_
#include "Component.hpp"
-struct Light : Component<Light>, entityx::Component<Light>
+struct Light : Component<Light>
{
public:
float r, g, b;
}
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_
#include "Component.hpp"
#include <string>
-struct Name : Component<Name>, entityx::Component<Name>
+struct Name : Component<Name>
{
public:
std::string name;
return *this;
}
+
+ void serialize(cereal::JSONOutputArchive& ar) final {
+ ar(name);
+ }
+
+ void serialize(cereal::JSONInputArchive& ar) final {
+ ar(name);
+ }
};
#endif // COMPONENT_NAME_HPP_
#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_
#include "Component.hpp"
-struct Position : Component<Position>, entityx::Component<Position>
+struct Position : Component<Position>
{
public:
double x, y;
}
return *this;
}
+
+ void serialize(cereal::JSONOutputArchive& ar) final {
+ ar(x, y);
+ }
+
+ void serialize(cereal::JSONInputArchive& ar) final {
+ ar(x, y);
+ }
};
#endif // COMPONENT_POSITION_HPP_
#include "Component.hpp"
#include "texture.hpp"
-struct Render : Component<Render>, entityx::Component<Render>
+struct Render : Component<Render>
{
public:
Texture texture;
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 {
}
return *this;
}
+
+ void serialize(cereal::JSONOutputArchive& ar) final {
+ ar(visible, flipX);
+ }
+
+ void serialize(cereal::JSONInputArchive& ar) final {
+ ar(visible, flipX);
+ }
};
#endif // COMPONENT_RENDER_HPP_
#include "Component.hpp"
-struct Scripted : Component<Scripted>, entityx::Component<Scripted>
+struct Scripted : Component<Scripted>
{
public:
sol::table caller;
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_
#include "Component.hpp"
-struct Velocity : Component<Velocity>, entityx::Component<Velocity>
+struct Velocity : Component<Velocity>
{
public:
double x, y;
}
return *this;
}
+
+ void serialize(cereal::JSONOutputArchive& ar) final {
+ ar(x, y);
+ }
+
+ void serialize(cereal::JSONInputArchive& ar) final {
+ ar(x, y);
+ }
};
#endif // COMPONENT_VELOCITY_HPP_
// 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)