SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
SerializationSchemes.h
1#pragma once
2
3#include "spark/core/Application.h"
4#include "spark/core/Component.h"
5#include "spark/core/GameObject.h"
6#include "spark/core/Scene.h"
7#include "spark/core/components/Circle.h"
8#include "spark/core/components/Collider.h"
9#include "spark/core/components/Image.h"
10#include "spark/core/components/Rectangle.h"
11#include "spark/core/components/Text.h"
12#include "spark/core/components/Transform.h"
13
14#include "spark/math/Vector2.h"
15
16template <typename SerializerType>
17struct experimental::ser::SerializerScheme<SerializerType, std::filesystem::path>
18{
19 static void serialize(SerializerType& serializer, const std::filesystem::path& obj)
20 {
21 serializer << obj.generic_string();
22 }
23
24 static void deserialize(SerializerType& deserializer, std::filesystem::path& obj)
25 {
26 std::string str;
27 deserializer >> str;
28 obj = str;
29 }
30};
31
32template <typename SerializerType, typename T>
33struct experimental::ser::SerializerScheme<SerializerType, std::optional<T>>
34{
35 static void serialize(SerializerType& serializer, const std::optional<T>& obj)
36 {
37 serializer << obj.has_value();
38 if (obj.has_value())
39 serializer << obj.value();
40 }
41
42 static void deserialize(SerializerType& deserializer, std::optional<T>& obj)
43 {
44 bool has_value = false;
45 deserializer >> has_value;
46 if (has_value)
47 {
48 T value;
49 deserializer >> value;
50 obj = value;
51 }
52 else
53 obj = std::nullopt;
54 }
55};
56
57template <typename SerializerType, typename T>
58struct experimental::ser::SerializerScheme<SerializerType, spark::math::Vector2<T>>
59{
60 static void serialize(SerializerType& serializer, const spark::math::Vector2<T>& obj)
61 {
62 serializer << obj.x;
63 serializer << obj.y;
64 }
65
66 static void deserialize(SerializerType& deserializer, spark::math::Vector2<T>& obj)
67 {
68 deserializer >> obj.x;
69 deserializer >> obj.y;
70 }
71};
72
73template <typename SerializerType, typename T>
74struct experimental::ser::SerializerScheme<SerializerType, spark::math::Rectangle<T>>
75{
76 static void serialize(SerializerType& serializer, const spark::math::Rectangle<T>& obj)
77 {
78 serializer << obj.position;
79 serializer << obj.extent;
80 }
81
82 static void deserialize(SerializerType& deserializer, spark::math::Rectangle<T>& obj)
83 {
84 deserializer >> obj.position;
85 deserializer >> obj.extent;
86 }
87};
88
89SPARK_DEFINE_EMPTY_SERIALIZER_SCHEME(spark::core::Component)
90
91SPARK_SERIALIZE_RTTI_CLASS(spark::core::components::Circle, radius)
92SPARK_SERIALIZE_RTTI_CLASS(spark::core::components::Collider, m_rectangle)
93SPARK_SERIALIZE_RTTI_CLASS(spark::core::components::StaticCollider)
94SPARK_SERIALIZE_RTTI_CLASS(spark::core::components::DynamicCollider)
95SPARK_SERIALIZE_RTTI_CLASS(spark::core::components::Image, m_path, m_size)
96SPARK_SERIALIZE_RTTI_CLASS(spark::core::components::Rectangle, size)
97SPARK_SERIALIZE_RTTI_CLASS(spark::core::components::Text, m_content, m_offset, m_fontPath)
98SPARK_SERIALIZE_RTTI_CLASS(spark::core::components::Transform, position, rotation, scale)
99
100template <typename SerializerType>
101struct experimental::ser::SerializerScheme<SerializerType, spark::core::GameObject>
102{
103 static void serialize(SerializerType& serializer, const spark::core::GameObject& obj)
104 {
105 serializer << obj.isShown;
106
107 serializer << obj.components().size();
108 for (const auto* component : obj.components())
109 {
110 serializer << component->rttiInstance().className();
111 spark::core::Application::Instance()->registries().component.serializer(component->rttiInstance().className())(serializer, *component);
112 }
113
114 serializer << obj.children().size();
115 for (const auto* child : obj.children())
116 {
117 serializer << child->rttiInstance().className();
118 serializer << child->name();
119 spark::core::Application::Instance()->registries().gameObject.serializer(child->rttiInstance().className())(serializer, *child);
120 }
121 }
122
123 static void deserialize(SerializerType& deserializer, spark::core::GameObject& obj)
124 {
125 deserializer >> obj.isShown;
126
127 std::size_t components_count;
128 deserializer >> components_count;
129 for (std::size_t i = 0; i < components_count; ++i)
130 {
131 std::string type;
132 deserializer >> type;
133
134 // If the class already haves the component, don't recreate it. Only deserialize in place.
135 spark::core::Component* component = nullptr;
136 if (auto it = obj.m_components.find(spark::rtti::RttiDatabase::Get(type)); it != obj.m_components.end())
137 component = it->second.first;
138 else
139 {
140 component = spark::core::Application::Instance()->registries().component.create(type, &obj).release();
141 obj.addComponent(component, true);
142 }
143
144 spark::core::Application::Instance()->registries().component.deserializer(type)(deserializer, *component);
145 }
146
147 std::size_t children_count;
148 deserializer >> children_count;
149
150 for (std::size_t i = 0; i < children_count; ++i)
151 {
152 std::string type, name;
153 deserializer >> type;
154 deserializer >> name;
155
156 spark::core::GameObject* game_object = nullptr;
157
158 auto children = obj.children();
159 if (auto it = std::ranges::find_if(children,
160 [&](const spark::core::GameObject* go)
161 {
162 return &go->rttiInstance() == spark::rtti::RttiDatabase::Get(type) && go->name() == name;
163 }); it != children.end())
164 game_object = *it;
165 else
166 game_object = spark::core::Application::Instance()->registries().gameObject.create(type, std::move(name), &obj).release();
167
168 spark::core::Application::Instance()->registries().gameObject.deserializer(type)(deserializer, *game_object);
169 }
170 }
171};
172
173template <typename SerializerType>
174struct experimental::ser::SerializerScheme<SerializerType, spark::core::Scene>
175{
176 static void serialize(SerializerType& serializer, const spark::core::Scene& obj)
177 {
178 serializer << *obj.m_root;
179 serializer << obj.m_isLoaded;
180 }
181
182 static void deserialize(SerializerType& deserializer, spark::core::Scene& obj)
183 {
184 SPARK_ASSERT(obj.m_root != nullptr && "Root GameObject must be set before deserialization")
185
186 deserializer >> *obj.m_root;
187 deserializer >> obj.m_isLoaded;
188 }
189};
static Application * Instance()
Gets the instance of the application.
Definition Application.cpp:53
Registries & registries()
Gets a reference to the registries for the application.
Definition Application.cpp:131
A component that can be attached to a GameObject to provide additional functionality.
Definition Component.h:20
A GameObject is any object in the game. It contains a list of components that provides functionality ...
Definition GameObject.h:26
const std::string & name() const
Gets the name of the GameObject.
Definition GameObject.cpp:58
void addComponent(Component *component, bool managed=false)
Adds a component to the GameObject.
Definition GameObject.cpp:68
bool isShown
A boolean indicating if the GameObject is shown in the scene.
Definition GameObject.h:195
std::vector< Component * > components() const
Gets all the components for this GameObject.
Definition GameObject.cpp:87
Definition Scene.h:13
A simple component to render a circle.
Definition Circle.h:15
A component representing a square collider.
Definition Collider.h:22
A square collider that moves and checks for collisions with other colliders.
Definition Collider.h:88
A simple component to render an image.
Definition Image.h:13
A simple component to render a square/rectangle.
Definition Rectangle.h:16
A square collider that does not move and is only used for collision detection.
Definition Collider.h:73
Definition Text.h:11
A component that stores the position of a game object.
Definition Transform.h:21
Represents a rectangle in 2D space.
Definition Rectangle.h:15
A vector with two components.
Definition Vector2.h:13
BasePtr create(const Key &key, Args &&... args) const
Creates an object of type BaseType. Throws an exception if the type is not registered.
Definition Factory.h:21
virtual RttiBase & rttiInstance() const =0
static RttiBase * Get(const std::string &class_name)
Gets an RTTI instance by class name.
Definition RttiDatabase.cpp:5