Flecs
v3.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
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
187
#define ecs_filter(world, ...)\
188
ecs_filter_init(world, &(ecs_filter_desc_t) __VA_ARGS__ )
189
197
#define ecs_query(world, ...)\
198
ecs_query_init(world, &(ecs_query_desc_t) __VA_ARGS__ )
199
209
#define ecs_observer(world, ...)\
210
ecs_observer_init(world, &(ecs_observer_desc_t) __VA_ARGS__ )
211
230
#define ecs_new(world, T) ecs_new_w_id(world, ecs_id(T))
231
232
#define ecs_new_w_pair(world, first, second)\
233
ecs_new_w_id(world, ecs_pair(first, second))
234
235
#define ecs_bulk_new(world, component, count)\
236
ecs_bulk_new_w_id(world, ecs_id(component), count)
237
238
#define ecs_new_entity(world, n)\
239
ecs_entity_init(world, &(ecs_entity_desc_t){\
240
.name = n,\
241
})
242
243
#define ecs_new_prefab(world, n)\
244
ecs_entity_init(world, &(ecs_entity_desc_t){\
245
.name = n,\
246
.add = {EcsPrefab}\
247
})
248
249
#define ecs_delete_children(world, parent)\
250
ecs_delete_with(world, ecs_pair(EcsChildOf, parent))
251
259
#define ecs_add(world, entity, T)\
260
ecs_add_id(world, entity, ecs_id(T))
261
262
#define ecs_add_pair(world, subject, first, second)\
263
ecs_add_id(world, subject, ecs_pair(first, second))
264
265
266
#define ecs_remove(world, entity, T)\
267
ecs_remove_id(world, entity, ecs_id(T))
268
269
#define ecs_remove_pair(world, subject, first, second)\
270
ecs_remove_id(world, subject, ecs_pair(first, second))
271
272
273
#define ecs_override(world, entity, T)\
274
ecs_override_id(world, entity, ecs_id(T))
275
276
#define ecs_override_pair(world, subject, first, second)\
277
ecs_override_id(world, subject, ecs_pair(first, second))
278
286
#define ecs_set_ptr(world, entity, component, ptr)\
287
ecs_set_id(world, entity, ecs_id(component), sizeof(component), ptr)
288
289
#define ecs_set(world, entity, component, ...)\
290
ecs_set_id(world, entity, ecs_id(component), sizeof(component), &(component)__VA_ARGS__)
291
292
#define ecs_set_pair(world, subject, First, second, ...)\
293
ecs_set_id(world, subject,\
294
ecs_pair(ecs_id(First), second),\
295
sizeof(First), &(First)__VA_ARGS__)
296
297
#define ecs_set_pair_second(world, subject, first, Second, ...)\
298
ecs_set_id(world, subject,\
299
ecs_pair(first, ecs_id(Second)),\
300
sizeof(Second), &(Second)__VA_ARGS__)
301
302
#define ecs_set_pair_object ecs_set_pair_second
303
304
#define ecs_set_override(world, entity, T, ...)\
305
ecs_add_id(world, entity, ECS_OVERRIDE | ecs_id(T));\
306
ecs_set(world, entity, T, __VA_ARGS__)
307
308
#define ecs_emplace(world, entity, T)\
309
(ECS_CAST(T*, ecs_emplace_id(world, entity, ecs_id(T))))
310
311
#define ecs_get(world, entity, T)\
312
(ECS_CAST(const T*, ecs_get_id(world, entity, ecs_id(T))))
313
314
#define ecs_get_pair(world, subject, First, second)\
315
(ECS_CAST(const First*, ecs_get_id(world, subject,\
316
ecs_pair(ecs_id(First), second))))
317
318
#define ecs_get_pair_second(world, subject, first, Second)\
319
(ECS_CAST(const Second*, ecs_get_id(world, subject,\
320
ecs_pair(first, ecs_id(Second)))))
321
322
#define ecs_get_pair_object ecs_get_pair_second
323
324
#define ecs_record_get(world, record, T)\
325
(ECS_CAST(const T*, ecs_record_get_id(world, record, ecs_id(T))))
326
327
#define ecs_record_get_pair(world, record, First, second)\
328
(ECS_CAST(const First*, ecs_record_get_id(world, record, \
329
ecs_pair(ecs_id(First), second))))
330
331
#define ecs_record_get_pair_second(world, record, first, Second)\
332
(ECS_CAST(const Second*, ecs_record_get_id(world, record,\
333
ecs_pair(first, ecs_id(Second)))))
334
335
#define ecs_record_get_mut(world, record, T)\
336
(ECS_CAST(T*, ecs_record_get_mut_id(world, record, ecs_id(T))))
337
338
#define ecs_record_get_mut_pair(world, record, First, second)\
339
(ECS_CAST(First*, ecs_record_get_mut_id(world, record, \
340
ecs_pair(ecs_id(First), second))))
341
342
#define ecs_record_get_mut_pair_second(world, record, first, Second)\
343
(ECS_CAST(Second*, ecs_record_get_mut_id(world, record,\
344
ecs_pair(first, ecs_id(Second)))))
345
346
#define ecs_record_get_mut_pair_object ecs_record_get_mut_pair_second
347
348
#define ecs_ref_init(world, entity, T)\
349
ecs_ref_init_id(world, entity, ecs_id(T))
350
351
#define ecs_ref_get(world, ref, T)\
352
(ECS_CAST(const T*, ecs_ref_get_id(world, ref, ecs_id(T))))
353
354
#define ecs_get_mut(world, entity, T)\
355
(ECS_CAST(T*, ecs_get_mut_id(world, entity, ecs_id(T))))
356
357
#define ecs_get_mut_pair(world, subject, First, second)\
358
(ECS_CAST(First*, ecs_get_mut_id(world, subject,\
359
ecs_pair(ecs_id(First), second))))
360
361
#define ecs_get_mut_pair_second(world, subject, first, Second)\
362
(ECS_CAST(Second*, ecs_get_mut_id(world, subject,\
363
ecs_pair(first, ecs_id(Second)))))
364
365
#define ecs_get_mut_pair_object ecs_get_mut_pair_second
366
367
#define ecs_modified(world, entity, component)\
368
ecs_modified_id(world, entity, ecs_id(component))
369
370
#define ecs_modified_pair(world, subject, first, second)\
371
ecs_modified_id(world, subject, ecs_pair(first, second))
372
380
#define ecs_singleton_add(world, comp)\
381
ecs_add(world, ecs_id(comp), comp)
382
383
#define ecs_singleton_remove(world, comp)\
384
ecs_remove(world, ecs_id(comp), comp)
385
386
#define ecs_singleton_get(world, comp)\
387
ecs_get(world, ecs_id(comp), comp)
388
389
#define ecs_singleton_set(world, comp, ...)\
390
ecs_set(world, ecs_id(comp), comp, __VA_ARGS__)
391
392
#define ecs_singleton_get_mut(world, comp)\
393
ecs_get_mut(world, ecs_id(comp), comp)
394
395
#define ecs_singleton_modified(world, comp)\
396
ecs_modified(world, ecs_id(comp), comp)
397
405
#define ecs_has(world, entity, T)\
406
ecs_has_id(world, entity, ecs_id(T))
407
408
#define ecs_has_pair(world, entity, first, second)\
409
ecs_has_id(world, entity, ecs_pair(first, second))
410
411
#define ecs_owns_id(world, entity, id)\
412
(ecs_search(world, ecs_get_table(world, entity), id, 0) != -1)
413
414
#define ecs_owns_pair(world, entity, first, second)\
415
ecs_owns_id(world, entity, ecs_pair(first, second))
416
417
#define ecs_owns(world, entity, T)\
418
ecs_owns_id(world, entity, ecs_id(T))
419
420
#define ecs_shares_id(world, entity, id)\
421
(ecs_search_relation(world, ecs_get_table(world, entity), 0, ecs_id(id), \
422
EcsIsA, 1, 0, 0, 0, 0) != -1)
423
424
#define ecs_shares_pair(world, entity, first, second)\
425
(ecs_shares_id(world, entity, ecs_pair(first, second)))
426
427
#define ecs_shares(world, entity, T)\
428
(ecs_shares_id(world, entity, ecs_id(T)))
429
437
#define ecs_enable_component(world, entity, T, enable)\
438
ecs_enable_id(world, entity, ecs_id(T), enable)
439
440
#define ecs_is_enabled_component(world, entity, T)\
441
ecs_is_enabled_id(world, entity, ecs_id(T))
442
443
#define ecs_enable_pair(world, entity, First, second, enable)\
444
ecs_enable_id(world, entity, ecs_pair(ecs_id(First), second), enable)
445
446
#define ecs_is_enabled_pair(world, entity, First, second)\
447
ecs_is_enabled_id(world, entity, ecs_pair(ecs_id(First), second))
448
456
#define ecs_lookup_path(world, parent, path)\
457
ecs_lookup_path_w_sep(world, parent, path, "."
, NULL, true)
458
459
#define ecs_lookup_fullpath(world, path)\
460
ecs_lookup_path_w_sep(world, 0, path, "."
, NULL, true)
461
462
#define ecs_get_path(world, parent, child)\
463
ecs_get_path_w_sep(world, parent, child, "."
, NULL)
464
465
#define ecs_get_fullpath(world, child)\
466
ecs_get_path_w_sep(world, 0, child, "."
, NULL)
467
468
#define ecs_get_fullpath_buf(world, child, buf)\
469
ecs_get_path_w_sep_buf(world, 0, child, "."
, NULL, buf)
470
471
#define ecs_new_from_path(world, parent, path)\
472
ecs_new_from_path_w_sep(world, parent, path, "."
, NULL)
473
474
#define ecs_new_from_fullpath(world, path)\
475
ecs_new_from_path_w_sep(world, 0, path, "."
, NULL)
476
477
#define ecs_add_path(world, entity, parent, path)\
478
ecs_add_path_w_sep(world, entity, parent, path, "."
, NULL)
479
480
#define ecs_add_fullpath(world, entity, path)\
481
ecs_add_path_w_sep(world, entity, 0, path, "."
, NULL)
482
492
#define ecs_set_hooks(world, T, ...)\
493
ecs_set_hooks_id(world, ecs_id(T), &(ecs_type_hooks_t)__VA_ARGS__)
494
495
#define ecs_get_hooks(world, T)\
496
ecs_get_hooks_id(world, ecs_id(T));
497
502
#define ECS_CTOR(type, var, ...)\
503
ECS_XTOR_IMPL(type, ctor, var, __VA_ARGS__)
504
509
#define ECS_DTOR(type, var, ...)\
510
ECS_XTOR_IMPL(type, dtor, var, __VA_ARGS__)
511
516
#define ECS_COPY(type, dst_var, src_var, ...)\
517
ECS_COPY_IMPL(type, dst_var, src_var, __VA_ARGS__)
518
523
#define ECS_MOVE(type, dst_var, src_var, ...)\
524
ECS_MOVE_IMPL(type, dst_var, src_var, __VA_ARGS__)
525
530
#define ECS_ON_ADD(type, ptr, ...)\
531
ECS_HOOK_IMPL(type, ecs_on_add(type), ptr, __VA_ARGS__)
532
#define ECS_ON_REMOVE(type, ptr, ...)\
533
ECS_HOOK_IMPL(type, ecs_on_remove(type), ptr, __VA_ARGS__)
534
#define ECS_ON_SET(type, ptr, ...)\
535
ECS_HOOK_IMPL(type, ecs_on_set(type), ptr, __VA_ARGS__)
536
537
/* Map from typename to function name of component lifecycle action */
538
#define ecs_ctor(type) type##_ctor
539
#define ecs_dtor(type) type##_dtor
540
#define ecs_copy(type) type##_copy
541
#define ecs_move(type) type##_move
542
#define ecs_on_set(type) type##_on_set
543
#define ecs_on_add(type) type##_on_add
544
#define ecs_on_remove(type) type##_on_remove
545
553
#define ecs_count(world, type)\
554
ecs_count_id(world, ecs_id(type))
555
563
#define ecs_field(it, T, index)\
564
(ECS_CAST(T*, ecs_field_w_size(it, sizeof(T), index)))
565
573
#define ecs_table_get(world, table, T, offset)\
574
(ECS_CAST(T*, ecs_table_get_id(world, table, ecs_id(T), offset)))
575
576
#define ecs_table_get_pair(world, table, First, second, offset)\
577
(ECS_CAST(First*, ecs_table_get_id(world, table, ecs_pair(ecs_id(First), second), offset)))
578
579
#define ecs_table_get_pair_second(world, table, first, Second, offset)\
580
(ECS_CAST(Second*, ecs_table_get_id(world, table, ecs_pair(first, ecs_id(Second)), offset)))
581
589
#define ecs_value(T, ptr) ((ecs_value_t){ecs_id(T), ptr})
590
#define ecs_value_new_t(world, T) ecs_value_new(world, ecs_id(T))
591
602
#define ecs_sort_table(id) ecs_id(id##_sort_table)
603
604
#define ecs_compare(id) ecs_id(id##_compare_fn)
605
606
/* Declare efficient table sorting operation that uses provided compare function.
607
* For best results use LTO or make the function body visible in the same compilation unit.
608
* Variadic arguments are prepended before generated functions, use it to declare static
609
* or exported functions.
610
* Parameters of the comparison function:
611
* ecs_entity_t e1, const void* ptr1,
612
* ecs_entity_t e2, const void* ptr2
613
* Parameters of the sort functions:
614
* ecs_world_t *world
615
* ecs_table_t *table
616
* ecs_entity_t *entities
617
* void *ptr
618
* int32_t elem_size
619
* int32_t lo
620
* int32_t hi
621
* ecs_order_by_action_t order_by - Pointer to the original comparison function. You are not supposed to use it.
622
* Example:
623
* 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; }
624
* ECS_SORT_TABLE_WITH_COMPARE(MyType, MyCustomCompare, CompareMyType)
625
*/
626
#define ECS_SORT_TABLE_WITH_COMPARE(id, op_name, compare_fn, ...) \
627
static int32_t ECS_CONCAT(op_name, _partition)( \
628
ecs_world_t *world, \
629
ecs_table_t *table, \
630
ecs_entity_t *entities, \
631
void *ptr, \
632
int32_t elem_size, \
633
int32_t lo, \
634
int32_t hi, \
635
ecs_order_by_action_t order_by) \
636
{ \
637
(void)(order_by); \
638
int32_t p = (hi + lo) / 2; \
639
void *pivot = ECS_ELEM(ptr, elem_size, p); \
640
ecs_entity_t pivot_e = entities[p]; \
641
int32_t i = lo - 1, j = hi + 1; \
642
void *el; \
643
repeat: \
644
{ \
645
do { \
646
i ++; \
647
el = ECS_ELEM(ptr, elem_size, i); \
648
} while ( compare_fn(entities[i], el, pivot_e, pivot) < 0); \
649
do { \
650
j --; \
651
el = ECS_ELEM(ptr, elem_size, j); \
652
} while ( compare_fn(entities[j], el, pivot_e, pivot) > 0); \
653
if (i >= j) { \
654
return j; \
655
} \
656
ecs_table_swap_rows(world, table, i, j); \
657
if (p == i) { \
658
pivot = ECS_ELEM(ptr, elem_size, j); \
659
pivot_e = entities[j]; \
660
} else if (p == j) { \
661
pivot = ECS_ELEM(ptr, elem_size, i); \
662
pivot_e = entities[i]; \
663
} \
664
goto repeat; \
665
} \
666
} \
667
__VA_ARGS__ void op_name( \
668
ecs_world_t *world, \
669
ecs_table_t *table, \
670
ecs_entity_t *entities, \
671
void *ptr, \
672
int32_t size, \
673
int32_t lo, \
674
int32_t hi, \
675
ecs_order_by_action_t order_by) \
676
{ \
677
if ((hi - lo) < 1) { \
678
return; \
679
} \
680
int32_t p = ECS_CONCAT(op_name, _partition)(world, table, entities, ptr, size, lo, hi, order_by); \
681
op_name(world, table, entities, ptr, size, lo, p, order_by); \
682
op_name(world, table, entities, ptr, size, p + 1, hi, order_by); \
683
}
684
685
/* Declare efficient table sorting operation that uses default component comparison operator.
686
* For best results use LTO or make the comparison operator visible in the same compilation unit.
687
* Variadic arguments are prepended before generated functions, use it to declare static
688
* or exported functions.
689
* Example:
690
* ECS_COMPARE(MyType, { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; });
691
* ECS_SORT_TABLE(MyType)
692
*/
693
#define ECS_SORT_TABLE(id, ...) \
694
ECS_SORT_TABLE_WITH_COMPARE(id, ecs_sort_table(id), ecs_compare(id), __VA_ARGS__)
695
696
/* Declare component comparison operations.
697
* Parameters:
698
* ecs_entity_t e1, const void* ptr1,
699
* ecs_entity_t e2, const void* ptr2
700
* Example:
701
* ECS_COMPARE(MyType, { const MyType* p1 = ptr1; const MyType* p2 = ptr2; return p1->value - p2->value; });
702
*/
703
#define ECS_COMPARE(id, ...) \
704
int ecs_compare(id)(ecs_entity_t e1, const void* ptr1, ecs_entity_t e2, const void* ptr2) { \
705
__VA_ARGS__ \
706
}
707
717
#define ecs_isa(e) ecs_pair(EcsIsA, e)
718
#define ecs_childof(e) ecs_pair(EcsChildOf, e)
719
#define ecs_dependson(e) ecs_pair(EcsDependsOn, e)
720
721
#define ecs_query_new(world, q_expr)\
722
ecs_query_init(world, &(ecs_query_desc_t){\
723
.filter.expr = q_expr\
724
})
725
726
#define ecs_rule_new(world, q_expr)\
727
ecs_rule_init(world, &(ecs_filter_desc_t){\
728
.expr = q_expr\
729
})
730
735
#endif
// FLECS_C_