From b7cd1bed48c0daeada4df0ece694fea8ce7fa6fe Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Wed, 14 Sep 2022 10:32:50 +0800 Subject: [PATCH] Added System Routines --- SHADE_Engine/SHADE_Engine.vcxproj | 4 ++ SHADE_Engine/SHADE_Engine.vcxproj.filters | 4 ++ .../src/ECS_Base/Managers/SHSystemManager.cpp | 35 +++++++++++-- .../src/ECS_Base/Managers/SHSystemManager.h | 40 +++++++++------ SHADE_Engine/src/ECS_Base/SHECSMacros.h | 4 +- .../ECS_Base/System/SHFixedSystemRoutine.cpp | 20 ++++++++ .../ECS_Base/System/SHFixedSystemRoutine.h | 31 ++++++++++++ .../src/ECS_Base/System/SHRoutineStats.h | 27 ++++++++++ SHADE_Engine/src/ECS_Base/System/SHSystem.h | 17 +++---- .../src/ECS_Base/System/SHSystemRoutine.cpp | 21 ++++++++ .../src/ECS_Base/System/SHSystemRoutine.h | 49 +++++++++++++++++++ 11 files changed, 224 insertions(+), 28 deletions(-) create mode 100644 SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp create mode 100644 SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.h create mode 100644 SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h create mode 100644 SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 1831bff6..d84f3c05 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -114,6 +114,8 @@ + + @@ -200,6 +202,8 @@ + + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 70a5b407..8fc412c5 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -386,6 +386,8 @@ Tools + + @@ -587,5 +589,7 @@ Tools + + \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp index 5b861161..acf7cba3 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp +++ b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp @@ -14,10 +14,14 @@ #include "SHSystemManager.h" #include +#include +#include +#include + 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(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(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; } diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h index 7d4e8c27..616a8db3 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h @@ -13,30 +13,29 @@ #pragma once -#include +#include #include #include #include #include #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 SystemFamily; class SHSystemManager { //type definition for the container we use to store our system - using SystemContainer = std::unordered_map>; - + using SystemContainer = std::map>; + using SystemRoutineContainer = std::vector>; 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 @@ -61,7 +60,7 @@ namespace SHADE SystemTypeID typeID = SystemFamily::GetID(); 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 @@ -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 + static void RegisterRoutine(SystemVersionID version = 0) noexcept + { + SHSystem* system = GetSystem(version); + if (system == nullptr) + return; + systemRoutineContainer.emplace_back(std::make_unique()); + + } + + /************************************************************************** * \brief * Call the Exit function of all systems. diff --git a/SHADE_Engine/src/ECS_Base/SHECSMacros.h b/SHADE_Engine/src/ECS_Base/SHECSMacros.h index 0a8971e7..02615ca4 100644 --- a/SHADE_Engine/src/ECS_Base/SHECSMacros.h +++ b/SHADE_Engine/src/ECS_Base/SHECSMacros.h @@ -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; diff --git a/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp new file mode 100644 index 00000000..2c97bfb4 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp @@ -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; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.h b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.h new file mode 100644 index 00000000..d9a2b510 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.h @@ -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 {}; + + }; + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h b/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h new file mode 100644 index 00000000..e294264a --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +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; + } + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index 3a0694e2..a2964a91 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -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() { diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp new file mode 100644 index 00000000..83107c60 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp @@ -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; + } + +} diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h index 531a62b0..cdb62438 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h @@ -1,4 +1,53 @@ + + + #pragma once +#include "../SHECSMacros.h" +#include "SHRoutineStats.h" +#include "SHSystem.h" +#include + + +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 {}; + }; + + + + +} + +