SpiecsEngine
 
Loading...
Searching...
No Matches
World.h
Go to the documentation of this file.
1/**
2* @file World.h.
3* @brief The World Class Definitions.
4* @author Spices.
5*/
6
7#pragma once
8#include "Core/Core.h"
9#include "Core/UUID.h"
10#include "entt.hpp"
11#include "World/WorldFunctions/WorldFunctions.h"
12
13#include "World/Components/CameraComponent.h"
14#include "World/Components/TransformComponent.h"
15#include "World/Components/MeshComponent.h"
16#include "World/Components/TagComponent.h"
17#include "World/Components/UUIDComponent.h"
18#include "World/Components/DirectionalLightComponent.h"
19#include "World/Components/NativeScriptComponent.h"
20#include "World/Components/PointLightComponent.h"
21#include "World/Components/SkyBoxComponent.h"
22#include "World/Components/SpriteComponent.h"
23#include "World/Components/EntityComponent.h"
24#include "World/Components/LandscapeComponent.h"
25#include "World/Components/ParticleComponent.h"
26#include "World/Components/WidgetComponent.h"
27
28namespace Spices {
29
30 /**
31 * Forward Declare
32 */
33 class Entity;
34
35 /**
36 * @brief World Class.
37 * This class defines the basic behaves of World.
38 * When we create an new world, we need inherit from this.
39 */
40 class World
41 {
42 public:
43
45 {
46 Clean = 0x00000001,
47 MeshAddedToWorld = 0x00000002,
48 FrushStableFrame = 0x00000004,
49 NeedUpdateTLAS = 0x00000008,
50 MAX = 0x7FFFFFFF
51 };
52
53 typedef uint32_t WorldMarkFlags;
54
55 public:
56
57 /**
58 * @brief Constructor Function.
59 */
60 World() = default;
61
62 /**
63 * @brief Destructor Function.
64 */
65 virtual ~World() = default;
66
67 /**
68 * @brief This interface define the specific world behaves before on activated.
69 */
70 virtual void OnPreActivate() = 0;
71
72 /**
73 * @brief This interface define the specific world behaves on activated.
74 * @param[in] ts TimeStep.
75 */
76 virtual void OnActivate(TimeStep& ts) = 0;
77
78 /**
79 * @brief This interface defines the specific world behaves after on activated.
80 */
81 virtual void OnDeactivate() = 0;
82
83 /**
84 * @brief Create a new empty entity in this world.
85 * @param[in] name Entity name.
86 */
87 Entity CreateEntity(const std::string& name = "None");
88
89 /**
90 * @brief Create a new empty entity with a uuid in this world.
91 * @param[in] uuid UUID.
92 * @param[in] name Entity name.
93 */
94 Entity CreateEntityWithUUID(UUID uuid, const std::string& name = "None");
95
96 /**
97 * @brief Destroy a entity from this world.
98 * @param[in] entity Entity.
99 */
100 void DestroyEntity(Entity& entity);
101
102 /**
103 * @brief Get Registry variable.
104 * @return Returns the Registry variable.
105 */
106 entt::registry& GetRegistry() { return m_Registry; }
107
108 /**
109 * @brief Get World Entity by id(entt::entity).
110 * @param[in] id Id(entt::entity)
111 * @return Returns valid Entity if fined.
112 */
113 Entity QueryEntitybyID(uint32_t id);
114
115 /**
116 * @brief Get WorldMarkFlags this frame.
117 * @return Returns the WorldMarkFlags this frame.
118 */
120
121 /**
122 * @brief Mark WorldMarkFlags with flags.
123 * @param[in] flags In flags.
124 */
125 void Mark(WorldMarkFlags flags) { m_Marker |= flags; }
126
127 /**
128 * @brief Reset WorldMarkFlags to Clean.
129 */
131
132 /**
133 * @brief Clear WorldMarkFlags with flags.
134 * @param[in] flags In flags.
135 */
137
138 /**
139 * @brief View all component in this world.
140 * @tparam T Component.
141 * @param fn View function.
142 */
143 template<typename T, typename F>
144 void ViewComponent(F&& fn);
145
146 /**
147 * @brief View all component in this world in ranges.
148 * @tparam T Component.
149 * @param[in] ranges view ranges.
150 * @param fn View function.
151 */
152 template<typename T, typename F>
153 void ViewComponent(const std::vector<uint32_t>& ranges, F&& fn);
154
155 /**
156 * @brief View all component in this world in ranges.
157 * @tparam T Component.
158 * @param[in] ranges view ranges.
159 * @param[in] floor ranges floor.
160 * @param[in] ceil ranges ceil.
161 * @param fn View function.
162 */
163 template<typename T, typename F>
164 void ViewComponent(const std::vector<uint32_t>& ranges, uint32_t floor, uint32_t ceil, F&& fn);
165
166 /**
167 * @brief View all root in this world.
168 * @param fn View function.
169 */
170 template<typename F>
171 void ViewRoot(F&& fn);
172
173 /**
174 * @brief Template Function.
175 * Used for add specific component to entity.
176 * @tparam T Specific component.
177 * @param[in] e entt::entity.
178 * @param[in] args Component construct parameters.
179 * @return Returns The specific component reference that added.
180 */
181 template<typename T, typename... Args>
182 T& AddComponent(entt::entity e, Args&&... args);
183
184 /**
185 * @brief Get Component owned by this entity.
186 * @tparam T Which Component we will get.
187 * @param[in] e entt::entity.
188 * @return Returns the specific Component.
189 */
190 template<typename T>
191 T& GetComponent(entt::entity e);
192
193 /**
194 * @brief Remove Component owned from this entity.
195 * @tparam T Which Component we will remove.
196 * @param[in] e entt::entity.
197 */
198 template<typename T>
199 void RemoveComponent(entt::entity e);
200
201 /**
202 * @brief If Component is owned by this entity or not.
203 * @tparam T Which Component we will search.
204 * @param[in] e entt::entity.
205 * @return Returns true if found.
206 */
207 template<typename T>
208 bool HasComponent(entt::entity e);
209
210 /**
211 * @brief Remove a entity from this world root.
212 * @param[in] entity Entity.
213 */
214 void RemoveFromRoot(Entity& entity);
215
216 /**
217 * @brief Add a entity to this world root.
218 * @param[in] entity Entity.
219 */
220 void AddToRoot(Entity& entity);
221
222 /**
223 * @brief Determine if a entity is in root.
224 * @param[in] entity Entity.
225 */
226 bool IsRootEntity(Entity& entity);
227
228 private:
229
231
232 /**
233 * @brief Called On any Component Added to this world.
234 * @param[in] entity Entity row pointer.
235 * @param[in] component Specific Component reference.
236 */
237 template<typename T>
238 void OnComponentAdded(Entity* entity, T& component);
239
240 protected:
241
242 /**
243 * @brief Mutex for world.
244 */
246
247 /**
248 * @brief This variable handles all entity.
249 */
251
252 /**
253 * @brief This variable is a cache.
254 * @noto Not in use now.
255 * @todo use it.
256 */
258
259 /**
260 * Allow Entity access all data.
261 */
262 friend class Entity;
263
264 /**
265 * Allow WorldFunctions access all data.
266 */
267 friend class WorldFunctions;
268
269 /**
270 * @brief World State this frame.
271 */
273 };
274
275 template <typename T, typename F>
276 void World::ViewComponent(F&& fn)
277 {
279
280 std::shared_lock<std::shared_mutex> lock(m_Mutex);
281
282 auto view = m_Registry.view<T>();
283
284 for(auto e : view)
285 {
286 auto& comp = m_Registry.get<T>(e);
287
288 if(fn(e, comp)) break;
289 }
290 }
291
292 template <typename T, typename F>
293 void World::ViewComponent(const std::vector<uint32_t>& ranges, F&& fn)
294 {
296
297 std::shared_lock<std::shared_mutex> lock(m_Mutex);
298
299 for(auto range : ranges)
300 {
301 auto e = static_cast<entt::entity>(range);
302 auto& comp = m_Registry.get<T>(e);
303
304 fn(e, comp);
305 }
306 }
307
308 template<typename T, typename F>
309 inline void World::ViewComponent(const std::vector<uint32_t>& ranges, uint32_t floor, uint32_t ceil, F&& fn)
310 {
312
313 std::shared_lock<std::shared_mutex> lock(m_Mutex);
314
315 assert(floor >= 0);
316 assert(ceil >= floor);
317 assert(ceil <= ranges.size() - 1);
318
319 for(int32_t i = floor; i < ceil; i++)
320 {
321 auto e = static_cast<entt::entity>(ranges[i]);
322 auto& comp = m_Registry.get<T>(e);
323
324 fn(e, comp);
325 }
326 }
327
328 template<typename F>
329 inline void World::ViewRoot(F&& fn)
330 {
332
333 std::shared_lock<std::shared_mutex> lock(m_Mutex);
334
335 for (auto entity : m_RootEntityMap)
336 {
337 Entity e(entity.second, this);
338 fn(e);
339 }
340 }
341
342 template <typename T, typename ... Args>
343 T& World::AddComponent(entt::entity e, Args&&... args)
344 {
346
347 std::unique_lock<std::shared_mutex> lock(m_Mutex);
348
349 return m_Registry.emplace<T>(e, std::forward<Args>(args)...);
350 }
351
352 template <typename T>
353 T& World::GetComponent(entt::entity e)
354 {
356
357 /**
358 * @note lock cause bug here.
359 */
360 //std::shared_lock<std::shared_mutex> lock(m_Mutex);
361
362 return m_Registry.get<T>(e);
363 }
364
365 template <typename T>
366 void World::RemoveComponent(entt::entity e)
367 {
369
370 std::unique_lock<std::shared_mutex> lock(m_Mutex);
371
372 m_Registry.remove<T>(e);
373 }
374
375 template <typename T>
376 bool World::HasComponent(entt::entity e)
377 {
379
380 std::shared_lock<std::shared_mutex> lock(m_Mutex);
381
382 return m_Registry.all_of<T>(e);
383 }
384
385 template<typename T>
386 void World::OnComponentAdded(Entity* entity, T& component)
387 {
389
390 component.OnComponentAdded(*entity);
391 }
392}
int main()
Main Function.
Definition EntryPoint.h:15
#define PROCESS_INSTANCE_ENTRY
Macros of modify Process instance state.
#define PROCESS_INSTANCE_EXIT
#define SPICES_PROFILE_ZONE
Application(const Application &)=delete
Copy Constructor Function.
virtual ~Application()
Destructor Function.
Application & operator=(const Application &)=delete
Copy Assignment Operation.
Application()
Constructor Function.
static void Run()
Run Our World.
Application Class. Our Engine Start here.
Definition Application.h:20
Entity Class. This class defines the specific behaves of Entity.
Definition Entity.h:20
static std::shared_ptr< spdlog::logger > s_ClientLogger
Game Stage Logger.
Definition Log.h:80
static bool m_IsInitialized
Definition Log.h:28
static std::shared_ptr< spdlog::logger > s_CoreLogger
Engine Stage Logger.
Definition Log.h:75
static void Init()
Init Log.
Definition Log.cpp:24
static std::shared_ptr< spdlog::logger > & GetCoreLogger()
Get Engine Stage Logger.
Definition Log.h:46
static std::shared_ptr< spdlog::logger > & GetClientLogger()
Get Game Stage Logger.
Definition Log.h:52
static void PostHandle(format_string_t< Args... > fmt, Args &&...args)
Post handle with log message.
Definition Log.h:84
static void ShutDown()
Shutdown Log.
Definition Log.cpp:82
float m_FrameTime
time step(s) during frames.
Definition TimeStep.h:85
std::chrono::steady_clock::time_point m_StartTime
Engine Start time.
Definition TimeStep.h:75
void Flush()
Refresh time in each engine loop.
Definition TimeStep.cpp:26
const float & ft() const
Get time step during frames.
Definition TimeStep.h:51
TimeStep(const TimeStep &)=delete
Copy Constructor Function.
const uint64_t & fs() const
Get frames count.
Definition TimeStep.h:63
TimeStep()
Constructor Function.
Definition TimeStep.cpp:12
virtual ~TimeStep()=default
Destructor Function.
std::chrono::steady_clock::time_point m_LastTime
Last frame time.
Definition TimeStep.h:80
float m_GameTime
time step(s) since Engine Start.
Definition TimeStep.h:90
uint64_t m_Frames
Frames since Engine Start.
Definition TimeStep.h:95
TimeStep & operator=(const TimeStep &)=delete
Copy Assignment Operation.
const float & gt() const
Get time step since Engine Start.
Definition TimeStep.h:57
This Class handles our engine time step during frames. Global Unique.
Definition TimeStep.h:22
static TracyGPUContext & Get()
Get this Single Instance.
static std::shared_ptr< TracyGPUContext > m_TracyGPUContext
This Single Instance.
virtual ~TracyGPUContext()=default
Destructor Function.
tracy::VkCtx * m_Context
Tracy Vulkan Context.
TracyGPUContext(VulkanState &state)
Constructor Function.
VulkanState & m_VulkanState
VulkanState.
tracy::VkCtx *& GetContext()
Get Context.
static void CreateInstance(VulkanState &state)
Create this Single Instance.
Wapper of Tracy GPU collect features.
This class helps to generate a uuid for one resource.
Definition UUID.h:16
World Functions Class.
WorldMarkFlags GetMarker() const
Get WorldMarkFlags this frame.
Definition World.h:119
void ViewComponent(const std::vector< uint32_t > &ranges, uint32_t floor, uint32_t ceil, F &&fn)
View all component in this world in ranges.
Definition World.h:309
uint32_t WorldMarkFlags
Definition World.h:53
void Mark(WorldMarkFlags flags)
Mark WorldMarkFlags with flags.
Definition World.h:125
Entity CreateEntity(const std::string &name="None")
Create a new empty entity in this world.
Definition World.cpp:13
void OnComponentAdded(Entity *entity, T &component)
Called On any Component Added to this world.
Definition World.h:386
void ReserMark()
Reset WorldMarkFlags to Clean.
Definition World.h:130
void ViewRoot(F &&fn)
View all root in this world.
Definition World.h:329
Entity CreateEmptyEntity(UUID uuid)
Definition World.cpp:85
Entity CreateEntityWithUUID(UUID uuid, const std::string &name="None")
Create a new empty entity with a uuid in this world.
Definition World.cpp:20
void ViewComponent(F &&fn)
View all component in this world.
Definition World.h:276
void ViewComponent(const std::vector< uint32_t > &ranges, F &&fn)
View all component in this world in ranges.
Definition World.h:293
virtual void OnDeactivate()=0
This interface defines the specific world behaves after on activated.
virtual ~World()=default
Destructor Function.
Entity QueryEntitybyID(uint32_t id)
Get World Entity by id(entt::entity).
Definition World.cpp:41
virtual void OnPreActivate()=0
This interface define the specific world behaves before on activated.
entt::registry m_Registry
This variable handles all entity.
Definition World.h:250
std::shared_mutex m_Mutex
Mutex for world.
Definition World.h:245
void DestroyEntity(Entity &entity)
Destroy a entity from this world.
Definition World.cpp:31
T & GetComponent(entt::entity e)
Get Component owned by this entity.
Definition World.h:353
@ FrushStableFrame
Definition World.h:48
@ MeshAddedToWorld
Definition World.h:47
@ NeedUpdateTLAS
Definition World.h:49
void RemoveComponent(entt::entity e)
Remove Component owned from this entity.
Definition World.h:366
WorldMarkFlags m_Marker
World State this frame.
Definition World.h:272
entt::registry & GetRegistry()
Get Registry variable.
Definition World.h:106
void RemoveFromRoot(Entity &entity)
Remove a entity from this world root.
Definition World.cpp:58
virtual void OnActivate(TimeStep &ts)=0
This interface define the specific world behaves on activated.
void AddToRoot(Entity &entity)
Add a entity to this world root.
Definition World.cpp:67
World()=default
Constructor Function.
std::unordered_map< UUID, entt::entity > m_RootEntityMap
This variable is a cache. @noto Not in use now.
Definition World.h:257
bool IsRootEntity(Entity &entity)
Determine if a entity is in root.
Definition World.cpp:76
void ClearMarkerWithBits(WorldMarkFlags flags)
Clear WorldMarkFlags with flags.
Definition World.cpp:48
bool HasComponent(entt::entity e)
If Component is owned by this entity or not.
Definition World.h:376
T & AddComponent(entt::entity e, Args &&... args)
Template Function. Used for add specific component to entity.
Definition World.h:343
World Class. This class defines the basic behaves of World. When we create an new world,...
Definition World.h:41
static const char * memoryPoolNames[3]
MemoryPool's name.
Definition Core.h:43
std::shared_ptr< World > CreateWorld()
extern WorldCreation definition in Game.
Definition EntryPoint.cpp:6
static constexpr char const * name
Define a named category tag type.
static constexpr char const * name
Define a custom domain tag type.
static constexpr char const * message
Define a registered string tag type.
This struct contains all Vulkan object in used global.
Definition VulkanUtils.h:74