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 "SceneManager.hpp"
11
12namespace galaxy
13{
15 : StateMachine {}
16 {
17 }
18
23
25 {
26 if (this != &w)
27 {
28 StateMachine::operator=(std::move(w));
29 }
30
31 return *this;
32 }
33
38
39 void SceneManager::on_event(SDL_Event& event)
40 {
41 if (auto scene = top())
42 {
43 scene->on_event(event);
44 }
45 }
46
48 {
49 if (auto scene = top())
50 {
51 scene->update(m_entity_manager);
52 }
53 }
54
56 {
57 if (auto scene = top())
58 {
59 scene->render();
60 }
61 }
62
64 {
65 pop_all();
66
67 m_stack.clear();
68 m_storage.clear();
69 }
70
71 nlohmann::json SceneManager::serialize()
72 {
73 nlohmann::json json = "{\"scenes\":{}}"_json;
74
75 for (auto& [id, scene] : m_storage)
76 {
77 json["scenes"][scene->name()] = scene->serialize();
78 }
79
80 for (auto i = 0; i < m_stack.size(); i++)
81 {
82 json["stack"][std::to_string(i)] = m_stack[i]->name();
83 }
84
85 /*
86 auto& em = entt::locator<meta::EntityFactory>::value();
87 json["entities"] = nlohmann::json::array();
88 for (const auto& [entity] : m_registry.m_entt.view<entt::entity>(entt::exclude<flags::NotSerializable>).each())
89 {
90 json["entities"].push_back(em.serialize_entity(entity, m_registry.m_entt));
91 }
92 */
93
94 return json;
95 }
96
97 void SceneManager::deserialize(const nlohmann::json& json)
98 {
99 clear();
100
101 const auto& scenes = json.at("scenes");
102 m_stack.reserve(scenes.size());
103 for (const auto& [name, data] : scenes.items())
104 {
105 auto scene = add(name);
106 if (scene)
107 {
108 scene->deserialize(data);
109 }
110 }
111
112 const auto& stack = json.at("stack");
113 m_stack.reserve(stack.size());
114 for (const auto& [index, name] : stack.items())
115 {
116 const auto hash = math::fnv1a(name.get<std::string>().c_str());
117 m_stack.insert(m_stack.begin() + std::stoi(index), m_storage[hash]);
118 }
119
120 /*
121 auto& em = entt::locator<meta::EntityFactory>::value();
122 const auto& entity_json = json.at("entities");
123 for (const auto& data : entity_json)
124 {
125 const auto entity = em.deserialize_entity(data, m_registry.m_entt);
126
127 if (!m_registry.m_entt.all_of<components::Tag>(entity))
128 {
129 auto& tag = m_registry.m_entt.emplace<components::Tag>(entity);
130 tag.m_tag = "Untagged";
131 }
132 }*/
133 }
134} // namespace galaxy
135
136/*
137 void SceneManager::update()
138 {
139 for (auto&& system : m_systems)
140 {
141 system->update(m_current->m_registry.m_entt);
142
143 if (m_current->m_world.get_active())
144 {
145 system->update(m_current->m_world.get_active()->m_registry.m_entt);
146 }
147 }
148 }
149
150 void SceneManager::only_update_rendering()
151 {
152 if ((m_rendersystem_index >= 0 && m_rendersystem_index < m_systems.size()) && m_current != nullptr)
153 {
154 m_systems[m_rendersystem_index]->update(m_current->m_registry.m_entt);
155
156 if (m_current->m_world.get_active())
157 {
158 m_systems[m_rendersystem_index]->update(m_current->m_world.get_active()->m_registry.m_entt);
159 }
160 }
161 }
162*/
void update()
Process events and updates.
void on_event(SDL_Event &event)
Handle an event for a scene.
virtual ~SceneManager()
Destructor.
void clear()
Removes all data.
EntityManager m_entity_manager
Entities belonging to all scenes.
SceneManager & operator=(SceneManager &&)
Move assignment operator.
void render()
Render scenes.
SceneManager() noexcept
Constructor.
void deserialize(const nlohmann::json &json) override
Deserializes from object.
nlohmann::json serialize() override
Serializes object.
std::shared_ptr< Stored > add(const std::string &key, Args &&... args)
StateMachine< Stored > & operator=(StateMachine< Stored > &&)
Move assignment operator.
std::shared_ptr< Stored > top() const noexcept
constexpr bits fnv1a(const char *const str, const bits value=fnv_1a_params< bits >::offset) noexcept
Convert string to hash.
Definition FNV1a.hpp:64
Tag.cpp galaxy.
Definition Tag.cpp:13
STL namespace.