Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
flecs_c.h
Go to the documentation of this file.
1
6#ifndef FLECS_C_
7#define FLECS_C_
8
23/* Use for declaring entity, tag, prefab / any other entity identifier */
24#define ECS_DECLARE(id)\
25 ecs_entity_t id, ecs_id(id)
26
28#define ECS_ENTITY_DECLARE ECS_DECLARE
29
35#define ECS_ENTITY_DEFINE(world, id_, ...) \
36 { \
37 ecs_entity_desc_t desc = {0}; \
38 desc.id = id_; \
39 desc.name = #id_; \
40 desc.add_expr = #__VA_ARGS__; \
41 id_ = ecs_entity_init(world, &desc); \
42 ecs_id(id_) = id_; \
43 ecs_assert(id_ != 0, ECS_INVALID_PARAMETER, NULL); \
44 } \
45 (void)id_; \
46 (void)ecs_id(id_);
47
53#define ECS_ENTITY(world, id, ...) \
54 ecs_entity_t ecs_id(id); \
55 ecs_entity_t id = 0; \
56 ECS_ENTITY_DEFINE(world, id, __VA_ARGS__);
57
59#define ECS_TAG_DECLARE ECS_DECLARE
60
66#define ECS_TAG_DEFINE(world, id) ECS_ENTITY_DEFINE(world, id, 0)
67
73#define ECS_TAG(world, id) ECS_ENTITY(world, id, 0)
74
76#define ECS_PREFAB_DECLARE ECS_DECLARE
77
83#define ECS_PREFAB_DEFINE(world, id, ...) ECS_ENTITY_DEFINE(world, id, Prefab, __VA_ARGS__)
84
90#define ECS_PREFAB(world, id, ...) ECS_ENTITY(world, id, Prefab, __VA_ARGS__)
91
93#define ECS_COMPONENT_DECLARE(id) ecs_entity_t ecs_id(id)
94
100#define ECS_COMPONENT_DEFINE(world, id_) \
101 {\
102 ecs_component_desc_t desc = {0}; \
103 ecs_entity_desc_t edesc = {0}; \
104 edesc.id = ecs_id(id_); \
105 edesc.use_low_id = true; \
106 edesc.name = #id_; \
107 edesc.symbol = #id_; \
108 desc.entity = ecs_entity_init(world, &edesc); \
109 desc.type.size = ECS_SIZEOF(id_); \
110 desc.type.alignment = ECS_ALIGNOF(id_); \
111 ecs_id(id_) = ecs_component_init(world, &desc);\
112 ecs_assert(ecs_id(id_) != 0, ECS_INVALID_PARAMETER, NULL);\
113 }
114
120#define ECS_COMPONENT(world, id)\
121 ecs_entity_t ecs_id(id) = 0;\
122 ECS_COMPONENT_DEFINE(world, id);\
123 (void)ecs_id(id)
124
125/* Forward declare an observer. */
126#define ECS_OBSERVER_DECLARE(id) ecs_entity_t ecs_id(id)
127
133#define ECS_OBSERVER_DEFINE(world, id_, kind, ...)\
134 {\
135 ecs_observer_desc_t desc = {0};\
136 ecs_entity_desc_t edesc = {0}; \
137 edesc.id = ecs_id(id_); \
138 edesc.name = #id_; \
139 desc.entity = ecs_entity_init(world, &edesc); \
140 desc.callback = id_;\
141 desc.filter.expr = #__VA_ARGS__;\
142 desc.events[0] = kind;\
143 ecs_id(id_) = ecs_observer_init(world, &desc);\
144 ecs_assert(ecs_id(id_) != 0, ECS_INVALID_PARAMETER, NULL);\
145 }
146
152#define ECS_OBSERVER(world, id, kind, ...)\
153 ecs_entity_t ecs_id(id) = 0; \
154 ECS_OBSERVER_DEFINE(world, id, kind, __VA_ARGS__);\
155 ecs_entity_t id = ecs_id(id);\
156 (void)ecs_id(id);\
157 (void)id;
158
166#define ecs_entity(world, ...)\
167 ecs_entity_init(world, &(ecs_entity_desc_t) __VA_ARGS__ )
168
177#define ecs_component(world, ...)\
178 ecs_component_init(world, &(ecs_component_desc_t) __VA_ARGS__ )
179
185#define ecs_component_t(world, T)\
186 ecs_component_init(world, &(ecs_component_desc_t) { \
187 .entity = ecs_entity(world, { \
188 .name = #T, \
189 .symbol = #T, \
190 .use_low_id = true \
191 }), \
192 .type.size = ECS_SIZEOF(T), \
193 .type.alignment = ECS_ALIGNOF(T) \
194 })
195
203#define ecs_filter(world, ...)\
204 ecs_filter_init(world, &(ecs_filter_desc_t) __VA_ARGS__ )
205
213#define ecs_query(world, ...)\
214 ecs_query_init(world, &(ecs_query_desc_t) __VA_ARGS__ )
215
225#define ecs_observer(world, ...)\
226 ecs_observer_init(world, &(ecs_observer_desc_t) __VA_ARGS__ )
227
246#define ecs_new(world, T) ecs_new_w_id(world, ecs_id(T))
247
248#define ecs_new_w_pair(world, first, second)\
249 ecs_new_w_id(world, ecs_pair(first, second))
250
251#define ecs_bulk_new(world, component, count)\
252 ecs_bulk_new_w_id(world, ecs_id(component), count)
253
254#define ecs_new_entity(world, n)\
255 ecs_entity_init(world, &(ecs_entity_desc_t){\
256 .name = n,\
257 })
258
259#define ecs_new_prefab(world, n)\
260 ecs_entity_init(world, &(ecs_entity_desc_t){\
261 .name = n,\
262 .add = {EcsPrefab}\
263 })
264
265#define ecs_delete_children(world, parent)\
266 ecs_delete_with(world, ecs_pair(EcsChildOf, parent))
267
275#define ecs_add(world, entity, T)\
276 ecs_add_id(world, entity, ecs_id(T))
277
278#define ecs_add_pair(world, subject, first, second)\
279 ecs_add_id(world, subject, ecs_pair(first, second))
280
281
282#define ecs_remove(world, entity, T)\
283 ecs_remove_id(world, entity, ecs_id(T))
284
285#define ecs_remove_pair(world, subject, first, second)\
286 ecs_remove_id(world, subject, ecs_pair(first, second))
287
288
289#define ecs_override(world, entity, T)\
290 ecs_override_id(world, entity, ecs_id(T))
291
292#define ecs_override_pair(world, subject, first, second)\
293 ecs_override_id(world, subject, ecs_pair(first, second))
294
302#define ecs_set_ptr(world, entity, component, ptr)\
303 ecs_set_id(world, entity, ecs_id(component), sizeof(component), ptr)
304
305#define ecs_set(world, entity, component, ...)\
306 ecs_set_id(world, entity, ecs_id(component), sizeof(component), &(component)__VA_ARGS__)
307
308#define ecs_set_pair(world, subject, First, second, ...)\
309 ecs_set_id(world, subject,\
310 ecs_pair(ecs_id(First), second),\
311 sizeof(First), &(First)__VA_ARGS__)
312
313#define ecs_set_pair_second(world, subject, first, Second, ...)\
314 ecs_set_id(world, subject,\
315 ecs_pair(first, ecs_id(Second)),\
316 sizeof(Second), &(Second)__VA_ARGS__)
317
318#define ecs_set_pair_object ecs_set_pair_second
319
320#define ecs_set_override(world, entity, T, ...)\
321 ecs_add_id(world, entity, ECS_OVERRIDE | ecs_id(T));\
322 ecs_set(world, entity, T, __VA_ARGS__)
323
324#define ecs_emplace(world, entity, T)\
325 (ECS_CAST(T*, ecs_emplace_id(world, entity, ecs_id(T))))
326
327#define ecs_emplace_pair(world, entity, First, second)\
328 (ECS_CAST(First*, ecs_emplace_id(world, entity, ecs_pair_t(First, second))))
329
330#define ecs_get(world, entity, T)\
331 (ECS_CAST(const T*, ecs_get_id(world, entity, ecs_id(T))))
332
333#define ecs_get_pair(world, subject, First, second)\
334 (ECS_CAST(const First*, ecs_get_id(world, subject,\
335 ecs_pair(ecs_id(First), second))))
336
337#define ecs_get_pair_second(world, subject, first, Second)\
338 (ECS_CAST(const Second*, ecs_get_id(world, subject,\
339 ecs_pair(first, ecs_id(Second)))))
340
341#define ecs_get_pair_object ecs_get_pair_second
342
343#define ecs_record_get(world, record, T)\
344 (ECS_CAST(const T*, ecs_record_get_id(world, record, ecs_id(T))))
345
346#define ecs_record_has(world, record, T)\
347 (ecs_record_has_id(world, record, ecs_id(T)))
348
349#define ecs_record_get_pair(world, record, First, second)\
350 (ECS_CAST(const First*, ecs_record_get_id(world, record, \
351 ecs_pair(ecs_id(First), second))))
352
353#define ecs_record_get_pair_second(world, record, first, Second)\
354 (ECS_CAST(const Second*, ecs_record_get_id(world, record,\
355 ecs_pair(first, ecs_id(Second)))))
356
357#define ecs_record_get_mut(world, record, T)\
358 (ECS_CAST(T*, ecs_record_get_mut_id(world, record, ecs_id(T))))
359
360#define ecs_record_get_mut_pair(world, record, First, second)\
361 (ECS_CAST(First*, ecs_record_get_mut_id(world, record, \
362 ecs_pair(ecs_id(First), second))))
363
364#define ecs_record_get_mut_pair_second(world, record, first, Second)\
365 (ECS_CAST(Second*, ecs_record_get_mut_id(world, record,\
366 ecs_pair(first, ecs_id(Second)))))
367
368#define ecs_record_get_mut_pair_object ecs_record_get_mut_pair_second
369
370#define ecs_ref_init(world, entity, T)\
371 ecs_ref_init_id(world, entity, ecs_id(T))
372
373#define ecs_ref_get(world, ref, T)\
374 (ECS_CAST(const T*, ecs_ref_get_id(world, ref, ecs_id(T))))
375
376#define ecs_get_mut(world, entity, T)\
377 (ECS_CAST(T*, ecs_get_mut_id(world, entity, ecs_id(T))))
378
379#define ecs_get_mut_pair(world, subject, First, second)\
380 (ECS_CAST(First*, ecs_get_mut_id(world, subject,\
381 ecs_pair(ecs_id(First), second))))
382
383#define ecs_get_mut_pair_second(world, subject, first, Second)\
384 (ECS_CAST(Second*, ecs_get_mut_id(world, subject,\
385 ecs_pair(first, ecs_id(Second)))))
386
387#define ecs_get_mut_pair_object ecs_get_mut_pair_second
388
389#define ecs_modified(world, entity, component)\
390 ecs_modified_id(world, entity, ecs_id(component))
391
392#define ecs_modified_pair(world, subject, first, second)\
393 ecs_modified_id(world, subject, ecs_pair(first, second))
394
402#define ecs_singleton_add(world, comp)\
403 ecs_add(world, ecs_id(comp), comp)
404
405#define ecs_singleton_remove(world, comp)\
406 ecs_remove(world, ecs_id(comp), comp)
407
408#define ecs_singleton_get(world, comp)\
409 ecs_get(world, ecs_id(comp), comp)
410
411#define ecs_singleton_set(world, comp, ...)\
412 ecs_set(world, ecs_id(comp), comp, __VA_ARGS__)
413
414#define ecs_singleton_get_mut(world, comp)\
415 ecs_get_mut(world, ecs_id(comp), comp)
416
417#define ecs_singleton_modified(world, comp)\
418 ecs_modified(world, ecs_id(comp), comp)
419
427#define ecs_has(world, entity, T)\
428 ecs_has_id(world, entity, ecs_id(T))
429
430#define ecs_has_pair(world, entity, first, second)\
431 ecs_has_id(world, entity, ecs_pair(first, second))
432
433#define ecs_owns_pair(world, entity, first, second)\
434 ecs_owns_id(world, entity, ecs_pair(first, second))
435
436#define ecs_owns(world, entity, T)\
437 ecs_owns_id(world, entity, ecs_id(T))
438
439#define ecs_shares_id(world, entity, id)\
440 (ecs_search_relation(world, ecs_get_table(world, entity), 0, ecs_id(id), \
441 EcsIsA, 1, 0, 0, 0, 0) != -1)
442
443#define ecs_shares_pair(world, entity, first, second)\
444 (ecs_shares_id(world, entity, ecs_pair(first, second)))
445
446#define ecs_shares(world, entity, T)\
447 (ecs_shares_id(world, entity, ecs_id(T)))
448
456#define ecs_enable_component(world, entity, T, enable)\
457 ecs_enable_id(world, entity, ecs_id(T), enable)
458
459#define ecs_is_enabled_component(world, entity, T)\
460 ecs_is_enabled_id(world, entity, ecs_id(T))
461
462#define ecs_enable_pair(world, entity, First, second, enable)\
463 ecs_enable_id(world, entity, ecs_pair(ecs_id(First), second), enable)
464
465#define ecs_is_enabled_pair(world, entity, First, second)\
466 ecs_is_enabled_id(world, entity, ecs_pair(ecs_id(First), second))
467
475#define ecs_lookup_path(world, parent, path)\
476 ecs_lookup_path_w_sep(world, parent, path, ".", NULL, true)
477
478#define ecs_lookup_fullpath(world, path)\
479 ecs_lookup_path_w_sep(world, 0, path, ".", NULL, true)
480
481#define ecs_get_path(world, parent, child)\
482 ecs_get_path_w_sep(world, parent, child, ".", NULL)
483
484#define ecs_get_fullpath(world, child)\
485 ecs_get_path_w_sep(world, 0, child, ".", NULL)
486
487#define ecs_get_fullpath_buf(world, child, buf)\
488 ecs_get_path_w_sep_buf(world, 0, child, ".", NULL, buf)
489
490#define ecs_new_from_path(world, parent, path)\
491 ecs_new_from_path_w_sep(world, parent, path, ".", NULL)
492
493#define ecs_new_from_fullpath(world, path)\
494 ecs_new_from_path_w_sep(world, 0, path, ".", NULL)
495
496#define ecs_add_path(world, entity, parent, path)\
497 ecs_add_path_w_sep(world, entity, parent, path, ".", NULL)
498
499#define ecs_add_fullpath(world, entity, path)\
500 ecs_add_path_w_sep(world, entity, 0, path, ".", NULL)
501
511#define ecs_set_hooks(world, T, ...)\
512 ecs_set_hooks_id(world, ecs_id(T), &(ecs_type_hooks_t)__VA_ARGS__)
513
514#define ecs_get_hooks(world, T)\
515 ecs_get_hooks_id(world, ecs_id(T));
516
521#define ECS_CTOR(type, var, ...)\
522 ECS_XTOR_IMPL(type, ctor, var, __VA_ARGS__)
523
528#define ECS_DTOR(type, var, ...)\
529 ECS_XTOR_IMPL(type, dtor, var, __VA_ARGS__)
530
535#define ECS_COPY(type, dst_var, src_var, ...)\
536 ECS_COPY_IMPL(type, dst_var, src_var, __VA_ARGS__)
537
542#define ECS_MOVE(type, dst_var, src_var, ...)\
543 ECS_MOVE_IMPL(type, dst_var, src_var, __VA_ARGS__)
544
549#define ECS_ON_ADD(type, ptr, ...)\
550 ECS_HOOK_IMPL(type, ecs_on_add(type), ptr, __VA_ARGS__)
551#define ECS_ON_REMOVE(type, ptr, ...)\
552 ECS_HOOK_IMPL(type, ecs_on_remove(type), ptr, __VA_ARGS__)
553#define ECS_ON_SET(type, ptr, ...)\
554 ECS_HOOK_IMPL(type, ecs_on_set(type), ptr, __VA_ARGS__)
555
556/* Map from typename to function name of component lifecycle action */
557#define ecs_ctor(type) type##_ctor
558#define ecs_dtor(type) type##_dtor
559#define ecs_copy(type) type##_copy
560#define ecs_move(type) type##_move
561#define ecs_on_set(type) type##_on_set
562#define ecs_on_add(type) type##_on_add
563#define ecs_on_remove(type) type##_on_remove
564
572#define ecs_count(world, type)\
573 ecs_count_id(world, ecs_id(type))
574
582#define ecs_field(it, T, index)\
583 (ECS_CAST(T*, ecs_field_w_size(it, sizeof(T), index)))
584
592#define ecs_table_get(world, table, T, offset)\
593 (ECS_CAST(T*, ecs_table_get_id(world, table, ecs_id(T), offset)))
594
595#define ecs_table_get_pair(world, table, First, second, offset)\
596 (ECS_CAST(First*, ecs_table_get_id(world, table, ecs_pair(ecs_id(First), second), offset)))
597
598#define ecs_table_get_pair_second(world, table, first, Second, offset)\
599 (ECS_CAST(Second*, ecs_table_get_id(world, table, ecs_pair(first, ecs_id(Second)), offset)))
600
608#define ecs_value(T, ptr) ((ecs_value_t){ecs_id(T), ptr})
609#define ecs_value_new_t(world, T) ecs_value_new(world, ecs_id(T))
610
621#define ecs_sort_table(id) ecs_id(id##_sort_table)
622
623#define ecs_compare(id) ecs_id(id##_compare_fn)
624
625/* Declare efficient table sorting operation that uses provided compare function.
626 * For best results use LTO or make the function body visible in the same compilation unit.
627 * Variadic arguments are prepended before generated functions, use it to declare static
628 * or exported functions.
629 * Parameters of the comparison function:
630 * ecs_entity_t e1, const void* ptr1,
631 * ecs_entity_t e2, const void* ptr2
632 * Parameters of the sort functions:
633 * ecs_world_t *world
634 * ecs_table_t *table
635 * ecs_entity_t *entities
636 * void *ptr
637 * int32_t elem_size
638 * int32_t lo
639 * int32_t hi
640 * ecs_order_by_action_t order_by - Pointer to the original comparison function. You are not supposed to use it.
641 * Example:
642 * int CompareMyType(ecs_entity_t e1, const void* ptr1, ecs_entity_t e2, const void* ptr2) { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; }
643 * ECS_SORT_TABLE_WITH_COMPARE(MyType, MyCustomCompare, CompareMyType)
644 */
645#define ECS_SORT_TABLE_WITH_COMPARE(id, op_name, compare_fn, ...) \
646 static int32_t ECS_CONCAT(op_name, _partition)( \
647 ecs_world_t *world, \
648 ecs_table_t *table, \
649 ecs_entity_t *entities, \
650 void *ptr, \
651 int32_t elem_size, \
652 int32_t lo, \
653 int32_t hi, \
654 ecs_order_by_action_t order_by) \
655 { \
656 (void)(order_by); \
657 int32_t p = (hi + lo) / 2; \
658 void *pivot = ECS_ELEM(ptr, elem_size, p); \
659 ecs_entity_t pivot_e = entities[p]; \
660 int32_t i = lo - 1, j = hi + 1; \
661 void *el; \
662 repeat: \
663 { \
664 do { \
665 i ++; \
666 el = ECS_ELEM(ptr, elem_size, i); \
667 } while ( compare_fn(entities[i], el, pivot_e, pivot) < 0); \
668 do { \
669 j --; \
670 el = ECS_ELEM(ptr, elem_size, j); \
671 } while ( compare_fn(entities[j], el, pivot_e, pivot) > 0); \
672 if (i >= j) { \
673 return j; \
674 } \
675 ecs_table_swap_rows(world, table, i, j); \
676 if (p == i) { \
677 pivot = ECS_ELEM(ptr, elem_size, j); \
678 pivot_e = entities[j]; \
679 } else if (p == j) { \
680 pivot = ECS_ELEM(ptr, elem_size, i); \
681 pivot_e = entities[i]; \
682 } \
683 goto repeat; \
684 } \
685 } \
686 __VA_ARGS__ void op_name( \
687 ecs_world_t *world, \
688 ecs_table_t *table, \
689 ecs_entity_t *entities, \
690 void *ptr, \
691 int32_t size, \
692 int32_t lo, \
693 int32_t hi, \
694 ecs_order_by_action_t order_by) \
695 { \
696 if ((hi - lo) < 1) { \
697 return; \
698 } \
699 int32_t p = ECS_CONCAT(op_name, _partition)(world, table, entities, ptr, size, lo, hi, order_by); \
700 op_name(world, table, entities, ptr, size, lo, p, order_by); \
701 op_name(world, table, entities, ptr, size, p + 1, hi, order_by); \
702 }
703
704/* Declare efficient table sorting operation that uses default component comparison operator.
705 * For best results use LTO or make the comparison operator visible in the same compilation unit.
706 * Variadic arguments are prepended before generated functions, use it to declare static
707 * or exported functions.
708 * Example:
709 * ECS_COMPARE(MyType, { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; });
710 * ECS_SORT_TABLE(MyType)
711 */
712#define ECS_SORT_TABLE(id, ...) \
713 ECS_SORT_TABLE_WITH_COMPARE(id, ecs_sort_table(id), ecs_compare(id), __VA_ARGS__)
714
715/* Declare component comparison operations.
716 * Parameters:
717 * ecs_entity_t e1, const void* ptr1,
718 * ecs_entity_t e2, const void* ptr2
719 * Example:
720 * ECS_COMPARE(MyType, { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; });
721 */
722#define ECS_COMPARE(id, ...) \
723 int ecs_compare(id)(ecs_entity_t e1, const void* ptr1, ecs_entity_t e2, const void* ptr2) { \
724 __VA_ARGS__ \
725 }
726
736#define ecs_isa(e) ecs_pair(EcsIsA, e)
737#define ecs_childof(e) ecs_pair(EcsChildOf, e)
738#define ecs_dependson(e) ecs_pair(EcsDependsOn, e)
739
740#define ecs_query_new(world, q_expr)\
741 ecs_query_init(world, &(ecs_query_desc_t){\
742 .filter.expr = q_expr\
743 })
744
745#define ecs_rule_new(world, q_expr)\
746 ecs_rule_init(world, &(ecs_filter_desc_t){\
747 .expr = q_expr\
748 })
749
754#endif // FLECS_C_