galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
World.cpp
Go to the documentation of this file.
1
7
8#include "World.hpp"
9
10namespace galaxy
11{
12 World::World() noexcept
13 : StateMachine {}
14 {
15 }
16
18 : StateMachine {std::move(w)}
19 {
20 }
21
23 {
24 if (this != &w)
25 {
26 StateMachine::operator=(std::move(w));
27 }
28
29 return *this;
30 }
31
33 {
34 clear();
35 }
36
38 {
39 if (auto state = top())
40 {
41 state->update(m_registry);
42 }
43 }
44
46 {
47 if (auto state = top())
48 {
49 state->render();
50 }
51 }
52
54 {
55 pop_all();
56
57 m_stack.clear();
58 m_storage.clear();
59 }
60} // namespace galaxy
61
62/*
63void SceneManager::load_app(const std::string& appdata_file)
64 {
65 const auto data = core::ServiceLocator<fs::VirtualFileSystem>::ref().read(appdata_file);
66 if (!data.empty())
67 {
68 const auto decoded_zlib = math::decode_zlib(data);
69 if (!decoded_zlib.empty())
70
71 {
72 const auto decoded_base64 = math::decode_base64(decoded_zlib);
73 if (!decoded_base64.empty())
74 {
75 const auto parsed = nlohmann::json::parse(decoded_base64);
76
77 if (!parsed.empty())
78 {
79 deserialize(parsed);
80 }
81 else
82 {
83 GALAXY_LOG(GALAXY_ERROR, "Failed to parse scenemanger JSON data from memory.");
84 }
85 }
86 else
87 {
88 GALAXY_LOG(GALAXY_ERROR, "Failed to decode base64 appdata '{0}'.", appdata_file);
89 }
90 }
91 else
92 {
93 GALAXY_LOG(GALAXY_ERROR, "Failed to decode zlib appdata '{0}'.", appdata_file);
94 }
95 }
96 else
97 {
98 GALAXY_LOG(GALAXY_ERROR, "Failed to load appdata '{0}'.", appdata_file);
99 }
100 }
101
102 void SceneManager::save_app(const std::string& file)
103 {
104 const auto json = serialize();
105
106 if (!json::write(file, json))
107 {
108 GALAXY_LOG(GALAXY_ERROR, "Failed to save '{0}' to disk.", file);
109 }
110 }
111
112 void SceneManager::update()
113 {
114 for (auto&& system : m_systems)
115 {
116 system->update(m_current->m_registry.m_entt);
117
118 if (m_current->m_world.get_active())
119 {
120 system->update(m_current->m_world.get_active()->m_registry.m_entt);
121 }
122 }
123 }
124
125 void SceneManager::only_update_rendering()
126 {
127 if ((m_rendersystem_index >= 0 && m_rendersystem_index < m_systems.size()) && m_current != nullptr)
128 {
129 m_systems[m_rendersystem_index]->update(m_current->m_registry.m_entt);
130
131 if (m_current->m_world.get_active())
132 {
133 m_systems[m_rendersystem_index]->update(m_current->m_world.get_active()->m_registry.m_entt);
134 }
135 }
136 }
137
138 nlohmann::json SceneManager::serialize()
139 {
140 nlohmann::json json = "{\"scenes\":{}}"_json;
141
142 for (auto& [name, scene] : m_scenes)
143 {
144 json["scenes"][scene->m_name] = scene->serialize();
145 }
146
147 if (m_current)
148 {
149 json["current"] = m_current->m_name;
150 }
151
152 return json;
153 }
154
155 void SceneManager::deserialize(const nlohmann::json& json)
156 {
157 clear();
158
159 const auto& scenes = json.at("scenes");
160
161 m_scenes.reserve(scenes.size());
162 for (const auto& [name, data] : scenes.items())
163 {
164 auto scene = add(name);
165 if (scene)
166 {
167 scene->deserialize(data);
168 }
169 }
170
171 if (json.contains("current"))
172 {
173 set_scene(json.at("current"));
174 }
175 }
176
177 void SceneManager::load_app(const std::string& appdata_file)
178 {
179 std::ifstream in(appdata_file, std::ifstream::in);
180
181 std::stringstream buffer;
182 buffer << in.rdbuf();
183
184 auto data = buffer.str();
185 if (!data.empty())
186 {
187 if constexpr (!GALAXY_DEBUG_BUILD)
188 {
189 data = math::decode_zlib(data);
190 if (!data.empty())
191 {
192 data = math::decode_base64(data);
193 if (data.empty())
194 {
195 GALAXY_LOG(GALAXY_ERROR, "Failed to decode base64 appdata '{0}'.", appdata_file);
196 }
197 }
198 else
199 {
200 GALAXY_LOG(GALAXY_ERROR, "Failed to decode zlib appdata '{0}'.", appdata_file);
201 }
202
203 if (data.empty())
204 {
205 return;
206 }
207 }
208
209 const auto parsed = nlohmann::json::parse(data);
210
211 if (!parsed.empty())
212 {
213 deserialize(parsed);
214 }
215 else
216 {
217 GALAXY_LOG(GALAXY_ERROR, "Failed to parse scenemanger JSON data from memory.");
218 }
219 }
220 else
221 {
222 GALAXY_LOG(GALAXY_ERROR, "Failed to load appdata '{0}'.", appdata_file);
223 }
224 }
225
226 void SceneManager::save_app(const std::string& file)
227 {
228 const auto json = serialize();
229 auto data = json::dump(json);
230
231 if constexpr (!GALAXY_DEBUG_BUILD)
232 {
233 data = math::encode_base64(data);
234 if (!data.empty())
235 {
236 data = math::encode_zlib(data);
237 if (data.empty())
238 {
239 GALAXY_LOG(GALAXY_ERROR, "Failed to encode scenemanager to zlib '{0}'.", file);
240 }
241 }
242 else
243 {
244 GALAXY_LOG(GALAXY_ERROR, "Failed to encode scenemanager to base64 '{0}'.", file);
245 }
246
247 if (data.empty())
248 {
249 return;
250 }
251 }
252
253 auto& fs = entt::locator<fs::VirtualFileSystem>::value();
254 if (!fs.write(data, file))
255 {
256 GALAXY_LOG(GALAXY_ERROR, "Failed to save '{0}' to disk.", file);
257 }
258 }
259
260 nlohmann::json SceneManager::serialize()
261 {
262 auto& em = entt::locator<meta::EntityFactory>::value();
263
264 nlohmann::json json = "{\"scenes\":{}}"_json;
265
266 for (auto& [id, scene] : m_scenes)
267 {
268 json["scenes"][scene->name()] = scene->serialize();
269 }
270
271 for (auto i = 0; i < m_stack.size(); i++)
272 {
273 json["stack"][std::to_string(i)] = m_stack[i]->name();
274 }
275
276 json["entities"] = nlohmann::json::array();
277 for (const auto& [entity] : m_registry.m_entt.view<entt::entity>(entt::exclude<flags::NotSerializable>).each())
278 {
279 json["entities"].push_back(em.serialize_entity(entity, m_registry.m_entt));
280 }
281
282 return json;
283 }
284
285 void SceneManager::deserialize(const nlohmann::json& json)
286 {
287 auto& em = entt::locator<meta::EntityFactory>::value();
288
289 pop_all();
290 clear();
291
292 const auto& scenes = json.at("scenes");
293 m_scenes.reserve(scenes.size());
294 for (const auto& [name, data] : scenes.items())
295 {
296 auto scene = create(name);
297 if (scene)
298 {
299 scene->deserialize(data);
300 }
301 }
302
303 const auto& stack = json.at("stack");
304 m_stack.reserve(stack.size());
305 for (const auto& [index, name] : stack.items())
306 {
307 const auto hash = math::fnv1a(name.get<std::string>().c_str());
308 m_stack.insert(m_stack.begin() + std::stoi(index), m_scenes[hash]);
309 }
310
311 const auto& entity_json = json.at("entities");
312 for (const auto& data : entity_json)
313 {
314 const auto entity = em.deserialize_entity(data, m_registry.m_entt);
315
316 if (!m_registry.m_entt.all_of<components::Tag>(entity))
317 {
318 auto& tag = m_registry.m_entt.emplace<components::Tag>(entity);
319 tag.m_tag = "Untagged";
320 }
321 }
322 }
323*/
A finite state machine.
StateMachine< Stored > & operator=(StateMachine< Stored > &&)
Move assignment operator.
std::shared_ptr< Stored > top() const noexcept
Scene, Entity and global game management.
Definition World.hpp:20
void clear()
Removes all data.
Definition World.cpp:53
World() noexcept
Constructor.
Definition World.cpp:12
void render()
Render scenes.
Definition World.cpp:45
virtual ~World()
Destructor.
Definition World.cpp:32
World & operator=(World &&)
Move assignment operator.
Definition World.cpp:22
void update() override
Process events and updates.
Definition World.cpp:37
Registry m_registry
Entity data.
Definition World.hpp:72
Animated.cpp galaxy.
Definition Animated.cpp:16
STL namespace.