Elevate Engine 1
Loading...
Searching...
No Matches
Scene.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "Scene.h"
3
6
8
11
14
15#include "ScenePrivate.h"
16
17// rapidjson
18#include <rapidjson/filereadstream.h>
19#include <rapidjson/document.h>
20#include <rapidjson/error/en.h>
21#include <rapidjson/rapidjson.h>
22#include <rapidjson/stringbuffer.h>
23#include <rapidjson/writer.h>
24
25namespace Elevate
26{
27 std::uint32_t Scene::s_nextRegistryId = 1;
28
29 Scene::Scene() : Scene("DefaultScene", SceneType::RuntimeScene) { }
30
31 Scene::Scene(std::string name, SceneType type) : m_name(name), m_type(type)
32 {
33 m_registryId = s_nextRegistryId;
34 s_nextRegistryId++;
35 GetRegistryMap()[m_registryId] = std::make_unique<entt::registry>();
36
37 EE_TRACE("Created scene '{}' with registry id: {}", m_name.c_str(), m_registryId);
38 }
39
40 //Scene::~Scene()
41 //{
42 // auto& registryMap = GetRegistryMap();
43 // auto registryIt = registryMap.find(m_registryId);
44
45 // if (registryIt != registryMap.end()) {
46 // registryMap.erase(registryIt);
47 // }
48 //}
49
51 {
53 {
54 return;
55 }
56
57 for (std::shared_ptr<GameObject> obj : m_rootObjects)
58 {
59 obj->Update();
60 }
61 }
62
64 {
65 if (!cam)
66 {
68 }
69
70 // Render the cubemap / skybox
71 if (cam)
72 {
73 // Either do not calculate here or stop calculating it in the layer
74 glm::mat4 view = glm::mat4(glm::mat3(cam->GenViewMatrix()));
75
76 if (m_cubemap)
77 {
78 m_cubemap->SetProjectionMatrix(cam->GetProjectionMatrix());
79 m_cubemap->SetViewMatrix(view);
80 m_cubemap->Draw();
81 }
82 }
83
84 // todo remove
85 //for (std::shared_ptr<GameObject> obj : m_rootObjects)
86 //{
87 // obj->PreRender();
88 //}
89
90 // todo remove
91 //if (m_sceneLighting)
92 //{
93 // Renderer::SetupShaders(this);
94 //}
95
96 for (std::shared_ptr<GameObject> obj : m_rootObjects)
97 {
98 switch (m_type)
99 {
100 case EditorScene:
102 {
103 obj->Render();
104 }
105
106 break;
107 case RuntimeScene:
108 obj->Render();
109
111 {
112 obj->RenderInEditor();
113 }
114
115 break;
116 case DebugScene:
117 obj->Render();
118 break;
119 }
120 }
121 }
122
124 {
125 for (std::shared_ptr<GameObject> obj : m_rootObjects)
126 {
127 obj->Notify(e);
128 }
129 }
130
131 void Scene::AddObject(std::shared_ptr<GameObject> newObject, std::shared_ptr<GameObject> parent)
132 {
133 if (!newObject)
134 return;
135
136 if (!parent)
137 {
138 AddRootObject(newObject);
139 }
140 else
141 {
142 if (newObject->GetScene() == parent->GetScene())
143 {
144 newObject->SetParent(parent);
145 }
146 else
147 {
148 EE_CORE_ERROR("Cannot add a child object from a different scene.");
149 }
150 }
151 }
152
153 ScenePtr Scene::Create(std::string name, SceneType type)
154 {
155 ScenePtr scene = std::make_shared<Scene>(name, type);
157 scene->Serialize();
158 return scene;
159 }
160
161 void Scene::SetSkybox(const std::string& skyboxFilePath)
162 {
163 m_cubemap.reset(Cubemap::CreateFromFile(skyboxFilePath));
164 }
165
166 std::weak_ptr<Cubemap> Scene::GetSkybox()
167 {
168 return m_cubemap;
169 }
170
171 void Scene::RemoveFromRoot(std::shared_ptr<GameObject> object)
172 {
173 m_rootObjects.erase(object);
174 }
175
176 void Scene::AddRootObject(std::shared_ptr<GameObject> newRootObject)
177 {
178 newRootObject->m_parent = nullptr;
179 newRootObject->m_scene = this;
180 m_rootObjects.insert(newRootObject);
181 }
182
183 std::string Scene::Serialize() const
184 {
185 //rapidjson::Document doc;
186 //doc.SetObject();
187 //auto& allocator = doc.GetAllocator();
188
189 //doc.AddMember("name", rapidjson::Value(m_name.c_str(), allocator), allocator);
190 //doc.AddMember("type", rapidjson::Value().SetInt(m_type), allocator);
191
195
196 //rapidjson::StringBuffer buffer;
197 //rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
198 //doc.Accept(writer);
199
200 //EE_CORE_TRACE("{}", buffer.GetString());
201 //return buffer.GetString();
202 return "scene";
203 }
204}
#define EE_TRACE(...)
Definition Log.h:66
static const GameContextState & GetGameState()
static Camera * GetCurrent()
const glm::mat4 & GetProjectionMatrix() const
Definition Camera.h:25
glm::mat4 GenViewMatrix() const
Definition Camera.cpp:69
static Cubemap * CreateFromFile(const std::string &filePath)
Definition Cubemap.cpp:70
static void LoadScene(ScenePtr scene)
void Notify(Event &event)
Definition Scene.cpp:123
static ScenePtr Create(std::string name, SceneType type=SceneType::RuntimeScene)
Definition Scene.cpp:153
void UpdateScene()
Definition Scene.cpp:50
void AddObject(std::shared_ptr< GameObject > newObject, std::shared_ptr< GameObject > parent)
Definition Scene.cpp:131
void RenderScene(Camera *cam=nullptr)
Definition Scene.cpp:63
void SetSkybox(const std::string &skyboxFilePath)
Definition Scene.cpp:161
std::weak_ptr< Cubemap > GetSkybox()
Definition Scene.cpp:166
std::string Serialize() const override
Definition Scene.cpp:183
std::unordered_map< uint32_t, std::unique_ptr< entt::registry > > & GetRegistryMap()
SceneType
Definition Scene.h:28
@ RuntimeScene
Definition Scene.h:29
@ DebugScene
Definition Scene.h:31
@ EditorScene
Definition Scene.h:30
std::shared_ptr< Scene > ScenePtr
Definition Renderer.h:12
@ EditorMode
Definition GameContext.h:8