diff --git a/system_scheduler.hpp b/system_scheduler.hpp new file mode 100644 index 0000000..7644142 --- /dev/null +++ b/system_scheduler.hpp @@ -0,0 +1,52 @@ +#pragma once +#include +#include + +namespace zecsy +{ + class system_scheduler final + { + public: + void add_system(float freq, auto&& func); + + void add_system(int freq, auto&& func); + + void update(float dt); + + private: + struct system_handler + { + double interval; + double accumulator = 0.0f; + std::function callback; + }; + + std::vector systems; + }; + + inline void system_scheduler::add_system(float freq, auto&& func) + { + systems.emplace_back(1.0f / freq, 0.0f, + std::forward(func)); + } + + inline void system_scheduler::add_system(int freq, auto&& func) + { + add_system(float(freq), func); + } + + inline void system_scheduler::update(float dt) + { + dt = std::max(0.0f, dt); + + for(auto& s: systems) + { + s.accumulator += dt; + while(s.accumulator >= s.interval) + { + s.callback(dt); + s.accumulator -= s.interval; + } + } + } +} // namespace zecsy diff --git a/tests/zecsy.cpp b/tests/zecsy.cpp index a0fbc67..e3d3bea 100644 --- a/tests/zecsy.cpp +++ b/tests/zecsy.cpp @@ -3,6 +3,7 @@ #include #include "../zecsy.hpp" +#include "../system_scheduler.hpp" using namespace zecsy; diff --git a/zecsy.hpp b/zecsy.hpp index 325a78f..82b616e 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include #include -#include #include #include #include @@ -29,52 +27,6 @@ namespace zecsy return true; }(); - class system_scheduler final - { - public: - void add_system(float freq, auto&& func); - - void add_system(int freq, auto&& func); - - void update(float dt); - - private: - struct system_handler - { - double interval; - double accumulator = 0.0f; - std::function callback; - }; - - std::vector systems; - }; - - inline void system_scheduler::add_system(float freq, auto&& func) - { - systems.emplace_back(1.0f / freq, 0.0f, - std::forward(func)); - } - - inline void system_scheduler::add_system(int freq, auto&& func) - { - add_system(float(freq), func); - } - - inline void system_scheduler::update(float dt) - { - dt = std::max(0.0f, dt); - - for(auto& s: systems) - { - s.accumulator += dt; - while(s.accumulator >= s.interval) - { - s.callback(dt); - s.accumulator -= s.interval; - } - } - } - class world final { public: