Plasma Engine  2.0
Loading...
Searching...
No Matches
WorldData.h
1#pragma once
2
3#include <Foundation/Communication/MessageQueue.h>
4#include <Foundation/Containers/HashTable.h>
5#include <Foundation/Math/Random.h>
6#include <Foundation/Memory/FrameAllocator.h>
7#include <Foundation/Threading/DelegateTask.h>
8#include <Foundation/Time/Clock.h>
9#include <Foundation/Types/SharedPtr.h>
10
11#include <Core/ResourceManager/ResourceHandle.h>
12#include <Core/World/GameObject.h>
13#include <Core/World/WorldDesc.h>
14
15namespace plInternal
16{
17 class PL_CORE_DLL WorldData
18 {
19 private:
20 friend class ::plWorld;
21 friend class ::plComponentManagerBase;
22
24 ~WorldData();
25
26 void Clear();
27
28 plHashedString m_sName;
29 mutable plProxyAllocator m_Allocator;
30 plLocalAllocatorWrapper m_AllocatorWrapper;
32 plDoubleBufferedLinearAllocator m_StackAllocator;
33
34 enum
35 {
38 };
39
40 // object storage
43 ObjectStorage m_ObjectStorage;
44
46 plEvent<const plGameObject*> m_ObjectDeletionEvent;
47
48 public:
49 class PL_CORE_DLL ConstObjectIterator
50 {
51 public:
52 const plGameObject& operator*() const;
53 const plGameObject* operator->() const;
54
55 operator const plGameObject*() const;
56
58 void Next();
59
61 bool IsValid() const;
62
64 void operator++();
65
66 private:
67 friend class ::plWorld;
68
69 ConstObjectIterator(ObjectStorage::ConstIterator iterator);
70
71 ObjectStorage::ConstIterator m_Iterator;
72 };
73
74 class PL_CORE_DLL ObjectIterator
75 {
76 public:
77 plGameObject& operator*();
78 plGameObject* operator->();
79
80 operator plGameObject*();
81
83 void Next();
84
86 bool IsValid() const;
87
89 void operator++();
90
91 private:
92 friend class ::plWorld;
93
94 ObjectIterator(ObjectStorage::Iterator iterator);
95
96 ObjectStorage::Iterator m_Iterator;
97 };
98
99 private:
100 // hierarchy structures
101 struct Hierarchy
102 {
104 using DataBlockArray = plDynamicArray<DataBlock>;
105
107 };
108
109 struct HierarchyType
110 {
111 enum Enum
112 {
113 Static,
114 Dynamic,
115 COUNT
116 };
117 };
118
119 Hierarchy m_Hierarchies[HierarchyType::COUNT];
120
121 static HierarchyType::Enum GetHierarchyType(bool bDynamic);
122
123 plGameObject::TransformationData* CreateTransformationData(bool bDynamic, plUInt32 uiHierarchyLevel);
124
125 void DeleteTransformationData(bool bDynamic, plUInt32 uiHierarchyLevel, plGameObject::TransformationData* pData);
126
127 template <typename VISITOR>
128 static plVisitorExecution::Enum TraverseHierarchyLevel(Hierarchy::DataBlockArray& blocks, void* pUserData = nullptr);
129 template <typename VISITOR>
130 plVisitorExecution::Enum TraverseHierarchyLevelMultiThreaded(Hierarchy::DataBlockArray& blocks, void* pUserData = nullptr);
131
132 using VisitorFunc = plDelegate<plVisitorExecution::Enum(plGameObject*)>;
133 void TraverseBreadthFirst(VisitorFunc& func);
134 void TraverseDepthFirst(VisitorFunc& func);
135 static plVisitorExecution::Enum TraverseObjectDepthFirst(plGameObject* pObject, VisitorFunc& func);
136
137 static void UpdateGlobalTransform(plGameObject::TransformationData* pData, plUInt32 uiUpdateCounter);
138 static void UpdateGlobalTransformWithParent(plGameObject::TransformationData* pData, plUInt32 uiUpdateCounter);
139
140 static void UpdateGlobalTransformAndSpatialData(plGameObject::TransformationData* pData, plUInt32 uiUpdateCounter, plSpatialSystem& spatialSystem);
141 static void UpdateGlobalTransformWithParentAndSpatialData(plGameObject::TransformationData* pData, plUInt32 uiUpdateCounter, plSpatialSystem& spatialSystem);
142
143 void UpdateGlobalTransforms();
144
145 void ResourceEventHandler(const plResourceEvent& e);
146
147 // game object lookups
150
151 // modules
154
155 // component management
157
158 struct InitBatch
159 {
160 InitBatch(plAllocator* pAllocator, plStringView sName, bool bMustFinishWithinOneFrame);
161
162 plHashedString m_sName;
163 bool m_bMustFinishWithinOneFrame = true;
164 bool m_bIsReady = false;
165
166 plUInt32 m_uiNextComponentToInitialize = 0;
167 plUInt32 m_uiNextComponentToStartSimulation = 0;
168 plDynamicArray<plComponentHandle> m_ComponentsToInitialize;
169 plDynamicArray<plComponentHandle> m_ComponentsToStartSimulation;
170 };
171
172 plTime m_MaxInitializationTimePerFrame;
174 InitBatch* m_pDefaultInitBatch = nullptr;
175 InitBatch* m_pCurrentInitBatch = nullptr;
176
177 struct RegisteredUpdateFunction
178 {
180 plHashedString m_sFunctionName;
181 float m_fPriority;
182 plUInt16 m_uiGranularity;
183 bool m_bOnlyUpdateWhenSimulating;
184
185 void FillFromDesc(const plWorldModule::UpdateFunctionDesc& desc);
186 bool operator<(const RegisteredUpdateFunction& other) const;
187 };
188
189 struct UpdateTask final : public plTask
190 {
191 virtual void Execute() override;
192
194 plUInt32 m_uiStartIndex;
195 plUInt32 m_uiCount;
196 };
197
198 plDynamicArray<RegisteredUpdateFunction, plLocalAllocatorWrapper> m_UpdateFunctions[plWorldModule::UpdateFunctionDesc::Phase::COUNT];
200
202
203 plUniquePtr<plSpatialSystem> m_pSpatialSystem;
204 plSharedPtr<plCoordinateSystemProvider> m_pCoordinateSystemProvider;
205 plUniquePtr<plTimeStepSmoothing> m_pTimeStepSmoothing;
206
207 plClock m_Clock;
208 plRandom m_Random;
209
210 struct QueuedMsgMetaData
211 {
212 PL_DECLARE_POD_TYPE();
213
214 PL_ALWAYS_INLINE QueuedMsgMetaData()
215 : m_uiReceiverData(0)
216 {
217 }
218
219 union
220 {
221 struct
222 {
223 plUInt64 m_uiReceiverObjectOrComponent : 62;
224 plUInt64 m_uiReceiverIsComponent : 1;
225 plUInt64 m_uiRecursive : 1;
226 };
227
228 plUInt64 m_uiReceiverData;
229 };
230
231 plTime m_Due;
232 };
233
235 mutable MessageQueue m_MessageQueues[plObjectMsgQueueType::COUNT];
236 mutable MessageQueue m_TimedMessageQueues[plObjectMsgQueueType::COUNT];
237 plObjectMsgQueueType::Enum m_ProcessingMessageQueue = plObjectMsgQueueType::COUNT;
238
239 plThreadID m_WriteThreadID;
240 plInt32 m_iWriteCounter = 0;
241 mutable plAtomicInteger32 m_iReadCounter;
242
243 plUInt32 m_uiUpdateCounter = 0;
244 bool m_bSimulateWorld = true;
245 bool m_bReportErrorWhenStaticObjectMoves = true;
246
248 plDelegate<plGameObjectHandle(const void*, plComponentHandle, plStringView)> m_GameObjectReferenceResolver;
249
250 struct ResourceReloadContext
251 {
252 plWorld* m_pWorld = nullptr;
253 plComponent* m_pComponent = nullptr;
254 void* m_pUserData = nullptr;
255 };
256
257 using ResourceReloadFunc = plDelegate<void(ResourceReloadContext&)>;
258
259 struct ResourceReloadFunctionData
260 {
261 plComponentHandle m_hComponent;
262 void* m_pUserData = nullptr;
263 ResourceReloadFunc m_Func;
264 };
265
266 using ReloadFunctionList = plHybridArray<ResourceReloadFunctionData, 8>;
269 ReloadFunctionList m_TempReloadFunctions;
270
271 public:
273 {
274 public:
275 void Lock();
276 void Unlock();
277
278 private:
279 friend class ::plInternal::WorldData;
280
281 ReadMarker(const WorldData& data);
282 const WorldData& m_Data;
283 };
284
286 {
287 public:
288 void Lock();
289 void Unlock();
290
291 private:
292 friend class ::plInternal::WorldData;
293
294 WriteMarker(WorldData& data);
295 WorldData& m_Data;
296 };
297
298 private:
299 mutable ReadMarker m_ReadMarker;
300 WriteMarker m_WriteMarker;
301
302 void* m_pUserData = nullptr;
303 };
304} // namespace plInternal
305
306#include <Core/World/Implementation/WorldData_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
A clock that can be speed up, slowed down, paused, etc. Useful for updating game logic,...
Definition Clock.h:15
Base class of all component types.
Definition Component.h:25
A double buffered stack allocator.
Definition FrameAllocator.h:7
Definition DynamicArray.h:81
Definition Event.h:177
This class represents an object inside the world.
Definition GameObject.h:32
Definition HashSet.h:191
Definition HashTable.h:333
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition IdTable.h:171
Definition WorldData.h:75
Definition WorldData.h:273
Definition WorldData.h:286
Definition WorldData.h:18
Definition MessageQueue.h:97
A random number generator. Currently uses the WELL512 algorithm.
Definition Random.h:9
Definition Set.h:238
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
Definition SpatialSystem.h:10
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Base class for custom tasks.
Definition Task.h:10
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
A handle to a component.
Definition Declarations.h:138
This struct represents a block of type T, typically 4kb.
Definition LargeBlockAllocator.h:14
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
A handle to a game object.
Definition Declarations.h:76
Definition AllocatorWrapper.h:30
Enum
Definition Declarations.h:274
These events may be sent by a specific plResource or by the plResourceManager.
Definition Declarations.h:22
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
Enum
Definition Declarations.h:344
Describes the initial state of a world.
Definition WorldDesc.h:14
Description of an update function that can be registered at the world.
Definition WorldModule.h:43