Sp3 209 system manager rework #22
|
@ -114,6 +114,8 @@
|
|||
<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\SHFixedSystemRoutine.h" />
|
||||
<ClInclude Include="src\ECS_Base\System\SHRoutineStats.h" />
|
||||
<ClInclude Include="src\ECS_Base\System\SHSystem.h" />
|
||||
<ClInclude Include="src\ECS_Base\System\SHSystemRoutine.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\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\Engine\SHEngine.cpp" />
|
||||
<ClCompile Include="src\Filesystem\SHFileSystem.cpp" />
|
||||
<ClCompile Include="src\Graphics\Buffers\SHVkBuffer.cpp" />
|
||||
|
|
|
@ -386,6 +386,8 @@
|
|||
<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" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\ECS_Base\Components\SHComponent.cpp">
|
||||
|
@ -587,5 +589,7 @@
|
|||
<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" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -14,10 +14,14 @@
|
|||
#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
|
||||
{
|
||||
|
@ -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
|
||||
{
|
||||
for (auto& system : systemContainer)
|
||||
for (SystemContainer::reverse_iterator it = systemContainer.rbegin(); it != systemContainer.rend(); ++it)
|
||||
{
|
||||
system.second->Exit();
|
||||
(*it).second->Exit();
|
||||
//delete system.second;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,30 +13,29 @@
|
|||
#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<SystemID, 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:
|
||||
/*!*************************************************************************
|
||||
|
@ -47,12 +46,12 @@ 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>
|
||||
|
@ -61,7 +60,7 @@ namespace SHADE
|
|||
SystemTypeID typeID = SystemFamily::GetID<T>();
|
||||
|
||||
SystemVersionID version = 0;
|
||||
SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID;
|
||||
SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID;
|
||||
while (systemContainer.find(id) != systemContainer.end())
|
||||
{
|
||||
++version;
|
||||
|
@ -73,11 +72,11 @@ namespace SHADE
|
|||
}
|
||||
|
||||
/**************************************************************************
|
||||
* \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.
|
||||
***************************************************************************/
|
||||
template<typename T>
|
||||
|
@ -97,13 +96,26 @@ namespace SHADE
|
|||
}
|
||||
|
||||
/**************************************************************************
|
||||
* \brief
|
||||
* \brief
|
||||
* Call the Init function of all systems.
|
||||
* \return
|
||||
* \return
|
||||
* none
|
||||
***************************************************************************/
|
||||
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
|
||||
* Call the Exit function of all systems.
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 {};
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -37,13 +37,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
|
||||
|
@ -75,10 +75,7 @@ namespace SHADE
|
|||
|
||||
}
|
||||
|
||||
void Run(float dt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Exit()
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +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 {};
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue