(this->m_EntityID, std::forward(param)...);
59 | }
60 |
61 | template
62 | void RemoveComponent()
63 | {
64 | this->m_ComponentManagerInstance->RemoveComponent(this->m_EntityID);
65 | }
66 |
67 | // COMPARE ENTITIES
68 |
69 | inline bool operator==(const IEntity& rhs) const { return this->m_EntityID == rhs.m_EntityID; }
70 | inline bool operator!=(const IEntity& rhs) const { return this->m_EntityID != rhs.m_EntityID; }
71 | inline bool operator==(const IEntity* rhs) const { return this->m_EntityID == rhs->m_EntityID; }
72 | inline bool operator!=(const IEntity* rhs) const { return this->m_EntityID != rhs->m_EntityID; }
73 |
74 | // ACCESORS
75 | virtual const EntityTypeId GetStaticEntityTypeID() const = 0;
76 |
77 | inline const EntityId GetEntityID() const { return this->m_EntityID; }
78 |
79 | void SetActive(bool active);
80 |
81 | inline bool IsActive() const { return this->m_Active; }
82 |
83 | virtual void OnEnable() {}
84 | virtual void OnDisable() {}
85 | };
86 |
87 | } // namespace ECS
88 |
89 | #endif // __I_ENTITY_H__
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/ISystem.h:
--------------------------------------------------------------------------------
1 | /*
2 | Author : Tobias Stein
3 | Date : 4th July, 2016
4 | File : ISystem.h
5 |
6 | Interface class for system class
7 |
8 | All Rights Reserved. (c) Copyright 2016.
9 | */
10 |
11 | #ifndef __I_SYSTEM_H__
12 | #define __I_SYSTEM_H__
13 |
14 | #include "API.h"
15 |
16 | namespace ECS
17 | {
18 | template
19 | class System;
20 |
21 | using SystemTypeId = TypeID;
22 |
23 | using SystemPriority = u16;
24 |
25 |
26 | static const SystemTypeId INVALID_SYSTEMID = INVALID_TYPE_ID;
27 |
28 |
29 |
30 | static const SystemPriority LOWEST_SYSTEM_PRIORITY = std::numeric_limits::min();
31 |
32 | static const SystemPriority VERY_LOW_SYSTEM_PRIORITY = 99;
33 | static const SystemPriority LOW_SYSTEM_PRIORITY = 100;
34 |
35 | static const SystemPriority NORMAL_SYSTEM_PRIORITY = 200;
36 |
37 | static const SystemPriority MEDIUM_SYSTEM_PRIORITY = 300;
38 |
39 | static const SystemPriority HIGH_SYSTEM_PRIORITY = 400;
40 | static const SystemPriority VERY_HIGH_SYSTEM_PRIORITY = 401;
41 |
42 | static const SystemPriority HIGHEST_SYSTEM_PRIORITY = std::numeric_limits::max();
43 |
44 |
45 | class ECS_API ISystem
46 | {
47 | friend class SystemManager;
48 |
49 | private:
50 |
51 | /// Summary: Duration since last system update in milliseconds.
52 | f32 m_TimeSinceLastUpdate;
53 |
54 | SystemPriority m_Priority;
55 |
56 | /// Summary: The system update interval.
57 | /// A negative value means system should update each time the engine receives an update.
58 | f32 m_UpdateInterval;
59 |
60 | u8 m_Enabled : 1;
61 | u8 m_NeedsUpdate : 1;
62 | u8 m_Reserved : 6;
63 |
64 | protected:
65 |
66 | ISystem(SystemPriority priority = NORMAL_SYSTEM_PRIORITY, f32 updateInterval_ms = -1.0f);
67 |
68 | public:
69 |
70 | virtual ~ISystem();
71 |
72 | virtual inline const SystemTypeId GetStaticSystemTypeID() const = 0;
73 | virtual inline const char* GetSystemTypeName() const = 0;
74 |
75 | virtual void PreUpdate(f32 dt) = 0;
76 | virtual void Update(f32 dt) = 0;
77 | virtual void PostUpdate(f32 dt) = 0;
78 | };
79 | }
80 |
81 | #endif // __I_SYSTEM_H__
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Log/Logger.h:
--------------------------------------------------------------------------------
1 | /*
2 | Author : Tobias Stein
3 | Date : 11th September, 2016
4 | File : Logger.h
5 |
6 | Class that manages the logging.
7 |
8 | All Rights Reserved. (c) Copyright 2016.
9 | */
10 |
11 | #ifndef __LOGGER_H__
12 | #define __LOGGER_H__
13 |
14 | #include "Platform.h"
15 |
16 | #include "log4cplus/logger.h"
17 | #include "log4cplus/loggingmacros.h"
18 |
19 | #if !ECS_DISABLE_LOGGING
20 |
21 | namespace ECS { namespace Log {
22 |
23 | class ECS_API Logger
24 | {
25 | Logger(const Logger&) = delete;
26 | Logger& operator=(Logger&) = delete;
27 |
28 | log4cplus::Logger m_logger;
29 |
30 |
31 | public:
32 |
33 | explicit Logger(log4cplus::Logger& logger);
34 |
35 | ~Logger();
36 |
37 | // trace
38 | template
39 | inline void LogTrace(const char* fmt, Args... args)
40 | {
41 | LOG4CPLUS_TRACE_FMT(this->m_logger, (const log4cplus::tchar*)fmt, std::forward(args)...);
42 | }
43 |
44 | // debug
45 | template
46 | inline void LogDebug(const char* fmt, Args... args)
47 | {
48 | LOG4CPLUS_DEBUG_FMT(this->m_logger, fmt, std::forward(args)...);
49 | }
50 |
51 | // info
52 | template
53 | inline void LogInfo(const char* fmt, Args... args)
54 | {
55 | LOG4CPLUS_INFO_FMT(this->m_logger, fmt, std::forward(args)...);
56 | }
57 |
58 | // warn
59 | template
60 | inline void LogWarning(const char* fmt, Args... args)
61 | {
62 | LOG4CPLUS_WARN_FMT(this->m_logger, fmt, std::forward(args)...);
63 | }
64 |
65 | // error
66 | template
67 | inline void LogError(const char* fmt, Args... args)
68 | {
69 | LOG4CPLUS_ERROR_FMT(this->m_logger, fmt, std::forward(args)...);
70 | }
71 |
72 | // fatal
73 | template
74 | inline void LogFatal(const char* fmt, Args... args)
75 | {
76 | LOG4CPLUS_FATAL_FMT(this->m_logger, fmt, std::forward(args)...);
77 | }
78 |
79 | }; // class Logger
80 |
81 |
82 | }} // namespace ECS::Log
83 |
84 | #endif // !ECS_DISABLE_LOGGING
85 |
86 | #include "Log/LoggerMacro.h"
87 |
88 | #endif // __LOGGER_H__
89 |
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Log/LoggerMacro.h:
--------------------------------------------------------------------------------
1 | ///-------------------------------------------------------------------------------------------------
2 | /// File: include\Log\LoggerMacro.h.
3 | ///
4 | /// Summary: Declares some macros to simply logging.
5 | ///-------------------------------------------------------------------------------------------------
6 |
7 |
8 | #ifndef __LOGGER_MACRO_H__
9 | #define __LOGGER_MACRO_H__
10 |
11 | //#define ECS_DISABLE_LOGGING 1
12 |
13 | #if !ECS_DISABLE_LOGGING
14 | #define DECLARE_LOGGER Log::Logger* LOGGER;
15 | #define DECLARE_STATIC_LOGGER static Log::Logger* LOGGER;
16 |
17 | #define DEFINE_LOGGER(name) LOGGER = ECS::Log::Internal::GetLogger(name);
18 | #define DEFINE_STATIC_LOGGER(clazz, name) Log::Logger* ##clazz::LOGGER = ECS::Log::Internal::GetLogger(name);
19 | #define DEFINE_STATIC_LOGGER_TEMPLATE(clazz, T, name) template Log::Logger* clazz::LOGGER = ECS::Log::Internal::GetLogger(name);
20 |
21 |
22 | #define LogTrace(format, ...) LOGGER->LogTrace(format, __VA_ARGS__);
23 | #define LogDebug(format, ...) LOGGER->LogDebug(format, __VA_ARGS__);
24 | #define LogInfo(format, ...) LOGGER->LogInfo(format, __VA_ARGS__);
25 | #define LogWarning(format, ...) LOGGER->LogWarning(format, __VA_ARGS__);
26 | #define LogError(format, ...) LOGGER->LogError(format, __VA_ARGS__);
27 | #define LogFatal(format, ...) LOGGER->LogFatal(format, __VA_ARGS__);
28 | #else
29 |
30 | #define DECLARE_LOGGER
31 | #define DECLARE_STATIC_LOGGER
32 |
33 | #define DEFINE_LOGGER(name)
34 | #define DEFINE_STATIC_LOGGER(class, name)
35 | #define DEFINE_STATIC_LOGGER_TEMPLATE(class, T, name)
36 |
37 | #define LogTrace(format, ...)
38 | #define LogDebug(format, ...)
39 | #define LogInfo(format, ...)
40 | #define LogWarning(format, ...)
41 | #define LogError(format, ...)
42 | #define LogFatal(format, ...)
43 | #endif
44 |
45 | #endif // __LOGGER_MACRO_H__
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Log/LoggerManager.h:
--------------------------------------------------------------------------------
1 | ///-------------------------------------------------------------------------------------------------
2 | /// File: include\Log\LoggerManager.h.
3 | ///
4 | /// Summary: Declares the logger manager class.
5 |
6 | #pragma once
7 |
8 | #if !ECS_DISABLE_LOGGING
9 |
10 | #ifndef __LOGGER_MANAGER_H__
11 | #define __LOGGER_MANAGER_H__
12 |
13 |
14 | #include "Platform.h"
15 |
16 | // Log4cplus logger support
17 | #include "log4cplus/logger.h"
18 |
19 | namespace ECS { namespace Log {
20 |
21 | class Logger;
22 |
23 | namespace Internal {
24 |
25 |
26 |
27 | class LoggerManager
28 | {
29 | using LoggerCache = std::unordered_map;
30 |
31 | static constexpr const char* LOG_FILE_NAME = "ECS.log";
32 | static constexpr const char* DEFAULT_LOGGER = "ECS";
33 | static constexpr const char* LOG_PATTERN = "%d{%H:%M:%S,%q} [%t] %-5p %c{1} %x- %m%n";
34 |
35 | // This class is not inteeded to be initialized
36 | LoggerManager(const LoggerManager&) = delete;
37 | LoggerManager& operator=(LoggerManager&) = delete;
38 |
39 | // root logger
40 | log4cplus::Logger m_RootLogger;
41 |
42 |
43 | /// Summary: Holds all acquired logger
44 | LoggerCache m_Cache;
45 |
46 | public:
47 |
48 | LoggerManager();
49 | ~LoggerManager();
50 |
51 |
52 | Logger* GetLogger(const char* logger = DEFAULT_LOGGER);
53 |
54 | }; // class LoggerManager
55 |
56 | }}} // namespace ECS::Log::Internal
57 |
58 |
59 | #endif // __LOGGER_MANAGER_H__
60 | #endif // !ECS_DISABLE_LOGGING
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Memory/Allocator/IAllocator.h:
--------------------------------------------------------------------------------
1 | /*
2 | Author : Tobias Stein
3 | Date : 9th July, 2016
4 | File : IAllocator.h
5 |
6 | Base allocator class.
7 |
8 | All Rights Reserved. (c) Copyright 2016.
9 | */
10 |
11 | #ifndef __I_ALLOC_H__
12 | #define __I_ALLOC_H__
13 |
14 | #include "API.h"
15 |
16 | namespace ECS { namespace Memory { namespace Allocator {
17 |
18 |
19 | // returns address aligned
20 | static inline void* AlignForward(void* address, u8 alignment)
21 | {
22 | return (void*)((reinterpret_cast(address)+static_cast(alignment - 1)) & static_cast(~(alignment - 1)));
23 | }
24 |
25 | // returns the number of bytes needed to align the address
26 | static inline u8 GetAdjustment(const void* address, u8 alignment)
27 | {
28 | u8 adjustment = alignment - (reinterpret_cast(address)& static_cast(alignment - 1));
29 |
30 | return adjustment == alignment ? 0 : adjustment;
31 | }
32 |
33 | static inline u8 GetAdjustment(const void* address, u8 alignment, u8 extra)
34 | {
35 | u8 adjustment = GetAdjustment(address, alignment);
36 |
37 | u8 neededSpace = extra;
38 |
39 | if (adjustment < neededSpace)
40 | {
41 | neededSpace -= adjustment;
42 |
43 | //Increase adjustment to fit header
44 | adjustment += alignment * (neededSpace / alignment);
45 |
46 | if (neededSpace % alignment > 0)
47 | adjustment += alignment;
48 | }
49 |
50 | return adjustment;
51 | }
52 |
53 |
54 | class ECS_API IAllocator
55 | {
56 | protected:
57 |
58 | const size_t m_MemorySize;
59 | const void* m_MemoryFirstAddress;
60 |
61 | size_t m_MemoryUsed;
62 | u64 m_MemoryAllocations;
63 |
64 | public:
65 |
66 | IAllocator(const size_t memSize, const void* mem);
67 | virtual ~IAllocator();
68 |
69 | virtual void* allocate(size_t size, u8 alignment) = 0;
70 | virtual void free(void* p) = 0;
71 | virtual void clear() = 0;
72 |
73 | // ACCESSOR
74 | inline size_t GetMemorySize() const
75 | {
76 | return this->m_MemorySize;
77 | }
78 |
79 | inline const void* GetMemoryAddress0() const
80 | {
81 | return this->m_MemoryFirstAddress;
82 | }
83 |
84 | inline size_t GetUsedMemory() const
85 | {
86 | return this->m_MemoryUsed;
87 | }
88 |
89 | inline u64 GetAllocationCount() const
90 | {
91 | return this->m_MemoryAllocations;
92 | }
93 |
94 | };
95 |
96 | }}} // ECS::Memory::Allocator
97 |
98 | #endif // __I_ALLOC_H__
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Memory/Allocator/LinearAllocator.h:
--------------------------------------------------------------------------------
1 | /*
2 | Author : Tobias Stein
3 | Date : 22nd October, 2016
4 | File : LinearAllocator.h
5 |
6 | Linear allocator.
7 |
8 | All Rights Reserved. (c) Copyright 2016.
9 | */
10 |
11 | #ifndef __LINEAR_ALLOC_H__
12 | #define __LINEAR_ALLOC_H__
13 |
14 | #include "Memory/Allocator/IAllocator.h"
15 |
16 | namespace ECS { namespace Memory { namespace Allocator {
17 |
18 | /*
19 | Allocates memory in a linear way.
20 |
21 | first 2 3 4
22 | allocatation alloaction
23 | v v v v
24 | |=================|=====|==|======| .... |
25 | ^ ^
26 | Initialial Last possible
27 | memory memory address
28 | address (mem + memSize)
29 | (mem)
30 |
31 |
32 | memory only can be freed by clearing all allocations
33 | */
34 | class ECS_API LinearAllocator : public IAllocator
35 | {
36 | public:
37 |
38 | LinearAllocator(size_t memSize, const void* mem);
39 |
40 | virtual ~LinearAllocator();
41 |
42 | virtual void* allocate(size_t size, u8 alignment) override;
43 | virtual void free(void* p) override;
44 | virtual void clear() override;
45 |
46 | }; // class LineaerAllocator
47 |
48 | } } } // namespace ECS::Memory::Allocator
49 |
50 | #endif // __LINEAR_ALLOC_H__
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Memory/Allocator/PoolAllocator.h:
--------------------------------------------------------------------------------
1 | /*
2 | Author : Tobias Stein
3 | Date : 22nd October, 2016
4 | File : PoolAllocator.h
5 |
6 | Pool allocator.
7 |
8 | All Rights Reserved. (c) Copyright 2016.
9 | */
10 |
11 | #ifndef __POOL_ALLOC_H__
12 | #define __POOL_ALLOC_H__
13 |
14 |
15 | #include "Memory/Allocator/IAllocator.h"
16 |
17 | namespace ECS { namespace Memory { namespace Allocator {
18 |
19 | class ECS_API PoolAllocator : public IAllocator
20 | {
21 | private:
22 |
23 | const size_t OBJECT_SIZE;
24 | const u8 OBJECT_ALIGNMENT;
25 |
26 | void** freeList;
27 |
28 | public:
29 |
30 | PoolAllocator::PoolAllocator(size_t memSize, const void* mem, size_t objectSize, u8 objectAlignment);
31 |
32 | virtual ~PoolAllocator();
33 |
34 | virtual void* allocate(size_t size, u8 alignment) override;
35 | virtual void free(void* p) override;
36 | virtual void clear() override;
37 |
38 | }; // class StackAllocator
39 |
40 | }}} // namespace ECS::Memory::Allocator
41 |
42 | #endif // __POOL_ALLOC_H__
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Memory/Allocator/StackAllocator.h:
--------------------------------------------------------------------------------
1 | /*
2 | Author : Tobias Stein
3 | Date : 22nd October, 2016
4 | File : StackAllocator.h
5 |
6 | Stack allocator.
7 |
8 | All Rights Reserved. (c) Copyright 2016.
9 | */
10 |
11 | #ifndef __STACK_ALLOC_H__
12 | #define __STACK_ALLOC_H__
13 |
14 | #include "Memory/Allocator/IAllocator.h"
15 |
16 | namespace ECS { namespace Memory { namespace Allocator {
17 |
18 | /*
19 | */
20 | class ECS_API StackAllocator : public IAllocator
21 | {
22 | private:
23 |
24 | struct AllocMetaInfo
25 | {
26 | u8 adjustment;
27 | };
28 |
29 | public:
30 |
31 | StackAllocator(size_t memSize, const void* mem);
32 |
33 | virtual ~StackAllocator();
34 |
35 | virtual void* allocate(size_t size, u8 alignment) override;
36 | virtual void free(void* p) override;
37 | virtual void clear() override;
38 |
39 | }; // class StackAllocator
40 |
41 | } } } // namespace ECS::Memory::Allocator
42 |
43 | #endif // __STACK_ALLOC_H__
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Memory/ECSMM.h:
--------------------------------------------------------------------------------
1 | /*
2 | Author : Tobias Stein
3 | Date : 6th September, 2017
4 | File : ECSMM.h
5 |
6 | Internal used memory manager.
7 |
8 | All Rights Reserved. (c) Copyright 2016 - 2017.
9 | */
10 |
11 | #ifndef __ECSSMM_H__
12 | #define __ECSSMM_H__
13 |
14 | #pragma once
15 |
16 | #define ECS_GLOBAL_MEMORY_CAPACITY 134217728 // 128 MB
17 |
18 | #include "API.h"
19 |
20 | #include "Memory/Allocator/StackAllocator.h"
21 |
22 | namespace ECS { namespace Memory { namespace Internal {
23 |
24 | class Allocator::StackAllocator;
25 |
26 | class MemoryManager
27 | {
28 | friend class GlobalMemoryUser;
29 |
30 | using StackAllocator = Allocator::StackAllocator;
31 |
32 | DECLARE_LOGGER
33 |
34 | public:
35 |
36 | static constexpr size_t MEMORY_CAPACITY = ECS_GLOBAL_MEMORY_CAPACITY;
37 |
38 | private:
39 |
40 | // Pointer to global allocated memory
41 | void* m_GlobalMemory;
42 |
43 | // Allocator used to manager memory allocation from global memory
44 | StackAllocator* m_MemoryAllocator;
45 |
46 | std::vector> m_PendingMemory;
47 |
48 | std::list m_FreedMemory;
49 |
50 | MemoryManager(const MemoryManager&) = delete;
51 | MemoryManager& operator=(MemoryManager&) = delete;
52 |
53 | public:
54 |
55 | MemoryManager();
56 | ~MemoryManager();
57 |
58 |
59 | inline void* Allocate(size_t memSize, const char* user = nullptr)
60 | {
61 | LogDebug("%s allocated %d bytes of global memory.", user != nullptr ? user : "Unknown", memSize);
62 | void* pMemory = m_MemoryAllocator->allocate(memSize, alignof(u8));
63 |
64 | this->m_PendingMemory.push_back(std::pair(user, pMemory));
65 |
66 | return pMemory;
67 | }
68 |
69 | inline void Free(void* pMem)
70 | {
71 | if (pMem == this->m_PendingMemory.back().second)
72 | {
73 | this->m_MemoryAllocator->free(pMem);
74 | this->m_PendingMemory.pop_back();
75 |
76 | bool bCheck = true;
77 | while(bCheck == true)
78 | {
79 | bCheck = false;
80 |
81 | // do not report already freed memory blocks.
82 | for (auto it : this->m_FreedMemory)
83 | {
84 | if (it == this->m_PendingMemory.back().second)
85 | {
86 | this->m_MemoryAllocator->free(pMem);
87 | this->m_PendingMemory.pop_back();
88 | this->m_FreedMemory.remove(it);
89 |
90 | bCheck = true;
91 | break;
92 | }
93 | }
94 | };
95 |
96 | }
97 | else
98 | {
99 | this->m_FreedMemory.push_back(pMem);
100 | }
101 | }
102 |
103 | void CheckMemoryLeaks();
104 |
105 | }; // class MemoryManager
106 |
107 | }}} // namespace ECS::Memory::Internal
108 |
109 | #endif // __ECSSMM_H__
--------------------------------------------------------------------------------
/EntityComponentSystem/include/ECS/Platform.h:
--------------------------------------------------------------------------------
1 | ///-------------------------------------------------------------------------------------------------
2 | /// File: include\Platform.h.
3 | ///
4 | /// Summary: Declares the platform specifics.
5 |
6 | #pragma once
7 |
8 | #ifndef __PLATFORM_H__
9 | #define __PLATFORM_H__
10 |
11 |
12 | #ifdef ECS_EXPORT
13 | #define ECS_API __declspec(dllexport)
14 | #else
15 | #define ECS_API __declspec(dllimport)
16 | #endif
17 |
18 |
19 | // Check if using 64-Bit architecture
20 | #if (defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined(_M_AMD64) || defined(_M_ARM64) || defined(_M_X64))
21 | #define ECS_64BIT 1
22 |
23 | // Check if using 32-Bit architecture
24 | #elif (defined(_WIN32) && !defined(_WIN64)) || defined(_M_IX86)
25 | #define ECS_32BIT 1
26 | #endif
27 |
28 |
29 |
30 | // Platform includes
31 | #include
32 | #include
33 | #include
34 | #include
35 |
36 | #include
37 | #include
38 | #include