forked from SanderMertens/flecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flecs.h
28059 lines (23628 loc) · 787 KB
/
flecs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Comment out this line when using as DLL
#define flecs_STATIC
/**
* @file flecs.h
* @brief Flecs public API.
*
* This file contains the public API for Flecs.
*/
#ifndef FLECS_H
#define FLECS_H
/**
* @defgroup c C API
*
* @{
* @}
*/
/**
* @defgroup core Core
* @brief Core ECS functionality (entities, storage, queries).
*
* \ingroup c
* @{
*/
/**
* @defgroup options API defines
* @brief Defines for customizing compile time features.
* @{
*/
/** \def ecs_float_t
* Customizable precision for floating point operations */
#ifndef ecs_float_t
#define ecs_float_t float
#endif
/** \def ecs_ftime_t
* Customizable precision for scalar time values. Change to double precision for
* processes that can run for a long time (e.g. longer than a day). */
#ifndef ecs_ftime_t
#define ecs_ftime_t ecs_float_t
#endif
/** \def FLECS_LEGACY
* Define when building for C89
*/
// #define FLECS_LEGACY
/** \def FLECS_NO_DEPRECATED_WARNINGS
* disables deprecated warnings
*/
#define FLECS_NO_DEPRECATED_WARNINGS
/** \def FLECS_ACCURATE_COUNTERS
* Define to ensure that global counters used for statistics (such as the
* allocation counters in the OS API) are accurate in multithreaded
* applications, at the cost of increased overhead.
*/
// #define FLECS_ACCURATE_COUNTERS
/* Make sure provided configuration is valid */
#if defined(FLECS_DEBUG) && defined(FLECS_NDEBUG)
#error "invalid configuration: cannot both define FLECS_DEBUG and FLECS_NDEBUG"
#endif
#if defined(FLECS_DEBUG) && defined(NDEBUG)
#error "invalid configuration: cannot both define FLECS_DEBUG and NDEBUG"
#endif
/** \def FLECS_DEBUG
* Used for input parameter checking and cheap sanity checks. There are lots of
* asserts in every part of the code, so this will slow down applications.
*/
#if !defined(FLECS_DEBUG) && !defined(FLECS_NDEBUG)
#if defined(NDEBUG)
#define FLECS_NDEBUG
#else
#define FLECS_DEBUG
#endif
#endif
/** \def FLECS_SANITIZE
* Enables expensive checks that can detect issues early. Recommended for
* running tests or when debugging issues. This will severely slow down code.
*/
#ifdef FLECS_SANITIZE
#define FLECS_DEBUG /* If sanitized mode is enabled, so is debug mode */
#endif
/* Tip: if you see weird behavior that you think might be a bug, make sure to
* test with the FLECS_DEBUG or FLECS_SANITIZE flags enabled. There's a good
* chance that this gives you more information about the issue! */
/** \def FLECS_SOFT_ASSERT
* Define to not abort for recoverable errors, like invalid parameters. An error
* is still thrown to the console. This is recommended for when running inside a
* third party runtime, such as the Unreal editor.
*
* Note that internal sanity checks (ECS_INTERNAL_ERROR) will still abort a
* process, as this gives more information than a (likely) subsequent crash.
*
* When a soft assert occurs, the code will attempt to minimize the number of
* side effects of the failed operation, but this may not always be possible.
* Even though an application may still be able to continue running after a soft
* assert, it should be treated as if in an undefined state.
*/
// #define FLECS_SOFT_ASSERT
/** \def FLECS_KEEP_ASSERT
* By default asserts are disabled in release mode, when either FLECS_NDEBUG or
* NDEBUG is defined. Defining FLECS_KEEP_ASSERT ensures that asserts are not
* disabled. This define can be combined with FLECS_SOFT_ASSERT.
*/
// #define FLECS_KEEP_ASSERT
/** \def FLECS_CUSTOM_BUILD
* This macro lets you customize which addons to build flecs with.
* Without any addons Flecs is just a minimal ECS storage, but addons add
* features such as systems, scheduling and reflection. If an addon is disabled,
* it is excluded from the build, so that it consumes no resources. By default
* all addons are enabled.
*
* You can customize a build by either whitelisting or blacklisting addons. To
* whitelist addons, first define the FLECS_CUSTOM_BUILD macro, which disables
* all addons. You can then manually select the addons you need by defining
* their macro, like "FLECS_SYSTEM".
*
* To blacklist an addon, make sure to *not* define FLECS_CUSTOM_BUILD, and
* instead define the addons you don't need by defining FLECS_NO_<addon>, for
* example "FLECS_NO_SYSTEM". If there are any addons that depend on the
* blacklisted addon, an error will be thrown during the build.
*
* Note that addons can have dependencies on each other. Addons will
* automatically enable their dependencies. To see the list of addons that was
* compiled in a build, enable tracing before creating the world by doing:
* ecs_log_set_level(0);
* which outputs the full list of addons Flecs was compiled with.
*/
// #define FLECS_CUSTOM_BUILD
#ifndef FLECS_CUSTOM_BUILD
// #define FLECS_C /**< C API convenience macros, always enabled */
#define FLECS_CPP /**< C++ API */
#define FLECS_MODULE /**< Module support */
#define FLECS_PARSER /**< String parser for queries */
#define FLECS_PLECS /**< ECS data definition format */
#define FLECS_RULES /**< Constraint solver for advanced queries */
#define FLECS_SNAPSHOT /**< Snapshot & restore ECS data */
#define FLECS_STATS /**< Access runtime statistics */
#define FLECS_MONITOR /**< Track runtime statistics periodically */
#define FLECS_SYSTEM /**< System support */
#define FLECS_PIPELINE /**< Pipeline support */
#define FLECS_TIMER /**< Timer support */
#define FLECS_META /**< Reflection support */
#define FLECS_META_C /**< Utilities for populating reflection data */
#define FLECS_UNITS /**< Builtin standard units */
#define FLECS_EXPR /**< Parsing strings to/from component values */
#define FLECS_JSON /**< Parsing JSON to/from component values */
#define FLECS_DOC /**< Document entities & components */
#define FLECS_COREDOC /**< Documentation for core entities & components */
#define FLECS_LOG /**< When enabled ECS provides more detailed logs */
#define FLECS_APP /**< Application addon */
#define FLECS_OS_API_IMPL /**< Default implementation for OS API */
#define FLECS_HTTP /**< Tiny HTTP server for connecting to remote UI */
#define FLECS_REST /**< REST API for querying application data */
// #define FLECS_JOURNAL /**< Journaling addon (disabled by default) */
#endif // ifndef FLECS_CUSTOM_BUILD
/** \def ECS_ID_CACHE_SIZE
* Maximum number of components to add/remove in a single operation */
#ifndef ECS_ID_CACHE_SIZE
#define ECS_ID_CACHE_SIZE (32)
#endif
/** \def ECS_TERM_DESC_CACHE_SIZE
* Maximum number of terms in desc (larger, as these are temp objects) */
#define ECS_TERM_DESC_CACHE_SIZE (16)
/** \def ECS_OBSERVER_DESC_EVENT_COUNT_MAX
* Maximum number of events to set in static array of observer descriptor */
#define ECS_OBSERVER_DESC_EVENT_COUNT_MAX (8)
/** \def ECS_VARIABLE_COUNT_MAX
* Maximum number of query variables per query */
#define ECS_VARIABLE_COUNT_MAX (64)
/** @} */
/**
* @file api_defines.h
* @brief Supporting defines for the public API.
*
* This file contains constants / macros that are typically not used by an
* application but support the public API, and therefore must be exposed. This
* header should not be included by itself.
*/
#ifndef FLECS_API_DEFINES_H
#define FLECS_API_DEFINES_H
/**
* @file api_flags.h
* @brief Bitset flags used by internals.
*/
#ifndef FLECS_API_FLAGS_H
#define FLECS_API_FLAGS_H
#ifdef __cplusplus
extern "C" {
#endif
////////////////////////////////////////////////////////////////////////////////
//// World flags
////////////////////////////////////////////////////////////////////////////////
#define EcsWorldQuitWorkers (1u << 0)
#define EcsWorldReadonly (1u << 1)
#define EcsWorldQuit (1u << 2)
#define EcsWorldFini (1u << 3)
#define EcsWorldMeasureFrameTime (1u << 4)
#define EcsWorldMeasureSystemTime (1u << 5)
#define EcsWorldMultiThreaded (1u << 6)
////////////////////////////////////////////////////////////////////////////////
//// OS API flags
////////////////////////////////////////////////////////////////////////////////
#define EcsOsApiHighResolutionTimer (1u << 0)
#define EcsOsApiLogWithColors (1u << 1)
#define EcsOsApiLogWithTimeStamp (1u << 2)
#define EcsOsApiLogWithTimeDelta (1u << 3)
////////////////////////////////////////////////////////////////////////////////
//// Entity flags (set in upper bits of ecs_record_t::row)
////////////////////////////////////////////////////////////////////////////////
#define EcsEntityIsId (1u << 31)
#define EcsEntityIsTarget (1u << 30)
#define EcsEntityIsTraversable (1u << 29)
////////////////////////////////////////////////////////////////////////////////
//// Id flags (used by ecs_id_record_t::flags)
////////////////////////////////////////////////////////////////////////////////
#define EcsIdOnDeleteRemove (1u << 0)
#define EcsIdOnDeleteDelete (1u << 1)
#define EcsIdOnDeletePanic (1u << 2)
#define EcsIdOnDeleteMask\
(EcsIdOnDeletePanic|EcsIdOnDeleteRemove|EcsIdOnDeleteDelete)
#define EcsIdOnDeleteObjectRemove (1u << 3)
#define EcsIdOnDeleteObjectDelete (1u << 4)
#define EcsIdOnDeleteObjectPanic (1u << 5)
#define EcsIdOnDeleteObjectMask\
(EcsIdOnDeleteObjectPanic|EcsIdOnDeleteObjectRemove|\
EcsIdOnDeleteObjectDelete)
#define EcsIdExclusive (1u << 6)
#define EcsIdDontInherit (1u << 7)
#define EcsIdTraversable (1u << 8)
#define EcsIdTag (1u << 9)
#define EcsIdWith (1u << 10)
#define EcsIdUnion (1u << 11)
#define EcsIdHasOnAdd (1u << 15) /* Same values as table flags */
#define EcsIdHasOnRemove (1u << 16)
#define EcsIdHasOnSet (1u << 17)
#define EcsIdHasUnSet (1u << 18)
#define EcsIdEventMask\
(EcsIdHasOnAdd|EcsIdHasOnRemove|EcsIdHasOnSet|EcsIdHasUnSet)
#define EcsIdMarkedForDelete (1u << 30)
/* Utilities for converting from flags to delete policies and vice versa */
#define ECS_ID_ON_DELETE(flags) \
((ecs_entity_t[]){0, EcsRemove, EcsDelete, 0, EcsPanic}\
[((flags) & EcsIdOnDeleteMask)])
#define ECS_ID_ON_DELETE_OBJECT(flags) ECS_ID_ON_DELETE(flags >> 3)
#define ECS_ID_ON_DELETE_FLAG(id) (1u << ((id) - EcsRemove))
#define ECS_ID_ON_DELETE_OBJECT_FLAG(id) (1u << (3 + ((id) - EcsRemove)))
////////////////////////////////////////////////////////////////////////////////
//// Iterator flags (used by ecs_iter_t::flags)
////////////////////////////////////////////////////////////////////////////////
#define EcsIterIsValid (1u << 0u) /* Does iterator contain valid result */
#define EcsIterNoData (1u << 1u) /* Does iterator provide (component) data */
#define EcsIterIsInstanced (1u << 2u) /* Is iterator instanced */
#define EcsIterHasShared (1u << 3u) /* Does result have shared terms */
#define EcsIterTableOnly (1u << 4u) /* Result only populates table */
#define EcsIterEntityOptional (1u << 5u) /* Treat terms with entity subject as optional */
#define EcsIterNoResults (1u << 6u) /* Iterator has no results */
#define EcsIterIgnoreThis (1u << 7u) /* Only evaluate non-this terms */
#define EcsIterMatchVar (1u << 8u)
#define EcsIterProfile (1u << 10u) /* Profile iterator performance */
////////////////////////////////////////////////////////////////////////////////
//// Filter flags (used by ecs_filter_t::flags)
////////////////////////////////////////////////////////////////////////////////
#define EcsEventTableOnly (1u << 8u) /* Table event (no data, same as iter flags) */
#define EcsEventNoOnSet (1u << 16u) /* Don't emit OnSet/UnSet for inherited ids */
////////////////////////////////////////////////////////////////////////////////
//// Filter flags (used by ecs_filter_t::flags)
////////////////////////////////////////////////////////////////////////////////
#define EcsFilterMatchThis (1u << 1u) /* Has terms that match This */
#define EcsFilterMatchOnlyThis (1u << 2u) /* Has only terms that match This */
#define EcsFilterMatchPrefab (1u << 3u) /* Does filter match prefabs */
#define EcsFilterMatchDisabled (1u << 4u) /* Does filter match disabled entities */
#define EcsFilterMatchEmptyTables (1u << 5u) /* Does filter return empty tables */
#define EcsFilterMatchAnything (1u << 6u) /* False if filter has no/only Not terms */
#define EcsFilterNoData (1u << 7u) /* When true, data fields won't be populated */
#define EcsFilterIsInstanced (1u << 8u) /* Is filter instanced (see ecs_filter_desc_t) */
#define EcsFilterPopulate (1u << 9u) /* Populate data, ignore non-matching fields */
#define EcsIterProfile (1u << 10u) /* Profile filter performance */
////////////////////////////////////////////////////////////////////////////////
//// Table flags (used by ecs_table_t::flags)
////////////////////////////////////////////////////////////////////////////////
#define EcsTableHasBuiltins (1u << 1u) /* Does table have builtin components */
#define EcsTableIsPrefab (1u << 2u) /* Does the table store prefabs */
#define EcsTableHasIsA (1u << 3u) /* Does the table have IsA relationship */
#define EcsTableHasChildOf (1u << 4u) /* Does the table type ChildOf relationship */
#define EcsTableHasPairs (1u << 5u) /* Does the table type have pairs */
#define EcsTableHasModule (1u << 6u) /* Does the table have module data */
#define EcsTableIsDisabled (1u << 7u) /* Does the table type has EcsDisabled */
#define EcsTableHasCtors (1u << 8u)
#define EcsTableHasDtors (1u << 9u)
#define EcsTableHasCopy (1u << 10u)
#define EcsTableHasMove (1u << 11u)
#define EcsTableHasUnion (1u << 12u)
#define EcsTableHasToggle (1u << 13u)
#define EcsTableHasOverrides (1u << 14u)
#define EcsTableHasOnAdd (1u << 15u) /* Same values as id flags */
#define EcsTableHasOnRemove (1u << 16u)
#define EcsTableHasOnSet (1u << 17u)
#define EcsTableHasUnSet (1u << 18u)
#define EcsTableHasObserved (1u << 20u)
#define EcsTableMarkedForDelete (1u << 30u)
/* Composite table flags */
#define EcsTableHasLifecycle (EcsTableHasCtors | EcsTableHasDtors)
#define EcsTableIsComplex (EcsTableHasLifecycle | EcsTableHasUnion | EcsTableHasToggle)
#define EcsTableHasAddActions (EcsTableHasIsA | EcsTableHasUnion | EcsTableHasCtors | EcsTableHasOnAdd | EcsTableHasOnSet)
#define EcsTableHasRemoveActions (EcsTableHasIsA | EcsTableHasDtors | EcsTableHasOnRemove | EcsTableHasUnSet)
////////////////////////////////////////////////////////////////////////////////
//// Query flags (used by ecs_query_t::flags)
////////////////////////////////////////////////////////////////////////////////
#define EcsQueryHasRefs (1u << 1u) /* Does query have references */
#define EcsQueryIsSubquery (1u << 2u) /* Is query a subquery */
#define EcsQueryIsOrphaned (1u << 3u) /* Is subquery orphaned */
#define EcsQueryHasOutColumns (1u << 4u) /* Does query have out columns */
#define EcsQueryHasMonitor (1u << 5u) /* Does query track changes */
////////////////////////////////////////////////////////////////////////////////
//// Aperiodic action flags (used by ecs_run_aperiodic)
////////////////////////////////////////////////////////////////////////////////
#define EcsAperiodicEmptyTables (1u << 1u) /* Process pending empty table events */
#define EcsAperiodicComponentMonitors (1u << 2u) /* Process component monitors */
#define EcsAperiodicEmptyQueries (1u << 4u) /* Process empty queries */
#ifdef __cplusplus
}
#endif
#endif
#if defined(_WIN32) || defined(_MSC_VER) || defined(__MING32__)
#define ECS_TARGET_WINDOWS
#elif defined(__ANDROID__)
#define ECS_TARGET_ANDROID
#define ECS_TARGET_POSIX
#elif defined(__linux__)
#define ECS_TARGET_LINUX
#define ECS_TARGET_POSIX
#elif defined(__FreeBSD__)
#define ECS_TARGET_FREEBSD
#define ECS_TARGET_POSIX
#elif defined(__APPLE__) && defined(__MACH__)
#define ECS_TARGET_DARWIN
#define ECS_TARGET_POSIX
#elif defined(__EMSCRIPTEN__)
#define ECS_TARGET_EM
#define ECS_TARGET_POSIX
#endif
#if defined(__MING32__)
#define ECS_TARGET_POSIX
#endif
#if defined(_MSC_VER)
#ifndef __clang__
#define ECS_TARGET_MSVC
#endif
#endif
#if defined(__clang__)
#define ECS_TARGET_CLANG
#endif
#if defined(__GNUC__)
#define ECS_TARGET_GNU
#endif
/* Map between clang and apple clang versions, as version 13 has a difference in
* the format of __PRETTY_FUNCTION__ which enum reflection depends on. */
#if defined(__clang__)
#if defined(__APPLE__)
#if __clang_major__ == 13
#if __clang_minor__ < 1
#define ECS_CLANG_VERSION 12
#else
#define ECS_CLANG_VERSION 13
#endif
#else
#define ECS_CLANG_VERSION __clang_major__
#endif
#else
#define ECS_CLANG_VERSION __clang_major__
#endif
#endif
/* Standard library dependencies */
#include <assert.h>
#include <stdarg.h>
#include <string.h>
/* Non-standard but required. If not provided by platform, add manually. */
#include <stdint.h>
/* Contains macros for importing / exporting symbols */
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef FLECS_BAKE_CONFIG_H
#define FLECS_BAKE_CONFIG_H
/* Headers of public dependencies */
/* No dependencies */
/* Convenience macro for exporting symbols */
#ifndef flecs_STATIC
#if defined(flecs_EXPORTS) && (defined(_MSC_VER) || defined(__MINGW32__))
#define FLECS_API __declspec(dllexport)
#elif defined(flecs_EXPORTS)
#define FLECS_API __attribute__((__visibility__("default")))
#elif defined(_MSC_VER)
#define FLECS_API __declspec(dllimport)
#else
#define FLECS_API
#endif
#else
#define FLECS_API
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __BAKE_LEGACY__
#define FLECS_LEGACY
#endif
/* Some symbols are only exported when building in debug build, to enable
* whitebox testing of internal datastructures */
#ifndef FLECS_NDEBUG
#define FLECS_DBG_API FLECS_API
#else
#define FLECS_DBG_API
#endif
////////////////////////////////////////////////////////////////////////////////
//// Language support defines
////////////////////////////////////////////////////////////////////////////////
#ifndef FLECS_LEGACY
#include <stdbool.h>
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
/* The API uses the native bool type in C++, or a custom one in C */
#if !defined(__cplusplus) && !defined(__bool_true_false_are_defined)
#undef bool
#undef true
#undef false
typedef char bool;
#define false 0
#define true !false
#endif
/* Utility types to indicate usage as bitmask */
typedef uint8_t ecs_flags8_t;
typedef uint16_t ecs_flags16_t;
typedef uint32_t ecs_flags32_t;
typedef uint64_t ecs_flags64_t;
/* Keep unsigned integers out of the codebase as they do more harm than good */
typedef int32_t ecs_size_t;
/* Allocator type */
typedef struct ecs_allocator_t ecs_allocator_t;
#define ECS_SIZEOF(T) ECS_CAST(ecs_size_t, sizeof(T))
/* Use alignof in C++, or a trick in C. */
#ifdef __cplusplus
#define ECS_ALIGNOF(T) static_cast<int64_t>(alignof(T))
#elif defined(ECS_TARGET_MSVC)
#define ECS_ALIGNOF(T) (int64_t)__alignof(T)
#elif defined(ECS_TARGET_GNU)
#define ECS_ALIGNOF(T) (int64_t)__alignof__(T)
#else
#define ECS_ALIGNOF(T) ((int64_t)&((struct { char c; T d; } *)0)->d)
#endif
#ifndef FLECS_NO_DEPRECATED_WARNINGS
#if defined(ECS_TARGET_GNU)
#define ECS_DEPRECATED(msg) __attribute__((deprecated(msg)))
#elif defined(ECS_TARGET_MSVC)
#define ECS_DEPRECATED(msg) __declspec(deprecated(msg))
#else
#define ECS_DEPRECATED(msg)
#endif
#else
#define ECS_DEPRECATED(msg)
#endif
#define ECS_ALIGN(size, alignment) (ecs_size_t)((((((size_t)size) - 1) / ((size_t)alignment)) + 1) * ((size_t)alignment))
/* Simple utility for determining the max of two values */
#define ECS_MAX(a, b) (((a) > (b)) ? a : b)
#define ECS_MIN(a, b) (((a) < (b)) ? a : b)
/* Abstraction on top of C-style casts so that C functions can be used in C++
* code without producing warnings */
#ifndef __cplusplus
#define ECS_CAST(T, V) ((T)(V))
#else
#define ECS_CAST(T, V) (static_cast<T>(V))
#endif
#define ECS_CONCAT(a, b) a ## b
////////////////////////////////////////////////////////////////////////////////
//// Magic numbers for sanity checking
////////////////////////////////////////////////////////////////////////////////
/* Magic number to identify the type of the object */
#define ecs_world_t_magic (0x65637377)
#define ecs_stage_t_magic (0x65637373)
#define ecs_query_t_magic (0x65637371)
#define ecs_rule_t_magic (0x65637375)
#define ecs_table_t_magic (0x65637374)
#define ecs_filter_t_magic (0x65637366)
#define ecs_trigger_t_magic (0x65637372)
#define ecs_observer_t_magic (0x65637362)
////////////////////////////////////////////////////////////////////////////////
//// Entity id macros
////////////////////////////////////////////////////////////////////////////////
#define ECS_ROW_MASK (0x0FFFFFFFu)
#define ECS_ROW_FLAGS_MASK (~ECS_ROW_MASK)
#define ECS_RECORD_TO_ROW(v) (ECS_CAST(int32_t, (ECS_CAST(uint32_t, v) & ECS_ROW_MASK)))
#define ECS_RECORD_TO_ROW_FLAGS(v) (ECS_CAST(uint32_t, v) & ECS_ROW_FLAGS_MASK)
#define ECS_ROW_TO_RECORD(row, flags) (ECS_CAST(uint32_t, (ECS_CAST(uint32_t, row) | (flags))))
#define ECS_ID_FLAGS_MASK (0xFFull << 60)
#define ECS_ENTITY_MASK (0xFFFFFFFFull)
#define ECS_GENERATION_MASK (0xFFFFull << 32)
#define ECS_GENERATION(e) ((e & ECS_GENERATION_MASK) >> 32)
#define ECS_GENERATION_INC(e) ((e & ~ECS_GENERATION_MASK) | ((0xFFFF & (ECS_GENERATION(e) + 1)) << 32))
#define ECS_COMPONENT_MASK (~ECS_ID_FLAGS_MASK)
#define ECS_HAS_ID_FLAG(e, flag) ((e) & ECS_##flag)
#define ECS_IS_PAIR(id) (((id) & ECS_ID_FLAGS_MASK) == ECS_PAIR)
#define ECS_PAIR_FIRST(e) (ecs_entity_t_hi(e & ECS_COMPONENT_MASK))
#define ECS_PAIR_SECOND(e) (ecs_entity_t_lo(e))
#define ECS_HAS_RELATION(e, rel) (ECS_HAS_ID_FLAG(e, PAIR) && (ECS_PAIR_FIRST(e) == rel))
////////////////////////////////////////////////////////////////////////////////
//// Convert between C typenames and variables
////////////////////////////////////////////////////////////////////////////////
/** Translate C type to id. */
#define ecs_id(T) FLECS__E##T
/** Translate C type to system function. */
#define ecs_iter_action(T) FLECS__F##T
////////////////////////////////////////////////////////////////////////////////
//// Utilities for working with pair identifiers
////////////////////////////////////////////////////////////////////////////////
#define ecs_entity_t_lo(value) ECS_CAST(uint32_t, value)
#define ecs_entity_t_hi(value) ECS_CAST(uint32_t, (value) >> 32)
#define ecs_entity_t_comb(lo, hi) ((ECS_CAST(uint64_t, hi) << 32) + ECS_CAST(uint32_t, lo))
#define ecs_pair(pred, obj) (ECS_PAIR | ecs_entity_t_comb(obj, pred))
#define ecs_pair_first(world, pair) ecs_get_alive(world, ECS_PAIR_FIRST(pair))
#define ecs_pair_second(world, pair) ecs_get_alive(world, ECS_PAIR_SECOND(pair))
#define ecs_pair_relation ecs_pair_first
#define ecs_pair_object ecs_pair_second
#define ecs_poly_id(tag) ecs_pair(ecs_id(EcsPoly), tag)
////////////////////////////////////////////////////////////////////////////////
//// Debug macros
////////////////////////////////////////////////////////////////////////////////
#ifndef FLECS_NDEBUG
#define ECS_TABLE_LOCK(world, table) ecs_table_lock(world, table)
#define ECS_TABLE_UNLOCK(world, table) ecs_table_unlock(world, table)
#else
#define ECS_TABLE_LOCK(world, table)
#define ECS_TABLE_UNLOCK(world, table)
#endif
////////////////////////////////////////////////////////////////////////////////
//// Convenience macros for ctor, dtor, move and copy
////////////////////////////////////////////////////////////////////////////////
#ifndef FLECS_LEGACY
/* Constructor/Destructor convenience macro */
#define ECS_XTOR_IMPL(type, postfix, var, ...)\
void type##_##postfix(\
void *_ptr,\
int32_t _count,\
const ecs_type_info_t *type_info)\
{\
(void)_ptr;\
(void)_count;\
(void)type_info;\
for (int32_t i = 0; i < _count; i ++) {\
type *var = &((type*)_ptr)[i];\
(void)var;\
__VA_ARGS__\
}\
}
/* Copy convenience macro */
#define ECS_COPY_IMPL(type, dst_var, src_var, ...)\
void type##_##copy(\
void *_dst_ptr,\
const void *_src_ptr,\
int32_t _count,\
const ecs_type_info_t *type_info)\
{\
(void)_dst_ptr;\
(void)_src_ptr;\
(void)_count;\
(void)type_info;\
for (int32_t i = 0; i < _count; i ++) {\
type *dst_var = &((type*)_dst_ptr)[i];\
type *src_var = &((type*)_src_ptr)[i];\
(void)dst_var;\
(void)src_var;\
__VA_ARGS__\
}\
}
/* Move convenience macro */
#define ECS_MOVE_IMPL(type, dst_var, src_var, ...)\
void type##_##move(\
void *_dst_ptr,\
void *_src_ptr,\
int32_t _count,\
const ecs_type_info_t *type_info)\
{\
(void)_dst_ptr;\
(void)_src_ptr;\
(void)_count;\
(void)type_info;\
for (int32_t i = 0; i < _count; i ++) {\
type *dst_var = &((type*)_dst_ptr)[i];\
type *src_var = &((type*)_src_ptr)[i];\
(void)dst_var;\
(void)src_var;\
__VA_ARGS__\
}\
}
#define ECS_HOOK_IMPL(type, func, var, ...)\
void func(ecs_iter_t *_it)\
{\
for (int32_t i = 0; i < _it->count; i ++) {\
ecs_entity_t entity = _it->entities[i];\
type *var = &((type*)_it->ptrs[0])[i];\
(void)entity;\
(void)var;\
__VA_ARGS__\
}\
}
#endif
#ifdef __cplusplus
}
#endif
#endif
/**
* @file vec.h
* @brief Vector with allocator support.
*/
#ifndef FLECS_VEC_H
#define FLECS_VEC_H
/** A component column. */
typedef struct ecs_vec_t {
void *array;
int32_t count;
int32_t size;
#ifdef FLECS_DEBUG
ecs_size_t elem_size;
#endif
} ecs_vec_t;
FLECS_API
ecs_vec_t* ecs_vec_init(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size,
int32_t elem_count);
#define ecs_vec_init_t(allocator, vec, T, elem_count) \
ecs_vec_init(allocator, vec, ECS_SIZEOF(T), elem_count)
FLECS_API
void ecs_vec_init_if(
ecs_vec_t *vec,
ecs_size_t size);
#define ecs_vec_init_if_t(vec, T) \
ecs_vec_init_if(vec, ECS_SIZEOF(T))
FLECS_API
void ecs_vec_fini(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size);
#define ecs_vec_fini_t(allocator, vec, T) \
ecs_vec_fini(allocator, vec, ECS_SIZEOF(T))
FLECS_API
ecs_vec_t* ecs_vec_reset(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size);
#define ecs_vec_reset_t(allocator, vec, T) \
ecs_vec_reset(allocator, vec, ECS_SIZEOF(T))
FLECS_API
void ecs_vec_clear(
ecs_vec_t *vec);
FLECS_API
void* ecs_vec_append(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size);
#define ecs_vec_append_t(allocator, vec, T) \
ECS_CAST(T*, ecs_vec_append(allocator, vec, ECS_SIZEOF(T)))
FLECS_API
void ecs_vec_remove(
ecs_vec_t *vec,
ecs_size_t size,
int32_t elem);
#define ecs_vec_remove_t(vec, T, elem) \
ecs_vec_remove(vec, ECS_SIZEOF(T), elem)
FLECS_API
void ecs_vec_remove_last(
ecs_vec_t *vec);
FLECS_API
ecs_vec_t ecs_vec_copy(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size);
#define ecs_vec_copy_t(allocator, vec, T) \
ecs_vec_copy(allocator, vec, ECS_SIZEOF(T))
FLECS_API
void ecs_vec_reclaim(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size);
#define ecs_vec_reclaim_t(allocator, vec, T) \
ecs_vec_reclaim(allocator, vec, ECS_SIZEOF(T))
FLECS_API
void ecs_vec_set_size(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size,
int32_t elem_count);
#define ecs_vec_set_size_t(allocator, vec, T, elem_count) \
ecs_vec_set_size(allocator, vec, ECS_SIZEOF(T), elem_count)
FLECS_API
void ecs_vec_set_min_size(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size,
int32_t elem_count);
#define ecs_vec_set_min_size_t(allocator, vec, T, elem_count) \
ecs_vec_set_min_size(allocator, vec, ECS_SIZEOF(T), elem_count)
FLECS_API
void ecs_vec_set_min_count(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size,
int32_t elem_count);
#define ecs_vec_set_min_count_t(allocator, vec, T, elem_count) \
ecs_vec_set_min_count(allocator, vec, ECS_SIZEOF(T), elem_count)
FLECS_API
void ecs_vec_set_count(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size,
int32_t elem_count);
#define ecs_vec_set_count_t(allocator, vec, T, elem_count) \
ecs_vec_set_count(allocator, vec, ECS_SIZEOF(T), elem_count)
FLECS_API
void* ecs_vec_grow(
struct ecs_allocator_t *allocator,
ecs_vec_t *vec,
ecs_size_t size,
int32_t elem_count);
#define ecs_vec_grow_t(allocator, vec, T, elem_count) \
ecs_vec_grow(allocator, vec, ECS_SIZEOF(T), elem_count)
FLECS_API
int32_t ecs_vec_count(
const ecs_vec_t *vec);
FLECS_API
int32_t ecs_vec_size(
const ecs_vec_t *vec);
FLECS_API
void* ecs_vec_get(
const ecs_vec_t *vec,
ecs_size_t size,
int32_t index);
#define ecs_vec_get_t(vec, T, index) \
ECS_CAST(T*, ecs_vec_get(vec, ECS_SIZEOF(T), index))
FLECS_API
void* ecs_vec_first(
const ecs_vec_t *vec);
#define ecs_vec_first_t(vec, T) \
ECS_CAST(T*, ecs_vec_first(vec))
FLECS_API
void* ecs_vec_last(
const ecs_vec_t *vec,
ecs_size_t size);
#define ecs_vec_last_t(vec, T) \
ECS_CAST(T*, ecs_vec_last(vec, ECS_SIZEOF(T)))
#endif
/**
* @file sparse.h
* @brief Sparse set data structure.
*/
#ifndef FLECS_SPARSE_H
#define FLECS_SPARSE_H
#ifdef __cplusplus
extern "C" {
#endif
/** The number of elements in a single page */
#define FLECS_SPARSE_CHUNK_SIZE (4096)
typedef struct ecs_sparse_t {
ecs_vec_t dense; /* Dense array with indices to sparse array. The
* dense array stores both alive and not alive
* sparse indices. The 'count' member keeps
* track of which indices are alive. */
ecs_vec_t pages; /* Chunks with sparse arrays & data */
ecs_size_t size; /* Element size */
int32_t count; /* Number of alive entries */
uint64_t max_id_local; /* Local max index (if no global is set) */
uint64_t *max_id; /* Maximum issued sparse index */
struct ecs_allocator_t *allocator;
struct ecs_block_allocator_t *page_allocator;
} ecs_sparse_t;
/** Initialize sparse set */
FLECS_DBG_API
void flecs_sparse_init(
ecs_sparse_t *sparse,
struct ecs_allocator_t *allocator,
struct ecs_block_allocator_t *page_allocator,
ecs_size_t elem_size);
#define flecs_sparse_init_t(sparse, allocator, page_allocator, T)\
flecs_sparse_init(sparse, allocator, page_allocator, ECS_SIZEOF(T))
FLECS_DBG_API
void flecs_sparse_fini(
ecs_sparse_t *sparse);
/** Remove all elements from sparse set */
FLECS_DBG_API
void flecs_sparse_clear(
ecs_sparse_t *sparse);
/** Set id source. This allows the sparse set to use an external variable for
* issuing and increasing new ids. */
FLECS_DBG_API
void flecs_sparse_set_id_source(
ecs_sparse_t *sparse,
uint64_t *id_source);
/** Add element to sparse set, this generates or recycles an id */
FLECS_DBG_API
void* flecs_sparse_add(
ecs_sparse_t *sparse,
ecs_size_t elem_size);
#define flecs_sparse_add_t(sparse, T)\
ECS_CAST(T*, flecs_sparse_add(sparse, ECS_SIZEOF(T)))
/** Get last issued id. */
FLECS_DBG_API