-
Notifications
You must be signed in to change notification settings - Fork 3
/
diana.c
1393 lines (1149 loc) · 37.8 KB
/
diana.c
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
// vim: ts=2:sw=2:noexpandtab
#include "diana.h"
#include <string.h>
#include <limits.h>
static int _malloc(struct diana *diana, size_t size, void ** r);
static int _realloc(struct diana *diana, void *ptr, size_t oldSize, size_t newSize, void ** r);
static int _free(struct diana *diana, void *ptr);
/* UNUSED
// ============================================================================
// VECTOR
struct _vector {
size_t size;
unsigned int capacity;
unsigned int length;
void *data;
};
void _vector_reserve(struct diana *diana, struct _vector *v, unsigned int capacity) {
v->data = _realloc(diana, v->data, v->size * v->capacity, v->size * capacity);
v->capacity = capacity;
}
void _vector_init(struct diana *diana, struct _vector *v, size_t size, unsigned int capacity) {
memset(v, 0, sizeof(*v));
v->size = size;
_vector_reserve(diana, v, capacity);
}
void _vector_push(struct diana *diana, struct _vector *v, void *data) {
unsigned int length = ++v->length;
if(length > v->capacity) {
_vector_reserve(diana, v, (length + 1) * 1.5);
}
if(data != NULL) {
memcpy((unsigned char *)v->data + v->length * v->size, data, v->size);
}
}
int _vector_pop(struct diana *diana, struct _vector *v, void *r) {
if(v->length == 0) {
return 0;
}
if(r != NULL) {
memcpy(r, (unsigned char *)v->data + --v->length * v->size, v->size);
}
return 1;
}
*/
// ============================================================================
// SPARSE INTEGER SET
// - faster clearing
// - easy tracking of population
// - can be used to 'pop' elements off
// - and iterating over
// - used for delaying entity adding, enabling, disabled and deleting
// - used by each system to track entities it has
struct _sparseIntegerSet {
unsigned int *dense;
unsigned int *sparse;
unsigned int population;
unsigned int capacity;
};
/* UNUSED
static int _sparseIntegerSet_contains(struct diana *diana, struct _sparseIntegerSet *is, unsigned int i) {
if(i >= is->capacity) {
return 0;
}
unsigned int a = is->sparse[i];
unsigned int n = is->population;
return a < n && is->dense[a] == i;
}
*/
static int _sparseIntegerSet_insert(struct diana *diana, struct _sparseIntegerSet *is, unsigned int i) {
if(i >= is->capacity) {
unsigned int newCapacity = (i + 1) * 1.5;
_realloc(diana, is->dense, is->capacity * sizeof(unsigned int), newCapacity * sizeof(unsigned int), (void **)&is->dense);
_realloc(diana, is->sparse, is->capacity * sizeof(unsigned int), newCapacity * sizeof(unsigned int), (void **)&is->sparse);
is->capacity = newCapacity;
}
unsigned int a = is->sparse[i];
unsigned int n = is->population;
if(a >= n || is->dense[a] != i) {
is->sparse[i] = n;
is->dense[n] = i;
is->population = n + 1;
return 0;
}
return 1;
}
static int _sparseIntegerSet_delete(struct diana *diana, struct _sparseIntegerSet *is, unsigned int i) {
if(i >= is->capacity || is->population == 0) {
return 0;
}
unsigned int a = is->sparse[i];
unsigned int n = is->population - 1;
if(a <= n || is->dense[a] == i) {
unsigned int e = is->dense[n];
is->population = n;
is->dense[n] = e;
is->sparse[e] = a;
return 1;
}
return 0;
}
static void _sparseIntegerSet_clear(struct diana *diana, struct _sparseIntegerSet *is) {
is->population = 0;
}
static int _sparseIntegerSet_isEmpty(struct diana *diana, struct _sparseIntegerSet *is) {
return is->population == 0;
}
static unsigned int _sparseIntegerSet_pop(struct diana *diana, struct _sparseIntegerSet *is) {
if(is->population >= 1) {
return is->dense[--is->population];
}
return UINT_MAX;
}
static void _sparseIntegerSet_free(struct diana *diana, struct _sparseIntegerSet *is) {
_free(diana, is->dense);
_free(diana, is->sparse);
memset(is, 0, sizeof(*is));
}
// ============================================================================
// DENSE INTEGER SET
// - more memory effecient
// - used for tracking components each entity is using
// - and for the active entities
static int _bits_set(unsigned char *bytes, unsigned int bit) {
int r = !!(bytes[bit >> 3] & (1 << (bit & 7)));
bytes[bit >> 3] |= (1 << (bit & 7));
return r;
}
static int _bits_isSet(unsigned char *bytes, unsigned int bit) {
return !!(bytes[bit >> 3] & (1 << (bit & 7)));
}
static int _bits_clear(unsigned char *bytes, unsigned int bit) {
int r = !!(bytes[bit >> 3] & (1 << (bit & 7)));
bytes[bit >> 3] &= ~(1 << (bit & 7));
return r;
}
// ============================================================================
struct _denseIntegerSet {
unsigned char *bytes;
unsigned int capacity;
};
/* UNUSED
static int _denseIntegerSet_contains(struct diana *diana, struct _denseIntegerSet *is, unsigned int i) {
return i < is->capacity && _bits_isSet(is->bytes, i);
}
*/
static unsigned int _denseIntegerSet_insert(struct diana *diana, struct _denseIntegerSet *is, unsigned int i) {
if(i >= is->capacity) {
unsigned int newCapacity = (i + 1) * 1.5;
_realloc(diana, is->bytes, (is->capacity + 7) >> 3, (newCapacity + 7) >> 3, (void **)&is->bytes);
is->capacity = newCapacity;
}
return _bits_set(is->bytes, i);
}
static unsigned int _denseIntegerSet_delete(struct diana *diana, struct _denseIntegerSet *is, unsigned int i) {
if(i < is->capacity) {
return _bits_clear(is->bytes, i);
}
return 0;
}
/* UNUSED
static void _denseIntegerSet_clear(struct diana *diana, struct _denseIntegerSet *is) {
memset(is->bytes, 0, (is->capacity + 7) >> 3);
}
static int _denseIntegerSet_isEmpty(struct diana *diana, struct _denseIntegerSet *is) {
unsigned int n = (is->capacity + 7) >> 3, i = 0;
for(; i < n; i++) {
if(is->bytes[i]) {
return 0;
}
}
return 1;
}
*/
static void _denseIntegerSet_free(struct diana *diana, struct _denseIntegerSet *is) {
_free(diana, is->bytes);
memset(is, 0, sizeof(*is));
}
// ============================================================================
// PRIMARY DATA
struct _componentBag {
unsigned int count;
unsigned int *indexes;
};
struct _component {
const char *name;
size_t size;
size_t offset;
unsigned int flags;
void **data;
struct _sparseIntegerSet freeDataIndexes;
unsigned int nextDataIndex;
#if DL_COMPUTE
void (*compute)(struct diana *, void *, unsigned int entity, unsigned int index, void *);
void *userData;
struct _sparseIntegerSet componentsToDirty;
#endif
};
static void _component_free(struct diana *diana, struct _component *component) {
unsigned int i = 0;
_free(diana, (void *)component->name);
for(i = 0; i < component->nextDataIndex; i++) {
_free(diana, component->data[i]);
}
_free(diana, component->data);
_sparseIntegerSet_free(diana, &component->freeDataIndexes);
#if DL_COMPUTE
_sparseIntegerSet_free(diana, &component->componentsToDirty);
#endif
memset(component, 0, sizeof(*component));
}
struct _system {
const char *name;
unsigned int flags;
void *userData;
void (*starting)(struct diana *, void *user_data);
void (*process)(struct diana *, void *user_data, unsigned int entity, float delta);
void (*ending)(struct diana *, void *user_data);
void (*subscribed)(struct diana *, void *user_data, unsigned int entity);
void (*unsubscribed)(struct diana *, void *user_data, unsigned int entity);
struct _sparseIntegerSet watch;
struct _sparseIntegerSet exclude;
struct _denseIntegerSet entities;
};
static void _system_free(struct diana *diana, struct _system *system) {
_free(diana, (void *)system->name);
_sparseIntegerSet_free(diana, &system->watch);
_sparseIntegerSet_free(diana, &system->exclude);
_denseIntegerSet_free(diana, &system->entities);
memset(system, 0, sizeof(*system));
}
struct _manager {
const char *name;
unsigned int flags;
void *userData;
void (*added)(struct diana *diana, void *userData, unsigned int entity);
void (*enabled)(struct diana *diana, void *userData, unsigned int entity);
void (*disabled)(struct diana *diana, void *userData, unsigned int entity);
void (*deleted)(struct diana *diana, void *userData, unsigned int entity);
};
static void _manager_free(struct diana *diana, struct _manager *manager) {
_free(diana, (void *)manager->name);
memset(manager, 0, sizeof(*manager));
}
#if DL_COMPUTE
struct _computingComponentStack {
struct _computingComponentStack *previous;
unsigned int component;
};
#endif
struct diana {
void *(*malloc)(size_t);
void (*free)(void *);
int initialized;
int processing;
// manage the entity ids
// reuse deleted entity ids
struct _sparseIntegerSet freeEntityIds;
unsigned int nextEntityId;
// entity data
// first 'column' is bits of components defined
// the rest are the components
unsigned int dataWidth;
unsigned int dataHeight;
unsigned int dataHeightCapacity;
void *data;
unsigned int processingDataHeight;
void **processingData;
// buffer entity status notifications
struct _sparseIntegerSet added;
struct _sparseIntegerSet enabled;
struct _sparseIntegerSet disabled;
struct _sparseIntegerSet deleted;
// all active entities (added and enabled)
struct _denseIntegerSet active;
unsigned int num_components;
struct _component *components;
unsigned int num_systems;
struct _system *systems;
unsigned int num_managers;
struct _manager *managers;
#if DL_COMPUTE
struct _computingComponentStack *computingComponentStack;
#endif
};
// ============================================================================
// UTILITY
#define FOREACH_SPARSEINTSET(I, N, S) for(N = 0, I = (S)->dense ? (S)->dense[N] : 0; N < (S)->population; N++, I = (S)->dense[N])
#define FOREACH_DENSEINTSET(I, D) for(I = 0; I < (D)->capacity; I++) if(_bits_isSet((D)->bytes, I))
#define FOREACH_ARRAY(T, N, A, S) for(N = 0, T = A; N < S; N++, T++)
static int _malloc(struct diana *diana, size_t size, void ** r) {
*r = diana->malloc(size);
if(*r == NULL) {
return DL_ERROR_OUT_OF_MEMORY;
}
memset(*r, 0, size);
return DL_ERROR_NONE;
}
static int _free(struct diana *diana, void *ptr) {
if(ptr != NULL) {
diana->free(ptr);
}
return DL_ERROR_NONE;
}
static int _strdup(struct diana *diana, const char *s, char ** r) {
unsigned int l;
if(s == NULL) {
return DL_ERROR_NONE;
}
l = strlen(s);
*r = diana->malloc(l + 1);
if(*r == NULL) {
return DL_ERROR_OUT_OF_MEMORY;
}
memcpy(*r, s, l + 1);
return DL_ERROR_NONE;
}
static int _realloc(struct diana *diana, void *ptr, size_t oldSize, size_t newSize, void ** r) {
if(oldSize == newSize) {
*r = ptr;
return DL_ERROR_NONE;
}
if(newSize == 0) {
_free(diana, ptr);
return DL_ERROR_NONE;
}
*r = diana->malloc(newSize);
if(*r == NULL) {
return DL_ERROR_OUT_OF_MEMORY;
}
if(oldSize < newSize) {
memset((unsigned char *)(*r) + oldSize, 0, newSize - oldSize);
}
if(ptr != NULL) {
memcpy(*r, ptr, oldSize < newSize ? oldSize : newSize);
diana->free(ptr);
}
return DL_ERROR_NONE;
}
int allocate_diana(void *(*malloc)(size_t), void (*free)(void *), struct diana ** r) {
*r = malloc(sizeof(**r));
if(*r == NULL) {
return DL_ERROR_OUT_OF_MEMORY;
}
memset(*r, 0, sizeof(**r));
(*r)->malloc = malloc;
(*r)->free = free;
return DL_ERROR_NONE;
}
static int _fixData(struct diana *diana);
int diana_free(struct diana *diana) {
struct _component *component;
struct _system *system;
struct _manager *manager;
unsigned int i, j;
int err = _fixData(diana);
if(err != DL_ERROR_NONE) {
return err;
}
for(i = 0; i < diana->nextEntityId; i++) {
for(j = 0; j < diana->num_components; j++) {
diana_removeComponents(diana, i, j);
}
}
_free(diana, diana->data);
_sparseIntegerSet_free(diana, &diana->freeEntityIds);
_sparseIntegerSet_free(diana, &diana->added);
_sparseIntegerSet_free(diana, &diana->enabled);
_sparseIntegerSet_free(diana, &diana->disabled);
_sparseIntegerSet_free(diana, &diana->deleted);
_denseIntegerSet_free(diana, &diana->active);
FOREACH_ARRAY(component, i, diana->components, diana->num_components) {
_component_free(diana, component);
}
_free(diana, diana->components);
FOREACH_ARRAY(system, i, diana->systems, diana->num_systems) {
_system_free(diana, system);
}
_free(diana, diana->systems);
FOREACH_ARRAY(manager, i, diana->managers, diana->num_managers) {
_manager_free(diana, manager);
}
_free(diana, diana->managers);
diana->free(diana);
return DL_ERROR_NONE;
}
// ============================================================================
// INITIALIZATION TIME
int diana_initialize(struct diana *diana) {
unsigned int extraBytes = (diana->num_components + 7) >> 3, n;
struct _component *c;
if(diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
FOREACH_ARRAY(c, n, diana->components, diana->num_components) {
c->offset += extraBytes;
}
diana->dataWidth += extraBytes;
diana->initialized = 1;
return DL_ERROR_NONE;
}
// ============================================================================
// component
int diana_createComponent(
struct diana *diana,
const char *name,
size_t size,
unsigned int flags,
unsigned int * component_ptr
) {
struct _component c;
int err = DL_ERROR_NONE;
if(diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
memset(&c, 0, sizeof(c));
err = _strdup(diana, name, (char **)&c.name);
if(err != DL_ERROR_NONE) {
return err;
}
c.size = size;
c.flags = flags;
c.offset = diana->dataWidth;
if(flags & DL_COMPONENT_MULTIPLE_BIT) {
size = sizeof(struct _componentBag);
} else if(flags & DL_COMPONENT_INDEXED_BIT) {
size = sizeof(unsigned int);
}
diana->dataWidth += size;
if(flags & DL_COMPONENT_LIMITED_BIT) {
unsigned int count = (flags >> 3);
err = _malloc(diana, c.size * count, (void **)&c.data);
if(err != DL_ERROR_NONE) {
_free(diana, (void *)c.name);
return err;
}
while(count) {
_sparseIntegerSet_insert(diana, &c.freeDataIndexes, count--);
}
}
err = _realloc(diana, diana->components, sizeof(*diana->components) * diana->num_components, sizeof(*diana->components) * (diana->num_components + 1), (void **)&diana->components);
if(err != DL_ERROR_NONE) {
_free(diana, (void *)c.name);
return err;
}
diana->components[diana->num_components++] = c;
*component_ptr = diana->num_components - 1;
return err;
}
#if DL_COMPUTE
int diana_componentCompute(struct diana *diana, unsigned int component, void (*compute)(struct diana *, void *, unsigned int entity, unsigned int index, void *), void *userData) {
if(diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
if(component >= diana->num_components) {
return DL_ERROR_INVALID_VALUE;
}
diana->components[component].compute = compute;
diana->components[component].userData = userData;
diana->dataWidth += sizeof(char);
// give this component room
while(component < diana->num_components) {
diana->components[component].offset += sizeof(char);
component++;
}
return DL_ERROR_NONE;
}
#endif
// ============================================================================
// system
int diana_createSystem(
struct diana *diana,
const char *name,
void (*starting)(struct diana *, void *),
void (*process)(struct diana *, void *, unsigned int, float),
void (*ending)(struct diana *, void *),
void (*subscribed)(struct diana *, void *, unsigned int),
void (*unsubscribed)(struct diana *, void *, unsigned int),
void *userData,
unsigned int flags,
unsigned int * system_ptr
) {
struct _system s;
int err = DL_ERROR_NONE;
if(diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
memset(&s, 0, sizeof(s));
err = _strdup(diana, name, (char **)&s.name);
if(err != DL_ERROR_NONE) {
return err;
}
s.starting = starting;
s.process = process;
s.ending = ending;
s.subscribed = subscribed;
s.unsubscribed = unsubscribed;
s.userData = userData;
s.flags = flags;
err = _realloc(diana, diana->systems, sizeof(*diana->systems) * diana->num_systems, sizeof(*diana->systems) * (diana->num_systems + 1), (void **)&diana->systems);
if(err != DL_ERROR_NONE) {
_free(diana, (void *)s.name);
return err;
}
diana->systems[diana->num_systems++] = s;
*system_ptr = diana->num_systems - 1;
return err;
}
int diana_watch(struct diana *diana, unsigned int system, unsigned int component) {
if(diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
if(system >= diana->num_systems) {
return DL_ERROR_INVALID_VALUE;
}
if(component >= diana->num_components) {
return DL_ERROR_INVALID_VALUE;
}
_sparseIntegerSet_insert(diana, &diana->systems[system].watch, component);
return DL_ERROR_NONE;
}
int diana_exclude(struct diana *diana, unsigned int system, unsigned int component) {
if(diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
if(system >= diana->num_systems) {
return DL_ERROR_INVALID_VALUE;
}
if(component >= diana->num_components) {
return DL_ERROR_INVALID_VALUE;
}
_sparseIntegerSet_insert(diana, &diana->systems[system].exclude, component);
return DL_ERROR_NONE;
}
// ============================================================================
// manager
int diana_createManager(
struct diana *diana,
const char *name,
void (*added)(struct diana *, void *, unsigned int),
void (*enabled)(struct diana *, void *, unsigned int),
void (*disabled)(struct diana *, void *, unsigned int),
void (*deleted)(struct diana *, void *, unsigned int),
void *userData,
unsigned int flags,
unsigned int * manager_ptr
) {
struct _manager m;
int err = DL_ERROR_NONE;
if(diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
memset(&m, 0, sizeof(m));
err = _strdup(diana, name, (char **)&m.name);
if(err != DL_ERROR_NONE) {
return err;
}
m.added = added;
m.enabled = enabled;
m.disabled = disabled;
m.deleted = deleted;
m.userData = userData;
m.flags = flags;
err = _realloc(diana, diana->managers, sizeof(*diana->managers) * diana->num_managers, sizeof(*diana->managers) * (diana->num_managers + 1), (void **)&diana->managers);
if(err != DL_ERROR_NONE) {
_free(diana, (char *)m.name);
return err;
}
diana->managers[diana->num_managers++] = m;
*manager_ptr = diana->num_managers - 1;
return err;
}
// ============================================================================
// RUNTIME
static unsigned char *_getEntityData(struct diana *diana, unsigned int entity) {
if(entity >= diana->dataHeightCapacity) {
return diana->processingData[entity - diana->dataHeightCapacity];
}
return (void *)((unsigned char *)diana->data + (diana->dataWidth * entity));
}
static void _subscribe(struct diana *diana, struct _system *system, unsigned int entity) {
int included = _denseIntegerSet_insert(diana, &system->entities, entity);
if(!included && system->subscribed != NULL) {
system->subscribed(diana, system->userData, entity);
}
}
static void _unsubscribe(struct diana *diana, struct _system *system, unsigned int entity) {
int included = _denseIntegerSet_delete(diana, &system->entities, entity);
if(included && system->unsubscribed != NULL) {
system->unsubscribed(diana, system->userData, entity);
}
}
static void _check(struct diana *diana, struct _system *system, unsigned int entity) {
unsigned char *entity_components = _getEntityData(diana, entity);
unsigned int component, i;
int wanted = 1;
FOREACH_SPARSEINTSET(component, i, &system->watch) {
if(!(entity_components[component >> 3] & (1 << (component & 7)))) {
wanted = 0;
break;
}
}
FOREACH_SPARSEINTSET(component, i, &system->exclude) {
if((entity_components[component >> 3] & (1 << (component & 7)))) {
wanted = 0;
break;
}
}
if(wanted) {
_subscribe(diana, system, entity);
} else {
_unsubscribe(diana, system, entity);
}
}
static int _fixData(struct diana *diana) {
// take care of spawns that happen during processing
if(diana->processingData != NULL) {
unsigned int newDataHeight = diana->dataHeight + diana->processingDataHeight, i;
if(newDataHeight >= diana->dataHeightCapacity) {
unsigned int newDataHeightCapacity = (newDataHeight + 1) * 1.5;
int err = _realloc(diana, diana->data, diana->dataWidth * diana->dataHeightCapacity, diana->dataWidth * newDataHeightCapacity, (void **)&diana->data);
if(err != DL_ERROR_NONE) {
return err;
}
diana->dataHeightCapacity = newDataHeightCapacity;
}
for(i = 0; i < diana->processingDataHeight; i++) {
memcpy((unsigned char *)diana->data + (diana->dataWidth * (diana->dataHeight + i)), diana->processingData[i], diana->dataWidth);
diana->free(diana->processingData[i]);
}
diana->free(diana->processingData);
diana->dataHeight = newDataHeight;
diana->processingData = NULL;
diana->processingDataHeight = 0;
}
return DL_ERROR_NONE;
}
int diana_process(struct diana *diana, float delta) {
unsigned int entity, i, j;
struct _system *system;
struct _manager *manager;
if(!diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
diana->processing = 1;
FOREACH_SPARSEINTSET(entity, i, &diana->added) {
FOREACH_ARRAY(manager, j, diana->managers, diana->num_managers) {
if(manager->added != NULL) {
manager->added(diana, manager->userData, entity);
}
}
}
_sparseIntegerSet_clear(diana, &diana->added);
FOREACH_SPARSEINTSET(entity, i, &diana->enabled) {
FOREACH_ARRAY(system, j, diana->systems, diana->num_systems) {
_check(diana, system, entity);
}
FOREACH_ARRAY(manager, j, diana->managers, diana->num_managers) {
if(manager->enabled != NULL) {
manager->enabled(diana, manager->userData, entity);
}
}
_denseIntegerSet_insert(diana, &diana->active, entity);
}
_sparseIntegerSet_clear(diana, &diana->enabled);
FOREACH_SPARSEINTSET(entity, i, &diana->disabled) {
FOREACH_ARRAY(system, j, diana->systems, diana->num_systems) {
_unsubscribe(diana, system, entity);
}
FOREACH_ARRAY(manager, j, diana->managers, diana->num_managers) {
if(manager->disabled != NULL) {
manager->disabled(diana, manager->userData, entity);
}
}
_denseIntegerSet_delete(diana, &diana->active, entity);
}
_sparseIntegerSet_clear(diana, &diana->disabled);
FOREACH_SPARSEINTSET(entity, i, &diana->deleted) {
FOREACH_ARRAY(system, j, diana->systems, diana->num_systems) {
_unsubscribe(diana, system, entity);
}
FOREACH_ARRAY(manager, j, diana->managers, diana->num_managers) {
if(manager->deleted != NULL) {
manager->deleted(diana, manager->userData, entity);
}
}
for(j = 0; j < diana->num_components; j++) {
diana_removeComponents(diana, entity, j);
}
_sparseIntegerSet_insert(diana, &diana->freeEntityIds, entity);
}
_sparseIntegerSet_clear(diana, &diana->deleted);
FOREACH_ARRAY(system, j, diana->systems, diana->num_systems) {
if(system->flags & DL_SYSTEM_PASSIVE_BIT) {
continue;
}
if(system->starting != NULL) {
system->starting(diana, system->userData);
}
FOREACH_DENSEINTSET(entity, &system->entities) {
system->process(diana, system->userData, entity, delta);
}
if(system->ending != NULL) {
system->ending(diana, system->userData);
}
}
diana->processing = 0;
return _fixData(diana);
}
int diana_processSystem(struct diana *diana, unsigned int system, float delta) {
struct _system *s;
unsigned int entity;
if(!diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
if(system >= diana->num_systems) {
return DL_ERROR_INVALID_VALUE;
}
s = diana->systems + system;
if(s->starting != NULL) {
s->starting(diana, s->userData);
}
FOREACH_DENSEINTSET(entity, &s->entities) {
s->process(diana, s->userData, entity, delta);
}
if(s->ending != NULL) {
s->ending(diana, s->userData);
}
return _fixData(diana);
}
// ============================================================================
// entity
int diana_spawn(struct diana *diana, unsigned int * entity_ptr) {
unsigned int r;
int err = DL_ERROR_NONE;
if(!diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
if(_sparseIntegerSet_isEmpty(diana, &diana->freeEntityIds)) {
r = diana->nextEntityId++;
} else {
r = _sparseIntegerSet_pop(diana, &diana->freeEntityIds);
}
diana->dataHeight = diana->dataHeight > (r + 1) ? diana->dataHeight : (r + 1);
if(diana->dataHeight > diana->dataHeightCapacity) {
if(diana->processing) {
void *entityData;
err = _malloc(diana, diana->dataWidth, &entityData);
if(err != DL_ERROR_NONE) {
return err;
}
if(entityData == NULL) {
return 0;
}
err = _realloc(diana, diana->processingData, sizeof(*diana->processingData) * diana->processingDataHeight, sizeof(*diana->processingData) * (diana->processingDataHeight + 1), (void **)&diana->processingData);
if(err != DL_ERROR_NONE) {
return err;
}
diana->processingData[diana->processingDataHeight++] = entityData;
} else {
unsigned int newDataHeightCapacity = diana->dataHeight * 1.5;
err = _realloc(diana, diana->data, diana->dataWidth * diana->dataHeightCapacity, diana->dataWidth * newDataHeightCapacity, (void **)&diana->data);
if(err != DL_ERROR_NONE) {
return err;
}
diana->dataHeightCapacity = newDataHeightCapacity;
}
}
*entity_ptr = r;
return err;
}
int diana_signal(struct diana *diana, unsigned int entity, unsigned int signal) {
int err = DL_ERROR_NONE;
if(!diana->initialized) {
return DL_ERROR_INVALID_OPERATION;
}
if((!diana->processing && entity >= diana->dataHeight) || (diana->processing && entity >= diana->dataHeightCapacity + diana->processingDataHeight)) {
return DL_ERROR_INVALID_VALUE;
}
switch(signal) {
case DL_ENTITY_ADDED:
_sparseIntegerSet_insert(diana, &diana->added, entity);
_sparseIntegerSet_insert(diana, &diana->enabled, entity);
_sparseIntegerSet_delete(diana, &diana->disabled, entity);
_sparseIntegerSet_delete(diana, &diana->deleted, entity);
break;
case DL_ENTITY_ENABLED:
_sparseIntegerSet_insert(diana, &diana->enabled, entity);
_sparseIntegerSet_delete(diana, &diana->disabled, entity);
_sparseIntegerSet_delete(diana, &diana->deleted, entity);
break;
case DL_ENTITY_DISABLED:
_sparseIntegerSet_delete(diana, &diana->enabled, entity);
_sparseIntegerSet_insert(diana, &diana->disabled, entity);
_sparseIntegerSet_delete(diana, &diana->deleted, entity);
break;
case DL_ENTITY_DELETED:
_sparseIntegerSet_delete(diana, &diana->added, entity);
_sparseIntegerSet_delete(diana, &diana->enabled, entity);
_sparseIntegerSet_insert(diana, &diana->disabled, entity);
_sparseIntegerSet_insert(diana, &diana->deleted, entity);
break;
default:
err = DL_ERROR_INVALID_VALUE;
}
return err;
}
static int _getAComponentIndex(struct diana *diana, struct _component *c, unsigned int * index) {
if(_sparseIntegerSet_isEmpty(diana, &c->freeDataIndexes)) {
if(c->flags & DL_COMPONENT_LIMITED_BIT) {
return DL_ERROR_FULL_COMPONENT;
}
*index = c->nextDataIndex++;
return _realloc(diana, c->data, sizeof(void *) * *index, sizeof(void *) * c->nextDataIndex, (void **)&c->data) ||
_malloc(diana, c->size, (void **)&c->data[*index]);
} else {
*index = _sparseIntegerSet_pop(diana, &c->freeDataIndexes);
}
return DL_ERROR_NONE;
}
static int _setComponentI(struct diana *diana, unsigned int entity, unsigned int component, unsigned int i, const void * data) {
unsigned char *entityData = _getEntityData(diana, entity);
struct _component *c = diana->components + component;
int defined = _bits_set(entityData, component);
void *componentData = NULL;
unsigned int err = DL_ERROR_NONE;
#if DL_COMPUTE
if(c->compute) {
entityData[c->offset - 1] = !defined;
}
#endif
if(c->flags & DL_COMPONENT_MULTIPLE_BIT) {
struct _componentBag *bag = (struct _componentBag *)(entityData + c->offset);
unsigned int index;
if(i >= bag->count) {
err = _getAComponentIndex(diana, c, &index);
if(err != DL_ERROR_NONE) {
return err;
}