ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
SystemScheduler.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <cstdint> // for uint32_t
10#include <vector> // for the system lists
11
12namespace Imago::Engine
13{
14
15 // NOTE: again, here we use a forward declaration because we do not need to know the full type of SceneContext
16 struct SceneContext;
17
23 public:
24
26 using SystemFn = void(*)(SceneContext&, float);
27
28 private:
29
30 static constexpr float DEFAULT_FIXED_TIMESTEP = 1.0f / 60.0f;
31 static constexpr uint32_t DEFAULT_MAX_FIXED_STEPS = 8;
32
33 float _accumulator = 0.0f;
34 float _fixedTimestep = DEFAULT_FIXED_TIMESTEP;
35 uint32_t _maxFixedSteps = DEFAULT_MAX_FIXED_STEPS;
36 SceneContext& _ctx;
37
38 std::vector<SystemFn> _coreSystems;
39 std::vector<SystemFn> _userSystems;
40 std::vector<SystemFn> _fixedSystems;
41
50 static void add_system(std::vector<SystemFn>& systems, SystemFn system);
51
59 static bool remove_system(std::vector<SystemFn>& systems, SystemFn system);
60
61 public:
62
67 explicit SystemScheduler(SceneContext& ctx);
68
69 // explicityly disallow copy and move behavior
70 SystemScheduler(const SystemScheduler&) = delete;
71 SystemScheduler& operator=(const SystemScheduler&) = delete;
73 SystemScheduler& operator=(SystemScheduler&&) = delete;
74
79 void tick(float dt);
80
85 void register_core(SystemFn system);
86
91 void register_update(SystemFn system);
92
97 void register_fixed(SystemFn system);
98
106 void unregister(SystemFn system);
107
108 //NOTE: This will mainly be used once we have editor integration so that the user does not have to change the order in code
113 void set_user_order(std::vector<SystemFn> order);
114
119 void set_fixed_timestep(float timestep);
120
125 void set_max_fixed_steps(uint32_t maxSteps);
126
130 float get_alpha() const;
131 };
132}
void register_fixed(SystemFn system)
Registers a fiexed timestep system. Called with the fixed step, not the frame time.
Definition SystemScheduler.cpp:105
void(*)(SceneContext &, float) SystemFn
Function pointer signature for a scheduled system.
Definition SystemScheduler.hpp:26
void tick(float dt)
Runs one frame or fixed step of scheduled systems, called once per frame by the Scene.
Definition SystemScheduler.cpp:18
SystemScheduler(SceneContext &ctx)
Construct a new System Scheduler object.
Definition SystemScheduler.cpp:14
void register_update(SystemFn system)
Registers a user per-frame system.
Definition SystemScheduler.cpp:104
void set_max_fixed_steps(uint32_t maxSteps)
Sets the most fixed steps that can run in one tick, must be greater than 0.
Definition SystemScheduler.cpp:154
void set_user_order(std::vector< SystemFn > order)
Set the user order object based off of a order vector they have arranged in the editor,...
Definition SystemScheduler.cpp:130
void set_fixed_timestep(float timestep)
Sets the fixed step size in seconds, must be greater than 0.
Definition SystemScheduler.cpp:144
void unregister(SystemFn system)
Removes a system from every list it is in.
Definition SystemScheduler.cpp:107
void register_core(SystemFn system)
Registers a core per-frame system that should run before user systems.
Definition SystemScheduler.cpp:103
float get_alpha() const
Returns the fraction of the fiexed step left in the accumulator (0 to <1) (For render interpolation).
Definition SystemScheduler.cpp:166
Gives systems access to the persistent engine objects they could need.
Definition SceneContext.hpp:30