Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
schedules.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include <string_view>
9
10namespace helios::app {
11
12/// @brief Stage for one-time startup execution.
14 static constexpr std::string_view kName = "helios::app::StartupStage";
15};
16
17/// @brief Builtin stage for startup.
18inline constexpr StartupStage kStartupStage{};
19
20/// @brief Stage for per-frame game logic.
22 static constexpr std::string_view kName = "helios::app::UpdateStage";
23};
24
25/// @brief Builtin stage for per-frame updates.
26inline constexpr UpdateStage kUpdateStage{};
27
28/// @brief Stage for per-frame extraction into sub-apps.
30 static constexpr std::string_view kName = "helios::app::ExtractStage";
31};
32
33/// @brief Builtin stage for extraction.
34inline constexpr ExtractStage kExtractStage{};
35
36/// @brief Stage for one-time shutdown execution.
38 static constexpr std::string_view kName = "helios::app::ShutdownStage";
39};
40
41/// @brief Builtin stage for shutdown.
42inline constexpr ShutdownStage kShutdownStage{};
43
44/// @brief Main-thread startup schedule (window creation, etc.).
46 static constexpr std::string_view kName = "helios::app::MainStartup";
47};
48
49/// @brief Builtin main-thread startup schedule.
50inline constexpr MainStartup kMainStartup{};
51
52/// @brief Pre-startup schedule.
53struct PreStartup {
54 static constexpr std::string_view kName = "helios::app::PreStartup";
55};
56
57/// @brief Builtin pre-startup schedule.
58inline constexpr PreStartup kPreStartup{};
59
60/// @brief Startup schedule.
61struct Startup {
62 static constexpr std::string_view kName = "helios::app::Startup";
63};
64
65/// @brief Builtin startup schedule.
66inline constexpr Startup kStartup{};
67
68/// @brief Post-startup schedule.
70 static constexpr std::string_view kName = "helios::app::PostStartup";
71};
72
73/// @brief Builtin post-startup schedule.
74inline constexpr PostStartup kPostStartup{};
75
76/// @brief First schedule in a frame.
77struct First {
78 static constexpr std::string_view kName = "helios::app::First";
79};
80
81/// @brief Builtin first schedule.
82inline constexpr First kFirst{};
83
84/// @brief Pre-update schedule.
85struct PreUpdate {
86 static constexpr std::string_view kName = "helios::app::PreUpdate";
87};
88
89/// @brief Builtin pre-update schedule.
90inline constexpr PreUpdate kPreUpdate{};
91
92/// @brief Update schedule.
93struct Update {
94 static constexpr std::string_view kName = "helios::app::Update";
95};
96
97/// @brief Builtin update schedule.
98inline constexpr Update kUpdate{};
99
100/// @brief Post-update schedule.
102 static constexpr std::string_view kName = "helios::app::PostUpdate";
103};
104
105/// @brief Builtin post-update schedule.
106inline constexpr PostUpdate kPostUpdate{};
107
108/// @brief Last schedule in a frame.
109struct Last {
110 static constexpr std::string_view kName = "helios::app::Last";
111};
112
113/// @brief Builtin last schedule.
114inline constexpr Last kLast{};
115
116/// @brief Extract schedule (runs after the last frame schedule).
117struct Extract {
118 static constexpr std::string_view kName = "helios::app::Extract";
119};
120
121/// @brief Builtin extract schedule.
122inline constexpr Extract kExtract{};
123
124/// @brief Pre-shutdown schedule.
126 static constexpr std::string_view kName = "helios::app::PreShutdown";
127};
128
129/// @brief Builtin pre-shutdown schedule.
130inline constexpr PreShutdown kPreShutdown{};
131
132/// @brief Shutdown schedule.
133struct Shutdown {
134 static constexpr std::string_view kName = "helios::app::Shutdown";
135};
136
137/// @brief Builtin shutdown schedule.
138inline constexpr Shutdown kShutdown{};
139
140/// @brief Post-shutdown schedule.
142 static constexpr std::string_view kName = "helios::app::PostShutdown";
143};
144
145/// @brief Builtin post-shutdown schedule.
146inline constexpr PostShutdown kPostShutdown{};
147
148/**
149 * @brief Registers default application schedules on an ECS scheduler.
150 * @details Creates empty schedules for all built-in types and assigns default
151 * executor kinds, stage membership, and ordering.
152 * @param scheduler ECS scheduler to populate
153 */
155 const auto add_in_stage = []<ecs::ScheduleTrait T, ecs::StageTrait S>(
156 ecs::Scheduler& sched, const T& schedule_type,
157 const S& stage, ecs::ExecutorKind kind) {
158 if (sched.TryGetSchedule(schedule_type) != nullptr) {
159 return;
160 }
161 auto schedule = ecs::Schedule::From(schedule_type);
162 schedule.Settings().executor_kind = kind;
163 sched.Add(schedule_type, std::move(schedule)).InStage(stage);
164 };
165
166 if (!scheduler.HasStage(kStartupStage)) {
167 scheduler.AddStage(kStartupStage);
168 }
169 if (!scheduler.HasStage(kUpdateStage)) {
170 scheduler.AddStage(kUpdateStage);
171 }
172 if (!scheduler.HasStage(kExtractStage)) {
173 scheduler.AddStage(kExtractStage);
174 }
175 if (!scheduler.HasStage(kShutdownStage)) {
176 scheduler.AddStage(kShutdownStage);
177 }
178
179 add_in_stage(scheduler, kMainStartup, kStartupStage,
181 add_in_stage(scheduler, kPreStartup, kStartupStage,
183 add_in_stage(scheduler, kStartup, kStartupStage,
185 add_in_stage(scheduler, kPostStartup, kStartupStage,
187 add_in_stage(scheduler, kFirst, kUpdateStage, ecs::ExecutorKind::kMainThread);
188 add_in_stage(scheduler, kPreUpdate, kUpdateStage,
190 add_in_stage(scheduler, kUpdate, kUpdateStage,
192 add_in_stage(scheduler, kPostUpdate, kUpdateStage,
194 add_in_stage(scheduler, kLast, kUpdateStage, ecs::ExecutorKind::kMainThread);
195 add_in_stage(scheduler, kExtract, kExtractStage,
197 add_in_stage(scheduler, kPreShutdown, kShutdownStage,
199 add_in_stage(scheduler, kShutdown, kShutdownStage,
201 add_in_stage(scheduler, kPostShutdown, kShutdownStage,
203
205 scheduler.Order(kStartup).After(kPreStartup);
206 scheduler.Order(kPostStartup).After(kStartup);
207 scheduler.Order(kPreUpdate).After(kFirst);
208 scheduler.Order(kUpdate).After(kPreUpdate);
209 scheduler.Order(kPostUpdate).After(kUpdate);
210 scheduler.Order(kLast).After(kPostUpdate);
211 scheduler.Order(kShutdown).After(kPreShutdown);
214}
215
216/**
217 * @brief Registers built-in schedules for a sub-app ECS scheduler.
218 * @details Same as @ref RegisterBuiltinSchedules except extract stage and
219 * schedules are omitted; extraction is driven by the main app via
220 * `SubApp::Extract`.
221 * @param scheduler ECS scheduler to populate
222 */
224 const auto add_in_stage = []<ecs::ScheduleTrait T, ecs::StageTrait S>(
225 ecs::Scheduler& sched, const T& schedule_type,
226 const S& stage, ecs::ExecutorKind kind) {
227 if (sched.TryGetSchedule(schedule_type) != nullptr) {
228 return;
229 }
230 auto schedule = ecs::Schedule::From(schedule_type);
231 schedule.Settings().executor_kind = kind;
232 sched.Add(schedule_type, std::move(schedule)).InStage(stage);
233 };
234
235 if (!scheduler.HasStage(kStartupStage)) {
236 scheduler.AddStage(kStartupStage);
237 }
238 if (!scheduler.HasStage(kUpdateStage)) {
239 scheduler.AddStage(kUpdateStage);
240 }
241 if (!scheduler.HasStage(kShutdownStage)) {
242 scheduler.AddStage(kShutdownStage);
243 }
244
245 add_in_stage(scheduler, kMainStartup, kStartupStage,
247 add_in_stage(scheduler, kPreStartup, kStartupStage,
249 add_in_stage(scheduler, kStartup, kStartupStage,
251 add_in_stage(scheduler, kPostStartup, kStartupStage,
253 add_in_stage(scheduler, kFirst, kUpdateStage, ecs::ExecutorKind::kMainThread);
254 add_in_stage(scheduler, kPreUpdate, kUpdateStage,
256 add_in_stage(scheduler, kUpdate, kUpdateStage,
258 add_in_stage(scheduler, kPostUpdate, kUpdateStage,
260 add_in_stage(scheduler, kLast, kUpdateStage, ecs::ExecutorKind::kMainThread);
261 add_in_stage(scheduler, kPreShutdown, kShutdownStage,
263 add_in_stage(scheduler, kShutdown, kShutdownStage,
265 add_in_stage(scheduler, kPostShutdown, kShutdownStage,
267
269 scheduler.Order(kStartup).After(kPreStartup);
270 scheduler.Order(kPostStartup).After(kStartup);
271 scheduler.Order(kPreUpdate).After(kFirst);
272 scheduler.Order(kUpdate).After(kPreUpdate);
273 scheduler.Order(kPostUpdate).After(kUpdate);
274 scheduler.Order(kLast).After(kPostUpdate);
275 scheduler.Order(kShutdown).After(kPreShutdown);
277}
278
279} // namespace helios::app
auto After(this auto &&self, ScheduleTypeIndex index) -> decltype(std::forward< decltype(self)>(self))
static Schedule From(const T &schedule={})
Creates a schedule named after the given label type.
Definition schedule.hpp:206
Orchestrates multiple named schedules, handles transitions, and executes them in topological order.
bool HasStage(StageTypeIndex index) const
ScheduleOrdering Order(ScheduleTypeIndex index)
StageOrdering OrderStage(StageTypeIndex index)
StageOrdering AddStage(StageTypeId id)
auto After(this auto &&self, StageTypeIndex index) -> decltype(std::forward< decltype(self)>(self))
Concept for schedules.
Definition schedule.hpp:51
Concept for stages.
Definition stage.hpp:22
constexpr ExtractStage kExtractStage
Builtin stage for extraction.
Definition schedules.hpp:34
constexpr PreShutdown kPreShutdown
Builtin pre-shutdown schedule.
constexpr MainStartup kMainStartup
Builtin main-thread startup schedule.
Definition schedules.hpp:50
constexpr PostUpdate kPostUpdate
Builtin post-update schedule.
constexpr Extract kExtract
Builtin extract schedule.
constexpr Startup kStartup
Builtin startup schedule.
Definition schedules.hpp:66
constexpr Last kLast
Builtin last schedule.
constexpr ShutdownStage kShutdownStage
Builtin stage for shutdown.
Definition schedules.hpp:42
constexpr StartupStage kStartupStage
Builtin stage for startup.
Definition schedules.hpp:18
constexpr PostShutdown kPostShutdown
Builtin post-shutdown schedule.
void RegisterBuiltinSubAppSchedules(ecs::Scheduler &scheduler)
Registers built-in schedules for a sub-app ECS scheduler.
constexpr Update kUpdate
Builtin update schedule.
Definition schedules.hpp:98
constexpr PreStartup kPreStartup
Builtin pre-startup schedule.
Definition schedules.hpp:58
constexpr PreUpdate kPreUpdate
Builtin pre-update schedule.
Definition schedules.hpp:90
constexpr Shutdown kShutdown
Builtin shutdown schedule.
constexpr First kFirst
Builtin first schedule.
Definition schedules.hpp:82
constexpr UpdateStage kUpdateStage
Builtin stage for per-frame updates.
Definition schedules.hpp:26
constexpr PostStartup kPostStartup
Builtin post-startup schedule.
Definition schedules.hpp:74
void RegisterBuiltinSchedules(ecs::Scheduler &scheduler)
Registers default application schedules on an ECS scheduler.
ExecutorKind
Kind of executor to use for schedule execution.
Definition executor.hpp:11
@ kMultiThreaded
Systems run in parallel using work-stealing.
Definition executor.hpp:18
Stage for per-frame extraction into sub-apps.
Definition schedules.hpp:29
static constexpr std::string_view kName
Definition schedules.hpp:30
Extract schedule (runs after the last frame schedule).
static constexpr std::string_view kName
First schedule in a frame.
Definition schedules.hpp:77
static constexpr std::string_view kName
Definition schedules.hpp:78
Last schedule in a frame.
static constexpr std::string_view kName
Main-thread startup schedule (window creation, etc.).
Definition schedules.hpp:45
static constexpr std::string_view kName
Definition schedules.hpp:46
Post-shutdown schedule.
static constexpr std::string_view kName
Post-startup schedule.
Definition schedules.hpp:69
static constexpr std::string_view kName
Definition schedules.hpp:70
Post-update schedule.
static constexpr std::string_view kName
Pre-shutdown schedule.
static constexpr std::string_view kName
Pre-startup schedule.
Definition schedules.hpp:53
static constexpr std::string_view kName
Definition schedules.hpp:54
Pre-update schedule.
Definition schedules.hpp:85
static constexpr std::string_view kName
Definition schedules.hpp:86
Stage for one-time shutdown execution.
Definition schedules.hpp:37
static constexpr std::string_view kName
Definition schedules.hpp:38
Shutdown schedule.
static constexpr std::string_view kName
Stage for one-time startup execution.
Definition schedules.hpp:13
static constexpr std::string_view kName
Definition schedules.hpp:14
Startup schedule.
Definition schedules.hpp:61
static constexpr std::string_view kName
Definition schedules.hpp:62
Stage for per-frame game logic.
Definition schedules.hpp:21
static constexpr std::string_view kName
Definition schedules.hpp:22
Update schedule.
Definition schedules.hpp:93
static constexpr std::string_view kName
Definition schedules.hpp:94