Flecs v4.0
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
24/* Use for declaring entity, tag, prefab / any other entity identifier */
25#define ECS_DECLARE(id)\
26 ecs_entity_t id, ecs_id(id)
27
29#define ECS_ENTITY_DECLARE ECS_DECLARE
30
39#define ECS_ENTITY_DEFINE(world, id_, ...) \
40 { \
41 ecs_entity_desc_t desc = {0}; \
42 desc.id = id_; \
43 desc.name = #id_; \
44 desc.add_expr = #__VA_ARGS__; \
45 id_ = ecs_entity_init(world, &desc); \
46 ecs_id(id_) = id_; \
47 ecs_assert(id_ != 0, ECS_INVALID_PARAMETER, "failed to create entity %s", #id_); \
48 } \
49 (void)id_; \
50 (void)ecs_id(id_)
51
60#define ECS_ENTITY(world, id, ...) \
61 ecs_entity_t ecs_id(id); \
62 ecs_entity_t id = 0; \
63 ECS_ENTITY_DEFINE(world, id, __VA_ARGS__)
64
66#define ECS_TAG_DECLARE ECS_DECLARE
67
76#define ECS_TAG_DEFINE(world, id) ECS_ENTITY_DEFINE(world, id, 0)
77
86#define ECS_TAG(world, id) ECS_ENTITY(world, id, 0)
87
89#define ECS_PREFAB_DECLARE ECS_DECLARE
90
99#define ECS_PREFAB_DEFINE(world, id, ...) ECS_ENTITY_DEFINE(world, id, Prefab, __VA_ARGS__)
100
109#define ECS_PREFAB(world, id, ...) ECS_ENTITY(world, id, Prefab, __VA_ARGS__)
110
112#define ECS_COMPONENT_DECLARE(id) ecs_entity_t ecs_id(id)
113
122#define ECS_COMPONENT_DEFINE(world, id_) \
123 {\
124 ecs_component_desc_t desc = {0}; \
125 ecs_entity_desc_t edesc = {0}; \
126 edesc.id = ecs_id(id_); \
127 edesc.use_low_id = true; \
128 edesc.name = #id_; \
129 edesc.symbol = #id_; \
130 desc.entity = ecs_entity_init(world, &edesc); \
131 desc.type.size = ECS_SIZEOF(id_); \
132 desc.type.alignment = ECS_ALIGNOF(id_); \
133 ecs_id(id_) = ecs_component_init(world, &desc);\
134 }\
135 ecs_assert(ecs_id(id_) != 0, ECS_INVALID_PARAMETER, "failed to create component %s", #id_)
136
145#define ECS_COMPONENT(world, id)\
146 ecs_entity_t ecs_id(id) = 0;\
147 ECS_COMPONENT_DEFINE(world, id);\
148 (void)ecs_id(id)
149
150/* Forward declare an observer. */
151#define ECS_OBSERVER_DECLARE(id) ecs_entity_t ecs_id(id)
152
161#define ECS_OBSERVER_DEFINE(world, id_, kind, ...)\
162 {\
163 ecs_observer_desc_t desc = {0};\
164 ecs_entity_desc_t edesc = {0}; \
165 edesc.id = ecs_id(id_); \
166 edesc.name = #id_; \
167 desc.entity = ecs_entity_init(world, &edesc); \
168 desc.callback = id_;\
169 desc.query.expr = #__VA_ARGS__;\
170 desc.events[0] = kind;\
171 ecs_id(id_) = ecs_observer_init(world, &desc);\
172 ecs_assert(ecs_id(id_) != 0, ECS_INVALID_PARAMETER, "failed to create observer %s", #id_);\
173 }
174
183#define ECS_OBSERVER(world, id, kind, ...)\
184 ecs_entity_t ecs_id(id) = 0; \
185 ECS_OBSERVER_DEFINE(world, id, kind, __VA_ARGS__);\
186 ecs_entity_t id = ecs_id(id);\
187 (void)ecs_id(id);\
188 (void)id
189
190/* Forward declare a query. */
191#define ECS_QUERY_DECLARE(name) ecs_query_t* name
192
201#define ECS_QUERY_DEFINE(world, name_, ...)\
202 {\
203 ecs_query_desc_t desc = {0};\
204 ecs_entity_desc_t edesc = {0}; \
205 edesc.name = #name_; \
206 desc.entity = ecs_entity_init(world, &edesc); \
207 desc.expr = #__VA_ARGS__;\
208 name_ = ecs_query_init(world, &desc);\
209 ecs_assert(name_ != NULL, ECS_INVALID_PARAMETER, "failed to create query %s", #name_);\
210 }
211
220#define ECS_QUERY(world, name, ...)\
221 ecs_query_t* name = NULL; \
222 ECS_QUERY_DEFINE(world, name, __VA_ARGS__);\
223 (void)name
224
235#define ecs_entity(world, ...)\
236 ecs_entity_init(world, &(ecs_entity_desc_t) __VA_ARGS__ )
237
249#define ecs_component(world, ...)\
250 ecs_component_init(world, &(ecs_component_desc_t) __VA_ARGS__ )
251
259#define ecs_query(world, ...)\
260 ecs_query_init(world, &(ecs_query_desc_t) __VA_ARGS__ )
261
274#define ecs_observer(world, ...)\
275 ecs_observer_init(world, &(ecs_observer_desc_t) __VA_ARGS__ )
276
296#define ecs_new_w(world, T) ecs_new_w_id(world, ecs_id(T))
297
298#define ecs_new_w_pair(world, first, second)\
299 ecs_new_w_id(world, ecs_pair(first, second))
300
301#define ecs_bulk_new(world, component, count)\
302 ecs_bulk_new_w_id(world, ecs_id(component), count)
303
311#define ecs_add(world, entity, T)\
312 ecs_add_id(world, entity, ecs_id(T))
313
314#define ecs_add_pair(world, subject, first, second)\
315 ecs_add_id(world, subject, ecs_pair(first, second))
316
317
318#define ecs_remove(world, entity, T)\
319 ecs_remove_id(world, entity, ecs_id(T))
320
321#define ecs_remove_pair(world, subject, first, second)\
322 ecs_remove_id(world, subject, ecs_pair(first, second))
323
324
325#define ecs_auto_override(world, entity, T)\
326 ecs_auto_override_id(world, entity, ecs_id(T))
327
328#define ecs_auto_override_pair(world, subject, first, second)\
329 ecs_auto_override_id(world, subject, ecs_pair(first, second))
330
338/* insert */
339#define ecs_insert(world, ...)\
340 ecs_entity(world, { .set = ecs_values(__VA_ARGS__)})
341
342/* set */
343
344#define ecs_set_ptr(world, entity, component, ptr)\
345 ecs_set_id(world, entity, ecs_id(component), sizeof(component), ptr)
346
347#define ecs_set(world, entity, component, ...)\
348 ecs_set_id(world, entity, ecs_id(component), sizeof(component), &(component)__VA_ARGS__)
349
350#define ecs_set_pair(world, subject, First, second, ...)\
351 ecs_set_id(world, subject,\
352 ecs_pair(ecs_id(First), second),\
353 sizeof(First), &(First)__VA_ARGS__)
354
355#define ecs_set_pair_second(world, subject, first, Second, ...)\
356 ecs_set_id(world, subject,\
357 ecs_pair(first, ecs_id(Second)),\
358 sizeof(Second), &(Second)__VA_ARGS__)
359
360#define ecs_set_override(world, entity, T, ...)\
361 ecs_add_id(world, entity, ECS_AUTO_OVERRIDE | ecs_id(T));\
362 ecs_set(world, entity, T, __VA_ARGS__)
363
364/* emplace */
365
366#define ecs_emplace(world, entity, T, is_new)\
367 (ECS_CAST(T*, ecs_emplace_id(world, entity, ecs_id(T), sizeof(T), is_new)))
368
369#define ecs_emplace_pair(world, entity, First, second, is_new)\
370 (ECS_CAST(First*, ecs_emplace_id(world, entity, ecs_pair_t(First, second), sizeof(First), is_new)))
371
372/* get */
373
374#define ecs_get(world, entity, T)\
375 (ECS_CAST(const T*, ecs_get_id(world, entity, ecs_id(T))))
376
377#define ecs_get_pair(world, subject, First, second)\
378 (ECS_CAST(const First*, ecs_get_id(world, subject,\
379 ecs_pair(ecs_id(First), second))))
380
381#define ecs_get_pair_second(world, subject, first, Second)\
382 (ECS_CAST(const Second*, ecs_get_id(world, subject,\
383 ecs_pair(first, ecs_id(Second)))))
384
385/* get_mut */
386
387#define ecs_get_mut(world, entity, T)\
388 (ECS_CAST(T*, ecs_get_mut_id(world, entity, ecs_id(T))))
389
390#define ecs_get_mut_pair(world, subject, First, second)\
391 (ECS_CAST(First*, ecs_get_mut_id(world, subject,\
392 ecs_pair(ecs_id(First), second))))
393
394#define ecs_get_mut_pair_second(world, subject, first, Second)\
395 (ECS_CAST(Second*, ecs_get_mut_id(world, subject,\
396 ecs_pair(first, ecs_id(Second)))))
397
398#define ecs_get_mut(world, entity, T)\
399 (ECS_CAST(T*, ecs_get_mut_id(world, entity, ecs_id(T))))
400
401/* ensure */
402
403#define ecs_ensure(world, entity, T)\
404 (ECS_CAST(T*, ecs_ensure_id(world, entity, ecs_id(T), sizeof(T))))
405
406#define ecs_ensure_pair(world, subject, First, second)\
407 (ECS_CAST(First*, ecs_ensure_id(world, subject,\
408 ecs_pair(ecs_id(First), second), sizeof(First))))
409
410#define ecs_ensure_pair_second(world, subject, first, Second)\
411 (ECS_CAST(Second*, ecs_ensure_id(world, subject,\
412 ecs_pair(first, ecs_id(Second)), sizeof(Second))))
413
414/* modified */
415
416#define ecs_modified(world, entity, component)\
417 ecs_modified_id(world, entity, ecs_id(component))
418
419#define ecs_modified_pair(world, subject, first, second)\
420 ecs_modified_id(world, subject, ecs_pair(first, second))
421
422/* record */
423
424#define ecs_record_get(world, record, T)\
425 (ECS_CAST(const T*, ecs_record_get_id(world, record, ecs_id(T))))
426
427#define ecs_record_has(world, record, T)\
428 (ecs_record_has_id(world, record, ecs_id(T)))
429
430#define ecs_record_get_pair(world, record, First, second)\
431 (ECS_CAST(const First*, ecs_record_get_id(world, record, \
432 ecs_pair(ecs_id(First), second))))
433
434#define ecs_record_get_pair_second(world, record, first, Second)\
435 (ECS_CAST(const Second*, ecs_record_get_id(world, record,\
436 ecs_pair(first, ecs_id(Second)))))
437
438#define ecs_record_ensure(world, record, T)\
439 (ECS_CAST(T*, ecs_record_ensure_id(world, record, ecs_id(T))))
440
441#define ecs_record_ensure_pair(world, record, First, second)\
442 (ECS_CAST(First*, ecs_record_ensure_id(world, record, \
443 ecs_pair(ecs_id(First), second))))
444
445#define ecs_record_ensure_pair_second(world, record, first, Second)\
446 (ECS_CAST(Second*, ecs_record_ensure_id(world, record,\
447 ecs_pair(first, ecs_id(Second)))))
448
449#define ecs_ref_init(world, entity, T)\
450 ecs_ref_init_id(world, entity, ecs_id(T))
451
452#define ecs_ref_get(world, ref, T)\
453 (ECS_CAST(T*, ecs_ref_get_id(world, ref, ecs_id(T))))
454
462#define ecs_singleton_add(world, comp)\
463 ecs_add(world, ecs_id(comp), comp)
464
465#define ecs_singleton_remove(world, comp)\
466 ecs_remove(world, ecs_id(comp), comp)
467
468#define ecs_singleton_get(world, comp)\
469 ecs_get(world, ecs_id(comp), comp)
470
471#define ecs_singleton_get_mut(world, comp)\
472 ecs_get_mut(world, ecs_id(comp), comp)
473
474#define ecs_singleton_set_ptr(world, comp, ptr)\
475 ecs_set_ptr(world, ecs_id(comp), comp, ptr)
476
477#define ecs_singleton_set(world, comp, ...)\
478 ecs_set(world, ecs_id(comp), comp, __VA_ARGS__)
479
480#define ecs_singleton_ensure(world, comp)\
481 ecs_ensure(world, ecs_id(comp), comp)
482
483#define ecs_singleton_emplace(world, comp, is_new)\
484 ecs_emplace(world, ecs_id(comp), comp, is_new)
485
486#define ecs_singleton_modified(world, comp)\
487 ecs_modified(world, ecs_id(comp), comp)
488
496#define ecs_has(world, entity, T)\
497 ecs_has_id(world, entity, ecs_id(T))
498
499#define ecs_has_pair(world, entity, first, second)\
500 ecs_has_id(world, entity, ecs_pair(first, second))
501
502#define ecs_owns_pair(world, entity, first, second)\
503 ecs_owns_id(world, entity, ecs_pair(first, second))
504
505#define ecs_owns(world, entity, T)\
506 ecs_owns_id(world, entity, ecs_id(T))
507
508#define ecs_shares_id(world, entity, id)\
509 (ecs_search_relation(world, ecs_get_table(world, entity), 0, ecs_id(id), \
510 EcsIsA, 1, 0, 0, 0, 0) != -1)
511
512#define ecs_shares_pair(world, entity, first, second)\
513 (ecs_shares_id(world, entity, ecs_pair(first, second)))
514
515#define ecs_shares(world, entity, T)\
516 (ecs_shares_id(world, entity, ecs_id(T)))
517
518#define ecs_get_target_for(world, entity, rel, T)\
519 ecs_get_target_for_id(world, entity, rel, ecs_id(T))
520
528#define ecs_enable_component(world, entity, T, enable)\
529 ecs_enable_id(world, entity, ecs_id(T), enable)
530
531#define ecs_is_enabled(world, entity, T)\
532 ecs_is_enabled_id(world, entity, ecs_id(T))
533
534#define ecs_enable_pair(world, entity, First, second, enable)\
535 ecs_enable_id(world, entity, ecs_pair(ecs_id(First), second), enable)
536
537#define ecs_is_enabled_pair(world, entity, First, second)\
538 ecs_is_enabled_id(world, entity, ecs_pair(ecs_id(First), second))
539
547#define ecs_lookup_from(world, parent, path)\
548 ecs_lookup_path_w_sep(world, parent, path, ".", NULL, true)
549
550#define ecs_get_path_from(world, parent, child)\
551 ecs_get_path_w_sep(world, parent, child, ".", NULL)
552
553#define ecs_get_path(world, child)\
554 ecs_get_path_w_sep(world, 0, child, ".", NULL)
555
556#define ecs_get_path_buf(world, child, buf)\
557 ecs_get_path_w_sep_buf(world, 0, child, ".", NULL, buf, false)
558
559#define ecs_new_from_path(world, parent, path)\
560 ecs_new_from_path_w_sep(world, parent, path, ".", NULL)
561
562#define ecs_add_path(world, entity, parent, path)\
563 ecs_add_path_w_sep(world, entity, parent, path, ".", NULL)
564
565#define ecs_add_fullpath(world, entity, path)\
566 ecs_add_path_w_sep(world, entity, 0, path, ".", NULL)
567
577#define ecs_set_hooks(world, T, ...)\
578 ecs_set_hooks_id(world, ecs_id(T), &(ecs_type_hooks_t)__VA_ARGS__)
579
580#define ecs_get_hooks(world, T)\
581 ecs_get_hooks_id(world, ecs_id(T));
582
590#define ECS_CTOR(type, var, ...)\
591 ECS_XTOR_IMPL(type, ctor, var, __VA_ARGS__)
592
600#define ECS_DTOR(type, var, ...)\
601 ECS_XTOR_IMPL(type, dtor, var, __VA_ARGS__)
602
610#define ECS_COPY(type, dst_var, src_var, ...)\
611 ECS_COPY_IMPL(type, dst_var, src_var, __VA_ARGS__)
612
620#define ECS_MOVE(type, dst_var, src_var, ...)\
621 ECS_MOVE_IMPL(type, dst_var, src_var, __VA_ARGS__)
622
630#define ECS_ON_ADD(type, ptr, ...)\
631 ECS_HOOK_IMPL(type, ecs_on_add(type), ptr, __VA_ARGS__)
632#define ECS_ON_REMOVE(type, ptr, ...)\
633 ECS_HOOK_IMPL(type, ecs_on_remove(type), ptr, __VA_ARGS__)
634#define ECS_ON_SET(type, ptr, ...)\
635 ECS_HOOK_IMPL(type, ecs_on_set(type), ptr, __VA_ARGS__)
636
637/* Map from typename to function name of component lifecycle action */
638#define ecs_ctor(type) type##_ctor
639#define ecs_dtor(type) type##_dtor
640#define ecs_copy(type) type##_copy
641#define ecs_move(type) type##_move
642#define ecs_on_set(type) type##_on_set
643#define ecs_on_add(type) type##_on_add
644#define ecs_on_remove(type) type##_on_remove
645
653#define ecs_count(world, type)\
654 ecs_count_id(world, ecs_id(type))
655
663#define ecs_field(it, T, index)\
664 (ECS_CAST(T*, ecs_field_w_size(it, sizeof(T), index)))
665
666#define ecs_field_self(it, T, index)\
667 (ECS_CAST(T*, ecs_field_self_w_size(it, sizeof(T), index)))
668
669#define ecs_field_at(it, T, index, row)\
670 (ECS_CAST(T*, ecs_field_at_w_size(it, sizeof(T), index, row)))
671
679#define ecs_table_get(world, table, T, offset)\
680 (ECS_CAST(T*, ecs_table_get_id(world, table, ecs_id(T), offset)))
681
682#define ecs_table_get_pair(world, table, First, second, offset)\
683 (ECS_CAST(First*, ecs_table_get_id(world, table, ecs_pair(ecs_id(First), second), offset)))
684
685#define ecs_table_get_pair_second(world, table, first, Second, offset)\
686 (ECS_CAST(Second*, ecs_table_get_id(world, table, ecs_pair(first, ecs_id(Second)), offset)))
687
696#define ecs_ids(...) (ecs_id_t[]){ __VA_ARGS__, 0 }
697
699#define ecs_values(...) (ecs_value_t[]){ __VA_ARGS__, {0, 0}}
700
702#define ecs_value_ptr(T, ptr) ((ecs_value_t){ecs_id(T), ptr})
703
705#define ecs_value_pair(R, t, ...) ((ecs_value_t){ecs_pair_t(R, t), &(R)__VA_ARGS__})
706
708#define ecs_value_pair_2nd(r, T, ...) ((ecs_value_t){ecs_pair(r, ecs_id(T)), &(T)__VA_ARGS__})
709
711#define ecs_value_new_t(world, T) ecs_value_new(world, ecs_id(T))
712
714#define ecs_value(T, ...) ((ecs_value_t){ecs_id(T), &(T)__VA_ARGS__})
715
726#define ecs_sort_table(id) ecs_id(id##_sort_table)
727
728#define ecs_compare(id) ecs_id(id##_compare_fn)
729
730/* Declare efficient table sorting operation that uses provided compare function.
731 * For best results use LTO or make the function body visible in the same compilation unit.
732 * Variadic arguments are prepended before generated functions, use it to declare static
733 * or exported functions.
734 * Parameters of the comparison function:
735 * ecs_entity_t e1, const void* ptr1,
736 * ecs_entity_t e2, const void* ptr2
737 * Parameters of the sort functions:
738 * ecs_world_t *world
739 * ecs_table_t *table
740 * ecs_entity_t *entities
741 * void *ptr
742 * int32_t elem_size
743 * int32_t lo
744 * int32_t hi
745 * ecs_order_by_action_t order_by - Pointer to the original comparison function. You are not supposed to use it.
746 * Example:
747 *
748 * @code
749 * 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; }
750 * ECS_SORT_TABLE_WITH_COMPARE(MyType, MyCustomCompare, CompareMyType)
751 * @endcode
752 */
753#define ECS_SORT_TABLE_WITH_COMPARE(id, op_name, compare_fn, ...) \
754 static int32_t ECS_CONCAT(op_name, _partition)( \
755 ecs_world_t *world, \
756 ecs_table_t *table, \
757 ecs_entity_t *entities, \
758 void *ptr, \
759 int32_t elem_size, \
760 int32_t lo, \
761 int32_t hi, \
762 ecs_order_by_action_t order_by) \
763 { \
764 (void)(order_by); \
765 int32_t p = (hi + lo) / 2; \
766 void *pivot = ECS_ELEM(ptr, elem_size, p); \
767 ecs_entity_t pivot_e = entities[p]; \
768 int32_t i = lo - 1, j = hi + 1; \
769 void *el; \
770 repeat: \
771 { \
772 do { \
773 i ++; \
774 el = ECS_ELEM(ptr, elem_size, i); \
775 } while ( compare_fn(entities[i], el, pivot_e, pivot) < 0); \
776 do { \
777 j --; \
778 el = ECS_ELEM(ptr, elem_size, j); \
779 } while ( compare_fn(entities[j], el, pivot_e, pivot) > 0); \
780 if (i >= j) { \
781 return j; \
782 } \
783 ecs_table_swap_rows(world, table, i, j); \
784 if (p == i) { \
785 pivot = ECS_ELEM(ptr, elem_size, j); \
786 pivot_e = entities[j]; \
787 } else if (p == j) { \
788 pivot = ECS_ELEM(ptr, elem_size, i); \
789 pivot_e = entities[i]; \
790 } \
791 goto repeat; \
792 } \
793 } \
794 __VA_ARGS__ void op_name( \
795 ecs_world_t *world, \
796 ecs_table_t *table, \
797 ecs_entity_t *entities, \
798 void *ptr, \
799 int32_t size, \
800 int32_t lo, \
801 int32_t hi, \
802 ecs_order_by_action_t order_by) \
803 { \
804 if ((hi - lo) < 1) { \
805 return; \
806 } \
807 int32_t p = ECS_CONCAT(op_name, _partition)(world, table, entities, ptr, size, lo, hi, order_by); \
808 op_name(world, table, entities, ptr, size, lo, p, order_by); \
809 op_name(world, table, entities, ptr, size, p + 1, hi, order_by); \
810 }
811
812/* Declare efficient table sorting operation that uses default component comparison operator.
813 * For best results use LTO or make the comparison operator visible in the same compilation unit.
814 * Variadic arguments are prepended before generated functions, use it to declare static
815 * or exported functions.
816 * Example:
817 *
818 * @code
819 * ECS_COMPARE(MyType, { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; });
820 * ECS_SORT_TABLE(MyType)
821 * @endcode
822 */
823#define ECS_SORT_TABLE(id, ...) \
824 ECS_SORT_TABLE_WITH_COMPARE(id, ecs_sort_table(id), ecs_compare(id), __VA_ARGS__)
825
826/* Declare component comparison operations.
827 * Parameters:
828 * ecs_entity_t e1, const void* ptr1,
829 * ecs_entity_t e2, const void* ptr2
830 * Example:
831 *
832 * @code
833 * ECS_COMPARE(MyType, { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; });
834 * @endcode
835 */
836#define ECS_COMPARE(id, ...) \
837 int ecs_compare(id)(ecs_entity_t e1, const void* ptr1, ecs_entity_t e2, const void* ptr2) { \
838 __VA_ARGS__ \
839 }
840
850#define ecs_isa(e) ecs_pair(EcsIsA, e)
851#define ecs_childof(e) ecs_pair(EcsChildOf, e)
852#define ecs_dependson(e) ecs_pair(EcsDependsOn, e)
853#define ecs_with(e) ecs_pair(EcsWith, e)
854
855#define ecs_each(world, id) ecs_each_id(world, ecs_id(id))
856#define ecs_each_pair(world, r, t) ecs_each_id(world, ecs_pair(r, t))
857#define ecs_each_pair_t(world, R, t) ecs_each_id(world, ecs_pair(ecs_id(R), t))
858
863#endif // FLECS_C_