-
-
Notifications
You must be signed in to change notification settings - Fork 585
Expand file tree
/
Copy pathworld.c
More file actions
2373 lines (2013 loc) · 73.1 KB
/
world.c
File metadata and controls
2373 lines (2013 loc) · 73.1 KB
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
/**
* @file world.c
* @brief World-level API.
*/
#include "private_api.h"
/* Id flags */
const ecs_id_t ECS_PAIR = (1ull << 63);
const ecs_id_t ECS_AUTO_OVERRIDE = (1ull << 62);
const ecs_id_t ECS_TOGGLE = (1ull << 61);
/** Builtin component ids */
const ecs_entity_t ecs_id(EcsComponent) = 1;
const ecs_entity_t ecs_id(EcsIdentifier) = 2;
const ecs_entity_t ecs_id(EcsPoly) = 3;
/* Poly target components */
const ecs_entity_t EcsQuery = 5;
const ecs_entity_t EcsObserver = 6;
const ecs_entity_t EcsSystem = 7;
/* Core scopes & entities */
const ecs_entity_t EcsWorld = FLECS_HI_COMPONENT_ID + 0;
const ecs_entity_t EcsFlecs = FLECS_HI_COMPONENT_ID + 1;
const ecs_entity_t EcsFlecsCore = FLECS_HI_COMPONENT_ID + 2;
const ecs_entity_t EcsFlecsInternals = FLECS_HI_COMPONENT_ID + 3;
const ecs_entity_t EcsModule = FLECS_HI_COMPONENT_ID + 4;
const ecs_entity_t EcsPrivate = FLECS_HI_COMPONENT_ID + 5;
const ecs_entity_t EcsPrefab = FLECS_HI_COMPONENT_ID + 6;
const ecs_entity_t EcsDisabled = FLECS_HI_COMPONENT_ID + 7;
const ecs_entity_t EcsNotQueryable = FLECS_HI_COMPONENT_ID + 8;
const ecs_entity_t EcsSlotOf = FLECS_HI_COMPONENT_ID + 9;
const ecs_entity_t EcsFlag = FLECS_HI_COMPONENT_ID + 10;
/* Marker entities for query encoding */
const ecs_entity_t EcsWildcard = FLECS_HI_COMPONENT_ID + 11;
const ecs_entity_t EcsAny = FLECS_HI_COMPONENT_ID + 12;
const ecs_entity_t EcsThis = FLECS_HI_COMPONENT_ID + 13;
const ecs_entity_t EcsVariable = FLECS_HI_COMPONENT_ID + 14;
/* Traits */
const ecs_entity_t EcsTransitive = FLECS_HI_COMPONENT_ID + 15;
const ecs_entity_t EcsReflexive = FLECS_HI_COMPONENT_ID + 16;
const ecs_entity_t EcsSymmetric = FLECS_HI_COMPONENT_ID + 17;
const ecs_entity_t EcsFinal = FLECS_HI_COMPONENT_ID + 18;
const ecs_entity_t EcsOnInstantiate = FLECS_HI_COMPONENT_ID + 19;
const ecs_entity_t EcsOverride = FLECS_HI_COMPONENT_ID + 20;
const ecs_entity_t EcsInherit = FLECS_HI_COMPONENT_ID + 21;
const ecs_entity_t EcsDontInherit = FLECS_HI_COMPONENT_ID + 22;
const ecs_entity_t EcsPairIsTag = FLECS_HI_COMPONENT_ID + 23;
const ecs_entity_t EcsExclusive = FLECS_HI_COMPONENT_ID + 24;
const ecs_entity_t EcsAcyclic = FLECS_HI_COMPONENT_ID + 25;
const ecs_entity_t EcsTraversable = FLECS_HI_COMPONENT_ID + 26;
const ecs_entity_t EcsWith = FLECS_HI_COMPONENT_ID + 27;
const ecs_entity_t EcsOneOf = FLECS_HI_COMPONENT_ID + 28;
const ecs_entity_t EcsCanToggle = FLECS_HI_COMPONENT_ID + 29;
const ecs_entity_t EcsTrait = FLECS_HI_COMPONENT_ID + 30;
const ecs_entity_t EcsRelationship = FLECS_HI_COMPONENT_ID + 31;
const ecs_entity_t EcsTarget = FLECS_HI_COMPONENT_ID + 32;
/* Builtin relationships */
const ecs_entity_t EcsChildOf = FLECS_HI_COMPONENT_ID + 33;
const ecs_entity_t EcsIsA = FLECS_HI_COMPONENT_ID + 34;
const ecs_entity_t EcsDependsOn = FLECS_HI_COMPONENT_ID + 35;
/* Identifier tags */
const ecs_entity_t EcsName = FLECS_HI_COMPONENT_ID + 36;
const ecs_entity_t EcsSymbol = FLECS_HI_COMPONENT_ID + 37;
const ecs_entity_t EcsAlias = FLECS_HI_COMPONENT_ID + 38;
/* Events */
const ecs_entity_t EcsOnAdd = FLECS_HI_COMPONENT_ID + 39;
const ecs_entity_t EcsOnRemove = FLECS_HI_COMPONENT_ID + 40;
const ecs_entity_t EcsOnSet = FLECS_HI_COMPONENT_ID + 41;
const ecs_entity_t EcsOnDelete = FLECS_HI_COMPONENT_ID + 43;
const ecs_entity_t EcsOnDeleteTarget = FLECS_HI_COMPONENT_ID + 44;
const ecs_entity_t EcsOnTableCreate = FLECS_HI_COMPONENT_ID + 45;
const ecs_entity_t EcsOnTableDelete = FLECS_HI_COMPONENT_ID + 46;
/* Timers */
const ecs_entity_t ecs_id(EcsTickSource) = FLECS_HI_COMPONENT_ID + 49;
const ecs_entity_t ecs_id(EcsTimer) = FLECS_HI_COMPONENT_ID + 50;
const ecs_entity_t ecs_id(EcsRateFilter) = FLECS_HI_COMPONENT_ID + 51;
/* Actions */
const ecs_entity_t EcsRemove = FLECS_HI_COMPONENT_ID + 52;
const ecs_entity_t EcsDelete = FLECS_HI_COMPONENT_ID + 53;
const ecs_entity_t EcsPanic = FLECS_HI_COMPONENT_ID + 54;
/* Storage */
const ecs_entity_t EcsSparse = FLECS_HI_COMPONENT_ID + 55;
const ecs_entity_t EcsUnion = FLECS_HI_COMPONENT_ID + 56;
/* Misc */
const ecs_entity_t ecs_id(EcsDefaultChildComponent) = FLECS_HI_COMPONENT_ID + 57;
/* Builtin predicate ids (used by query engine) */
const ecs_entity_t EcsPredEq = FLECS_HI_COMPONENT_ID + 58;
const ecs_entity_t EcsPredMatch = FLECS_HI_COMPONENT_ID + 59;
const ecs_entity_t EcsPredLookup = FLECS_HI_COMPONENT_ID + 60;
const ecs_entity_t EcsScopeOpen = FLECS_HI_COMPONENT_ID + 61;
const ecs_entity_t EcsScopeClose = FLECS_HI_COMPONENT_ID + 62;
/* Systems */
const ecs_entity_t EcsMonitor = FLECS_HI_COMPONENT_ID + 63;
const ecs_entity_t EcsEmpty = FLECS_HI_COMPONENT_ID + 64;
const ecs_entity_t ecs_id(EcsPipeline) = FLECS_HI_COMPONENT_ID + 65;
const ecs_entity_t EcsOnStart = FLECS_HI_COMPONENT_ID + 66;
const ecs_entity_t EcsPreFrame = FLECS_HI_COMPONENT_ID + 67;
const ecs_entity_t EcsOnLoad = FLECS_HI_COMPONENT_ID + 68;
const ecs_entity_t EcsPostLoad = FLECS_HI_COMPONENT_ID + 69;
const ecs_entity_t EcsPreUpdate = FLECS_HI_COMPONENT_ID + 70;
const ecs_entity_t EcsOnUpdate = FLECS_HI_COMPONENT_ID + 71;
const ecs_entity_t EcsOnValidate = FLECS_HI_COMPONENT_ID + 72;
const ecs_entity_t EcsPostUpdate = FLECS_HI_COMPONENT_ID + 73;
const ecs_entity_t EcsPreStore = FLECS_HI_COMPONENT_ID + 74;
const ecs_entity_t EcsOnStore = FLECS_HI_COMPONENT_ID + 75;
const ecs_entity_t EcsPostFrame = FLECS_HI_COMPONENT_ID + 76;
const ecs_entity_t EcsPhase = FLECS_HI_COMPONENT_ID + 77;
/* Meta primitive components (don't use low ids to save id space) */
#ifdef FLECS_META
const ecs_entity_t ecs_id(ecs_bool_t) = FLECS_HI_COMPONENT_ID + 80;
const ecs_entity_t ecs_id(ecs_char_t) = FLECS_HI_COMPONENT_ID + 81;
const ecs_entity_t ecs_id(ecs_byte_t) = FLECS_HI_COMPONENT_ID + 82;
const ecs_entity_t ecs_id(ecs_u8_t) = FLECS_HI_COMPONENT_ID + 83;
const ecs_entity_t ecs_id(ecs_u16_t) = FLECS_HI_COMPONENT_ID + 84;
const ecs_entity_t ecs_id(ecs_u32_t) = FLECS_HI_COMPONENT_ID + 85;
const ecs_entity_t ecs_id(ecs_u64_t) = FLECS_HI_COMPONENT_ID + 86;
const ecs_entity_t ecs_id(ecs_uptr_t) = FLECS_HI_COMPONENT_ID + 87;
const ecs_entity_t ecs_id(ecs_i8_t) = FLECS_HI_COMPONENT_ID + 88;
const ecs_entity_t ecs_id(ecs_i16_t) = FLECS_HI_COMPONENT_ID + 89;
const ecs_entity_t ecs_id(ecs_i32_t) = FLECS_HI_COMPONENT_ID + 90;
const ecs_entity_t ecs_id(ecs_i64_t) = FLECS_HI_COMPONENT_ID + 91;
const ecs_entity_t ecs_id(ecs_iptr_t) = FLECS_HI_COMPONENT_ID + 92;
const ecs_entity_t ecs_id(ecs_f32_t) = FLECS_HI_COMPONENT_ID + 93;
const ecs_entity_t ecs_id(ecs_f64_t) = FLECS_HI_COMPONENT_ID + 94;
const ecs_entity_t ecs_id(ecs_string_t) = FLECS_HI_COMPONENT_ID + 95;
const ecs_entity_t ecs_id(ecs_entity_t) = FLECS_HI_COMPONENT_ID + 96;
const ecs_entity_t ecs_id(ecs_id_t) = FLECS_HI_COMPONENT_ID + 97;
/** Meta module component ids */
const ecs_entity_t ecs_id(EcsType) = FLECS_HI_COMPONENT_ID + 98;
const ecs_entity_t ecs_id(EcsTypeSerializer) = FLECS_HI_COMPONENT_ID + 99;
const ecs_entity_t ecs_id(EcsPrimitive) = FLECS_HI_COMPONENT_ID + 100;
const ecs_entity_t ecs_id(EcsEnum) = FLECS_HI_COMPONENT_ID + 101;
const ecs_entity_t ecs_id(EcsBitmask) = FLECS_HI_COMPONENT_ID + 102;
const ecs_entity_t ecs_id(EcsMember) = FLECS_HI_COMPONENT_ID + 103;
const ecs_entity_t ecs_id(EcsMemberRanges) = FLECS_HI_COMPONENT_ID + 104;
const ecs_entity_t ecs_id(EcsStruct) = FLECS_HI_COMPONENT_ID + 105;
const ecs_entity_t ecs_id(EcsArray) = FLECS_HI_COMPONENT_ID + 106;
const ecs_entity_t ecs_id(EcsVector) = FLECS_HI_COMPONENT_ID + 107;
const ecs_entity_t ecs_id(EcsOpaque) = FLECS_HI_COMPONENT_ID + 108;
const ecs_entity_t ecs_id(EcsUnit) = FLECS_HI_COMPONENT_ID + 109;
const ecs_entity_t ecs_id(EcsUnitPrefix) = FLECS_HI_COMPONENT_ID + 110;
const ecs_entity_t EcsQuantity = FLECS_HI_COMPONENT_ID + 112;
#endif
const ecs_entity_t EcsConstant = FLECS_HI_COMPONENT_ID + 111;
/* Doc module components */
#ifdef FLECS_DOC
const ecs_entity_t ecs_id(EcsDocDescription) = FLECS_HI_COMPONENT_ID + 113;
const ecs_entity_t EcsDocBrief = FLECS_HI_COMPONENT_ID + 114;
const ecs_entity_t EcsDocDetail = FLECS_HI_COMPONENT_ID + 115;
const ecs_entity_t EcsDocLink = FLECS_HI_COMPONENT_ID + 116;
const ecs_entity_t EcsDocColor = FLECS_HI_COMPONENT_ID + 117;
const ecs_entity_t EcsDocUuid = FLECS_HI_COMPONENT_ID + 118;
#endif
/* REST module components */
#ifdef FLECS_REST
const ecs_entity_t ecs_id(EcsRest) = FLECS_HI_COMPONENT_ID + 119;
#endif
/* Max static id:
* #define EcsFirstUserEntityId (FLECS_HI_COMPONENT_ID + 128) */
/* Default lookup path */
static ecs_entity_t ecs_default_lookup_path[2] = { 0, 0 };
/* Declarations for addons. Located in world.c to avoid issues during linking of
* static library */
#ifdef FLECS_ALERTS
ECS_COMPONENT_DECLARE(EcsAlert);
ECS_COMPONENT_DECLARE(EcsAlertInstance);
ECS_COMPONENT_DECLARE(EcsAlertsActive);
ECS_TAG_DECLARE(EcsAlertInfo);
ECS_TAG_DECLARE(EcsAlertWarning);
ECS_TAG_DECLARE(EcsAlertError);
ECS_TAG_DECLARE(EcsAlertCritical);
#endif
#ifdef FLECS_UNITS
ecs_entity_t EcsUnitPrefixes;
ecs_entity_t EcsYocto;
ecs_entity_t EcsZepto;
ecs_entity_t EcsAtto;
ecs_entity_t EcsFemto;
ecs_entity_t EcsPico;
ecs_entity_t EcsNano;
ecs_entity_t EcsMicro;
ecs_entity_t EcsMilli;
ecs_entity_t EcsCenti;
ecs_entity_t EcsDeci;
ecs_entity_t EcsDeca;
ecs_entity_t EcsHecto;
ecs_entity_t EcsKilo;
ecs_entity_t EcsMega;
ecs_entity_t EcsGiga;
ecs_entity_t EcsTera;
ecs_entity_t EcsPeta;
ecs_entity_t EcsExa;
ecs_entity_t EcsZetta;
ecs_entity_t EcsYotta;
ecs_entity_t EcsKibi;
ecs_entity_t EcsMebi;
ecs_entity_t EcsGibi;
ecs_entity_t EcsTebi;
ecs_entity_t EcsPebi;
ecs_entity_t EcsExbi;
ecs_entity_t EcsZebi;
ecs_entity_t EcsYobi;
ecs_entity_t EcsDuration;
ecs_entity_t EcsPicoSeconds;
ecs_entity_t EcsNanoSeconds;
ecs_entity_t EcsMicroSeconds;
ecs_entity_t EcsMilliSeconds;
ecs_entity_t EcsSeconds;
ecs_entity_t EcsMinutes;
ecs_entity_t EcsHours;
ecs_entity_t EcsDays;
ecs_entity_t EcsTime;
ecs_entity_t EcsDate;
ecs_entity_t EcsMass;
ecs_entity_t EcsGrams;
ecs_entity_t EcsKiloGrams;
ecs_entity_t EcsElectricCurrent;
ecs_entity_t EcsAmpere;
ecs_entity_t EcsAmount;
ecs_entity_t EcsMole;
ecs_entity_t EcsLuminousIntensity;
ecs_entity_t EcsCandela;
ecs_entity_t EcsForce;
ecs_entity_t EcsNewton;
ecs_entity_t EcsLength;
ecs_entity_t EcsMeters;
ecs_entity_t EcsPicoMeters;
ecs_entity_t EcsNanoMeters;
ecs_entity_t EcsMicroMeters;
ecs_entity_t EcsMilliMeters;
ecs_entity_t EcsCentiMeters;
ecs_entity_t EcsKiloMeters;
ecs_entity_t EcsMiles;
ecs_entity_t EcsPixels;
ecs_entity_t EcsPressure;
ecs_entity_t EcsPascal;
ecs_entity_t EcsBar;
ecs_entity_t EcsSpeed;
ecs_entity_t EcsMetersPerSecond;
ecs_entity_t EcsKiloMetersPerSecond;
ecs_entity_t EcsKiloMetersPerHour;
ecs_entity_t EcsMilesPerHour;
ecs_entity_t EcsAcceleration;
ecs_entity_t EcsTemperature;
ecs_entity_t EcsKelvin;
ecs_entity_t EcsCelsius;
ecs_entity_t EcsFahrenheit;
ecs_entity_t EcsData;
ecs_entity_t EcsBits;
ecs_entity_t EcsKiloBits;
ecs_entity_t EcsMegaBits;
ecs_entity_t EcsGigaBits;
ecs_entity_t EcsBytes;
ecs_entity_t EcsKiloBytes;
ecs_entity_t EcsMegaBytes;
ecs_entity_t EcsGigaBytes;
ecs_entity_t EcsKibiBytes;
ecs_entity_t EcsGibiBytes;
ecs_entity_t EcsMebiBytes;
ecs_entity_t EcsDataRate;
ecs_entity_t EcsBitsPerSecond;
ecs_entity_t EcsKiloBitsPerSecond;
ecs_entity_t EcsMegaBitsPerSecond;
ecs_entity_t EcsGigaBitsPerSecond;
ecs_entity_t EcsBytesPerSecond;
ecs_entity_t EcsKiloBytesPerSecond;
ecs_entity_t EcsMegaBytesPerSecond;
ecs_entity_t EcsGigaBytesPerSecond;
ecs_entity_t EcsPercentage;
ecs_entity_t EcsAngle;
ecs_entity_t EcsRadians;
ecs_entity_t EcsDegrees;
ecs_entity_t EcsColor;
ecs_entity_t EcsColorRgb;
ecs_entity_t EcsColorHsl;
ecs_entity_t EcsColorCss;
ecs_entity_t EcsBel;
ecs_entity_t EcsDeciBel;
ecs_entity_t EcsFrequency;
ecs_entity_t EcsHertz;
ecs_entity_t EcsKiloHertz;
ecs_entity_t EcsMegaHertz;
ecs_entity_t EcsGigaHertz;
ecs_entity_t EcsUri;
ecs_entity_t EcsUriHyperlink;
ecs_entity_t EcsUriImage;
ecs_entity_t EcsUriFile;
#endif
/* -- Private functions -- */
ecs_stage_t* flecs_stage_from_readonly_world(
const ecs_world_t *world)
{
ecs_assert(flecs_poly_is(world, ecs_world_t) ||
flecs_poly_is(world, ecs_stage_t),
ECS_INTERNAL_ERROR,
NULL);
if (flecs_poly_is(world, ecs_world_t)) {
return ECS_CONST_CAST(ecs_stage_t*, world->stages[0]);
} else if (flecs_poly_is(world, ecs_stage_t)) {
return ECS_CONST_CAST(ecs_stage_t*, world);
}
return NULL;
}
ecs_stage_t* flecs_stage_from_world(
ecs_world_t **world_ptr)
{
ecs_world_t *world = *world_ptr;
ecs_assert(flecs_poly_is(world, ecs_world_t) ||
flecs_poly_is(world, ecs_stage_t),
ECS_INTERNAL_ERROR,
NULL);
if (flecs_poly_is(world, ecs_world_t)) {
return world->stages[0];
}
*world_ptr = ((ecs_stage_t*)world)->world;
return ECS_CONST_CAST(ecs_stage_t*, world);
}
ecs_world_t* flecs_suspend_readonly(
const ecs_world_t *stage_world,
ecs_suspend_readonly_state_t *state)
{
ecs_assert(stage_world != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(state != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_world_t *world =
ECS_CONST_CAST(ecs_world_t*, ecs_get_world(stage_world));
flecs_poly_assert(world, ecs_world_t);
bool is_readonly = ECS_BIT_IS_SET(world->flags, EcsWorldReadonly);
ecs_world_t *temp_world = world;
ecs_stage_t *stage = flecs_stage_from_world(&temp_world);
if (!is_readonly && !stage->defer) {
state->is_readonly = false;
state->is_deferred = false;
return world;
}
ecs_dbg_3("suspending readonly mode");
/* Cannot suspend when running with multiple threads */
ecs_assert(!(world->flags & EcsWorldReadonly) ||
!(world->flags & EcsWorldMultiThreaded),
ECS_INVALID_WHILE_READONLY, NULL);
state->is_readonly = is_readonly;
state->is_deferred = stage->defer != 0;
state->cmd_flushing = stage->cmd_flushing;
/* Silence readonly checks */
world->flags &= ~EcsWorldReadonly;
stage->cmd_flushing = false;
/* Hack around safety checks (this ought to look ugly) */
state->defer_count = stage->defer;
state->cmd_stack[0] = stage->cmd_stack[0];
state->cmd_stack[1] = stage->cmd_stack[1];
state->cmd = stage->cmd;
flecs_commands_init(stage, &stage->cmd_stack[0]);
flecs_commands_init(stage, &stage->cmd_stack[1]);
stage->cmd = &stage->cmd_stack[0];
state->scope = stage->scope;
state->with = stage->with;
stage->defer = 0;
return world;
}
void flecs_resume_readonly(
ecs_world_t *world,
ecs_suspend_readonly_state_t *state)
{
flecs_poly_assert(world, ecs_world_t);
ecs_assert(state != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_world_t *temp_world = world;
ecs_stage_t *stage = flecs_stage_from_world(&temp_world);
if (state->is_readonly || state->is_deferred) {
ecs_dbg_3("resuming readonly mode");
ecs_run_aperiodic(world, 0);
/* Restore readonly state / defer count */
ECS_BIT_COND(world->flags, EcsWorldReadonly, state->is_readonly);
stage->defer = state->defer_count;
stage->cmd_flushing = state->cmd_flushing;
flecs_commands_fini(stage, &stage->cmd_stack[0]);
flecs_commands_fini(stage, &stage->cmd_stack[1]);
stage->cmd_stack[0] = state->cmd_stack[0];
stage->cmd_stack[1] = state->cmd_stack[1];
stage->cmd = state->cmd;
stage->scope = state->scope;
stage->with = state->with;
}
}
/* Evaluate component monitor. If a monitored entity changed it will have set a
* flag in one of the world's component monitors. Queries can register
* themselves with component monitors to determine whether they need to rematch
* with tables. */
static
void flecs_eval_component_monitor(
ecs_world_t *world)
{
flecs_poly_assert(world, ecs_world_t);
if (!world->monitors.is_dirty) {
return;
}
world->info.eval_comp_monitors_total ++;
ecs_os_perf_trace_push("flecs.component_monitor.eval");
world->monitors.is_dirty = false;
ecs_map_iter_t it = ecs_map_iter(&world->monitors.monitors);
while (ecs_map_next(&it)) {
ecs_monitor_t *m = ecs_map_ptr(&it);
if (!m->is_dirty) {
continue;
}
m->is_dirty = false;
int32_t i, count = ecs_vec_count(&m->queries);
ecs_query_t **elems = ecs_vec_first(&m->queries);
for (i = 0; i < count; i ++) {
ecs_query_t *q = elems[i];
flecs_poly_assert(q, ecs_query_t);
flecs_query_cache_notify(world, q, &(ecs_query_cache_event_t) {
.kind = EcsQueryTableRematch
});
}
}
ecs_os_perf_trace_pop("flecs.component_monitor.eval");
}
void flecs_monitor_mark_dirty(
ecs_world_t *world,
ecs_entity_t id)
{
ecs_map_t *monitors = &world->monitors.monitors;
/* Only flag if there are actually monitors registered, so that we
* don't waste cycles evaluating monitors if there's no interest */
if (ecs_map_is_init(monitors)) {
ecs_monitor_t *m = ecs_map_get_deref(monitors, ecs_monitor_t, id);
if (m) {
if (!world->monitors.is_dirty) {
world->monitor_generation ++;
}
m->is_dirty = true;
world->monitors.is_dirty = true;
}
}
}
void flecs_monitor_register(
ecs_world_t *world,
ecs_entity_t id,
ecs_query_t *query)
{
ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(id != 0, ECS_INTERNAL_ERROR, NULL);
ecs_assert(query != NULL, ECS_INTERNAL_ERROR, NULL);
flecs_poly_assert(query, ecs_query_t);
ecs_map_t *monitors = &world->monitors.monitors;
ecs_map_init_if(monitors, &world->allocator);
ecs_monitor_t *m = ecs_map_ensure_alloc_t(monitors, ecs_monitor_t, id);
ecs_vec_init_if_t(&m->queries, ecs_query_t*);
ecs_query_t **q = ecs_vec_append_t(
&world->allocator, &m->queries, ecs_query_t*);
*q = query;
}
void flecs_monitor_unregister(
ecs_world_t *world,
ecs_entity_t id,
ecs_query_t *query)
{
ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(id != 0, ECS_INTERNAL_ERROR, NULL);
ecs_assert(query != NULL, ECS_INTERNAL_ERROR, NULL);
flecs_poly_assert(query, ecs_query_t);
ecs_map_t *monitors = &world->monitors.monitors;
if (!ecs_map_is_init(monitors)) {
return;
}
ecs_monitor_t *m = ecs_map_get_deref(monitors, ecs_monitor_t, id);
if (!m) {
return;
}
int32_t i, count = ecs_vec_count(&m->queries);
ecs_query_t **queries = ecs_vec_first(&m->queries);
for (i = 0; i < count; i ++) {
if (queries[i] == query) {
ecs_vec_remove_t(&m->queries, ecs_query_t*, i);
count --;
break;
}
}
if (!count) {
ecs_vec_fini_t(&world->allocator, &m->queries, ecs_query_t*);
ecs_map_remove_free(monitors, id);
}
if (!ecs_map_count(monitors)) {
ecs_map_fini(monitors);
}
}
static
void flecs_init_store(
ecs_world_t *world)
{
ecs_os_memset(&world->store, 0, ECS_SIZEOF(ecs_store_t));
ecs_allocator_t *a = &world->allocator;
ecs_vec_init_t(a, &world->store.records, ecs_table_record_t, 0);
ecs_vec_init_t(a, &world->store.marked_ids, ecs_marked_id_t, 0);
ecs_vec_init_t(a, &world->store.deleted_components, ecs_entity_t, 0);
/* Initialize entity index */
flecs_entities_init(world);
/* Initialize table sparse set */
flecs_sparse_init_t(&world->store.tables,
a, &world->allocators.sparse_chunk, ecs_table_t);
/* Initialize table map */
flecs_table_hashmap_init(world, &world->store.table_map);
/* Initialize root table */
flecs_init_root_table(world);
}
static
void flecs_clean_tables(
ecs_world_t *world)
{
int32_t i, count = flecs_sparse_count(&world->store.tables);
/* Ensure that first table in sparse set has id 0. This is a dummy table
* that only exists so that there is no table with id 0 */
ecs_table_t *first = flecs_sparse_get_dense_t(&world->store.tables,
ecs_table_t, 0);
(void)first;
for (i = 1; i < count; i ++) {
ecs_table_t *t = flecs_sparse_get_dense_t(&world->store.tables,
ecs_table_t, i);
flecs_table_fini(world, t);
}
/* Free table types separately so that if application destructors rely on
* a type it's still valid. */
for (i = 1; i < count; i ++) {
ecs_table_t *t = flecs_sparse_get_dense_t(&world->store.tables,
ecs_table_t, i);
flecs_table_free_type(world, t);
}
/* Clear the root table */
if (count) {
flecs_table_reset(world, &world->store.root);
}
}
static
void flecs_fini_root_tables(
ecs_world_t *world,
ecs_id_record_t *idr,
bool fini_targets)
{
ecs_stage_t *stage0 = world->stages[0];
bool finished = false;
const ecs_size_t MAX_DEFERRED_DELETE_QUEUE_SIZE = 4096;
while (!finished) {
ecs_table_cache_iter_t it;
ecs_size_t queue_size = 0;
finished = true;
bool has_roots = flecs_table_cache_iter(&idr->cache, &it);
ecs_assert(has_roots == true, ECS_INTERNAL_ERROR, NULL);
(void)has_roots;
const ecs_table_record_t *tr;
while ((tr = flecs_table_cache_next(&it, ecs_table_record_t))) {
ecs_table_t *table = tr->hdr.table;
if (table->flags & EcsTableHasBuiltins) {
continue; /* Query out modules */
}
int32_t i, count = ecs_table_count(table);
const ecs_entity_t *entities = ecs_table_entities(table);
if (fini_targets) {
/* Only delete entities that are used as pair target. Iterate
* backwards to minimize moving entities around in table. */
for (i = count - 1; i >= 0; i --) {
ecs_record_t *r = flecs_entities_get(world, entities[i]);
ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(r->table == table, ECS_INTERNAL_ERROR, NULL);
if (ECS_RECORD_TO_ROW_FLAGS(r->row) & EcsEntityIsTarget) {
ecs_delete(world, entities[i]);
queue_size++;
/* Flush the queue before it grows too big: */
if(queue_size >= MAX_DEFERRED_DELETE_QUEUE_SIZE) {
finished = false;
break; /* restart iteration */
}
}
}
} else {
/* Delete remaining entities that are not in use (added to another
* entity). This limits table moves during cleanup and delays
* cleanup of tags. */
for (i = count - 1; i >= 0; i --) {
ecs_record_t *r = flecs_entities_get(world, entities[i]);
ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(r->table == table, ECS_INTERNAL_ERROR, NULL);
if (!ECS_RECORD_TO_ROW_FLAGS(r->row)) {
ecs_delete(world, entities[i]);
queue_size++;
/* Flush the queue before it grows too big: */
if(queue_size >= MAX_DEFERRED_DELETE_QUEUE_SIZE) {
finished = false;
break; /* restart iteration */
}
}
}
}
if(!finished) {
/* flush queue and restart iteration */
flecs_defer_end(world, stage0);
flecs_defer_begin(world, stage0);
break;
}
}
}
}
static
void flecs_fini_roots(
ecs_world_t *world)
{
ecs_id_record_t *idr = flecs_id_record_get(world, ecs_pair(EcsChildOf, 0));
/* Delete root entities that are not modules. This prioritizes deleting
* regular entities first, which reduces the chance of components getting
* destructed in random order because it got deleted before entities,
* thereby bypassing the OnDeleteTarget policy. */
flecs_defer_begin(world, world->stages[0]);
flecs_fini_root_tables(world, idr, true);
flecs_defer_end(world, world->stages[0]);
flecs_defer_begin(world, world->stages[0]);
flecs_fini_root_tables(world, idr, false);
flecs_defer_end(world, world->stages[0]);
}
static
void flecs_fini_store(ecs_world_t *world) {
flecs_clean_tables(world);
flecs_sparse_fini(&world->store.tables);
flecs_table_fini(world, &world->store.root);
flecs_entities_clear(world);
flecs_hashmap_fini(&world->store.table_map);
ecs_assert(ecs_vec_count(&world->store.marked_ids) == 0,
ECS_INTERNAL_ERROR, NULL);
ecs_assert(ecs_vec_count(&world->store.deleted_components) == 0,
ECS_INTERNAL_ERROR, NULL);
ecs_allocator_t *a = &world->allocator;
ecs_vec_fini_t(a, &world->store.records, ecs_table_record_t);
ecs_vec_fini_t(a, &world->store.marked_ids, ecs_marked_id_t);
ecs_vec_fini_t(a, &world->store.deleted_components, ecs_entity_t);
}
static
void flecs_world_allocators_init(
ecs_world_t *world)
{
ecs_world_allocators_t *a = &world->allocators;
flecs_allocator_init(&world->allocator);
ecs_map_params_init(&a->ptr, &world->allocator);
ecs_map_params_init(&a->query_table_list, &world->allocator);
flecs_ballocator_init_t(&a->query_table, ecs_query_cache_table_t);
flecs_ballocator_init_t(&a->query_table_match, ecs_query_cache_table_match_t);
flecs_ballocator_init_n(&a->graph_edge_lo, ecs_graph_edge_t, FLECS_HI_COMPONENT_ID);
flecs_ballocator_init_t(&a->graph_edge, ecs_graph_edge_t);
flecs_ballocator_init_t(&a->id_record, ecs_id_record_t);
flecs_ballocator_init_n(&a->id_record_chunk, ecs_id_record_t, FLECS_SPARSE_PAGE_SIZE);
flecs_ballocator_init_t(&a->table_diff, ecs_table_diff_t);
flecs_ballocator_init_n(&a->sparse_chunk, int32_t, FLECS_SPARSE_PAGE_SIZE);
flecs_ballocator_init_t(&a->hashmap, ecs_hashmap_t);
flecs_table_diff_builder_init(world, &world->allocators.diff_builder);
}
static
void flecs_world_allocators_fini(
ecs_world_t *world)
{
ecs_world_allocators_t *a = &world->allocators;
ecs_map_params_fini(&a->ptr);
ecs_map_params_fini(&a->query_table_list);
flecs_ballocator_fini(&a->query_table);
flecs_ballocator_fini(&a->query_table_match);
flecs_ballocator_fini(&a->graph_edge_lo);
flecs_ballocator_fini(&a->graph_edge);
flecs_ballocator_fini(&a->id_record);
flecs_ballocator_fini(&a->id_record_chunk);
flecs_ballocator_fini(&a->table_diff);
flecs_ballocator_fini(&a->sparse_chunk);
flecs_ballocator_fini(&a->hashmap);
flecs_table_diff_builder_fini(world, &world->allocators.diff_builder);
flecs_allocator_fini(&world->allocator);
}
#define ECS_STRINGIFY_INNER(x) #x
#define ECS_STRINGIFY(x) ECS_STRINGIFY_INNER(x)
static const char flecs_compiler_info[]
#if defined(__clang__)
= "clang " __clang_version__;
#elif defined(__GNUC__)
= "gcc " ECS_STRINGIFY(__GNUC__) "." ECS_STRINGIFY(__GNUC_MINOR__);
#elif defined(_MSC_VER)
= "msvc " ECS_STRINGIFY(_MSC_VER);
#elif defined(__TINYC__)
= "tcc " ECS_STRINGIFY(__TINYC__);
#else
= "unknown compiler";
#endif
static const char *flecs_addons_info[] = {
#ifdef FLECS_CPP
"FLECS_CPP",
#endif
#ifdef FLECS_MODULE
"FLECS_MODULE",
#endif
#ifdef FLECS_STATS
"FLECS_STATS",
#endif
#ifdef FLECS_METRICS
"FLECS_METRICS",
#endif
#ifdef FLECS_ALERTS
"FLECS_ALERTS",
#endif
#ifdef FLECS_SYSTEM
"FLECS_SYSTEM",
#endif
#ifdef FLECS_PIPELINE
"FLECS_PIPELINE",
#endif
#ifdef FLECS_TIMER
"FLECS_TIMER",
#endif
#ifdef FLECS_META
"FLECS_META",
#endif
#ifdef FLECS_UNITS
"FLECS_UNITS",
#endif
#ifdef FLECS_JSON
"FLECS_JSON",
#endif
#ifdef FLECS_DOC
"FLECS_DOC",
#endif
#ifdef FLECS_LOG
"FLECS_LOG",
#endif
#ifdef FLECS_JOURNAL
"FLECS_JOURNAL",
#endif
#ifdef FLECS_APP
"FLECS_APP",
#endif
#ifdef FLECS_OS_API_IMPL
"FLECS_OS_API_IMPL",
#endif
#ifdef FLECS_PARSER
"FLECS_PARSER",
#endif
#ifdef FLECS_QUERY_DSL
"FLECS_QUERY_DSL",
#endif
#ifdef FLECS_SCRIPT
"FLECS_SCRIPT",
#endif
#ifdef FLECS_HTTP
"FLECS_HTTP",
#endif
#ifdef FLECS_REST
"FLECS_REST",
#endif
NULL
};
static const ecs_build_info_t flecs_build_info = {
.compiler = flecs_compiler_info,
.addons = flecs_addons_info,
#ifdef FLECS_DEBUG
.debug = true,
#endif
#ifdef FLECS_SANITIZE
.sanitize = true,
#endif
#ifdef FLECS_PERF_TRACE
.perf_trace = true,
#endif
.version = FLECS_VERSION,
.version_major = FLECS_VERSION_MAJOR,
.version_minor = FLECS_VERSION_MINOR,
.version_patch = FLECS_VERSION_PATCH
};
static
void flecs_log_build_info(void) {
const ecs_build_info_t *bi = ecs_get_build_info();
ecs_assert(bi != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_trace("flecs version %s", bi->version);
ecs_trace("addons included in build:");
ecs_log_push();
const char **addon = bi->addons;
do {
ecs_trace(addon[0]);
} while ((++ addon)[0]);
ecs_log_pop();
if (bi->sanitize) {
ecs_trace("sanitize build, rebuild without FLECS_SANITIZE for (much) "
"improved performance");
} else if (bi->debug) {
ecs_trace("debug build, rebuild with NDEBUG or FLECS_NDEBUG for "
"improved performance");
} else {
ecs_trace("#[green]release#[reset] build");
}
ecs_trace("compiled with %s", bi->compiler);
}
/* -- Public functions -- */
const ecs_build_info_t* ecs_get_build_info(void) {
return &flecs_build_info;
}
const ecs_world_info_t* ecs_get_world_info(
const ecs_world_t *world)
{
world = ecs_get_world(world);
return &world->info;
}
ecs_world_t *ecs_mini(void) {
#ifdef FLECS_OS_API_IMPL
ecs_set_os_api_impl();
#endif
ecs_os_init();
ecs_trace("#[bold]bootstrapping world");
ecs_log_push();
ecs_trace("tracing enabled, call ecs_log_set_level(-1) to disable");
if (!ecs_os_has_heap()) {
ecs_abort(ECS_MISSING_OS_API, NULL);
}
if (!ecs_os_has_threading()) {
ecs_trace("threading unavailable, to use threads set OS API first (see examples)");
}
if (!ecs_os_has_time()) {
ecs_trace("time management not available");
}
flecs_log_build_info();
ecs_world_t *world = ecs_os_calloc_t(ecs_world_t);
ecs_assert(world != NULL, ECS_OUT_OF_MEMORY, NULL);
flecs_poly_init(world, ecs_world_t);
world->flags |= EcsWorldInit;
flecs_world_allocators_init(world);
ecs_allocator_t *a = &world->allocator;
ecs_map_init(&world->type_info, a);
ecs_map_init_w_params(&world->id_index_hi, &world->allocators.ptr);
world->id_index_lo = ecs_os_calloc_n(
ecs_id_record_t*, FLECS_HI_ID_RECORD_ID);
flecs_observable_init(&world->observable);
flecs_name_index_init(&world->aliases, a);
flecs_name_index_init(&world->symbols, a);
ecs_vec_init_t(a, &world->fini_actions, ecs_action_elem_t, 0);
ecs_vec_init_t(a, &world->component_ids, ecs_id_t, 0);
world->info.time_scale = 1.0;
if (ecs_os_has_time()) {
ecs_os_get_time(&world->world_start_time);
}
ecs_set_stage_count(world, 1);
ecs_default_lookup_path[0] = EcsFlecsCore;
ecs_set_lookup_path(world, ecs_default_lookup_path);
flecs_init_store(world);
flecs_bootstrap(world);
world->flags &= ~EcsWorldInit;
ecs_trace("world ready!");
ecs_log_pop();
return world;
}
ecs_world_t *ecs_init(void) {