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 <nlohmann/json.hpp>
9#include <sol/sol.hpp>
10
12#include "galaxy/core/ServiceLocator.hpp"
14#include "galaxy/flags/DenySerialization.hpp"
17#include "galaxy/meta/EntityMeta.hpp"
20#include "galaxy/scripting/JSON.hpp"
21#include "galaxy/scripting/Lua.hpp"
22
23#include "Scene.hpp"
24
25#ifdef GALAXY_WIN_PLATFORM
26#pragma warning(push)
27#pragma warning(disable : 26487)
28#pragma warning(disable : 4244)
29#endif
30
31namespace galaxy
32{
33 namespace scene
34 {
35 Scene::Scene(const std::string& name)
36 : Serializable {}
37 , m_name {name}
38 , m_camera {}
39 , m_b2world {b2Vec2 {0.0f, 0.0f}}
40 , m_velocity_iterations {8}
41 , m_position_iterations {3}
42 {
43 auto& w = core::ServiceLocator<core::Window>::ref();
44 m_camera.set_viewport(w.frame_width(), w.frame_height());
45 }
46
48 {
49 }
50
52 {
53 core::ServiceLocator<core::Window>::ref().set_dispatcher(&m_dispatcher);
54 }
55
57 {
58 core::ServiceLocator<sol::state>::ref().collect_garbage();
59 }
60
67
81
82 bool Scene::load_world(const std::string& file)
83 {
84 if (m_world.load(file))
85 {
86 m_world.parse();
87 return true;
88 }
89
90 return false;
91 }
92
93 nlohmann::json Scene::serialize()
94 {
95 nlohmann::json json = "{}"_json;
96 json["camera"] = m_camera.serialize();
97 json["entities"] = nlohmann::json::array();
98
99 auto& em = core::ServiceLocator<meta::EntityMeta>::ref();
100
101 for (const auto& [entity] : m_registry.m_entt.view<entt::entity>(entt::exclude<flags::DenySerialization>).each())
102 {
103 json["entities"].push_back(em.serialize_entity(entity, m_registry.m_entt));
104 }
105
106 json["physics"] = nlohmann::json::object();
107 auto& physics = json.at("physics");
108
109 auto gravity = m_b2world.GetGravity();
110 physics["gravity"]["x"] = gravity.x;
111 physics["gravity"]["y"] = gravity.y;
112
113 physics["allow_sleeping"] = m_b2world.GetAllowSleeping();
114 physics["allow_autoclearforces"] = m_b2world.GetAutoClearForces();
115 physics["velocity_iterations"] = m_velocity_iterations;
116 physics["position_iterations"] = m_position_iterations;
117 json["name"] = m_name;
118 json["ldtk_world"] = m_world.file();
119 json["current_map"] = m_world.get_active() ? m_world.get_active()->name() : "";
120
121 return json;
122 }
123
124 void Scene::deserialize(const nlohmann::json& json)
125 {
126 m_camera.deserialize(json.at("camera"));
127 auto& em = core::ServiceLocator<meta::EntityMeta>::ref();
128
130
131 const auto& physics = json.at("physics");
132 const auto& gravity = physics.at("gravity");
133
134 m_b2world.SetGravity({gravity.at("x"), gravity.at("y")});
135 m_b2world.SetAllowSleeping(physics.at("allow_sleeping"));
136 m_b2world.SetAutoClearForces(physics.at("allow_autoclearforces"));
137 m_velocity_iterations = physics.at("velocity_iterations");
138 m_position_iterations = physics.at("position_iterations");
139
140 const auto& entity_json = json.at("entities");
141 for (const auto& data : entity_json)
142 {
143 const auto entity = em.deserialize_entity(data, m_registry.m_entt);
144
145 if (!m_registry.m_entt.all_of<components::Tag>(entity))
146 {
147 auto& tag = m_registry.m_entt.emplace<components::Tag>(entity);
148 tag.m_tag = "Untagged";
149 }
150 }
151
152 m_name = json.at("name");
153
154 if (load_world(json.at("ldtk_world")))
155 {
156 m_world.set_active(json.at("current_map"));
157 }
158 }
159
160 } // namespace scene
161} // namespace galaxy
162
163#ifdef GALAXY_WIN_PLATFORM
164#pragma warning(pop)
165#endif
Tag an entity.
Definition Tag.hpp:21
std::string m_tag
Tag.
Definition Tag.hpp:87
void clear()
Clear any pending data.
Definition Registry.cpp:127
void update(b2World &b2World)
Updates pending component data.
Definition Registry.cpp:96
entt::registry m_entt
Scene entities.
Definition Registry.hpp:166
void set_viewport(const float width, const float height)
Set Viewport.
Definition Camera.cpp:92
nlohmann::json serialize() override
Serializes object.
Definition Camera.cpp:158
void deserialize(const nlohmann::json &json) override
Deserializes from object.
Definition Camera.cpp:173
void end_default()
Swaps buffers.
Definition Renderer.cpp:238
void begin_post()
Begin rendering to post process framebuffer.
Definition Renderer.cpp:200
void render_post()
Renders final post processing output to active framebuffer.
Definition Renderer.cpp:212
void end_post()
Renders effects to post processing framebuffer and rebinds to default framebuffer.
Definition Renderer.cpp:206
void flush()
Deletes all submitted render commands.
Definition Renderer.cpp:189
void submit_camera(Camera &camera)
Set the camera to use when calling draw().
Definition Renderer.cpp:76
void draw()
Draw all submitted render commands to screen.
Definition Renderer.cpp:166
static Renderer & ref()
Get reference to renderer singleton.
Definition Renderer.cpp:19
void begin_default()
Start rendering to default framebuffer.
Definition Renderer.cpp:217
const std::string & name() const
Get map name.
Definition Map.cpp:150
bool load(const std::string &file)
Load a world.
Definition World.cpp:37
void set_active(const std::string &map)
Set currently active map.
Definition World.cpp:85
map::Map * get_active() const
Get currently active map.
Definition World.cpp:97
void parse()
Parse world and create entities.
Definition World.cpp:63
const std::string & file() const
Get file.
Definition World.cpp:107
map::World m_world
LDTK world.
Definition Scene.hpp:119
virtual void load()
When scene is loaded and made active.
Definition Scene.cpp:51
Scene()=delete
Constructor.
virtual ~Scene()
Destructor.
Definition Scene.cpp:47
virtual void render()
Render scene.
Definition Scene.cpp:68
virtual void update()
Process events and updates.
Definition Scene.cpp:61
int m_position_iterations
Box2d world position iterations.
Definition Scene.hpp:129
graphics::Camera m_camera
Camera.
Definition Scene.hpp:99
std::string m_name
Scene name for debug purposes.
Definition Scene.hpp:94
nlohmann::json serialize() override
Serializes object.
Definition Scene.cpp:93
entt::dispatcher m_dispatcher
Scene event handler.
Definition Scene.hpp:104
b2World m_b2world
Box2D physics world.
Definition Scene.hpp:114
core::Registry m_registry
Entity data.
Definition Scene.hpp:109
bool load_world(const std::string &file)
Loads an LDTK world for this scene.
Definition Scene.cpp:82
void deserialize(const nlohmann::json &json) override
Deserializes from object.
Definition Scene.cpp:124
int m_velocity_iterations
Box2D world velocity iterations.
Definition Scene.hpp:124
virtual void unload()
When scene is deactivated / unloaded.
Definition Scene.cpp:56
Animated.cpp galaxy.
Definition Animated.cpp:16