Kale
Loading...
Searching...
No Matches
Scene.hpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
22
23#include <list>
24#include <vector>
25#include <queue>
26#include <utility>
27#include <memory>
28#include <mutex>
29#include <condition_variable>
30
31#include <nlohmann/json.hpp>
32
33namespace Kale {
34
39 using JSON = nlohmann::json;
40
45 class Scene : public EventHandler {
46 private:
47
53 inline static std::unordered_map<std::string, std::function<std::shared_ptr<Node>(JSON)>> nodeMap;
54
58 std::list<std::shared_ptr<Node>> nodes;
59
63 std::vector<std::list<std::shared_ptr<Node>>> updateNodes;
64
68 std::vector<std::list<std::shared_ptr<Node>>> preUpdateNodes;
69
73 std::vector<std::pair<float, float>> threadedNodePerformanceTimes;
74
78 std::queue<std::shared_ptr<Node>> nodesToAdd;
79
83 std::queue<std::shared_ptr<Node>> nodesToRemove;
84
89
94
98 std::condition_variable nodePreUpdateCondVar;
99
104
109
115
120 void render(float deltaTime) const;
121
127 void update(size_t threadNum, float deltaTime);
128
134
135 friend class Application;
136 friend class Node;
137
138 protected:
139
143 Vector4f bgColor = 0xFFFFFF;
144
149
157
163
168 template <typename T> void addNode(std::shared_ptr<T>& node) {
169 std::shared_ptr<Kale::Node> nodePtr = std::dynamic_pointer_cast<Kale::Node>(node);
170 std::lock_guard guard(nodeQueueUpdateMutex);
171 nodesToAdd.push(nodePtr);
172 }
173
178 void removeNode(std::shared_ptr<Node>& node);
179
184 template <typename T> void removeNode(std::shared_ptr<T>& node) {
185 std::shared_ptr<Kale::Node> nodePtr = std::dynamic_pointer_cast<Kale::Node>(node);
186 std::lock_guard guard(nodeQueueUpdateMutex);
187 nodesToRemove.push(nodePtr);
188 }
189
193 virtual void onPresent();
194
198 virtual void onSceneChange();
199
207 virtual void onUpdate(size_t threadNum, float deltaTime);
208
216 virtual void onPreUpdate(size_t threadNum, float deltaTime);
217
221 void onWindowResize(Vector2ui oldSize, Vector2ui newSize) override;
222
223 friend class Application;
224
225 public:
226
230 Scene();
231
236 Scene(const std::string& filename);
237
242 const std::list<std::shared_ptr<Node>>& getNodes() const;
243
248 Color getBgColor() const;
249
254 const Camera& getCamera() const;
255
260 Vector2f getViewport() const;
261
267 Rect getSceneBounds() const;
268
274 static void addNodeSaveStateConstructor(const std::string& key, std::function<std::shared_ptr<Node>(JSON)> constructor) {
275 nodeMap[key] = constructor;
276 }
277
278 };
279}
Rect sceneBounds
Definition Scene.hpp:162
virtual void onPresent()
Definition Scene.cpp:282
void update(size_t threadNum, float deltaTime)
Definition Scene.cpp:228
std::mutex nodePreUpdateMutex
Definition Scene.hpp:93
void updateNodeStructures()
Definition Scene.cpp:117
std::list< std::shared_ptr< Node > > nodes
Definition Scene.hpp:58
virtual void onSceneChange()
Definition Scene.cpp:289
std::condition_variable nodePreUpdateCondVar
Definition Scene.hpp:98
std::queue< std::shared_ptr< Node > > nodesToRemove
Definition Scene.hpp:83
Vector4f bgColor
Definition Scene.hpp:143
void removeNode(std::shared_ptr< Node > &node)
const std::list< std::shared_ptr< Node > > & getNodes() const
Definition Scene.cpp:297
void removeNode(std::shared_ptr< T > &node)
Definition Scene.hpp:184
void addNode(std::shared_ptr< T > &node)
Definition Scene.hpp:168
Color getBgColor() const
Definition Scene.cpp:305
void render(float deltaTime) const
Definition Scene.cpp:203
Transform worldToScreen
Definition Scene.hpp:114
size_t generation
Definition Scene.hpp:108
Rect getSceneBounds() const
Definition Scene.cpp:330
std::mutex nodeQueueUpdateMutex
Definition Scene.hpp:88
static void addNodeSaveStateConstructor(const std::string &key, std::function< std::shared_ptr< Node >(JSON)> constructor)
Definition Scene.hpp:274
std::queue< std::shared_ptr< Node > > nodesToAdd
Definition Scene.hpp:78
Camera camera
Definition Scene.hpp:148
void onWindowResize(Vector2ui oldSize, Vector2ui newSize) override
Definition Scene.cpp:103
virtual void onPreUpdate(size_t threadNum, float deltaTime)
Definition Scene.cpp:275
static std::unordered_map< std::string, std::function< std::shared_ptr< Node >(JSON)> > nodeMap
Definition Scene.hpp:53
virtual void onUpdate(size_t threadNum, float deltaTime)
Definition Scene.cpp:264
std::vector< std::list< std::shared_ptr< Node > > > updateNodes
Definition Scene.hpp:63
std::vector< std::pair< float, float > > threadedNodePerformanceTimes
Definition Scene.hpp:73
Vector2f getViewport() const
Definition Scene.cpp:321
size_t nodesPreUpdated
Definition Scene.hpp:103
std::vector< std::list< std::shared_ptr< Node > > > preUpdateNodes
Definition Scene.hpp:68
const Camera & getCamera() const
Definition Scene.cpp:313
Vector2f viewport
Definition Scene.hpp:156
nlohmann::json JSON
Definition Scene.hpp:39