KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
KarmaUtilities.h
Go to the documentation of this file.
1
10#pragma once
11
12#include "krpch.h"
13#include "SubClassOf.h"
14#include "Actor.h"
15#include "stb_image.h"
16#include "Level.h"
17#include "World.h"
18#include "WorldSettings.h"
19
20namespace Karma
21{
22 class UObject;
23 class AActor;
24
25 class UClass;
26
31 {
32 public:
41 static std::string ReadFileToSpitString(const std::string& filePath);
42
49 static std::string GetFilePath(const std::string& str);
50
80 static unsigned char* GetImagePixelData(char const* fileName, int* width, int* height, int* channels, int req_comp);
81 };
82
90 {
91 public:
94
97
99 int32_t m_Index;
100
103
106
109
112
115
117 //FDelegateHandle ActorSpawnedDelegateHandle;
118
125
134 FActorIteratorState(const UWorld* InWorld, const TSubclassOf<AActor> InClass) :
135 m_CurrentWorld(InWorld),
136 m_Index(-1),
137 m_ReachedEnd(false),
139 m_CurrentActor(nullptr),
140 m_DesiredClass(InClass)
141 {
142 //check(IsInGameThread());
143 KR_CORE_ASSERT(m_CurrentWorld, "");
144
145 #if WITH_EDITOR
146 // In the editor, you are more likely to have many worlds in memory at once.
147 // As an optimization to avoid iterating over many actors that are not in the world we are asking for,
148 // if the filter class is AActor, just use the actors that are in the world you asked for.
149 // This could be useful in runtime code as well if there are many worlds in memory, but for now we will leave
150 // it in editor code.
151 if (InClass == AActor::StaticClass())
152 {
153 // First determine the number of actors in the world to reduce reallocations when we append them to the array below.
154 int32 NumActors = 0;
155 for (ULevel* Level : InWorld->GetLevels())
156 {
157 if (Level)
158 {
159 NumActors += Level->Actors.Num();
160 }
161 }
162
163 // Presize the array
164 ObjectArray.Reserve(NumActors);
165
166
167 // Fill the array
168 for (ULevel* Level : InWorld->GetLevels())
169 {
170 if (Level)
171 {
172 ObjectArray.Append(Level->Actors);
173 }
174 }
175 }
176 else
177 #endif // WITH_EDITOR
178 {
179 constexpr EObjectFlags ExcludeFlags = RF_ClassDefaultObject;
180 GetObjectsOfClass(InClass, m_ObjectArray, true, ExcludeFlags, EInternalObjectFlags::Garbage);
181 }
182
183
184 // const auto ActorSpawnedDelegate = FOnActorSpawned::FDelegate::CreateRaw(this, &FActorIteratorState::OnActorSpawned);
185 // m_ActorSpawnedDelegateHandle = m_CurrentWorld->AddOnActorSpawnedHandler(ActorSpawnedDelegate);
186 }
187
194 {
195 //m_CurrentWorld->RemoveOnActorSpawnedHandler(ActorSpawnedDelegateHandle);
196 }
197
198
206 {
207 KR_CORE_ASSERT(m_CurrentActor, "");
208 KR_CORE_ASSERT(!m_CurrentActor->IsUnreachable(), "");
209
210 return m_CurrentActor;
211 }
212
213 private:
214 /*
215 void OnActorSpawned(AActor* InActor)
216 {
217 if (InActor->IsA(DesiredClass))
218 {
219 SpawnedActorArray.AddUnique(InActor);
220 }
221 }*/
222 };
223
228 {
232 AllActors = 0x00000000,
236 SkipPendingKill = 0x00000001,
240 OnlySelectedActors = 0x00000002,
244 OnlyActiveLevels = 0x00000004,
245 };
246
251 {
252 End
253 };
254
258 template <typename Derived>
260 {
261 private:
262 EActorIteratorFlags m_Flags;
263
264 // smart pointer?
265 std::shared_ptr<FActorIteratorState> m_State;
266
267 protected:
280
291 TActorIteratorBase(const UWorld* InWorld, TSubclassOf<AActor> InClass, const EActorIteratorFlags InFlags)
292 : m_Flags(InFlags)
293 {
294 m_State.reset(new FActorIteratorState(InWorld, InClass));
295 }
296
297 public:
306 {
307 // Use local version to avoid LHSs as compiler is not required to write out member variables to memory.
308 AActor* localCurrentActor = nullptr;
309 int32_t localIndex = m_State->m_Index;
310 KarmaVector<UObject*>& localObjectArray = m_State->m_ObjectArray;// note that localObjectArray is a vector
311 KarmaVector<AActor*>& localSpawnedActorArray = m_State->m_SpawnedActorArray; // Contains any actors spawned during iteration
312 const UWorld* localCurrentWorld = m_State->m_CurrentWorld;
313
314 int32_t lObjectArrayNum = int32_t(localObjectArray.Num());
315 int32_t lSAArrayNum = int32_t(localSpawnedActorArray.Num());
316
317 while (++localIndex < (lObjectArrayNum + lSAArrayNum))
318 {
319 if (localIndex < lObjectArrayNum)
320 {
321 localCurrentActor = static_cast<AActor*>(localObjectArray.GetElements()[localIndex]);
322 }
323 else
324 {
325 localCurrentActor = localSpawnedActorArray.GetElements()[localIndex - lObjectArrayNum];
326 }
327 m_State->m_ConsideredCount++;// Number of actors that have been considered thus far
328
329 ULevel* actorLevel = localCurrentActor ? localCurrentActor->GetLevel() : nullptr;
330
331 if (actorLevel
332 && static_cast<const Derived*>(this)->IsActorSuitable(localCurrentActor)
333 && static_cast<const Derived*>(this)->CanIterateLevel(actorLevel) && actorLevel->GetWorld() == localCurrentWorld)
334 {
335 // ignore non-persistent world settings
336 if (actorLevel == localCurrentWorld->GetPersistentLevel() || !localCurrentActor->IsA(AWorldSettings::StaticClass()))
337 {
338 m_State->m_CurrentActor = localCurrentActor;
339 m_State->m_Index = localIndex;
340 return;
341 }
342 }
343 }
344 m_State->m_CurrentActor = nullptr;
345 m_State->m_ReachedEnd = true;
346 }
347
355 {
356 return m_State->GetActorChecked();
357 }
358
366 {
367 return m_State->GetActorChecked();
368 }
369
377 FORCEINLINE explicit operator bool() const
378 {
379 return !m_State->m_ReachedEnd;
380 }
381
388 {
389 KR_CORE_ASSERT(!m_State->m_ReachedEnd, "");
390 m_State->m_CurrentWorld->RemoveActor(m_State->m_CurrentActor, true);
391 }
392
401 {
402 return m_State->m_ConsideredCount;
403 }
404
414 FORCEINLINE bool CanIterateLevel(const ULevel* Level) const
415 {
416 /*
417 if (EnumHasAnyFlags(Flags, EActorIteratorFlags::OnlyActiveLevels))
418 {
419 const bool bIsLevelVisibleOrAssociating = (Level->bIsVisible && !Level->bIsBeingRemoved) || Level->bIsAssociatingLevel || Level->bIsDisassociatingLevel;
420
421 // Only allow iteration of Level if it's in the currently active level collection of the world, or is a static level.
422 const FLevelCollection* const ActorLevelCollection = Level->GetCachedLevelCollection();
423 const FLevelCollection* const ActiveLevelCollection = Level->OwningWorld ? Level->OwningWorld->GetActiveLevelCollection() : nullptr;
424
425 // If the world's active level collection is null, we can't apply any meaningful filter,
426 // so just allow iteration in this case.
427 const bool bIsCurrentLevelCollectionTicking = !ActiveLevelCollection || (ActorLevelCollection == ActiveLevelCollection);
428
429 const bool bIsLevelCollectionNullOrStatic = !ActorLevelCollection || ActorLevelCollection->GetType() == ELevelCollectionType::StaticLevels;
430 const bool bShouldIterateLevelCollection = bIsCurrentLevelCollectionTicking || bIsLevelCollectionNullOrStatic;
431
432 return bIsLevelVisibleOrAssociating && bShouldIterateLevelCollection;
433 }*/
434
435 return true;
436 }
437
447 FORCEINLINE bool IsActorSuitable(const AActor* Actor) const
448 {
450 {
451 return false;
452 }
453
455 {
456 return false;
457 }
458
459 return true;
460 }
461 };
462}
This file contains the class AActor.
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
#define FORCEINLINE
Typical inlining macro for clarity.
Definition Core.h:157
EActorIteratorFlags
Iteration flags, specifies which types of levels and actors should be iterated.
Definition KarmaUtilities.h:228
@ OnlySelectedActors
Only iterate actors that are selected.
Definition KarmaUtilities.h:240
@ SkipPendingKill
Skip pending kill actors.
Definition KarmaUtilities.h:236
@ OnlyActiveLevels
Only iterate active levels.
Definition KarmaUtilities.h:244
@ AllActors
No flags, iterate all actors.
Definition KarmaUtilities.h:232
EActorIteratorType
Type enum, used to represent the special End iterator.
Definition KarmaUtilities.h:251
This file contains the class ULevel.
FORCEINLINE bool IsValid(const UObject *Test)
Test validity of object.
Definition Object.h:239
This file contains the class TSubclassOf and relevant helpers.
constexpr bool EnumHasAnyFlags(Enum Flags, Enum Contains)
Sees if a specified flag(s) exists in the linear combination of flags.
Definition UObjectGlobals.h:45
EObjectFlags
Flags describing an object instance.
Definition UObjectGlobals.h:183
@ RF_ClassDefaultObject
This object is its class's default object.
Definition UObjectGlobals.h:194
@ Garbage
Garbage from logical point of view and should not be referenced. This flag is mirrored in EObjectFlag...
Definition UObjectGlobals.h:247
signed int int32
32-bit signed integer
Definition UObjectGlobals.h:33
This file contains UWorld class and FActorSpawnParameters.
This file contains the class AWorldSettings.
Actor is the base class for an object that can be placed or spawned in a level.
Definition Actor.h:34
ULevel * GetLevel() const
Definition Actor.cpp:18
Abstract base class for actor iteration. Implements all operators and relies on IsActorSuitable to be...
Definition KarmaUtilities.h:90
FActorIteratorState(const UWorld *InWorld, const TSubclassOf< AActor > InClass)
Default constructor, initializes everything relevant.
Definition KarmaUtilities.h:134
UClass * m_DesiredClass
Definition KarmaUtilities.h:114
FActorIteratorState()
Default constructor.
Definition KarmaUtilities.h:124
KarmaVector< UObject * > m_ObjectArray
Definition KarmaUtilities.h:96
bool m_ReachedEnd
Definition KarmaUtilities.h:102
const UWorld * m_CurrentWorld
Definition KarmaUtilities.h:93
KarmaVector< AActor * > m_SpawnedActorArray
Definition KarmaUtilities.h:111
~FActorIteratorState()
A destructor.
Definition KarmaUtilities.h:193
FORCEINLINE AActor * GetActorChecked() const
Returns the current suitable actor pointed at by the Iterator.
Definition KarmaUtilities.h:205
int32_t m_Index
Definition KarmaUtilities.h:99
int32_t m_ConsideredCount
Definition KarmaUtilities.h:105
AActor * m_CurrentActor
Definition KarmaUtilities.h:108
The basic utilities class.
Definition KarmaUtilities.h:31
static unsigned char * GetImagePixelData(char const *fileName, int *width, int *height, int *channels, int req_comp)
Gathers image pixel data, arranged left-to-right, top-to-bottom, for the supplied image file.
Definition KarmaUtilities.cpp:32
static std::string GetFilePath(const std::string &str)
Extracts the file path from file source string (filepath + filename.extention)
Definition KarmaUtilities.cpp:26
static std::string ReadFileToSpitString(const std::string &filePath)
Generates a sting of text contained within a file.
Definition KarmaUtilities.cpp:5
FORCEINLINE AActor * operator->() const
Returns the current suitable actor pointed at by the Iterator.
Definition KarmaUtilities.h:365
void operator++()
Iterates to next suitable actor.
Definition KarmaUtilities.h:305
TActorIteratorBase(const UWorld *InWorld, TSubclassOf< AActor > InClass, const EActorIteratorFlags InFlags)
Constructor for setting m_State (FActorIteratorState) with the supplied InWorld and InClass.
Definition KarmaUtilities.h:291
FORCEINLINE bool CanIterateLevel(const ULevel *Level) const
Used to examine whether this level is valid for iteration or not. This function should be redefined (...
Definition KarmaUtilities.h:414
FORCEINLINE bool IsActorSuitable(const AActor *Actor) const
Determines whether this is a valid actor or not. This function should be redefined (thus hiding this ...
Definition KarmaUtilities.h:447
FORCEINLINE AActor * operator*() const
Returns the current suitable actor pointed at by the Iterator.
Definition KarmaUtilities.h:354
TActorIteratorBase(EActorIteratorType)
A constructor.
Definition KarmaUtilities.h:276
void ClearCurrent()
Clears the current Actor in the array (setting it to NULL).
Definition KarmaUtilities.h:387
int32 GetProgressNumerator() const
Returns the number of actors considered thus far. Can be used in combination with GetProgressDenomina...
Definition KarmaUtilities.h:400
Template to allow TClassType's to be passed around with type safety.
Definition SubClassOf.h:119
An object class.
Definition Class.h:158
A Level is a collection of Actors (lights, volumes, mesh instances etc.). Multiple Levels can be load...
Definition Level.h:30
virtual UWorld * GetWorld() const override final
Override for UObject's GetWorld.
Definition Level.cpp:17
FORCEINLINE bool IsA(OtherClassType SomeBase) const
Definition UObjectBase.h:278
The base class of all the game code relevant objects.
Definition Object.h:106
bool IsSelected() const
Test the selection state of a UObject.
Definition Object.cpp:181
The World is the top level object representing a map or a sandbox in which Actors and Components will...
Definition World.h:150
ULevel * GetPersistentLevel() const
Definition World.h:195
Karma's std::vector wrapper.
Definition KarmaTypes.h:152
uint32_t Num() const
Returns the total number of elements in a vector.
Definition KarmaTypes.h:302
const std::vector< BuildingBlock > & GetElements() const
Getter for the elements of vector.
Definition KarmaTypes.h:371