Flecs: include/flecs/addons/system.h Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.1 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
system.h
Go to the documentation of this file.
1
9
10#ifdef FLECS_SYSTEM
11
19
20#ifndef FLECS_MODULE
21#define FLECS_MODULE
22#endif
23
24#ifndef FLECS_SYSTEM_H
25#define FLECS_SYSTEM_H
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
32 typedef struct EcsTickSource {
33 bool tick;
34 ecs_ftime_t time_elapsed;
35} EcsTickSource;
36
38 typedef struct ecs_system_desc_t {
39 int32_t _canary;
40
42 ecs_entity_t entity;
43
45 ecs_query_desc_t query;
46
49 ecs_entity_t phase;
50
54 ecs_iter_action_t callback;
55
67 ecs_run_action_t run;
68
70 void *ctx;
71
73 ecs_ctx_free_t ctx_free;
74
76 void *callback_ctx;
77
79 ecs_ctx_free_t callback_ctx_free;
80
82 void *run_ctx;
83
85 ecs_ctx_free_t run_ctx_free;
86
88 ecs_ftime_t interval;
89
91 int32_t rate;
92
94 ecs_entity_t tick_source;
95
97 bool multi_threaded;
98
101 bool immediate;
102} ecs_system_desc_t;
103
113FLECS_API
114 ecs_entity_t ecs_system_init(
115 ecs_world_t *world,
116 const ecs_system_desc_t *desc);
117
132FLECS_API
133 ecs_entity_t ecs_system_update(
134 ecs_world_t *world,
135 ecs_entity_t system,
136 const ecs_system_desc_t *desc);
137
139 typedef struct ecs_system_t {
140 ecs_header_t hdr;
141
143 ecs_run_action_t run;
144
146 ecs_iter_action_t action;
147
149 ecs_query_t *query;
150
152 uint64_t group_id;
153
155 bool group_id_set;
156
158 ecs_entity_t tick_source;
159
161 bool multi_threaded;
162
164 bool immediate;
165
167 const char *name;
168
170 void *ctx;
171
173 void *callback_ctx;
174
176 void *run_ctx;
177
179 ecs_ctx_free_t ctx_free;
180
182 ecs_ctx_free_t callback_ctx_free;
183
185 ecs_ctx_free_t run_ctx_free;
186
188 ecs_ftime_t time_spent;
189
191 ecs_ftime_t time_passed;
192
194 int64_t last_frame;
195
197 flecs_poly_dtor_t dtor;
198} ecs_system_t;
199
208FLECS_API
209 const ecs_system_t* ecs_system_get(
210 const ecs_world_t *world,
211 ecs_entity_t system);
212
222FLECS_API
223 void ecs_system_set_group(
224 ecs_world_t *world,
225 ecs_entity_t system,
226 uint64_t group_id);
227
228#ifndef FLECS_LEGACY
229
231#define ECS_SYSTEM_DECLARE(id) ecs_entity_t ecs_id(id)
232
241#define ECS_SYSTEM_DEFINE(world, id_, phase_, ...) \
242 { \
243 ecs_system_desc_t desc = {0}; \
244 ecs_entity_desc_t edesc = {0}; \
245 edesc.id = ecs_id(id_);\
246 edesc.name = #id_;\
247 desc.entity = ecs_entity_init(world, &edesc);\
248 desc.query.expr = #__VA_ARGS__; \
249 desc.phase = phase_; \
250 desc.callback = id_; \
251 ecs_id(id_) = ecs_system_init(world, &desc); \
252 } \
253 ecs_assert(ecs_id(id_) != 0, ECS_INVALID_PARAMETER, "failed to create system %s", #id_)
254
263#define ECS_SYSTEM(world, id, phase, ...) \
264 ecs_entity_t ecs_id(id) = 0; ECS_SYSTEM_DEFINE(world, id, phase, __VA_ARGS__);\
265 ecs_entity_t id = ecs_id(id);\
266 (void)ecs_id(id);\
267 (void)id
268
287#define ecs_system(world, ...)\
288 ecs_system_init(world, &(ecs_system_desc_t) __VA_ARGS__ )
289
290#endif
291
319FLECS_API
320 ecs_entity_t ecs_run(
321 ecs_world_t *world,
322 ecs_entity_t system,
323 ecs_ftime_t delta_time,
324 void *param);
325
336FLECS_API
337 ecs_entity_t ecs_run_worker(
338 ecs_world_t *world,
339 ecs_entity_t system,
340 int32_t stage_current,
341 int32_t stage_count,
342 ecs_ftime_t delta_time,
343 void *param);
344
353FLECS_API
354 void FlecsSystemImport(
355 ecs_world_t *world);
356
357#ifdef __cplusplus
358}
359#endif
360
361#endif
362
364
365#endif
ecs_system_get
FLECS_API const ecs_system_t * ecs_system_get(const ecs_world_t *world, ecs_entity_t system)
Get a system object.
FlecsSystemImport
FLECS_API void FlecsSystemImport(ecs_world_t *world)
System module import function.
ecs_run
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.
ecs_system_init
FLECS_API ecs_entity_t ecs_system_init(ecs_world_t *world, const ecs_system_desc_t *desc)
Create a system.
ecs_system_set_group
FLECS_API void ecs_system_set_group(ecs_world_t *world, ecs_entity_t system, uint64_t group_id)
Set query group for system.
ecs_system_update
FLECS_API ecs_entity_t ecs_system_update(ecs_world_t *world, ecs_entity_t system, const ecs_system_desc_t *desc)
Update an existing system.
ecs_run_worker
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.
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:385
ecs_world_t
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:429
ecs_iter_action_t
void(* ecs_iter_action_t)(ecs_iter_t *it)
Function prototype for iterables.
Definition flecs.h:581
flecs_poly_dtor_t
void(* flecs_poly_dtor_t)(ecs_poly_t *poly)
Destructor function for poly objects.
Definition flecs.h:695
ecs_run_action_t
void(* ecs_run_action_t)(ecs_iter_t *it)
Function prototype for runnables (systems, observers).
Definition flecs.h:572
ecs_ctx_free_t
void(* ecs_ctx_free_t)(void *ctx)
Function to clean up context data.
Definition flecs.h:650
ecs_ftime_t
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
EcsTickSource
Component used to provide a tick source to systems.
Definition system.h:32
EcsTickSource::time_elapsed
ecs_ftime_t time_elapsed
Time elapsed since the last tick.
Definition system.h:34
EcsTickSource::tick
bool tick
True if providing a tick.
Definition system.h:33
ecs_header_t
Header for ecs_poly_t objects.
Definition flecs.h:524
ecs_query_desc_t
Used with ecs_query_init().
Definition flecs.h:1301
ecs_query_t
Queries are lists of constraints (terms) that match entities.
Definition flecs.h:838
ecs_system_desc_t
Use with ecs_system_init() and ecs_system_update().
Definition system.h:38
ecs_system_desc_t::rate
int32_t rate
Rate at which the system should run.
Definition system.h:91
ecs_system_desc_t::ctx
void * ctx
Context to be passed to callback (as ecs_iter_t::param).
Definition system.h:70
ecs_system_desc_t::run_ctx_free
ecs_ctx_free_t run_ctx_free
Callback to free run ctx.
Definition system.h:85
ecs_system_desc_t::ctx_free
ecs_ctx_free_t ctx_free
Callback to free ctx.
Definition system.h:73
ecs_system_desc_t::phase
ecs_entity_t phase
Optional pipeline phase for the system to run in.
Definition system.h:49
ecs_system_desc_t::multi_threaded
bool multi_threaded
If true, the system will be run on multiple threads.
Definition system.h:97
ecs_system_desc_t::immediate
bool immediate
If true, the system will have access to the actual world.
Definition system.h:101
ecs_system_desc_t::callback_ctx
void * callback_ctx
Context associated with callback (for language bindings).
Definition system.h:76
ecs_system_desc_t::run_ctx
void * run_ctx
Context associated with run (for language bindings).
Definition system.h:82
ecs_system_desc_t::interval
ecs_ftime_t interval
Interval in seconds at which the system should run.
Definition system.h:88
ecs_system_desc_t::_canary
int32_t _canary
Used for validity testing.
Definition system.h:39
ecs_system_desc_t::callback
ecs_iter_action_t callback
Callback that is run for each result returned by the system's query.
Definition system.h:54
ecs_system_desc_t::entity
ecs_entity_t entity
Existing entity to associate with the system (optional).
Definition system.h:42
ecs_system_desc_t::query
ecs_query_desc_t query
System query parameters.
Definition system.h:45
ecs_system_desc_t::callback_ctx_free
ecs_ctx_free_t callback_ctx_free
Callback to free callback ctx.
Definition system.h:79
ecs_system_desc_t::run
ecs_run_action_t run
Callback that is invoked when a system is run.
Definition system.h:67
ecs_system_desc_t::tick_source
ecs_entity_t tick_source
External tick source that determines when the system ticks.
Definition system.h:94
ecs_system_t
System type, get with ecs_system_get().
Definition system.h:139
ecs_system_t::action
ecs_iter_action_t action
See ecs_system_desc_t.
Definition system.h:146
ecs_system_t::query
ecs_query_t * query
System query.
Definition system.h:149
ecs_system_t::group_id_set
bool group_id_set
True if a query group is configured.
Definition system.h:155
ecs_system_t::hdr
ecs_header_t hdr
Object header.
Definition system.h:140
ecs_system_t::ctx_free
ecs_ctx_free_t ctx_free
Callback to free ctx.
Definition system.h:179
ecs_system_t::run_ctx
void * run_ctx
Run language binding context.
Definition system.h:176
ecs_system_t::run
ecs_run_action_t run
See ecs_system_desc_t.
Definition system.h:143
ecs_system_t::time_passed
ecs_ftime_t time_passed
Time passed since the last invocation.
Definition system.h:191
ecs_system_t::last_frame
int64_t last_frame
Last frame for which the system was considered.
Definition system.h:194
ecs_system_t::time_spent
ecs_ftime_t time_spent
Time spent on running the system.
Definition system.h:188
ecs_system_t::name
const char * name
Cached system name (for perf tracing).
Definition system.h:167
ecs_system_t::run_ctx_free
ecs_ctx_free_t run_ctx_free
Callback to free run ctx.
Definition system.h:185
ecs_system_t::immediate
bool immediate
Whether the system is run in immediate mode.
Definition system.h:164
ecs_system_t::multi_threaded
bool multi_threaded
Whether the system is multithreaded.
Definition system.h:161
ecs_system_t::ctx
void * ctx
Userdata for the system.
Definition system.h:170
ecs_system_t::callback_ctx_free
ecs_ctx_free_t callback_ctx_free
Callback to free callback ctx.
Definition system.h:182
ecs_system_t::group_id
uint64_t group_id
Query group to iterate.
Definition system.h:152
ecs_system_t::tick_source
ecs_entity_t tick_source
Tick source associated with the system.
Definition system.h:158
ecs_system_t::dtor
flecs_poly_dtor_t dtor
Mixin destructor.
Definition system.h:197
ecs_system_t::callback_ctx
void * callback_ctx
Callback language binding context.
Definition system.h:173