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
CommandBuffer.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <vector> // for storing the queues of deffered binds/unbinds/destroys
10#include <functional> // for std::function for type-erased std::function<void()>>
11
12#include "Imago/ECS/Entity.hpp"
13
14namespace Imago::ECS
15{
16 // forward delcaration to Nexus (see below comment)
17 class Nexus;
18
32 private:
33
34 Nexus& _nexus;
35 std::vector<Entity> _destroyQueue;
36 std::vector<std::function<void()>> _bindQueue;
37 std::vector<std::function<void()>> _unbindQueue;
38
39 public:
40
45 explicit CommandBuffer(Nexus& nexus);
46
51 void defer_destroy(Entity e);
52
53 // NOTE: we are storing our bind and unbind queues as type erased std::function<void()> storing lambda functions objects
54 // that wrap a lambda with the logic necessary to bind/unbind/patch on specific component. This solves the problem that not every bind command is the same
55 // and therefore cannot be stored in a std::vector since its type depends on what component is being bound. Wrapping them in a std::function<void()> erases
56 // that type difference, allowing them to all be stored in a single vector.
57
64 template <typename T>
65 void defer_bind(Entity e, T component);
66
72 template <typename T>
73 void defer_unbind(Entity e);
74
78 void flush();
79 };
80
81 // NOTE: Command Buffer definitions are located in Nexus.hpp. This is becuase of the circular include. C++
82 // will not allow Imago/ECS/Nexus.hpp to be included in this file because this file is included in Nexus.hpp
83 // this means we have to do an incomplete class definition instead of an include to get _nexus in the class
84 // definition. However, we can not have the function definitions here becuase they actually call a Nexus
85 // method and therefore need the full definitions. And, since they are templated they cannot be in CommandBuffer.cpp either.
86 // The most natural and least complicated place for them now is at the bottom of the Nexus.hpp file since the CommandBuffer
87 // is intimately related to the Nexus.
88}
Defines the Entity alias and the free functions used to pack and unpack it.
uint32_t Entity
A packed identifier: 20 bits of index, 12 bits of generation.
Definition Entity.hpp:20
void defer_bind(Entity e, T component)
Queues a bind_immediate command lambda.
Definition Nexus.hpp:536
void defer_unbind(Entity e)
Queues an unbind_immediate command lambda.
Definition Nexus.hpp:545
CommandBuffer(Nexus &nexus)
Construct a new Command Buffer object with the Nexus that it will apply its queued operations to.
Definition CommandBuffer.cpp:8
void defer_destroy(Entity e)
Queues an Entity for destruction.
Definition CommandBuffer.cpp:11
void flush()
Goes through all the defer queues and actually applies the operations to the associated Nexus.
Definition CommandBuffer.cpp:17
the Nexus is the brain of the ECS world. It is the object the user will use to interact with everythi...
Definition Nexus.hpp:84