ECSTASY
All in the name
Loading...
Searching...
No Matches
Pipeline.cpp
Go to the documentation of this file.
1
11
12#include "Pipeline.hpp"
14
15namespace ecstasy
16{
17
19 {
20 return _pipeline._systemsIds.begin() + static_cast<long>(_begin);
21 }
22
24 {
25 return _pipeline._systemsIds.begin() + static_cast<long>(end_idx());
26 }
27
29 {
30 if (!_timer.trigger())
31 return;
32 for (auto it = begin(); it < end(); ++it) {
33 _pipeline._registry.runSystem(*it);
34 }
35 }
36
37 Pipeline::Pipeline(Registry &registry) : _registry(registry)
38 {
39 }
40
42 {
43 Phase &phase = getPhase(phaseId);
44
45 // Phase end is always after the last valid system (like end() in std::vector)
46 _systemsIds.insert(_systemsIds.begin() + static_cast<long>(phase.end_idx()), system);
47 ++phase._size;
48
50 auto phaseIt = _phases.find(phaseId);
51 size_t nextPhaseBegin = phase.end_idx();
52
53 for (++phaseIt; phaseIt != _phases.end(); ++phaseIt) {
54 phaseIt->second._begin = nextPhaseBegin;
55 nextPhaseBegin = phaseIt->second.end_idx();
56 }
57 }
58
60 {
61 for (auto &phase : _phases) {
62 phase.second.run();
63 }
64 }
65
67 {
68 auto phaseIt = _phases.find(phase);
69
70 if (phaseIt == _phases.end())
71 return;
72
73 phaseIt->second.run();
74 }
75
77 {
78 if (autoRegister)
79 return registerPhase(id);
80 auto it = _phases.find(id);
81
82 if (it != _phases.end())
83 return it->second;
84 throw std::out_of_range("Phase not found");
85 }
86
88 {
89 auto it = std::find(_systemsIds.begin(), _systemsIds.end(), system);
90
91 if (it == _systemsIds.end())
92 throw std::out_of_range("System not found in any phase");
93 for (auto &phase : _phases) {
94 if (it >= phase.second.begin() && it < phase.second.end())
95 return phase.second;
96 }
97 throw std::out_of_range("System not found in any phase");
98 }
99
101 {
102 auto it = _phases.find(id);
103
104 if (it != _phases.end())
105 return it->second;
106
107 it = _phases.emplace(id, Phase(*this, id)).first;
108 Phase &phase = it->second;
109
110 // No phase preceeding this one, set begin to the start of the systems list
111 if (it == _phases.begin()) {
112 phase._begin = 0;
113 } else {
114 phase._begin = std::prev(it)->second.end_idx();
115 }
116 // No need to update following phases since our phase is empty
117 return phase;
118 }
119
120} // namespace ecstasy
Registry class definition.
T begin(T... args)
A phase in the pipeline.
Definition Pipeline.hpp:66
SystemIterator begin() const noexcept
Get the iterator to the first system in this phase.
Definition Pipeline.cpp:18
std::size_t _begin
Index of the first system in this phase.
Definition Pipeline.hpp:178
Pipeline & _pipeline
Owning pipeline.
Definition Pipeline.hpp:176
std::size_t _size
Number of systems in this phase.
Definition Pipeline.hpp:180
void run()
Run all systems in this phase.
Definition Pipeline.cpp:28
SystemIterator end() const noexcept
Get the iterator past the last system in this phase.
Definition Pipeline.cpp:23
constexpr std::size_t end_idx() const
Get the index past the last system in this phase.
Definition Pipeline.hpp:209
Phase & getPhase(PhaseId id, bool autoRegister=true)
Get a Phase instance from its identifier.
Definition Pipeline.cpp:76
std::map< PhaseId, Phase > _phases
Ordered map of phases.
Definition Pipeline.hpp:326
Pipeline(Registry &registry)
Construct a new Pipeline owned by the given registry.
Definition Pipeline.cpp:37
Phase & registerPhase(PhaseId id)
Register a phase with the given identifier.
Definition Pipeline.cpp:100
const Phase & getSystemPhase() const
Get the phase containing the given system.
Definition Pipeline.hpp:269
void run()
Run a frame of the pipeline.
Definition Pipeline.cpp:59
void addSystem(std::type_index system, PhaseId phase=static_cast< std::size_t >(PredefinedPhases::OnUpdate))
Add a system to the pipeline.
Definition Pipeline.cpp:41
Registry & _registry
Owning registry.
Definition Pipeline.hpp:328
std::vector< std::type_index >::const_iterator SystemIterator
Type of system iterator.
Definition Pipeline.hpp:35
std::vector< std::type_index > _systemsIds
Ordered list of systems.
Definition Pipeline.hpp:324
Base of an ECS architecture.
Definition Registry.hpp:82
T end(T... args)
T find(T... args)
T insert(T... args)
Namespace containing all symbols specific to ecstasy.
Definition ecstasy.hpp:30
T prev(T... args)