Flecs v4.1
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
262#define ecs_query(world, ...)\
263 ecs_query_init(world, &(ecs_query_desc_t) __VA_ARGS__ )
264
277#define ecs_observer(world, ...)\
278 ecs_observer_init(world, &(ecs_observer_desc_t) __VA_ARGS__ )
279
299#define ecs_new_w(world, T) ecs_new_w_id(world, ecs_id(T))
300
301#define ecs_new_w_pair(world, first, second)\
302 ecs_new_w_id(world, ecs_pair(first, second))
303
304#define ecs_bulk_new(world, component, count)\
305 ecs_bulk_new_w_id(world, ecs_id(component), count)
306
314#define ecs_add(world, entity, T)\
315 ecs_add_id(world, entity, ecs_id(T))
316
317#define ecs_add_pair(world, subject, first, second)\
318 ecs_add_id(world, subject, ecs_pair(first, second))
319
320
321#define ecs_remove(world, entity, T)\
322 ecs_remove_id(world, entity, ecs_id(T))
323
324#define ecs_remove_pair(world, subject, first, second)\
325 ecs_remove_id(world, subject, ecs_pair(first, second))
326
327
328#define ecs_auto_override(world, entity, T)\
329 ecs_auto_override_id(world, entity, ecs_id(T))
330
331#define ecs_auto_override_pair(world, subject, first, second)\
332 ecs_auto_override_id(world, subject, ecs_pair(first, second))
333
341/* insert */
342#define ecs_insert(world, ...)\
343 ecs_entity(world, { .set = ecs_values(__VA_ARGS__)})
344
345/* set */
346
347#define ecs_set_ptr(world, entity, component, ptr)\
348 ecs_set_id(world, entity, ecs_id(component), sizeof(component), ptr)
349
350#define ecs_set(world, entity, component, ...)\
351 ecs_set_id(world, entity, ecs_id(component), sizeof(component), &(component)__VA_ARGS__)
352
353#define ecs_set_pair(world, subject, First, second, ...)\
354 ecs_set_id(world, subject,\
355 ecs_pair(ecs_id(First), second),\
356 sizeof(First), &(First)__VA_ARGS__)
357
358#define ecs_set_pair_second(world, subject, first, Second, ...)\
359 ecs_set_id(world, subject,\
360 ecs_pair(first, ecs_id(Second)),\
361 sizeof(Second), &(Second)__VA_ARGS__)
362
363#define ecs_set_override(world, entity, T, ...)\
364 ecs_add_id(world, entity, ECS_AUTO_OVERRIDE | ecs_id(T));\
365 ecs_set(world, entity, T, __VA_ARGS__)
366
367/* emplace */
368
369#define ecs_emplace(world, entity, T, is_new)\
370 (ECS_CAST(T*, ecs_emplace_id(world, entity, ecs_id(T), sizeof(T), is_new)))
371
372#define ecs_emplace_pair(world, entity, First, second, is_new)\
373 (ECS_CAST(First*, ecs_emplace_id(world, entity, ecs_pair_t(First, second), sizeof(First), is_new)))
374
375/* get */
376
377#define ecs_get(world, entity, T)\
378 (ECS_CAST(const T*, ecs_get_id(world, entity, ecs_id(T))))
379
380#define ecs_get_pair(world, subject, First, second)\
381 (ECS_CAST(const First*, ecs_get_id(world, subject,\
382 ecs_pair(ecs_id(First), second))))
383
384#define ecs_get_pair_second(world, subject, first, Second)\
385 (ECS_CAST(const Second*, ecs_get_id(world, subject,\
386 ecs_pair(first, ecs_id(Second)))))
387
388/* get_mut */
389
390#define ecs_get_mut(world, entity, T)\
391 (ECS_CAST(T*, ecs_get_mut_id(world, entity, ecs_id(T))))
392
393#define ecs_get_mut_pair(world, subject, First, second)\
394 (ECS_CAST(First*, ecs_get_mut_id(world, subject,\
395 ecs_pair(ecs_id(First), second))))
396
397#define ecs_get_mut_pair_second(world, subject, first, Second)\
398 (ECS_CAST(Second*, ecs_get_mut_id(world, subject,\
399 ecs_pair(first, ecs_id(Second)))))
400
401#define ecs_get_mut(world, entity, T)\
402 (ECS_CAST(T*, ecs_get_mut_id(world, entity, ecs_id(T))))
403
404/* ensure */
405
406#define ecs_ensure(world, entity, T)\
407 (ECS_CAST(T*, ecs_ensure_id(world, entity, ecs_id(T), sizeof(T))))
408
409#define ecs_ensure_pair(world, subject, First, second)\
410 (ECS_CAST(First*, ecs_ensure_id(world, subject,\
411 ecs_pair(ecs_id(First), second), sizeof(First))))
412
413#define ecs_ensure_pair_second(world, subject, first, Second)\
414 (ECS_CAST(Second*, ecs_ensure_id(world, subject,\
415 ecs_pair(first, ecs_id(Second)), sizeof(Second))))
416
417/* modified */
418
419#define ecs_modified(world, entity, component)\
420 ecs_modified_id(world, entity, ecs_id(component))
421
422#define ecs_modified_pair(world, subject, first, second)\
423 ecs_modified_id(world, subject, ecs_pair(first, second))
424
425/* record */
426
427#define ecs_record_get(world, record, T)\
428 (ECS_CAST(const T*, ecs_record_get_id(world, record, ecs_id(T))))
429
430#define ecs_record_has(world, record, T)\
431 (ecs_record_has_id(world, record, ecs_id(T)))
432
433#define ecs_record_get_pair(world, record, First, second)\
434 (ECS_CAST(const First*, ecs_record_get_id(world, record, \
435 ecs_pair(ecs_id(First), second))))
436
437#define ecs_record_get_pair_second(world, record, first, Second)\
438 (ECS_CAST(const Second*, ecs_record_get_id(world, record,\
439 ecs_pair(first, ecs_id(Second)))))
440
441#define ecs_record_ensure(world, record, T)\
442 (ECS_CAST(T*, ecs_record_ensure_id(world, record, ecs_id(T))))
443
444#define ecs_record_ensure_pair(world, record, First, second)\
445 (ECS_CAST(First*, ecs_record_ensure_id(world, record, \
446 ecs_pair(ecs_id(First), second))))
447
448#define ecs_record_ensure_pair_second(world, record, first, Second)\
449 (ECS_CAST(Second*, ecs_record_ensure_id(world, record,\
450 ecs_pair(first, ecs_id(Second)))))
451
452#define ecs_ref_init(world, entity, T)\
453 ecs_ref_init_id(world, entity, ecs_id(T))
454
455#define ecs_ref_get(world, ref, T)\
456 (ECS_CAST(T*, ecs_ref_get_id(world, ref, ecs_id(T))))
457
465#define ecs_singleton_add(world, comp)\
466 ecs_add(world, ecs_id(comp), comp)
467
468#define ecs_singleton_remove(world, comp)\
469 ecs_remove(world, ecs_id(comp), comp)
470
471#define ecs_singleton_get(world, comp)\
472 ecs_get(world, ecs_id(comp), comp)
473
474#define ecs_singleton_get_mut(world, comp)\
475 ecs_get_mut(world, ecs_id(comp), comp)
476
477#define ecs_singleton_set_ptr(world, comp, ptr)\
478 ecs_set_ptr(world, ecs_id(comp), comp, ptr)
479
480#define ecs_singleton_set(world, comp, ...)\
481 ecs_set(world, ecs_id(comp), comp, __VA_ARGS__)
482
483#define ecs_singleton_ensure(world, comp)\
484 ecs_ensure(world, ecs_id(comp), comp)
485
486#define ecs_singleton_emplace(world, comp, is_new)\
487 ecs_emplace(world, ecs_id(comp), comp, is_new)
488
489#define ecs_singleton_modified(world, comp)\
490 ecs_modified(world, ecs_id(comp), comp)
491
499#define ecs_has(world, entity, T)\
500 ecs_has_id(world, entity, ecs_id(T))
501
502#define ecs_has_pair(world, entity, first, second)\
503 ecs_has_id(world, entity, ecs_pair(first, second))
504
505#define ecs_owns_pair(world, entity, first, second)\
506 ecs_owns_id(world, entity, ecs_pair(first, second))
507
508#define ecs_owns(world, entity, T)\
509 ecs_owns_id(world, entity, ecs_id(T))
510
511#define ecs_shares_id(world, entity, id)\
512 (ecs_search_relation(world, ecs_get_table(world, entity), 0, ecs_id(id), \
513 EcsIsA, 1, 0, 0, 0, 0) != -1)
514
515#define ecs_shares_pair(world, entity, first, second)\
516 (ecs_shares_id(world, entity, ecs_pair(first, second)))
517
518#define ecs_shares(world, entity, T)\
519 (ecs_shares_id(world, entity, ecs_id(T)))
520
521#define ecs_get_target_for(world, entity, rel, T)\
522 ecs_get_target_for_id(world, entity, rel, ecs_id(T))
523
531#define ecs_enable_component(world, entity, T, enable)\
532 ecs_enable_id(world, entity, ecs_id(T), enable)
533
534#define ecs_is_enabled(world, entity, T)\
535 ecs_is_enabled_id(world, entity, ecs_id(T))
536
537#define ecs_enable_pair(world, entity, First, second, enable)\
538 ecs_enable_id(world, entity, ecs_pair(ecs_id(First), second), enable)
539
540#define ecs_is_enabled_pair(world, entity, First, second)\
541 ecs_is_enabled_id(world, entity, ecs_pair(ecs_id(First), second))
542
550#define ecs_lookup_from(world, parent, path)\
551 ecs_lookup_path_w_sep(world, parent, path, ".", NULL, true)
552
553#define ecs_get_path_from(world, parent, child)\
554 ecs_get_path_w_sep(world, parent, child, ".", NULL)
555
556#define ecs_get_path(world, child)\
557 ecs_get_path_w_sep(world, 0, child, ".", NULL)
558
559#define ecs_get_path_buf(world, child, buf)\
560 ecs_get_path_w_sep_buf(world, 0, child, ".", NULL, buf, false)
561
562#define ecs_new_from_path(world, parent, path)\
563 ecs_new_from_path_w_sep(world, parent, path, ".", NULL)
564
565#define ecs_add_path(world, entity, parent, path)\
566 ecs_add_path_w_sep(world, entity, parent, path, ".", NULL)
567
568#define ecs_add_fullpath(world, entity, path)\
569 ecs_add_path_w_sep(world, entity, 0, path, ".", NULL)
570
580#define ecs_set_hooks(world, T, ...)\
581 ecs_set_hooks_id(world, ecs_id(T), &(ecs_type_hooks_t)__VA_ARGS__)
582
583#define ecs_get_hooks(world, T)\
584 ecs_get_hooks_id(world, ecs_id(T));
585
593#define ECS_CTOR(type, var, ...)\
594 ECS_XTOR_IMPL(type, ctor, var, __VA_ARGS__)
595
603#define ECS_DTOR(type, var, ...)\
604 ECS_XTOR_IMPL(type, dtor, var, __VA_ARGS__)
605
613#define ECS_COPY(type, dst_var, src_var, ...)\
614 ECS_COPY_IMPL(type, dst_var, src_var, __VA_ARGS__)
615
623#define ECS_MOVE(type, dst_var, src_var, ...)\
624 ECS_MOVE_IMPL(type, dst_var, src_var, __VA_ARGS__)
625
633#define ECS_ON_ADD(type, ptr, ...)\
634 ECS_HOOK_IMPL(type, ecs_on_add(type), ptr, __VA_ARGS__)
635#define ECS_ON_REMOVE(type, ptr, ...)\
636 ECS_HOOK_IMPL(type, ecs_on_remove(type), ptr, __VA_ARGS__)
637#define ECS_ON_SET(type, ptr, ...)\
638 ECS_HOOK_IMPL(type, ecs_on_set(type), ptr, __VA_ARGS__)
639
640/* Map from typename to function name of component lifecycle action */
641#define ecs_ctor(type) type##_ctor
642#define ecs_dtor(type) type##_dtor
643#define ecs_copy(type) type##_copy
644#define ecs_move(type) type##_move
645#define ecs_on_set(type) type##_on_set
646#define ecs_on_add(type) type##_on_add
647#define ecs_on_remove(type) type##_on_remove
648
656#define ecs_count(world, type)\
657 ecs_count_id(world, ecs_id(type))
658
666#define ecs_field(it, T, index)\
667 (ECS_CAST(T*, ecs_field_w_size(it, sizeof(T), index)))
668
669#define ecs_field_self(it, T, index)\
670 (ECS_CAST(T*, ecs_field_self_w_size(it, sizeof(T), index)))
671
672#define ecs_field_at(it, T, index, row)\
673 (ECS_CAST(T*, ecs_field_at_w_size(it, sizeof(T), index, row)))
674
682#define ecs_table_get(world, table, T, offset)\
683 (ECS_CAST(T*, ecs_table_get_id(world, table, ecs_id(T), offset)))
684
685#define ecs_table_get_pair(world, table, First, second, offset)\
686 (ECS_CAST(First*, ecs_table_get_id(world, table, ecs_pair(ecs_id(First), second), offset)))
687
688#define ecs_table_get_pair_second(world, table, first, Second, offset)\
689 (ECS_CAST(Second*, ecs_table_get_id(world, table, ecs_pair(first, ecs_id(Second)), offset)))
690
699#define ecs_ids(...) (ecs_id_t[]){ __VA_ARGS__, 0 }
700
702#define ecs_values(...) (ecs_value_t[]){ __VA_ARGS__, {0, 0}}
703
705#define ecs_value_ptr(T, ptr) ((ecs_value_t){ecs_id(T), ptr})
706
708#define ecs_value_pair(R, t, ...) ((ecs_value_t){ecs_pair_t(R, t), &(R)__VA_ARGS__})
709
711#define ecs_value_pair_2nd(r, T, ...) ((ecs_value_t){ecs_pair(r, ecs_id(T)), &(T)__VA_ARGS__})
712
714#define ecs_value_new_t(world, T) ecs_value_new(world, ecs_id(T))
715
717#define ecs_value(T, ...) ((ecs_value_t){ecs_id(T), &(T)__VA_ARGS__})
718
729#define ecs_sort_table(id) ecs_id(id##_sort_table)
730
731#define ecs_compare(id) ecs_id(id##_compare_fn)
732
733/* Declare efficient table sorting operation that uses provided compare function.
734 * For best results use LTO or make the function body visible in the same compilation unit.
735 * Variadic arguments are prepended before generated functions, use it to declare static
736 * or exported functions.
737 * Parameters of the comparison function:
738 * ecs_entity_t e1, const void* ptr1,
739 * ecs_entity_t e2, const void* ptr2
740 * Parameters of the sort functions:
741 * ecs_world_t *world
742 * ecs_table_t *table
743 * ecs_entity_t *entities
744 * void *ptr
745 * int32_t elem_size
746 * int32_t lo
747 * int32_t hi
748 * ecs_order_by_action_t order_by - Pointer to the original comparison function. You are not supposed to use it.
749 * Example:
750 *
751 * @code
752 * 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; }
753 * ECS_SORT_TABLE_WITH_COMPARE(MyType, MyCustomCompare, CompareMyType)
754 * @endcode
755 */
756#define ECS_SORT_TABLE_WITH_COMPARE(id, op_name, compare_fn, ...) \
757 static int32_t ECS_CONCAT(op_name, _partition)( \
758 ecs_world_t *world, \
759 ecs_table_t *table, \
760 ecs_entity_t *entities, \
761 void *ptr, \
762 int32_t elem_size, \
763 int32_t lo, \
764 int32_t hi, \
765 ecs_order_by_action_t order_by) \
766 { \
767 (void)(order_by); \
768 int32_t p = (hi + lo) / 2; \
769 void *pivot = ECS_ELEM(ptr, elem_size, p); \
770 ecs_entity_t pivot_e = entities[p]; \
771 int32_t i = lo - 1, j = hi + 1; \
772 void *el; \
773 repeat: \
774 { \
775 do { \
776 i ++; \
777 el = ECS_ELEM(ptr, elem_size, i); \
778 } while ( compare_fn(entities[i], el, pivot_e, pivot) < 0); \
779 do { \
780 j --; \
781 el = ECS_ELEM(ptr, elem_size, j); \
782 } while ( compare_fn(entities[j], el, pivot_e, pivot) > 0); \
783 if (i >= j) { \
784 return j; \
785 } \
786 ecs_table_swap_rows(world, table, i, j); \
787 if (p == i) { \
788 pivot = ECS_ELEM(ptr, elem_size, j); \
789 pivot_e = entities[j]; \
790 } else if (p == j) { \
791 pivot = ECS_ELEM(ptr, elem_size, i); \
792 pivot_e = entities[i]; \
793 } \
794 goto repeat; \
795 } \
796 } \
797 __VA_ARGS__ void op_name( \
798 ecs_world_t *world, \
799 ecs_table_t *table, \
800 ecs_entity_t *entities, \
801 void *ptr, \
802 int32_t size, \
803 int32_t lo, \
804 int32_t hi, \
805 ecs_order_by_action_t order_by) \
806 { \
807 if ((hi - lo) < 1) { \
808 return; \
809 } \
810 int32_t p = ECS_CONCAT(op_name, _partition)(world, table, entities, ptr, size, lo, hi, order_by); \
811 op_name(world, table, entities, ptr, size, lo, p, order_by); \
812 op_name(world, table, entities, ptr, size, p + 1, hi, order_by); \
813 }
814
815/* Declare efficient table sorting operation that uses default component comparison operator.
816 * For best results use LTO or make the comparison operator visible in the same compilation unit.
817 * Variadic arguments are prepended before generated functions, use it to declare static
818 * or exported functions.
819 * Example:
820 *
821 * @code
822 * ECS_COMPARE(MyType, { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; });
823 * ECS_SORT_TABLE(MyType)
824 * @endcode
825 */
826#define ECS_SORT_TABLE(id, ...) \
827 ECS_SORT_TABLE_WITH_COMPARE(id, ecs_sort_table(id), ecs_compare(id), __VA_ARGS__)
828
829/* Declare component comparison operations.
830 * Parameters:
831 * ecs_entity_t e1, const void* ptr1,
832 * ecs_entity_t e2, const void* ptr2
833 * Example:
834 *
835 * @code
836 * ECS_COMPARE(MyType, { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; });
837 * @endcode
838 */
839#define ECS_COMPARE(id, ...) \
840 int ecs_compare(id)(ecs_entity_t e1, const void* ptr1, ecs_entity_t e2, const void* ptr2) { \
841 __VA_ARGS__ \
842 }
843
853#define ecs_isa(e) ecs_pair(EcsIsA, e)
854#define ecs_childof(e) ecs_pair(EcsChildOf, e)
855#define ecs_dependson(e) ecs_pair(EcsDependsOn, e)
856#define ecs_with(e) ecs_pair(EcsWith, e)
857
858#define ecs_each(world, id) ecs_each_id(world, ecs_id(id))
859#define ecs_each_pair(world, r, t) ecs_each_id(world, ecs_pair(r, t))
860#define ecs_each_pair_t(world, R, t) ecs_each_id(world, ecs_pair(ecs_id(R), t))
861
866#endif // FLECS_C_