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
SceneManager.cpp
Go to the documentation of this file.
1
7
8#include <nlohmann/json.hpp>
9
10#include "galaxy/core/ServiceLocator.hpp"
14#include "galaxy/math/ZLib.hpp"
15#include "galaxy/scripting/JSON.hpp"
19
20#include "SceneManager.hpp"
21
22namespace galaxy
23{
24 namespace scene
25 {
34
36 {
37 m_scenes.clear();
38 }
39
40 Scene* SceneManager::add(const std::string& name)
41 {
42 const auto hash = math::fnv1a_64(name.c_str());
43
44 if (!m_scenes.contains(hash))
45 {
46 m_scenes[hash] = std::make_unique<Scene>(name);
47 }
48 else
49 {
50 GALAXY_LOG(GALAXY_WARNING, "Tried to add a duplicate scene '{0}'.", name);
51 }
52
53 return m_scenes[hash].get();
54 }
55
56 Scene* SceneManager::get(const std::string& name)
57 {
58 const auto hash = math::fnv1a_64(name.c_str());
59
60 if (m_scenes.contains(hash))
61 {
62 return m_scenes[hash].get();
63 }
64 else
65 {
66 GALAXY_LOG(GALAXY_ERROR, "Failed to find '{0}'.", name);
67 return nullptr;
68 }
69 }
70
71 void SceneManager::remove(const std::string& name)
72 {
73 m_scenes.erase(math::fnv1a_64(name.c_str()));
74 }
75
76 bool SceneManager::has(const std::string& name)
77 {
78 return m_scenes.contains(math::fnv1a_64(name.c_str()));
79 }
80
81 void SceneManager::set_scene(const std::string& name)
82 {
83 const auto hash = math::fnv1a_64(name.c_str());
84 if (m_scenes.contains(hash))
85 {
86 m_current = m_scenes[hash].get();
87 }
88 }
89
90 void SceneManager::load_app(const std::string& appdata_file)
91 {
92 const auto data = core::ServiceLocator<fs::VirtualFileSystem>::ref().read(appdata_file);
93 if (!data.empty())
94 {
95 const auto decoded_zlib = math::decode_zlib(data);
96 if (!decoded_zlib.empty())
97
98 {
99 const auto decoded_base64 = math::decode_base64(decoded_zlib);
100 if (!decoded_base64.empty())
101 {
102 const auto parsed = nlohmann::json::parse(decoded_base64);
103
104 if (!parsed.empty())
105 {
106 deserialize(parsed);
107 }
108 else
109 {
110 GALAXY_LOG(GALAXY_ERROR, "Failed to parse scenemanger JSON data from memory.");
111 }
112 }
113 else
114 {
115 GALAXY_LOG(GALAXY_ERROR, "Failed to decode base64 appdata '{0}'.", appdata_file);
116 }
117 }
118 else
119 {
120 GALAXY_LOG(GALAXY_ERROR, "Failed to decode zlib appdata '{0}'.", appdata_file);
121 }
122 }
123 else
124 {
125 GALAXY_LOG(GALAXY_ERROR, "Failed to load appdata '{0}'.", appdata_file);
126 }
127 }
128
129 void SceneManager::save_app(const std::string& file)
130 {
131 const auto json = serialize();
132
133 if (!json::write(file, json))
134 {
135 GALAXY_LOG(GALAXY_ERROR, "Failed to save '{0}' to disk.", file);
136 }
137 }
138
140 {
141 if (m_current)
142 {
143 m_current->update();
144
145 for (auto&& system : m_systems)
146 {
147 system->update(m_current->m_registry.m_entt);
148
150 {
151 system->update(m_current->m_world.get_active()->m_registry.m_entt);
152 }
153 }
154 }
155 }
156
169
171 {
172 if (m_current)
173 {
174 m_current->render();
175 }
176 }
177
179 {
180 m_current = nullptr;
181 m_scenes.clear();
182 }
183
185 {
186 return m_current;
187 }
188
190 {
191 return m_scenes;
192 }
193
195 {
196 return m_scenes.size() == 0;
197 }
198
199 nlohmann::json SceneManager::serialize()
200 {
201 nlohmann::json json = "{\"scenes\":{}}"_json;
202
203 for (auto& [name, scene] : m_scenes)
204 {
205 json["scenes"][scene->m_name] = scene->serialize();
206 }
207
208 if (m_current)
209 {
210 json["current"] = m_current->m_name;
211 }
212
213 return json;
214 }
215
216 void SceneManager::deserialize(const nlohmann::json& json)
217 {
218 clear();
219
220 const auto& scenes = json.at("scenes");
221
222 m_scenes.reserve(scenes.size());
223 for (const auto& [name, data] : scenes.items())
224 {
225 auto scene = add(name);
226 if (scene)
227 {
228 scene->deserialize(data);
229 }
230 }
231
232 if (json.contains("current"))
233 {
234 set_scene(json.at("current"));
235 }
236 }
237 } // namespace scene
238} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_ERROR
Definition Log.hpp:24
entt::registry m_entt
Scene entities.
Definition Registry.hpp:166
core::Registry m_registry
Map entities.
Definition Map.hpp:103
map::Map * get_active() const
Get currently active map.
Definition World.cpp:97
void create_system(Args &&... args)
Add a system to the manager.
void only_update_rendering()
Only update rendering.
void save_app(const std::string &file)
Save all active scenes and sub data within those scenes.
bool has(const std::string &name)
Does a scene exist.
scene::Scene * current() const
Get current scene.
virtual ~SceneManager()
Destructor.
std::size_t m_rendersystem_index
Rendersystem index.
SystemContainer m_systems
Stores systems.
void clear()
Deletes all scene data.
void deserialize(const nlohmann::json &json) override
Deserializes from object.
void update()
Handle events and update logic.
void render()
Handle rendering.
nlohmann::json serialize() override
Serializes object.
void load_app(const std::string &appdata_file)
Load app data file into scene manager.
Scene * get(const std::string &name)
Get a specific scene.
Scene * add(const std::string &name)
Add a new scene.
ankerl::unordered_dense::map< std::uint64_t, std::unique_ptr< Scene > > Map
void set_scene(const std::string &name)
Scene to set to currently active.
void remove(const std::string &name)
Remove a specific scene.
bool empty() const
Are there any scenes.
const Map & map() const
Get all scenes.
scene::Scene * m_current
Current scene.
Represents a scene in a game. Like the menu, game, etc. Does not share resources.
Definition Scene.hpp:29
map::World m_world
LDTK world.
Definition Scene.hpp:119
virtual void render()
Render scene.
Definition Scene.cpp:68
virtual void update()
Process events and updates.
Definition Scene.cpp:61
std::string m_name
Scene name for debug purposes.
Definition Scene.hpp:94
core::Registry m_registry
Entity data.
Definition Scene.hpp:109
constexpr std::uint64_t fnv1a_64(const char *const str) noexcept
Convert string to 64bit hash.
Definition FNV1a.hpp:92
std::string decode_base64(const std::string &input)
Decompresses string into Base64.
Definition Base64.cpp:105
Animated.cpp galaxy.
Definition Animated.cpp:16