Added System Routines

This commit is contained in:
maverickdgg 2022-09-14 10:32:50 +08:00
parent f342fdcb95
commit b7cd1bed48
11 changed files with 224 additions and 28 deletions

View File

@ -114,6 +114,8 @@
<ClInclude Include="src\ECS_Base\Managers\SHEntityManager.h" /> <ClInclude Include="src\ECS_Base\Managers\SHEntityManager.h" />
<ClInclude Include="src\ECS_Base\Managers\SHSystemManager.h" /> <ClInclude Include="src\ECS_Base\Managers\SHSystemManager.h" />
<ClInclude Include="src\ECS_Base\SHECSMacros.h" /> <ClInclude Include="src\ECS_Base\SHECSMacros.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\SHSystem.h" />
<ClInclude Include="src\ECS_Base\System\SHSystemRoutine.h" /> <ClInclude Include="src\ECS_Base\System\SHSystemRoutine.h" />
<ClInclude Include="src\Engine\SHEngine.h" /> <ClInclude Include="src\Engine\SHEngine.h" />
@ -200,6 +202,8 @@
<ClCompile Include="src\ECS_Base\Managers\SHComponentManager.cpp" /> <ClCompile Include="src\ECS_Base\Managers\SHComponentManager.cpp" />
<ClCompile Include="src\ECS_Base\Managers\SHEntityManager.cpp" /> <ClCompile Include="src\ECS_Base\Managers\SHEntityManager.cpp" />
<ClCompile Include="src\ECS_Base\Managers\SHSystemManager.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\Engine\SHEngine.cpp" /> <ClCompile Include="src\Engine\SHEngine.cpp" />
<ClCompile Include="src\Filesystem\SHFileSystem.cpp" /> <ClCompile Include="src\Filesystem\SHFileSystem.cpp" />
<ClCompile Include="src\Graphics\Buffers\SHVkBuffer.cpp" /> <ClCompile Include="src\Graphics\Buffers\SHVkBuffer.cpp" />

View File

@ -386,6 +386,8 @@
<Filter>Tools</Filter> <Filter>Tools</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\ECS_Base\System\SHSystemRoutine.h" /> <ClInclude Include="src\ECS_Base\System\SHSystemRoutine.h" />
<ClInclude Include="src\ECS_Base\System\SHFixedSystemRoutine.h" />
<ClInclude Include="src\ECS_Base\System\SHRoutineStats.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\ECS_Base\Components\SHComponent.cpp"> <ClCompile Include="src\ECS_Base\Components\SHComponent.cpp">
@ -587,5 +589,7 @@
<ClCompile Include="src\Tools\SHLogger.cpp"> <ClCompile Include="src\Tools\SHLogger.cpp">
<Filter>Tools</Filter> <Filter>Tools</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\ECS_Base\System\SHSystemRoutine.cpp" />
<ClCompile Include="src\ECS_Base\System\SHFixedSystemRoutine.cpp" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -14,10 +14,14 @@
#include "SHSystemManager.h" #include "SHSystemManager.h"
#include <iostream> #include <iostream>
#include <chrono>
#include <ratio>
#include <ctime>
namespace SHADE namespace SHADE
{ {
SHSystemManager::SystemContainer SHSystemManager::systemContainer; SHSystemManager::SystemContainer SHSystemManager::systemContainer;
SHSystemManager::SystemRoutineContainer SHSystemManager::systemRoutineContainer;
void SHSystemManager::Init() noexcept void SHSystemManager::Init() noexcept
{ {
@ -30,11 +34,36 @@ namespace SHADE
} }
} }
void SHSystemManager::RunRoutines(bool editorPause) 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(0.0);
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(0.0);
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 void SHSystemManager::Exit() noexcept
{ {
for (auto& system : systemContainer) for (SystemContainer::reverse_iterator it = systemContainer.rbegin(); it != systemContainer.rend(); ++it)
{ {
system.second->Exit(); (*it).second->Exit();
//delete system.second; //delete system.second;
} }

View File

@ -13,30 +13,29 @@
#pragma once #pragma once
#include <unordered_map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <cassert> #include <cassert>
#include <climits> #include <climits>
#include "../System/SHSystem.h" #include "../System/SHSystem.h"
#include "../General/SHFamily.h" #include "../General/SHFamily.h"
#include "../System/SHSystemRoutine.h"
namespace SHADE namespace SHADE
{ {
typedef uint32_t SystemTypeID;
typedef uint32_t SystemVersionID;
typedef uint64_t SystemID;
typedef SHFamilyID<SHSystem> SystemFamily; typedef SHFamilyID<SHSystem> SystemFamily;
class SHSystemManager class SHSystemManager
{ {
//type definition for the container we use to store our system //type definition for the container we use to store our system
using SystemContainer = std::unordered_map<SystemID, std::unique_ptr<SHSystem>>; using SystemContainer = std::map<SystemID, std::unique_ptr<SHSystem>>;
using SystemRoutineContainer = std::vector<std::unique_ptr<SHSystemRoutine>>;
private: private:
static SystemContainer systemContainer; static SystemContainer systemContainer;
static SystemRoutineContainer systemRoutineContainer;
public: public:
/*!************************************************************************* /*!*************************************************************************
@ -104,6 +103,19 @@ namespace SHADE
***************************************************************************/ ***************************************************************************/
static void Init() noexcept; static void Init() noexcept;
static void RunRoutines(bool editorPause) 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>());
}
/************************************************************************** /**************************************************************************
* \brief * \brief
* Call the Exit function of all systems. * Call the Exit function of all systems.

View File

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

View File

@ -0,0 +1,20 @@
#include "SHFixedSystemRoutine.h"
#include "../SHECSMacros.h"
namespace SHADE
{
void SHFixedSystemRoutine::Execute(double dt)
{
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,27 @@
#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;
}
}

View File

@ -37,13 +37,13 @@ namespace SHADE
***************************************************************************/ ***************************************************************************/
virtual void Init() = 0; virtual void Init() = 0;
/*!************************************************************************* ///*!*************************************************************************
* \brief // * \brief
* Pure virtual Run function. Derived class must implement this // * Pure virtual Run function. Derived class must implement this
* \param dt // * \param dt
* Delta time // * Delta time
***************************************************************************/ //***************************************************************************/
virtual void Run(float dt) = 0; //virtual void Run(float dt) = 0;
/*!************************************************************************* /*!*************************************************************************
* \brief * \brief
@ -75,10 +75,7 @@ namespace SHADE
} }
void Run(float dt)
{
}
void Exit() void Exit()
{ {

View File

@ -0,0 +1,21 @@
#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

@ -1,4 +1,53 @@
#pragma once #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 {};
};
}