-
Notifications
You must be signed in to change notification settings - Fork 23
/
pico_ecs.h
1553 lines (1251 loc) · 44.7 KB
/
pico_ecs.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
/**
@file pico_ecs.h
@brief A pure and simple ECS written in C99.
----------------------------------------------------------------------------
Licensing information at end of header
----------------------------------------------------------------------------
Features:
---------
- Written in C99
- Single header library for easy build system integration
- Excellent performance
- Pure ECS design (strict separation between data and logic)
- Simple and concise API
- Permissive license (zlib or public domain)
Summary:
--------
This library implements an ECS (Entity-Component-System). Entities
(sometimes called game objects) are defined by their components. For
example, an entity might have position, sprite, and physics components.
Systems operate on the components of entities that match the system's
requirements. Entities are matched to systems based upon which components
they have and also the system's matching crieria.
In the above example, a sprite renderer system would match entities having
poition and sprite components. The system would send the appropriate
geometry and texture ID to the game's graphics API.
Traditional game engines tightly couple state with logic. For example, in
C++ a game object typically has its own update method that operates
on that state. They also usually rely heavily on inheritance.
If the state specified by the class changes this could ripple through the
class, and perhaps subclasses as well. It could well be that the class no
longer belongs in the existing class hierarchy forcing even more revisions.
It could even be true that a class doesn't neatly fit into the inheritance
tree at all.
An ECS solves these problems while also granting more flexibility in
general. In an ECS there is a clear separation between data (components) and
logic (systems), which makes it possible to build complex simulations with
fewer assumptions about how that data will be used. In an ECS it effortless
to change functionality, by either adding or removing components from
entities, and/or by changing system requirements. Adding new logic is also
simple as well, just register a new system!
Please see the examples and unit tests for more details.
Usage:
------
To use this library in your project, add the following
> #define PICO_ECS_IMPLEMENTATION
> #include "pico_ecs.h"
to a source file (once), then simply include the header normally.
Constants:
--------
- PICO_ECS_MAX_COMPONENTS (default: 32)
- PICO_ECS_MAX_SYSTEMS (default: 16)
Must be defined before PICO_ECS_IMPLEMENTATION
Todo:
-----
- Better default assertion macro
- Port Rogue demo to Windows
*/
#ifndef PICO_ECS_H
#define PICO_ECS_H
#include <stdbool.h> // bool, true, false
#include <stddef.h> // size_t
#include <stdint.h> // uint32_t
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief ECS context
*/
typedef struct ecs_s ecs_t;
/**
* @brief ID used for entity and components
*/
typedef uint32_t ecs_id_t;
/**
* @brief NULL/invalid/undefined value
*/
#define ECS_NULL ((ecs_id_t)-1)
/**
* @brief Return code for update callback and calling functions
*/
typedef int8_t ecs_ret_t;
/**
* @brief Determine delta-time type
*/
#ifndef ECS_DT_TYPE
#define ECS_DT_TYPE double
#endif
typedef ECS_DT_TYPE ecs_dt_t;
/**
* @brief Creates an ECS instance.
*
* @param entity_count The inital number of pooled entities
* @param mem_ctx The Context for a custom allocator
*
* @returns An ECS instance or NULL if out of memory
*/
ecs_t* ecs_new(size_t entity_count, void* mem_ctx);
/**
* @brief Destroys an ECS instance
*
* @param ecs The ECS instance
*/
void ecs_free(ecs_t* ecs);
/**
* @brief Removes all entities from the ECS, preserving systems and components.
*/
void ecs_reset(ecs_t* ecs);
/**
* @brief Called when a component is created (via ecs_add)
*
* @param ecs The ECS instance
* @param entity_id The entity being constructed
* @param ptr The pointer to the component
* @param udata The user data passed to the callback
*/
typedef void (*ecs_constructor_fn)(ecs_t* ecs,
ecs_id_t entity_id,
void* ptr,
void* args);
/**
* @brief Called when a component is destroyed (via ecs_remove or ecs_destroy)
*
* @param ecs The ECS instance
* @param entity_id The entity being destoryed
* @param ptr The pointer to the component
* @param udata The user data passed to the callback
*/
typedef void (*ecs_destructor_fn)(ecs_t* ecs,
ecs_id_t entity_id,
void* ptr);
/**
* @brief Registers a component
*
* Registers a component with the specfied size in bytes. Components define the
* game state (usually contained within structs) and are manipulated by systems.
*
* @param ecs The ECS instance
* @param size The number of bytes to allocate for each component instance
* @param constructor Called when a component is created (disabled if NULL)
* @param destructor Called when a component is destroyed (disabled if NULL)
* @param udata Data passed to callbacks (can be NULL)
* @returns The component's ID
*/
ecs_id_t ecs_register_component(ecs_t* ecs,
size_t size,
ecs_constructor_fn constructor,
ecs_destructor_fn destructor);
/**
* @brief System update callback
*
* Systems implement the core logic of an ECS by manipulating entities
* and components.
*
* @param ecs The ECS instance
* @param entities An array of entity IDs managed by the system
* @param entity_count The number of entities in the array
* @param dt The time delta
* @param udata The user data associated with the system
*/
typedef ecs_ret_t (*ecs_system_fn)(ecs_t* ecs,
ecs_id_t* entities,
int entity_count,
ecs_dt_t dt,
void* udata);
/**
* @brief Called when an entity is added to a system
*
* @param ecs The ECS instance
* @param entity_id The enitty being added
* @param udata The user data passed to the callback
*/
typedef void (*ecs_added_fn)(ecs_t* ecs, ecs_id_t entity_id, void* udata);
/**
* @brief Called when an entity is removed from a system
*
* @param ecs The ECS instance
* @param entity_id The enitty being removed
* @param udata The user data passed to the callback
*/
typedef void (*ecs_removed_fn)(ecs_t* ecs, ecs_id_t entity_id, void* udata);
/**
* @brief Registers a system
*
* Registers a system with the specified parameters. Systems contain the
* core logic of a game by manipulating game state as defined by components.
*
* @param ecs The ECS instance
* @param system_cb Callback that is fired every update
* @param add_cb Called when an entity is added to the system (can be NULL)
* @param remove_cb Called when an entity is removed from the system (can be NULL)
* @param udata The user data passed to the callbacks
* @returns The system's ID
*/
ecs_id_t ecs_register_system(ecs_t* ecs,
ecs_system_fn system_cb,
ecs_added_fn add_cb,
ecs_removed_fn remove_cb,
void* udata);
/**
* @brief Determines which components are available to the specified system.
*
* @param ecs The ECS instance
* @param sys_id The target system ID
* @param comp_id The component ID
*/
void ecs_require_component(ecs_t* ecs, ecs_id_t sys_id, ecs_id_t comp_id);
/**
* @brief Excludes entities having the specified component from being added to
* the target system.
*
* @param ecs The ECS instance
* @param sys_id The target system ID
* @param comp_id The component ID tp exclude
*/
void ecs_exclude_component(ecs_t* ecs, ecs_id_t sys_id, ecs_id_t comp_id);
/**
* @brief Enables a system
*
* @param ecs The ECS instance
* @param sys_id The specified system ID
*/
void ecs_enable_system(ecs_t* ecs, ecs_id_t sys_id);
/**
* @brief Disables a system
*
* @param ecs The ECS instance
* @param sys_id The specified system ID
*/
void ecs_disable_system(ecs_t* ecs, ecs_id_t sys_id);
/**
* @brief Creates an entity
*
* @param ecs The ECS instance
*
* @returns The new entity ID
*/
ecs_id_t ecs_create(ecs_t* ecs);
/**
* @brief Returns true if the entity is currently active
*
* @param ecs The ECS instance
* @param entity_id The target entity
*/
bool ecs_is_ready(ecs_t* ecs, ecs_id_t entity_id);
/**
* @brief Destroys an entity
*
* Destroys an entity, releasing resources and returning it to the pool.
*
* @param ecs The ECS instance
* @param entity_id The ID of the entity to destroy
*/
void ecs_destroy(ecs_t* ecs, ecs_id_t entity_id);
/**
* @brief Test if entity has the specified component
*
* @param ecs The ECS instance
* @param entity_id The entity ID
* @param comp_id The component ID
*
* @returns True if the entity has the component
*/
bool ecs_has(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id);
/**
* @brief Adds a component instance to an entity
*
* @param ecs The ECS instance
* @param entity_id The entity ID
* @param comp_id The component ID
*
* @returns The component instance
*/
void* ecs_add(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id, void* args);
/**
* @brief Gets a component instance associated with an entity
*
* @param ecs The ECS instance
* @param entity_id The entity ID
* @param comp_id The component ID
*
* @returns The component instance
*/
void* ecs_get(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id);
/**
* @brief Removes a component instance from an entity
*
* @param ecs The ECS instance
* @param entity_id The entity ID
* @param comp_id The component ID
*/
void ecs_remove(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id);
/**
* @brief Queues an entity for destruction at the end of system execution
*
* Queued entities are destroyed after the curent iteration.
*
* @param ecs The ECS instance
* @param entity_id The ID of the entity to destroy
*/
void ecs_queue_destroy(ecs_t* ecs, ecs_id_t entity_id);
/**
* @brief Queues a component for removable
*
* Queued entity/component pairs that will be deleted after the current system
* returns
*
* @param ecs The ECS instance
* @param entity_id The ID of the entity that has the component
* @param comp_id The component to remove
*/
void ecs_queue_remove(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id);
/**
* @brief Update an individual system
*
* This function should be called once per frame.
*
* @param ecs The ECS instance
* @param sys_id The system to update
* @param dt The time delta
*/
ecs_ret_t ecs_update_system(ecs_t* ecs, ecs_id_t sys_id, ecs_dt_t dt);
/**
* @brief Updates all systems
*
* This function should be called once per frame.
*
* @param ecs The ECS instance
* @param dt The time delta
*/
ecs_ret_t ecs_update_systems(ecs_t* ecs, ecs_dt_t dt);
#ifdef __cplusplus
}
#endif
#endif // PICO_ECS_H
#ifdef PICO_ECS_IMPLEMENTATION // Define once
#include <stdint.h> // uint32_t
#include <stdlib.h> // malloc, realloc, free
#include <string.h> // memcpy, memset
#ifndef PICO_ECS_MAX_COMPONENTS
#define PICO_ECS_MAX_COMPONENTS 32
#endif
#ifndef PICO_ECS_MAX_SYSTEMS
#define PICO_ECS_MAX_SYSTEMS 16
#endif
#ifdef NDEBUG
#define PICO_ECS_ASSERT(expr) ((void)0)
#else
#ifndef PICO_ECS_ASSERT
#include <assert.h>
#define PICO_ECS_ASSERT(expr) (assert(expr))
#endif
#endif
#if !defined(PICO_ECS_MALLOC) || !defined(PICO_ECS_REALLOC) || !defined(PICO_ECS_FREE)
#include <stdlib.h>
#define PICO_ECS_MALLOC(size, ctx) (malloc(size))
#define PICO_ECS_REALLOC(ptr, size, ctx) (realloc(ptr, size))
#define PICO_ECS_FREE(ptr, ctx) (free(ptr))
#endif
/*=============================================================================
* Internal aliases
*============================================================================*/
#define ECS_ASSERT PICO_ECS_ASSERT
#define ECS_MAX_COMPONENTS PICO_ECS_MAX_COMPONENTS
#define ECS_MAX_SYSTEMS PICO_ECS_MAX_SYSTEMS
#define ECS_MALLOC PICO_ECS_MALLOC
#define ECS_REALLOC PICO_ECS_REALLOC
#define ECS_FREE PICO_ECS_FREE
/*=============================================================================
* Internal data structures
*============================================================================*/
#if ECS_MAX_COMPONENTS <= 32
typedef uint32_t ecs_bitset_t;
#elif ECS_MAX_COMPONENTS <= 64
typedef uint64_t ecs_bitset_t;
#else
#define ECS_BITSET_WIDTH 64
#define ECS_BITSET_SIZE (((ECS_MAX_COMPONENTS - 1) / ECS_BITSET_WIDTH) + 1)
typedef struct
{
uint64_t array[ECS_BITSET_SIZE];
} ecs_bitset_t;
#endif // ECS_MAX_COMPONENTS
// Data-structure for a packed array implementation that provides O(1) functions
// for adding, removing, and accessing entity IDs
typedef struct
{
size_t capacity;
size_t size;
size_t* sparse;
ecs_id_t* dense;
} ecs_sparse_set_t;
// Data-structure for an ID pool that provides O(1) operations for pooling IDs
typedef struct
{
size_t capacity;
size_t size;
ecs_id_t* array;
} ecs_stack_t;
typedef struct
{
size_t capacity;
size_t count;
size_t size;
void* data;
} ecs_array_t;
typedef struct
{
ecs_bitset_t comp_bits;
bool ready;
} ecs_entity_t;
typedef struct
{
ecs_constructor_fn constructor;
ecs_destructor_fn destructor;
} ecs_comp_t;
typedef struct
{
bool active;
ecs_sparse_set_t entity_ids;
ecs_system_fn system_cb;
ecs_added_fn add_cb;
ecs_removed_fn remove_cb;
ecs_bitset_t require_bits;
ecs_bitset_t exclude_bits;
void* udata;
} ecs_sys_t;
struct ecs_s
{
ecs_stack_t entity_pool;
ecs_stack_t destroy_queue;
ecs_stack_t remove_queue;
ecs_entity_t* entities;
size_t entity_count;
ecs_comp_t comps[ECS_MAX_COMPONENTS];
ecs_array_t comp_arrays[ECS_MAX_COMPONENTS];
size_t comp_count;
ecs_sys_t systems[ECS_MAX_SYSTEMS];
size_t system_count;
void* mem_ctx;
};
/*=============================================================================
* Internal realloc wrapper
*============================================================================*/
void* ecs_realloc_zero(ecs_t* ecs, void* ptr, size_t old_size, size_t new_size);
/*=============================================================================
* Internal functions to flush destroyed entities and removed component
*============================================================================*/
static void ecs_flush_destroyed(ecs_t* ecs);
static void ecs_flush_removed(ecs_t* ecs);
/*=============================================================================
* Internal bit set functions
*============================================================================*/
static inline void ecs_bitset_flip(ecs_bitset_t* set, int bit, bool on);
static inline bool ecs_bitset_is_zero(ecs_bitset_t* set);
static inline bool ecs_bitset_test(ecs_bitset_t* set, int bit);
static inline ecs_bitset_t ecs_bitset_and(ecs_bitset_t* set1, ecs_bitset_t* set2);
static inline ecs_bitset_t ecs_bitset_or(ecs_bitset_t* set1, ecs_bitset_t* set2);
static inline ecs_bitset_t ecs_bitset_not(ecs_bitset_t* set);
static inline bool ecs_bitset_equal(ecs_bitset_t* set1, ecs_bitset_t* set2);
static inline bool ecs_bitset_true(ecs_bitset_t* set);
/*=============================================================================
* Internal sparse set functions
*============================================================================*/
static void ecs_sparse_set_init(ecs_t* ecs, ecs_sparse_set_t* set, size_t capacity);
static void ecs_sparse_set_free(ecs_t* ecs, ecs_sparse_set_t* set);
static bool ecs_sparse_set_add(ecs_t* ecs, ecs_sparse_set_t* set, ecs_id_t id);
static size_t ecs_sparse_set_find(ecs_sparse_set_t* set, ecs_id_t id);
static bool ecs_sparse_set_remove(ecs_sparse_set_t* set, ecs_id_t id);
/*=============================================================================
* Internal system entity add/remove functions
*============================================================================*/
static bool ecs_entity_system_test(ecs_bitset_t* require_bits,
ecs_bitset_t* exclude_bits,
ecs_bitset_t* entity_bits);
/*=============================================================================
* Internal ID pool functions
*============================================================================*/
static void ecs_stack_init(ecs_t* ecs, ecs_stack_t* pool, int capacity);
static void ecs_stack_free(ecs_t* ecs, ecs_stack_t* pool);
static void ecs_stack_push(ecs_t* ecs, ecs_stack_t* pool, ecs_id_t id);
static ecs_id_t ecs_stack_pop(ecs_stack_t* pool);
static int ecs_stack_size(ecs_stack_t* pool);
/*=============================================================================
* Internal array functions
*============================================================================*/
static void ecs_array_init(ecs_t* ecs, ecs_array_t* array, size_t size, size_t capacity);
static void ecs_array_free(ecs_t* ecs, ecs_array_t* array);
static void ecs_array_resize(ecs_t* ecs, ecs_array_t* array, size_t capacity);
/*=============================================================================
* Internal validation functions
*============================================================================*/
#ifndef NDEBUG
static bool ecs_is_not_null(void* ptr);
static bool ecs_is_valid_component_id(ecs_id_t id);
static bool ecs_is_valid_system_id(ecs_id_t id);
static bool ecs_is_entity_ready(ecs_t* ecs, ecs_id_t entity_id);
static bool ecs_is_component_ready(ecs_t* ecs, ecs_id_t comp_id);
static bool ecs_is_system_ready(ecs_t* ecs, ecs_id_t sys_id);
#endif // NDEBUG
/*=============================================================================
* Public API implementation
*============================================================================*/
ecs_t* ecs_new(size_t entity_count, void* mem_ctx)
{
ECS_ASSERT(entity_count > 0);
ecs_t* ecs = (ecs_t*)ECS_MALLOC(sizeof(ecs_t), mem_ctx);
// Out of memory
if (NULL == ecs)
return NULL;
memset(ecs, 0, sizeof(ecs_t));
ecs->entity_count = entity_count;
ecs->mem_ctx = mem_ctx;
// Initialize entity pool and queues
ecs_stack_init(ecs, &ecs->entity_pool, entity_count);
ecs_stack_init(ecs, &ecs->destroy_queue, entity_count);
ecs_stack_init(ecs, &ecs->remove_queue, entity_count * 2);
// Allocate entity array
ecs->entities = (ecs_entity_t*)ECS_MALLOC(ecs->entity_count * sizeof(ecs_entity_t),
ecs->mem_ctx);
// Zero entity array
memset(ecs->entities, 0, ecs->entity_count * sizeof(ecs_entity_t));
// Pre-populate the the ID pool
for (ecs_id_t id = 0; id < entity_count; id++)
{
ecs_stack_push(ecs, &ecs->entity_pool, id);
}
return ecs;
}
void ecs_free(ecs_t* ecs)
{
ECS_ASSERT(ecs_is_not_null(ecs));
for (ecs_id_t entity_id = 0; entity_id < ecs->entity_count; entity_id++)
{
if (ecs->entities[entity_id].ready)
ecs_destroy(ecs, entity_id);
}
ecs_stack_free(ecs, &ecs->entity_pool);
ecs_stack_free(ecs, &ecs->destroy_queue);
ecs_stack_free(ecs, &ecs->remove_queue);
for (ecs_id_t comp_id = 0; comp_id < ecs->comp_count; comp_id++)
{
ecs_array_t* comp_array = &ecs->comp_arrays[comp_id];
ecs_array_free(ecs, comp_array);
}
for (ecs_id_t sys_id = 0; sys_id < ecs->system_count; sys_id++)
{
ecs_sys_t* sys = &ecs->systems[sys_id];
ecs_sparse_set_free(ecs, &sys->entity_ids);
}
ECS_FREE(ecs->entities, ecs->mem_ctx);
ECS_FREE(ecs, ecs->mem_ctx);
}
void ecs_reset(ecs_t* ecs)
{
ECS_ASSERT(ecs_is_not_null(ecs));
for (ecs_id_t entity_id = 0; entity_id < ecs->entity_count; entity_id++)
{
if (ecs->entities[entity_id].ready)
ecs_destroy(ecs, entity_id);
}
ecs->entity_pool.size = 0;
ecs->destroy_queue.size = 0;
ecs->remove_queue.size = 0;
memset(ecs->entities, 0, ecs->entity_count * sizeof(ecs_entity_t));
for (ecs_id_t entity_id = 0; entity_id < ecs->entity_count; entity_id++)
{
ecs_stack_push(ecs, &ecs->entity_pool, entity_id);
}
for (ecs_id_t sys_id = 0; sys_id < ecs->system_count; sys_id++)
{
ecs->systems[sys_id].entity_ids.size = 0;
}
}
ecs_id_t ecs_register_component(ecs_t* ecs,
size_t size,
ecs_constructor_fn constructor,
ecs_destructor_fn destructor)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs->comp_count < ECS_MAX_COMPONENTS);
ECS_ASSERT(size > 0);
ecs_id_t comp_id = ecs->comp_count;
ecs_array_t* comp_array = &ecs->comp_arrays[comp_id];
ecs_array_init(ecs, comp_array, size, ecs->entity_count);
ecs->comps[comp_id].constructor = constructor;
ecs->comps[comp_id].destructor = destructor;
ecs->comp_count++;
return comp_id;
}
ecs_id_t ecs_register_system(ecs_t* ecs,
ecs_system_fn system_cb,
ecs_added_fn add_cb,
ecs_removed_fn remove_cb,
void* udata)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs->system_count < ECS_MAX_SYSTEMS);
ECS_ASSERT(NULL != system_cb);
ecs_id_t sys_id = ecs->system_count;
ecs_sys_t* sys = &ecs->systems[sys_id];
ecs_sparse_set_init(ecs, &sys->entity_ids, ecs->entity_count);
sys->active = true;
sys->system_cb = system_cb;
sys->add_cb = add_cb;
sys->remove_cb = remove_cb;
sys->udata = udata;
ecs->system_count++;
return sys_id;
}
void ecs_require_component(ecs_t* ecs, ecs_id_t sys_id, ecs_id_t comp_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_system_id(sys_id));
ECS_ASSERT(ecs_is_valid_component_id(comp_id));
ECS_ASSERT(ecs_is_system_ready(ecs, sys_id));
ECS_ASSERT(ecs_is_component_ready(ecs, comp_id));
// Set system component bit for the specified component
ecs_sys_t* sys = &ecs->systems[sys_id];
ecs_bitset_flip(&sys->require_bits, comp_id, true);
}
void ecs_exclude_component(ecs_t* ecs, ecs_id_t sys_id, ecs_id_t comp_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_system_id(sys_id));
ECS_ASSERT(ecs_is_valid_component_id(comp_id));
ECS_ASSERT(ecs_is_system_ready(ecs, sys_id));
ECS_ASSERT(ecs_is_component_ready(ecs, comp_id));
// Set system component bit for the specified component
ecs_sys_t* sys = &ecs->systems[sys_id];
ecs_bitset_flip(&sys->exclude_bits, comp_id, true);
}
void ecs_enable_system(ecs_t* ecs, ecs_id_t sys_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_system_id(sys_id));
ECS_ASSERT(ecs_is_system_ready(ecs, sys_id));
ecs_sys_t* sys = &ecs->systems[sys_id];
sys->active = true;
}
void ecs_disable_system(ecs_t* ecs, ecs_id_t sys_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_system_id(sys_id));
ECS_ASSERT(ecs_is_system_ready(ecs, sys_id));
ecs_sys_t* sys = &ecs->systems[sys_id];
sys->active = false;
}
ecs_id_t ecs_create(ecs_t* ecs)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ecs_stack_t* pool = &ecs->entity_pool;
// If pool is empty, increase the number of entity IDs
if (0 == ecs_stack_size(pool))
{
size_t old_count = ecs->entity_count;
size_t new_count = old_count + (old_count / 2) + 2;
// Reallocates entities and zeros new ones
ecs->entities = (ecs_entity_t*)ecs_realloc_zero(ecs, ecs->entities,
old_count * sizeof(ecs_entity_t),
new_count * sizeof(ecs_entity_t));
// Push new entity IDs into the pool
for (ecs_id_t id = old_count; id < new_count; id++)
{
ecs_stack_push(ecs, pool, id);
}
// Update entity count
ecs->entity_count = new_count;
}
ecs_id_t entity_id = ecs_stack_pop(pool);
ecs->entities[entity_id].ready = true;
return entity_id;
}
bool ecs_is_ready(ecs_t* ecs, ecs_id_t entity_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
return ecs->entities[entity_id].ready;
}
void ecs_destroy(ecs_t* ecs, ecs_id_t entity_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_entity_ready(ecs, entity_id));
// Load entity
ecs_entity_t* entity = &ecs->entities[entity_id];
// Remove entity from systems
for (ecs_id_t sys_id = 0; sys_id < ecs->system_count; sys_id++)
{
ecs_sys_t* sys = &ecs->systems[sys_id];
// Just attempting to remove the entity from the sparse set is faster
// than calling ecs_entity_system_test
if (ecs_sparse_set_remove(&sys->entity_ids, entity_id))
{
if (sys->remove_cb)
sys->remove_cb(ecs, entity_id, sys->udata);
}
}
// Push entity ID back into pool
ecs_stack_t* pool = &ecs->entity_pool;
ecs_stack_push(ecs, pool, entity_id);
// Loop through components and call the destructors
for (ecs_id_t comp_id = 0; comp_id < ecs->comp_count; comp_id++)
{
if (ecs_bitset_test(&entity->comp_bits, comp_id))
{
ecs_comp_t* comp = &ecs->comps[comp_id];
if (comp->destructor)
{
void* ptr = ecs_get(ecs, entity_id, comp_id);
comp->destructor(ecs, entity_id, ptr);
}
}
}
// Reset entity (sets bitset to 0 and ready to false)
memset(entity, 0, sizeof(ecs_entity_t));
}
bool ecs_has(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_component_id(comp_id));
ECS_ASSERT(ecs_is_entity_ready(ecs, entity_id));
// Load entity
ecs_entity_t* entity = &ecs->entities[entity_id];
// Return true if the component belongs to the entity
return ecs_bitset_test(&entity->comp_bits, comp_id);
}
void* ecs_get(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_component_id(comp_id));
ECS_ASSERT(ecs_is_component_ready(ecs, comp_id));
ECS_ASSERT(ecs_is_entity_ready(ecs, entity_id));
// Return pointer to component
// eid0, eid1 eid2, ...
// [comp0, comp1, comp2, ...]
ecs_array_t* comp_array = &ecs->comp_arrays[comp_id];
return (char*)comp_array->data + (comp_array->size * entity_id);
}
void* ecs_add(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id, void* args)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_component_id(comp_id));
ECS_ASSERT(ecs_is_entity_ready(ecs, entity_id));
ECS_ASSERT(ecs_is_component_ready(ecs, comp_id));
// Load entity
ecs_entity_t* entity = &ecs->entities[entity_id];
// Load component
ecs_array_t* comp_array = &ecs->comp_arrays[comp_id];
ecs_comp_t* comp = &ecs->comps[comp_id];
// Grow the component array
ecs_array_resize(ecs, comp_array, entity_id);
// Get pointer to component
void* ptr = ecs_get(ecs, entity_id, comp_id);
// Zero component
memset(ptr, 0, comp_array->size);
// Call constructor
if (comp->constructor)
comp->constructor(ecs, entity_id, ptr, args);
// Set entity component bit that determines which systems this entity
// belongs to
ecs_bitset_flip(&entity->comp_bits, comp_id, true);
// Add entity to systems
for (ecs_id_t sys_id = 0; sys_id < ecs->system_count; sys_id++)
{
ecs_sys_t* sys = &ecs->systems[sys_id];
if (ecs_entity_system_test(&sys->require_bits, &sys->exclude_bits, &entity->comp_bits))
{
if (ecs_sparse_set_add(ecs, &sys->entity_ids, entity_id))
{
if (sys->add_cb)
sys->add_cb(ecs, entity_id, sys->udata);
}
}
}
// Return component
return ptr;
}
void ecs_remove(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_component_id(comp_id));
ECS_ASSERT(ecs_is_component_ready(ecs, comp_id));
ECS_ASSERT(ecs_is_entity_ready(ecs, entity_id));
// Load entity
ecs_entity_t* entity = &ecs->entities[entity_id];
// Create bit mask with comp bit flipped on
ecs_bitset_t comp_bit;
memset(&comp_bit, 0, sizeof(ecs_bitset_t));
ecs_bitset_flip(&comp_bit, comp_id, true);
for (ecs_id_t sys_id = 0; sys_id < ecs->system_count; sys_id++)
{
ecs_sys_t* sys = &ecs->systems[sys_id];
if (ecs_entity_system_test(&sys->require_bits, &sys->exclude_bits, &comp_bit))
{
if (ecs_sparse_set_remove(&sys->entity_ids, entity_id))
{
if (sys->remove_cb)
sys->remove_cb(ecs, entity_id, sys->udata);
}
}
}
ecs_comp_t* comp = &ecs->comps[comp_id];
if (comp->destructor)
{
void* ptr = ecs_get(ecs, entity_id, comp_id);
comp->destructor(ecs, entity_id, ptr);
}
// Reset the relevant component mask bit
ecs_bitset_flip(&entity->comp_bits, comp_id, false);
}
void ecs_queue_destroy(ecs_t* ecs, ecs_id_t entity_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_entity_ready(ecs, entity_id));
ecs_stack_push(ecs, &ecs->destroy_queue, entity_id);
}
void ecs_queue_remove(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_entity_ready(ecs, entity_id));
ECS_ASSERT(ecs_has(ecs, entity_id, comp_id));
ecs_stack_push(ecs, &ecs->remove_queue, entity_id);
ecs_stack_push(ecs, &ecs->remove_queue, comp_id);
}
ecs_ret_t ecs_update_system(ecs_t* ecs, ecs_id_t sys_id, ecs_dt_t dt)
{
ECS_ASSERT(ecs_is_not_null(ecs));
ECS_ASSERT(ecs_is_valid_system_id(sys_id));
ECS_ASSERT(ecs_is_system_ready(ecs, sys_id));
ECS_ASSERT(dt >= 0.0f);
ecs_sys_t* sys = &ecs->systems[sys_id];
if (!sys->active)