co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
schedule.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <co_ecs/registry.hpp>
5
8
9namespace co_ecs {
10
11class schedule_executor;
12
15class schedule {
16private:
17 using self_type = schedule;
18
19public:
26 template<typename... Args>
27 auto add_init_system(Args&&... args) -> self_type& {
28 _init_stage.add_system(std::forward<Args>(args)...);
29 return *this;
30 }
31
38 auto begin_stage(std::string_view name = {}) -> stage& {
39 return _stages.emplace_back(*this, name);
40 }
41
49 auto create_executor(registry& registry, void* user_context = nullptr) -> std::unique_ptr<schedule_executor> {
50 std::vector<std::unique_ptr<stage_executor>> stage_executors;
51 for (auto& stage : _stages) {
52 stage_executors.emplace_back(stage.create_executor(registry, user_context));
53 }
54
55 return std::make_unique<schedule_executor>(
56 registry, std::move(stage_executors), _init_stage.create_executor(registry, user_context));
57 }
58
59private:
60 stage _init_stage{ *this };
61 std::vector<stage> _stages;
62};
63
67public:
77 std::vector<std::unique_ptr<stage_executor>> stages,
78 std::unique_ptr<stage_executor> init) :
79 _registry(registry), _stages(std::move(stages)), _init(std::move(init)) {
80 // Run initial systems
81 _init->run();
82
83 // Flush commands
84 command_buffer::flush(_registry);
85 }
86
90 void run_once() {
91 for (auto& stage : _stages) {
92 stage->run();
93 }
94
95 // Flush commands
96 command_buffer::flush(_registry);
97 }
98
99private:
100 registry& _registry;
101 std::vector<std::unique_ptr<stage_executor>> _stages;
102 std::unique_ptr<stage_executor> _init;
103};
104
105} // namespace co_ecs
static void flush(registry &registry)
Flushes all commands in the command buffers to the given registry.
Definition command.hpp:27
Registry is a container for all our entities and components. Components are stored in continuously in...
Definition registry.hpp:13
Executes the schedule by running all stages.
Definition schedule.hpp:66
void run_once()
Executes the schedule once.
Definition schedule.hpp:90
schedule_executor(registry &registry, std::vector< std::unique_ptr< stage_executor > > stages, std::unique_ptr< stage_executor > init)
Constructs a schedule executor.
Definition schedule.hpp:76
Manages the execution of systems in different stages.
Definition schedule.hpp:15
auto begin_stage(std::string_view name={}) -> stage &
Begins a new stage in the schedule.
Definition schedule.hpp:38
auto create_executor(registry &registry, void *user_context=nullptr) -> std::unique_ptr< schedule_executor >
Creates an executor for the schedule.
Definition schedule.hpp:49
auto add_init_system(Args &&... args) -> self_type &
Adds an initialization system to the initial stage.
Definition schedule.hpp:27
Class representing a stage in the schedule.
Definition stage.hpp:29
auto create_executor(registry &registry, void *user_context=nullptr) -> std::unique_ptr< stage_executor >
Creates an executor for the stage.
Definition stage.hpp:74
auto add_system(main_thread_execution_policty_t policy, auto &&... args) -> self_type &
Adds a system to the main thread execution list.
Definition stage.hpp:53
Definition archetype.hpp:11
STL namespace.