Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
21 entity_t id,
22 int32_t stage_current,
23 int32_t stage_count,
24 ecs_ftime_t delta_time,
25 void *param)
26 : stage_(world)
27 , id_(id)
28 , delta_time_(delta_time)
29 , param_(param)
30 , stage_current_(stage_current)
31 , stage_count_(stage_count) { }
32
35 offset_ = offset;
36 return *this;
37 }
38
41 limit_ = limit;
42 return *this;
43 }
44
47 stage_ = stage.c_ptr();
48 return *this;
49 }
50
53 if (stage_count_) {
55 stage_, id_, stage_current_, stage_count_, delta_time_,
56 param_);
57 } else {
58 ecs_run(stage_, id_, delta_time_, param_);
59 }
60 }
61
62private:
63 world_t *stage_;
64 entity_t id_;
65 ecs_ftime_t delta_time_;
66 void *param_;
67 int32_t offset_;
68 int32_t limit_;
69 int32_t stage_current_;
70 int32_t stage_count_;
71};
72
77struct system final : entity
78{
79 using entity::entity;
80
82 explicit system() {
83 id_ = 0;
84 world_ = nullptr;
85 }
86
89 world_ = world;
90 id_ = ecs_system_init(world, desc);
91 }
92
94 void ctx(void *ctx) {
95 ecs_system_desc_t desc = {};
96 desc.entity = id_;
97 desc.ctx = ctx;
98 ecs_system_init(world_, &desc);
99 }
100
102 void* ctx() const {
103 return ecs_system_get(world_, id_)->ctx;
104 }
105
110
112 system& set_group(uint64_t group_id) {
113 ecs_system_set_group(world_, id_, group_id);
114 return *this;
115 }
116
118 template <typename Group>
121 return *this;
122 }
123
125 system_runner_fluent run(ecs_ftime_t delta_time = 0.0f, void *param = nullptr) const {
126 return system_runner_fluent(world_, id_, 0, 0, delta_time, param);
127 }
128
131 int32_t stage_current,
132 int32_t stage_count,
133 ecs_ftime_t delta_time = 0.0f,
134 void *param = nullptr) const
135 {
137 world_, id_, stage_current, stage_count, delta_time, param);
138 }
139
140# ifdef FLECS_TIMER
142# endif
143
144};
145
147inline system world::system(flecs::entity e) const {
148 return flecs::system(world_, e);
149}
150
151template <typename... Comps, typename... Args>
152inline system_builder<Comps...> world::system(Args &&... args) const {
153 return flecs::system_builder<Comps...>(world_, FLECS_FWD(args)...);
154}
155
156namespace _ {
157
158template <typename ArgList>
160
161template <typename ... Args>
162struct system_each_normalize_args<arg_list<Args...>> {
163 using type = arg_list<remove_reference_t<Args>...>;
164};
165
166template <typename ArgList, typename = int>
170
171template <typename First, typename ... Args>
172struct system_each_callback_args<arg_list<First, Args...>,
173 if_t<is_same<decay_t<First>, flecs::entity>::value>> {
174 using type = typename system_each_normalize_args<arg_list<Args...>>::type;
175};
176
177template <typename First, typename Second, typename ... Args>
178struct system_each_callback_args<arg_list<First, Second, Args...>,
179 if_t<is_same<decay_t<First>, flecs::iter>::value>> {
180 using type = typename system_each_normalize_args<arg_list<Args...>>::type;
181};
182
183inline void system_init(flecs::world& world) {
184 world.component<TickSource>("flecs::system::TickSource");
185}
186
187} // namespace _
188
189template <typename ... Components>
190template <typename Func>
191inline system system_builder<Components...>::each(Func&& func) {
192 if constexpr (sizeof...(Components) == 0) {
193 using CallbackComponents =
194 typename _::system_each_callback_args<arg_list_t<Func>>::type;
195 return this->each_callback(CallbackComponents{}, FLECS_FWD(func));
196 } else {
197 // Faster version of each() that iterates the query on the C++ side.
198 return this->run_each(FLECS_FWD(func));
199 }
200}
201
202template <typename ... Components>
203template <typename ... CallbackComponents, typename Func>
204inline system system_builder<Components...>::each_callback(
205 _::arg_list<CallbackComponents...>,
206 Func&& func)
207{
208 this->template prepend_each_callback_signature<CallbackComponents...>();
209
210 using Delegate = typename _::each_delegate<
211 typename std::decay<Func>::type, CallbackComponents...>;
212
213 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
214 this->desc_.run = Delegate::run_each;
215 this->desc_.run_ctx = ctx;
216 this->desc_.run_ctx_free = _::free_obj<Delegate>;
217 return system(this->world_, &this->desc_);
218}
219
220template <typename ... Components>
221template <typename ... CallbackComponents>
222inline void system_builder<Components...>::prepend_each_callback_signature() {
223 if constexpr (sizeof...(Components) == 0 && sizeof...(CallbackComponents) != 0) {
224 constexpr int32_t callback_term_count =
225 static_cast<int32_t>(sizeof...(CallbackComponents));
226
227 ecs_assert(this->term_index_ + callback_term_count <= FLECS_TERM_COUNT_MAX,
228 ECS_INVALID_PARAMETER, "maximum number of terms exceeded");
229
230 const int32_t existing_term_count = this->term_index_;
231 this->set_term(nullptr);
232
233 for (int32_t i = existing_term_count - 1; i >= 0; i --) {
234 this->desc_.query.terms[i + callback_term_count] =
235 this->desc_.query.terms[i];
236 }
237
238 for (int32_t i = 0; i < callback_term_count; i ++) {
239 this->desc_.query.terms[i] = ecs_term_t{};
240 }
241
242 this->term_index_ = 0;
243 _::sig<CallbackComponents...>(this->world_).populate(this);
244 this->term_index_ = existing_term_count + callback_term_count;
245 }
246}
247
248} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
FLECS_API const ecs_system_t * ecs_system_get(const ecs_world_t *world, ecs_entity_t system)
Get a system object.
FLECS_API ecs_entity_t ecs_run(ecs_world_t *world, ecs_entity_t system, ecs_ftime_t delta_time, void *param)
Run a specific system manually.
FLECS_API ecs_entity_t ecs_system_init(ecs_world_t *world, const ecs_system_desc_t *desc)
Create a system.
FLECS_API void ecs_system_set_group(ecs_world_t *world, ecs_entity_t system, uint64_t group_id)
Set query group for system.
FLECS_API ecs_entity_t ecs_run_worker(ecs_world_t *world, ecs_entity_t system, int32_t stage_current, int32_t stage_count, ecs_ftime_t delta_time, void *param)
Same as ecs_run(), but subdivides entities across a number of provided stages.
flecs::system system(flecs::entity e) const
Upcast an entity to a system.
flecs::component< T > component(Args &&... args) const
Find or register a component.
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
#define FLECS_TERM_COUNT_MAX
Maximum number of terms in queries.
Definition flecs.h:326
System builder.
Component used to provide a tick source to systems.
Definition system.h:32
Use with ecs_system_init() to create or update a system.
Definition system.h:38
void * ctx
Context to be passed to callback (as ecs_iter_t::param).
Definition system.h:70
ecs_entity_t entity
Existing entity to associate with the system (optional).
Definition system.h:42
void * ctx
Userdata for the system.
Definition system.h:147
Type that describes a term (single element in a query).
Definition flecs.h:810
Entity.
Definition entity.hpp:30
entity()
Default constructor.
Definition entity.hpp:32
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::world world() const
Get the world.
Definition impl.hpp:58
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
Class for iterating over query results.
Definition iter.hpp:68
Typed query.
Definition impl.hpp:220
System builder.
Definition builder.hpp:25
Fluent interface for running a system.
Definition impl.hpp:17
~system_runner_fluent()
Destructor.
Definition impl.hpp:52
system_runner_fluent & stage(flecs::world &stage)
Set the stage for the system runner.
Definition impl.hpp:46
system_runner_fluent & limit(int32_t limit)
Set the limit for the system runner.
Definition impl.hpp:40
system_runner_fluent(world_t *world, entity_t id, int32_t stage_current, int32_t stage_count, ecs_ftime_t delta_time, void *param)
Construct a system runner.
Definition impl.hpp:19
system_runner_fluent & offset(int32_t offset)
Set the offset for the system runner.
Definition impl.hpp:34
System.
Definition impl.hpp:78
system(flecs::world_t *world, ecs_system_desc_t *desc)
Construct from a world and a system descriptor.
Definition impl.hpp:88
system & set_group(uint64_t group_id)
Set the query group.
Definition impl.hpp:112
void ctx(void *ctx)
Set the system context.
Definition impl.hpp:94
flecs::query query() const
Get the query for this system.
Definition impl.hpp:107
system_runner_fluent run(ecs_ftime_t delta_time=0.0f, void *param=nullptr) const
Run the system.
Definition impl.hpp:125
void * ctx() const
Get the system context.
Definition impl.hpp:102
system & set_group()
Set the query group.
Definition impl.hpp:119
system()
Default constructor.
Definition impl.hpp:82
system_runner_fluent run_worker(int32_t stage_current, int32_t stage_count, ecs_ftime_t delta_time=0.0f, void *param=nullptr) const
Run the system on a specific worker stage.
Definition impl.hpp:130
The world.
Definition world.hpp:246
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1545
Timer module system mixin.
enable_if_t< V, int > if_t
Convenience enable_if alias using int as default type.
Definition utils.hpp:168