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
Scene.cpp
Go to the documentation of this file.
1
7
8#include <entt/locator/locator.hpp>
9#include <nlohmann/json.hpp>
10#include <sol/sol.hpp>
11
12#include "Scene.hpp"
13
14namespace galaxy
15{
16 namespace scene
17 {
18 Scene::Scene(const std::string& name)
19 : Serializable {}
20 , m_name {name}
21 {
22 /*
23 auto& w = core::ServiceLocator<core::Window>::ref();
24 m_camera.set_viewport(w.frame_width(), w.frame_height());
25 auto& nui = core::ServiceLocator<ui::NuklearUI>::ref();
26 m_dispatcher.sink<events::WindowResized>().connect<&Scene::on_window_resized>(this);
27 m_dispatcher.sink<events::MousePressed>().connect<&ui::NuklearUI::on_mouse_pressed>(nui);
28 m_dispatcher.sink<events::MouseWheel>().connect<&ui::NuklearUI::on_mouse_wheel>(nui);
29 m_dispatcher.sink<events::KeyChar>().connect<&ui::NuklearUI::on_key_char>(nui);
30 m_dispatcher.sink<events::KeyPress>().connect<&ui::NuklearUI::on_key_press>(nui);
31 m_dispatcher.sink<events::ContentScale>().connect<&ui::NuklearUI::on_content_scale>(nui);
32 */
33 }
34
36 {
37 m_systems.clear();
38 }
39
40 void Scene::add_system(const std::string& system)
41 {
42 auto& sf = entt::locator<meta::SystemFactory>::value();
43 sf.create_system(system, m_systems);
44 }
45
47 {
48 }
49
51 {
52 entt::locator<sol::state>::value().collect_garbage();
53 }
54
56 {
57 // graphics::Renderer::ref().flush();
58 // m_registry.update(m_b2world);
59 // m_b2world.Step(GALAXY_DT, m_velocity_iterations, m_position_iterations);
60 for (auto&& system : m_systems)
61 {
62 // system->update(registry);
63 }
64 }
65
66 /*void Scene::update_ui()
67 {
68 const auto view = m_registry.m_entt.view<components::GUI>(entt::exclude<flags::Disabled>);
69 for (auto&& [entity, gui] : view.each())
70 {
71 if (gui.m_update.valid())
72 {
73 gui.m_update(gui.m_self);
74 }
75 }
76 }*/
77
79 {
80 /*
81 graphics::Renderer::ref().begin_post();
82
83 // Scene specific.
84 graphics::Renderer::ref().submit_camera(m_camera);
85 graphics::Renderer::ref().draw();
86
87 graphics::Renderer::ref().end_post();
88 graphics::Renderer::ref().begin_default();
89 graphics::Renderer::ref().render_post();
90 graphics::Renderer::ref().end_default();
91
92 // Scene specific.
93 auto& nui = core::ServiceLocator<ui::NuklearUI>::ref();
94
95 nui.new_frame();
96 update_ui();
97 nui.render();
98
99 graphics::Renderer::ref().end_default();
100 */
101 }
102
103 const std::string& Scene::name() const noexcept
104 {
105 return m_name;
106 }
107
108 /*bool Scene::load_world(const std::string& file)
109 {
110 if (m_world.load(file))
111 {
112 m_world.parse();
113 return true;
114 }
115
116 return false;
117 }*/
118
119 nlohmann::json Scene::serialize()
120 {
121 /*
122 json["camera"] = m_camera.serialize();
123 json["physics"] = nlohmann::json::object();
124 auto& physics = json.at("physics");
125
126 auto gravity = m_b2world.GetGravity();
127 physics["gravity"]["x"] = gravity.x;
128 physics["gravity"]["y"] = gravity.y;
129
130 physics["allow_sleeping"] = m_b2world.GetAllowSleeping();
131 physics["allow_autoclearforces"] = m_b2world.GetAutoClearForces();
132 physics["velocity_iterations"] = m_velocity_iterations;
133 physics["position_iterations"] = m_position_iterations;
134 json["name"] = m_name;
135 json["ldtk_world"] = m_world.file();
136 json["current_map"] = m_world.get_active() ? m_world.get_active()->name() : "";
137 */
138
139 nlohmann::json json = "{}"_json;
140 json["name"] = m_name;
141
142 json["systems"] = nlohmann::json::object();
143 for (auto i = 0; i < m_systems.size(); i++)
144 {
145 json["systems"][std::to_string(i)] = m_systems[i]->id();
146 }
147
148 return json;
149 }
150
151 void Scene::deserialize(const nlohmann::json& json)
152 {
153 m_name = json.at("name");
154
155 const auto& systems = json.at("systems");
156 m_systems.reserve(systems.size());
157 for (const auto& [index, name] : systems.items())
158 {
159 add_system(name.get<std::string>());
160 }
161
162 /*
163 m_camera.deserialize(json.at("camera"));
164 auto& em = core::ServiceLocator<meta::EntityMeta>::ref();
165
166 const auto& physics = json.at("physics");
167 const auto& gravity = physics.at("gravity");
168
169 m_b2world.SetGravity({gravity.at("x"), gravity.at("y")});
170 m_b2world.SetAllowSleeping(physics.at("allow_sleeping"));
171 m_b2world.SetAutoClearForces(physics.at("allow_autoclearforces"));
172 m_velocity_iterations = physics.at("velocity_iterations");
173 m_position_iterations = physics.at("position_iterations");
174
175 if (load_world(json.at("ldtk_world")))
176 {
177 m_world.set_active(json.at("current_map"));
178 }
179 */
180 }
181 } // namespace scene
182} // namespace galaxy
Wrapper around entt::registry to expand functionality.
Definition Registry.hpp:21
void add_system(const std::string &system)
Add a system to operate on entities in this scene.
Definition Scene.cpp:40
void load()
When scene is loaded and made active.
Definition Scene.cpp:46
Scene()=delete
Constructor.
const std::string & name() const noexcept
Get scene name.
Definition Scene.cpp:103
virtual ~Scene()
Destructor.
Definition Scene.cpp:35
void render()
Update ui.
Definition Scene.cpp:78
meta::SystemStack m_systems
List of systems to run.
Definition Scene.hpp:115
std::string m_name
Scene name for debug purposes.
Definition Scene.hpp:110
nlohmann::json serialize() override
Serializes object.
Definition Scene.cpp:119
void update(core::Registry &registry)
Process events and updates.
Definition Scene.cpp:55
void deserialize(const nlohmann::json &json) override
Deserializes from object.
Definition Scene.cpp:151
void unload()
When scene is deactivated / unloaded.
Definition Scene.cpp:50
Timer.hpp galaxy.
Definition Async.hpp:17
Application.hpp galaxy.