SP3-5 ECS #27

Merged
XiaoQiDigipen merged 16 commits from SP3-5-ECS into main 2022-09-16 14:15:23 +08:00
33 changed files with 659 additions and 185 deletions
Showing only changes of commit 6da2fa9c4e - Show all commits

View File

@ -1,5 +1,6 @@
#include "SBpch.h"
#include "SBApplication.h"
#include "ECS_Base/UnitTesting/SHECSUnitTest.h"
#ifdef SHEDITOR
#include "Editor/SHEditor.h"
@ -23,11 +24,13 @@ namespace Sandbox
{
window.Create(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
#ifdef SHEDITOR
#else
#endif
}
void SBApplication::Update(void)

View File

@ -110,11 +110,17 @@
<ClInclude Include="src\ECS_Base\General\SHSparseBase.h" />
<ClInclude Include="src\ECS_Base\General\SHSparseSet.h" />
<ClInclude Include="src\ECS_Base\General\SHSparseSetContainer.h" />
<ClInclude Include="src\ECS_Base\Managers\SHComponentManager.h" />
<ClInclude Include="src\ECS_Base\Managers\SHEntityManager.h" />
<ClInclude Include="src\ECS_Base\Managers\SHSystemManager.h" />
<ClInclude Include="src\ECS_Base\SHECSMacros.h" />
<ClInclude Include="src\ECS_Base\System\SHComponentManager.h" />
<ClInclude Include="src\ECS_Base\System\SHEntityManager.h" />
<ClInclude Include="src\ECS_Base\System\SHFixedSystemRoutine.h" />
<ClInclude Include="src\ECS_Base\System\SHRoutineStats.h" />
<ClInclude Include="src\ECS_Base\System\SHSystem.h" />
<ClInclude Include="src\ECS_Base\System\SHSystemManager.h" />
<ClInclude Include="src\ECS_Base\System\SHSystemRoutine.h" />
<ClInclude Include="src\ECS_Base\UnitTesting\SHECSUnitTest.h" />
<ClInclude Include="src\ECS_Base\UnitTesting\SHTestComponents.h" />
<ClInclude Include="src\ECS_Base\UnitTesting\SHTestSystems.h" />
<ClInclude Include="src\Engine\SHEngine.h" />
<ClInclude Include="src\Filesystem\SHFileSystem.h" />
<ClInclude Include="src\Graphics\Buffers\SHVkBuffer.h" />
@ -196,9 +202,12 @@
<ClCompile Include="src\ECS_Base\Components\SHComponent.cpp" />
<ClCompile Include="src\ECS_Base\Components\SHComponentGroup.cpp" />
<ClCompile Include="src\ECS_Base\Entity\SHEntity.cpp" />
<ClCompile Include="src\ECS_Base\System\SHComponentManager.cpp" />
<ClCompile Include="src\ECS_Base\System\SHEntityManager.cpp" />
<ClCompile Include="src\ECS_Base\System\SHSystemManager.cpp" />
<ClCompile Include="src\ECS_Base\Managers\SHComponentManager.cpp" />
<ClCompile Include="src\ECS_Base\Managers\SHEntityManager.cpp" />
<ClCompile Include="src\ECS_Base\Managers\SHSystemManager.cpp" />
<ClCompile Include="src\ECS_Base\System\SHFixedSystemRoutine.cpp" />
<ClCompile Include="src\ECS_Base\System\SHSystemRoutine.cpp" />
<ClCompile Include="src\ECS_Base\UnitTesting\SHECSUnitTest.cpp" />
<ClCompile Include="src\Engine\SHEngine.cpp" />
<ClCompile Include="src\Filesystem\SHFileSystem.cpp" />
<ClCompile Include="src\Graphics\Buffers\SHVkBuffer.cpp" />

View File

@ -13,6 +13,9 @@
<Filter Include="ECS_Base\General">
<UniqueIdentifier>{07FEB307-F3F6-D259-1C29-B8DE0881B265}</UniqueIdentifier>
</Filter>
<Filter Include="ECS_Base\Managers">
<UniqueIdentifier>{37718753-A387-85E9-ECFE-100B58545671}</UniqueIdentifier>
</Filter>
<Filter Include="ECS_Base\System">
<UniqueIdentifier>{EE037863-5A8F-E527-63A0-681CCFAA4128}</UniqueIdentifier>
</Filter>
@ -141,21 +144,21 @@
<ClInclude Include="src\ECS_Base\General\SHSparseSetContainer.h">
<Filter>ECS_Base\General</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\Managers\SHComponentManager.h">
<Filter>ECS_Base\Managers</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\Managers\SHEntityManager.h">
<Filter>ECS_Base\Managers</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\Managers\SHSystemManager.h">
<Filter>ECS_Base\Managers</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\SHECSMacros.h">
<Filter>ECS_Base</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\System\SHComponentManager.h">
<Filter>ECS_Base\System</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\System\SHEntityManager.h">
<Filter>ECS_Base\System</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\System\SHSystem.h">
<Filter>ECS_Base\System</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\System\SHSystemManager.h">
<Filter>ECS_Base\System</Filter>
</ClInclude>
<ClInclude Include="src\Engine\SHEngine.h">
<Filter>Engine</Filter>
</ClInclude>
@ -382,6 +385,12 @@
<ClInclude Include="src\Tools\SHUtilities.h">
<Filter>Tools</Filter>
</ClInclude>
<ClInclude Include="src\ECS_Base\System\SHSystemRoutine.h" />
<ClInclude Include="src\ECS_Base\System\SHFixedSystemRoutine.h" />
<ClInclude Include="src\ECS_Base\System\SHRoutineStats.h" />
<ClInclude Include="src\ECS_Base\UnitTesting\SHTestComponents.h" />
<ClInclude Include="src\ECS_Base\UnitTesting\SHTestSystems.h" />
<ClInclude Include="src\ECS_Base\UnitTesting\SHECSUnitTest.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\ECS_Base\Components\SHComponent.cpp">
@ -393,14 +402,14 @@
<ClCompile Include="src\ECS_Base\Entity\SHEntity.cpp">
<Filter>ECS_Base\Entity</Filter>
</ClCompile>
<ClCompile Include="src\ECS_Base\System\SHComponentManager.cpp">
<Filter>ECS_Base\System</Filter>
<ClCompile Include="src\ECS_Base\Managers\SHComponentManager.cpp">
<Filter>ECS_Base\Managers</Filter>
</ClCompile>
<ClCompile Include="src\ECS_Base\System\SHEntityManager.cpp">
<Filter>ECS_Base\System</Filter>
<ClCompile Include="src\ECS_Base\Managers\SHEntityManager.cpp">
<Filter>ECS_Base\Managers</Filter>
</ClCompile>
<ClCompile Include="src\ECS_Base\System\SHSystemManager.cpp">
<Filter>ECS_Base\System</Filter>
<ClCompile Include="src\ECS_Base\Managers\SHSystemManager.cpp">
<Filter>ECS_Base\Managers</Filter>
</ClCompile>
<ClCompile Include="src\Engine\SHEngine.cpp">
<Filter>Engine</Filter>
@ -583,5 +592,8 @@
<ClCompile Include="src\Tools\SHLogger.cpp">
<Filter>Tools</Filter>
</ClCompile>
<ClCompile Include="src\ECS_Base\System\SHSystemRoutine.cpp" />
<ClCompile Include="src\ECS_Base\System\SHFixedSystemRoutine.cpp" />
<ClCompile Include="src\ECS_Base\UnitTesting\SHECSUnitTest.cpp" />
</ItemGroup>
</Project>

View File

@ -9,8 +9,7 @@
*********************************************************************/
#ifndef SH_COMPONENT_H
#define SH_COMPONENT_H
#pragma once
#include "SHpch.h"
#include "../SHECSMacros.h"
@ -118,4 +117,3 @@ namespace SHADE
};
}
#endif

View File

@ -14,7 +14,7 @@
#include "SHpch.h"
#include "SHComponentGroup.h"
#include "../System/SHComponentManager.h"
#include "../Managers/SHComponentManager.h"
namespace SHADE

View File

@ -12,9 +12,7 @@
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_COMPONENT_GROUP
#define SH_COMPONENT_GROUP
#pragma once
#include "../SHECSMacros.h"
#include "../General/SHFamily.h"
@ -180,6 +178,3 @@ namespace SHADE
};
}
#endif

View File

@ -9,9 +9,9 @@
*********************************************************************/
#include "SHpch.h"
#include "SHEntity.h"
#include "../System/SHEntityManager.h"
#include "../Managers/SHEntityManager.h"
//#include "Scene/SHSceneGraph.h"
#include "../System/SHComponentManager.h"
#include "../Managers/SHComponentManager.h"
namespace SHADE
{

View File

@ -8,12 +8,11 @@
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_ENTITY_H
#define SH_ENTITY_H
#pragma once
#include "../SHECSMacros.h"
#include "../Components/SHComponent.h"
#include "../System/SHComponentManager.h"
#include "../Managers/SHComponentManager.h"
//#include "../../Scene/SHSceneNode.h"
@ -166,6 +165,3 @@ namespace SHADE
};
}
#endif

View File

@ -11,8 +11,7 @@
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_FAMILY_H
#define SH_FAMILY_H
#pragma once
#include "../SHECSMacros.h"
@ -81,8 +80,4 @@ namespace SHADE
template<typename BaseClass>
ComponentTypeID SHFamilyID<BaseClass>::currentID = 0;
}
#endif
}

View File

@ -11,8 +11,7 @@
or disclosure of this file or its contents without the prior written
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_HANDLE_GENERATOR_H
#define SH_HANDLE_GENERATOR_H
#pragma once
#include <vector>
#include <iostream>
@ -299,7 +298,4 @@ namespace SHADE
typedef SHHandleGenerator<EntityID, EntityIndex> EntityHandleGenerator;
}
#endif
}

View File

@ -10,8 +10,7 @@
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_SPARSE_BASE_H
#define SH_SPARSE_BASE_H
#pragma once
#include "../SHECSMacros.h"
@ -44,7 +43,4 @@ namespace SHADE
};
}
#endif
}

View File

@ -9,8 +9,7 @@
or disclosure of this file or its contents without the prior written
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_SPARSE_SET_H
#define SH_SPARSE_SET_H
#pragma once
#include "../SHECSMacros.h"
#include "../General/SHSparseBase.h"
@ -351,6 +350,4 @@ namespace SHADE
}
};
}
#endif
}

View File

@ -10,8 +10,7 @@
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_SPARSE_SET_CONTAINER_H
#define SH_SPARSE_SET_CONTAINER_H
#pragma once
#include "SHSparseSet.h"
#include "SHFamily.h"
@ -243,6 +242,3 @@ namespace SHADE
};
}
#endif

View File

@ -12,8 +12,7 @@
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_ENGINE_H
#define SH_ENGINE_H
#pragma once
#include "../General/SHSparseSetContainer.h"
#include "../Components/SHComponent.h"
@ -477,7 +476,4 @@ namespace SHADE
}
#endif
}

View File

@ -12,9 +12,7 @@
or disclosure of this file or its contents without the prior written
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_ENTITY_MANAGER_H
#define SH_ENTITY_MANAGER_H
#pragma once
#include <vector>
#include <memory>
@ -198,7 +196,3 @@ namespace SHADE
}
#endif

View File

@ -0,0 +1,77 @@
/*********************************************************************
* \file SHSystemManager.cpp
* \author Daniel Chua Yee Chen
* \brief Implementation for the SHSystemManager class.
* SHSystemManager is the interface class where users of the engine create
* the systems that gives the components their functionality. This also
* ensures that the Init and Exit functions are ran at the appropriate time
*
* \copyright Copyright (c) 2021 DigiPen Institute of Technology. Reproduction
or disclosure of this file or its contents without the prior written
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#include "SHpch.h"
#include "SHSystemManager.h"
#include <iostream>
#include <chrono>
#include <ratio>
#include <ctime>
namespace SHADE
{
SHSystemManager::SystemContainer SHSystemManager::systemContainer;
SHSystemManager::SystemRoutineContainer SHSystemManager::systemRoutineContainer;
void SHSystemManager::Init() noexcept
{
for (auto& system : systemContainer)
{
system.second->Init();
#ifdef _DEBUG
std::cout << system.first << " Init" << std::endl;
#endif
}
}
void SHSystemManager::RunRoutines(bool editorPause, double deltaTime) noexcept
{
for (auto& routine : systemRoutineContainer)
{
if (editorPause == true)
{
if (routine.get()->IsRunInEditorPause)
{
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
routine.get()->Execute(deltaTime);
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
routine.get()->stats.executionTime = std::chrono::duration<double, std::milli>(end - start).count();
}
}
else
{
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
routine.get()->Execute(deltaTime);
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
routine.get()->stats.executionTime = std::chrono::duration<double, std::milli>(end - start).count();
}
}
}
void SHSystemManager::Exit() noexcept
{
systemRoutineContainer.clear();
for (SystemContainer::reverse_iterator it = systemContainer.rbegin(); it != systemContainer.rend(); ++it)
{
(*it).second->Exit();
//delete system.second;
}
systemContainer.clear();
}
}

View File

@ -10,34 +10,32 @@
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_SYSTEM_MANAGER_H
#define SH_SYSTEM_MANAGER_H
#pragma once
#include <unordered_map>
#include <map>
#include <memory>
#include <string>
#include <cassert>
#include <climits>
#include "../System/SHSystem.h"
#include "../General/SHFamily.h"
#include "../System/SHSystemRoutine.h"
namespace SHADE
{
typedef uint32_t SystemTypeID;
typedef uint32_t SystemVersionID;
typedef uint64_t SystemID;
typedef SHFamilyID<SHSystem> SystemFamily;
class SHSystemManager
{
//type definition for the container we use to store our system
using SystemContainer = std::unordered_map<std::string, std::unique_ptr<SHSystem>>;
using SystemContainer = std::map<SystemID, std::unique_ptr<SHSystem>>;
using SystemRoutineContainer = std::vector<std::unique_ptr<SHSystemRoutine>>;
private:
static SystemContainer systemContainer;
static SystemRoutineContainer systemRoutineContainer;
public:
/*!*************************************************************************
@ -48,45 +46,79 @@ namespace SHADE
~SHSystemManager() = delete;
/**************************************************************************
* \brief
* \brief
* Create a system of type T and map it to a name.
* throws an error if a system with the same name already exists.
* \param name
* name of the system
* \return
* \return
* none
***************************************************************************/
template<typename T>
static std::enable_if_t<std::is_base_of_v<SHSystem, T>, void> CreateSystem(std::string const& name)
static std::enable_if_t<std::is_base_of_v<SHSystem, T>, SystemID> CreateSystem()
{
if (systemContainer.find(name) != systemContainer.end())
SystemTypeID typeID = SystemFamily::GetID<T>();
SystemVersionID version = 0;
SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID;
while (systemContainer.find(id) != systemContainer.end())
{
assert("System Creation Error: System with the same name already exist.");
++version;
id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID;
}
systemContainer.emplace(id, std::make_unique<T>());
systemContainer[id].get()->systemID = id;
systemContainer.emplace(name, std::make_unique<T>());
return id;
}
/**************************************************************************
* \brief
* \brief
* Get a pointer to the System with a specified name.
* \param name
* Name of the system in the map
* \return
* \return
* Base System pointer.
***************************************************************************/
static SHSystem* GetSystem(std::string name);
template<typename T>
static std::enable_if_t<std::is_base_of_v<SHSystem, T>, T*> GetSystem(SystemVersionID version = 0)
{
SystemTypeID typeID = SystemFamily::GetID<T>();
SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID;
if (systemContainer.find(id) == systemContainer.end())
{
std::cout << "System Manager error: System Version " << version << " does not exit." << std::endl;
return nullptr;
}
return (T*)systemContainer.find(id)->second.get();
}
/**************************************************************************
* \brief
* \brief
* Call the Init function of all systems.
* \return
* \return
* none
***************************************************************************/
static void Init() noexcept;
static void RunRoutines(bool editorPause, double deltaTime) noexcept;
template<typename SystemType, typename RoutineType>
static void RegisterRoutine(SystemVersionID version = 0) noexcept
{
SHSystem* system = GetSystem<SystemType>(version);
if (system == nullptr)
return;
systemRoutineContainer.emplace_back(std::make_unique<RoutineType>());
systemRoutineContainer.back().get()->system = system;
}
/**************************************************************************
* \brief
* Call the Exit function of all systems.
@ -100,8 +132,4 @@ namespace SHADE
};
}
#endif
}

View File

@ -9,7 +9,9 @@
typedef uint32_t EntityID;
typedef uint16_t EntityIndex;
typedef uint32_t ComponentTypeID;
typedef uint32_t SystemTypeID;
typedef uint32_t SystemVersionID;
typedef uint64_t SystemID;

View File

@ -0,0 +1,21 @@
#include "SHpch.h"
#include "SHFixedSystemRoutine.h"
#include "../SHECSMacros.h"
namespace SHADE
{
void SHFixedSystemRoutine::Execute(double dt) noexcept
{
accumulatedTime += dt;
int counter = 0;
while (accumulatedTime >= fixedTimeStep)
{
++counter;
accumulatedTime -= fixedTimeStep;
FixedExecute(fixedTimeStep);
}
stats.numSteps = counter;
}
}

View File

@ -0,0 +1,31 @@
#pragma once
#include "SHSystemRoutine.h"
#define DEFAULT_FIXED_STEP 1.0/60.0
namespace SHADE
{
class SHFixedSystemRoutine: public SHSystemRoutine
{
private:
double accumulatedTime;
double fixedTimeStep;
protected:
SHFixedSystemRoutine(double timeStep = DEFAULT_FIXED_STEP, std::string routineName = "Default Fixed Routine Name", bool editorPause = false)
:SHSystemRoutine(routineName, editorPause), accumulatedTime(0.0), fixedTimeStep(timeStep){}
public:
~SHFixedSystemRoutine() = default;
virtual void Execute(double dt) noexcept;
virtual void FixedExecute(double dt) noexcept {};
};
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <string>
#include <iostream>
namespace SHADE
{
struct SHRoutineStats
{
SHRoutineStats(std::string name)
:name(name)
{
}
std::string name;
double executionTime;
int numSteps{1};
//friend std::ostream& operator<<(std::ostream& os, const SHRoutineStats& stats);
};
//std::ostream& operator<<(std::ostream& os, const SHRoutineStats& stats)
//{
// os << stats.name << ": Execution Time: " << stats.executionTime << " Number of steps: " << stats.numSteps << std::endl;
// return os;
//}
}

View File

@ -8,24 +8,29 @@
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#ifndef SH_SYSTEM_H
#define SH_SYSTEM_H
#pragma once
#include "../SHECSMacros.h"
namespace SHADE
{
class SHSystemManager;
class SHSystem
{
private:
SystemID systemID;
protected:
/*!*************************************************************************
* \brief
* Protected default constructor for SHSytem class
***************************************************************************/
SHSystem()= default;
public:
/*!*************************************************************************
@ -40,13 +45,13 @@ namespace SHADE
***************************************************************************/
virtual void Init() = 0;
/*!*************************************************************************
* \brief
* Pure virtual Run function. Derived class must implement this
* \param dt
* Delta time
***************************************************************************/
virtual void Run(float dt) = 0;
///*!*************************************************************************
// * \brief
// * Pure virtual Run function. Derived class must implement this
// * \param dt
// * Delta time
//***************************************************************************/
//virtual void Run(float dt) = 0;
/*!*************************************************************************
* \brief
@ -54,7 +59,14 @@ namespace SHADE
***************************************************************************/
virtual void Exit() = 0;
};
}
friend class SHSystemManager;
#endif
inline SystemID GetSystemID(void) const noexcept { return systemID; }
inline SystemVersionID GetSystemVersion(void) const noexcept { return static_cast<SystemVersionID>(systemID >> sizeof(SystemVersionID) * CHAR_BIT); }
};
}

View File

@ -1,57 +0,0 @@
/*********************************************************************
* \file SHSystemManager.cpp
* \author Daniel Chua Yee Chen
* \brief Implementation for the SHSystemManager class.
* SHSystemManager is the interface class where users of the engine create
* the systems that gives the components their functionality. This also
* ensures that the Init and Exit functions are ran at the appropriate time
*
* \copyright Copyright (c) 2021 DigiPen Institute of Technology. Reproduction
or disclosure of this file or its contents without the prior written
consent of DigiPen Institute of Technology is prohibited.
*********************************************************************/
#include "SHpch.h"
#include "SHSystemManager.h"
#include <iostream>
namespace SHADE
{
SHSystemManager::SystemContainer SHSystemManager::systemContainer;
SHSystem* SHSystemManager::GetSystem(std::string name)
{
if (systemContainer.find(name) == systemContainer.end())
{
assert("Get System Error: No system with such name exist.");
return nullptr;
}
return systemContainer.find(name)->second.get();
}
void SHSystemManager::Init() noexcept
{
for (auto& system : systemContainer)
{
system.second->Init();
#ifdef _DEBUG
std::cout << system.first << " Init" << std::endl;
#endif
}
}
void SHSystemManager::Exit() noexcept
{
for (auto& system : systemContainer)
{
system.second->Exit();
//delete system.second;
}
systemContainer.clear();
}
}

View File

@ -0,0 +1,23 @@
#include "SHpch.h"
#include "SHSystemRoutine.h"
namespace SHADE
{
SHSystem* SHSystemRoutine::GetSystem() const noexcept
{
return system;
}
std::string const SHSystemRoutine::GetName()const noexcept
{
return name;
}
SHRoutineStats const& SHSystemRoutine::GetStats()const noexcept
{
return stats;
}
}

View File

@ -0,0 +1,53 @@
#pragma once
#include "../SHECSMacros.h"
#include "SHRoutineStats.h"
#include "SHSystem.h"
#include <string>
namespace SHADE
{
class SHSystemManager;
class SHSystemRoutine
{
friend class SHSystemManager;
protected:
SHSystemRoutine(std::string routineName = "Default Routine Name", bool editorPause = false)
:system(nullptr), name(routineName), stats(routineName),IsRunInEditorPause(editorPause){};
SHSystem* system;
std::string name;
SHRoutineStats stats;
//Whether or not this routine should run when the editor is still in pause
bool IsRunInEditorPause;
public:
~SHSystemRoutine() = default;
SHSystem* GetSystem()const noexcept;
std::string const GetName() const noexcept;
SHRoutineStats const& GetStats()const noexcept;
virtual void Execute(double dt) noexcept {};
};
}

View File

@ -0,0 +1,181 @@
#include "SHpch.h"
#include "SHECSUnitTest.h"
#include "../Managers/SHComponentManager.h"
#include "../Managers/SHEntityManager.h"
#include "../Managers/SHSystemManager.h"
#include "SHTestComponents.h"
#include "SHTestSystems.h"
#include "Tools/SHLogger.h"
namespace SHADE
{
void SHECSUnitTest::TestAll(void) noexcept
{
TestBasicEntityCreate();
TestEntityCreateTemplate();
TestEntityDestroy();
TestSystemRoutine();
}
void SHECSUnitTest::TestBasicEntityCreate(void) noexcept
{
SHComponentManager::CreateComponentSparseSet<SHComponent_A>();
SHComponentManager::CreateComponentSparseSet<SHComponent_B>();
SHComponentManager::CreateComponentSparseSet<SHComponent_C>();
SHLOG_INFO("Test for add and remove component")
EntityID id1 = SHEntityManager::CreateEntity();
EntityID id2 = SHEntityManager::CreateEntity();
EntityID id3 = SHEntityManager::CreateEntity();
SHComponentManager::AddComponent<SHComponent_A>(id1);
}
void SHECSUnitTest::TestEntityCreateTemplate(void) noexcept
{
std::cout << "\nTest2" << std::endl;
//Test entity Creation.
SHComponentManager::CreateComponentSparseSet<SHComponent_A>();
SHComponentManager::CreateComponentSparseSet<SHComponent_B>();
SHComponentManager::CreateComponentSparseSet<SHComponent_C>();
for (size_t i = 0; i < 10000; ++i)
{
switch (i % 3)
{
case 0:
{
SHEntityManager::CreateEntity<SHComponent_A, SHComponent_B>();
}break;
case 1:
{
SHEntityManager::CreateEntity<SHComponent_A, SHComponent_C>();
}break;
case 2:
{
SHEntityManager::CreateEntity<SHComponent_A>();
}break;
default:
break;
}
}
auto& denseA = SHComponentManager::GetDense<SHComponent_A>();
auto& denseB = SHComponentManager::GetDense<SHComponent_B>();
auto& denseC = SHComponentManager::GetDense<SHComponent_C>();
std::cout << "Test Entity Creation" << std::endl;
std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 10000) ? " Success" : " Failure") << std::endl;
std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 3334) ? " Success" : " Failure") << std::endl;
std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 3333) ? " Success" : " Failure") << std::endl;
std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 10000 ? " Success" : " Failure") << std::endl;
SHEntityManager::DestroyAllEntity();
std::cout << std::endl << "Test Destroy All Entity" << std::endl;
std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 0) ? " Success" : " Failure") << std::endl;
std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 0) ? " Success" : " Failure") << std::endl;
std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 0) ? " Success" : " Failure") << std::endl;
std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 0 ? " Success" : " Failure") << std::endl;
}
void SHECSUnitTest::TestEntityDestroy(void) noexcept
{
std::cout << "\nTest3" << std::endl;
SHComponentManager::CreateComponentSparseSet<SHComponent_A>();
SHComponentManager::CreateComponentSparseSet<SHComponent_B>();
SHComponentManager::CreateComponentSparseSet<SHComponent_C>();
for (size_t i = 0; i < 10000; ++i)
{
switch (i % 3)
{
case 0:
{
SHEntityManager::CreateEntity<SHComponent_A, SHComponent_B>();
}break;
case 1:
{
SHEntityManager::CreateEntity<SHComponent_A, SHComponent_C>();
}break;
case 2:
{
SHEntityManager::CreateEntity<SHComponent_A>();
}break;
default:
break;
}
}
SHEntityManager::DestroyEntity(5000);
SHEntityManager::DestroyEntity(5001);
auto& denseA = SHComponentManager::GetDense<SHComponent_A>();
auto& denseB = SHComponentManager::GetDense<SHComponent_B>();
auto& denseC = SHComponentManager::GetDense<SHComponent_C>();
std::cout << "Test Entity Deletion" << std::endl;
std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 9998) ? " Success" : " Failure") << std::endl;
std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 3333) ? " Success" : " Failure") << std::endl;
std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 3333) ? " Success" : " Failure") << std::endl;
std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 9998 ? " Success" : " Failure") << std::endl;
std::cout << std::endl << "Test Entity Recreation" << std::endl;
EntityID id = SHEntityManager::CreateEntity<SHComponent_C>();
std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 9998) ? " Success" : " Failure") << std::endl;
std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 3333) ? " Success" : " Failure") << std::endl;
std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 3334) ? " Success" : " Failure") << std::endl;
std::cout << "Entity ID: " << id << " EntityIndex: " << EntityHandleGenerator::GetIndex(id) << (EntityHandleGenerator::GetIndex(id) == 5001 ? " Success" : " Failure") << std::endl;
std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 9999 ? " Success" : " Failure") << std::endl;
SHEntityManager::DestroyAllEntity();
std::cout << std::endl << "Check Destroy All Entity" << std::endl;
std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 0) ? " Success" : " Failure") << std::endl;
std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 0) ? " Success" : " Failure") << std::endl;
std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 0) ? " Success" : " Failure") << std::endl;
std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 0 ? " Success" : " Failure") << std::endl;
}
void SHECSUnitTest::TestSystemRoutine(void) noexcept
{
SHSystemManager::CreateSystem<SHTestSystem>();
SHSystemManager::CreateSystem<SHTestSystem>();
SHSystemManager::RegisterRoutine<SHTestSystem, SHTestSystem::SHTestRoutine>(1);
SHSystemManager::RunRoutines(false, 1.0 / 120.0);
SHSystemManager::Exit();
}
}

View File

@ -0,0 +1,22 @@
#pragma once
namespace SHADE
{
class SHECSUnitTest
{
public:
SHECSUnitTest() = delete;
~SHECSUnitTest() = delete;
static void TestBasicEntityCreate(void) noexcept;
static void TestEntityCreateTemplate(void) noexcept;
static void TestEntityDestroy(void) noexcept;
static void TestSystemRoutine(void) noexcept;
static void TestAll(void) noexcept;
};
}

View File

@ -0,0 +1,30 @@
#pragma once
#include "../Components/SHComponent.h"
namespace SHADE
{
class SHComponent_A :public SHComponent
{
public:
int value{};
};
class SHComponent_B :public SHComponent
{
public:
float x{};
float y{};
float z{};
};
class SHComponent_C :public SHComponent
{
public:
std::string value{};
};
}

View File

@ -0,0 +1,40 @@
#pragma once
#include <string>
#include "../System/SHSystem.h"
#include "../System/SHSystemRoutine.h"
namespace SHADE
{
class SHTestSystem : public SHSystem
{
public:
SHTestSystem() {};
~SHTestSystem() {};
std::string test{ "Test system" };
void Init() {};
void Exit() {};
class SHTestRoutine : public SHSystemRoutine
{
public:
SHTestRoutine()
:SHSystemRoutine("Test System Routine", false) {}
virtual void Execute(double dt) noexcept
{
std::cout << GetName() << " System Version: " << GetSystem()->GetSystemVersion() << std::endl;
}
};
};
}

View File

@ -14,7 +14,7 @@
#include "SHSceneGraph.h"
// Project Headers
#include "ECS_Base/System/SHEntityManager.h"
#include "ECS_Base/Managers/SHEntityManager.h"
#include "Tools/SHLogger.h"
#include "Tools/SHException.h"

View File

@ -10,11 +10,11 @@
*********************************************************************/
#include "SHpch.h"
#include "SHSceneManager.h"
#include "ECS_Base/System/SHComponentManager.h"
#include "ECS_Base/Managers/SHComponentManager.h"
//#include "Input/SHInputManager.h"
//#include "Rendering/Window/SHRenderingWindow.h"
#include "ECS_Base/System/SHEntityManager.h"
#include "ECS_Base/System/SHSystemManager.h"
#include "ECS_Base/Managers/SHEntityManager.h"
#include "ECS_Base/Managers/SHSystemManager.h"
//#include "FRC/SHFrameRateController.h"
//#include "ECS_Base/System/SHApplication.h"