-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs.cpp
721 lines (632 loc) · 22.6 KB
/
ecs.cpp
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
#include "ecs.h"
#include "ecs_management.h"
#include "ecs_internal.h"
#include "assert.h"
#include "string.h"
uint8_t*
GetComponentAddress(const chunk* Chunk, int32_t IndexInChunk, int32_t ComponentOffset,
int32_t ComponentSize)
{
uint8_t* DesiredAddress = ((uint8_t*)Chunk) + ComponentOffset + IndexInChunk * ComponentSize;
return DesiredAddress;
}
uint8_t*
GetComponentAddress(const ecs_world* World, entity_storage_info EntityStorage,
int32_t ComponentOffsetInChunk, component_struct_info ComponentInfo)
{
return GetComponentAddress(GetChunkAtIndex(World->Runtime, EntityStorage.ChunkIndex),
EntityStorage.IndexInChunk, ComponentOffsetInChunk,
ComponentInfo.Size);
}
// Note(Lukas) all of this data is known at compile time and could be cached
bool
DoesArchetypeMatchRequest(const archetype& Archetype, const archetype_request& Request)
{
for(int r = 0; r < Request.ComponentCount; r++)
{
component_request CurrentRequest = Request.ComponentRequests[r];
bool MatchedRequest = false;
for(int c = 0; c < Archetype.ComponentTypes.Count; c++)
{
if(CurrentRequest.ID == Archetype.ComponentTypes[c].ID &&
CurrentRequest.Type != REQUEST_Subtractive)
{
MatchedRequest = true;
}
}
if(!MatchedRequest)
{
return false;
}
}
return true;
}
// Note(Lukas) this data is known at compile time and could be cached
void
GetUsedComponents(fixed_stack<component_id, ECS_ARCHETYPE_COMPONENT_MAX_COUNT>* OutUsedComponents,
const archetype_request& ArchetypeRequest)
{
OutUsedComponents->Clear();
for(int i = 0; i < ArchetypeRequest.ComponentCount; i++)
{
if((ArchetypeRequest.ComponentRequests[i].Type == REQUEST_Permission_R) ||
(ArchetypeRequest.ComponentRequests[i].Type == REQUEST_Permission_RW))
{
OutUsedComponents->Push(ArchetypeRequest.ComponentRequests[i].ID);
}
}
assert(OutUsedComponents->Count != 0);
}
uintptr_t
GetComponentOffset(const archetype& Archetype, component_id ComponentID)
{
for(int i = 0; i < Archetype.ComponentTypes.Count; i++)
{
if(Archetype.ComponentTypes[i].ID == ComponentID)
{
uintptr_t Result = Archetype.ComponentTypes[i].Offset;
return Result;
}
}
assert(0 && "GetComponentOffset: Invalid codepath");
return 0;
}
// Initialization
void
InitializeChunkHeap(ecs_runtime* Runtime, uint8_t* ChunkMemory, int32_t AvailableMemory)
{
Runtime->ChunkHeap.Create(ChunkMemory, AvailableMemory);
}
void
InitializeArchetypeAndComponentTables(ecs_runtime* Runtime,
const component_struct_info* ComponentInfos,
const char** ComponentNames, int ComponentCount)
{
Runtime->ComponentStructInfos.Clear();
Runtime->ComponentNames.Clear();
Runtime->Archetypes.Clear();
Runtime->VacantArchetypeIndices.Clear();
for(int i = 0; i < ComponentCount; i++)
{
Runtime->ComponentStructInfos.Push(ComponentInfos[i]);
Runtime->ComponentNames.Push(ComponentNames[i]);
}
}
void
InitializeWorld(ecs_world* World, const ecs_runtime* Runtime)
{
World->Entities.Clear();
World->VacantEntityIndices.Clear();
World->EntityCommands.Clear();
World->Runtime = (ecs_runtime*)Runtime;
}
// Integrating saved worlds (used importing deserialize'ing)
void
AdaptWorldToNewRuntime(ecs_world* World, uint8_t* OldRuntimMemory, const ecs_runtime* OldRuntime,
const ecs_runtime* NewRuntime)
{
}
void
ExecuteECSJob(ecs_runtime* runtime, const ecs_world* World,
const archetype_request& ArchetypeRequest, ECS_JOB_FUNCTION_PARAMETERS(JobFunc))
{
// Find matching archetypes
fixed_stack<archetype*, ECS_ARCHETYPE_MAX_COUNT> MatchedArchetypes;
MatchedArchetypes.Clear();
for(int a = 0; a < runtime->Archetypes.Count; a++)
{
if(DoesArchetypeMatchRequest(runtime->Archetypes[a], ArchetypeRequest))
{
MatchedArchetypes.Push(&runtime->Archetypes[a]);
a++;
}
}
fixed_stack<component_id, ECS_ARCHETYPE_COMPONENT_MAX_COUNT> UsedComponents;
UsedComponents.Clear();
GetUsedComponents(&UsedComponents, ArchetypeRequest);
assert(UsedComponents.Count < ECS_ARCHETYPE_COMPONENT_MAX_COUNT);
for(int a = 0; a < MatchedArchetypes.Count; a++)
{
uintptr_t ComponentOffsetArray[ECS_ARCHETYPE_COMPONENT_MAX_COUNT];
for(int CompInd = 0; CompInd < UsedComponents.Count; CompInd++)
{
ComponentOffsetArray[CompInd] =
GetComponentOffset(*MatchedArchetypes[a], UsedComponents[CompInd]);
}
// Schedule jobs for execution
// TODO(Lukas): compute the component offset arrays and shedule the chunk updates in a different
for(chunk* CurrentChunk = MatchedArchetypes[a]->FirstChunk; CurrentChunk != NULL;
CurrentChunk = CurrentChunk->Header.NextChunk)
{
for(int CompInd = 0; CompInd < UsedComponents.Count; CompInd++)
{
ComponentOffsetArray[CompInd] += (uintptr_t)CurrentChunk;
}
JobFunc(&ComponentOffsetArray[0], UsedComponents.Count);
}
}
}
archetype*
GetEntityArchetype(const ecs_world* World, entity_id EntityID)
{
assert(0 <= EntityID && EntityID < World->Entities.Count);
archetype* Archetype = NULL;
if(World->Entities[EntityID].ChunkIndex != -1)
{
chunk* Chunks = (chunk*)World->Runtime->ChunkHeap.GetBase();
entity_storage_info EntityStorage = World->Entities[EntityID];
Archetype = &World->Runtime->Archetypes[Chunks[EntityStorage.ChunkIndex].Header.ArchetypeIndex];
}
return Archetype;
}
// Entity API
// ChunkIndex == -1 && IndexInChunk == 0 means that entity is created but has no components
// ChunkIndex == -1 && IndexInChunk == -1 means that has been destroyed
entity_id
CreateEntity(ecs_world* World)
{
assert(!World->Entities.Full() || !World->VacantEntityIndices.Empty());
entity_id NewEntityID = {};
if(!World->VacantEntityIndices.Empty())
{
NewEntityID = World->VacantEntityIndices.Pop();
}
else
{
NewEntityID = (entity_id)World->Entities.Count;
World->Entities.Push({});
}
entity_storage_info* NewEntity = &World->Entities[NewEntityID];
NewEntity->ChunkIndex = -1;
NewEntity->IndexInChunk = 0;
return (entity_id)NewEntityID;
}
bool
DoesEntityExist(const ecs_world* World, entity_id EntityID)
{
if(0 <= EntityID && EntityID < World->Entities.Count)
{
entity_storage_info EntityStorage = World->Entities[EntityID];
if(0 <= EntityStorage.ChunkIndex)
{
return true;
}
else if(EntityStorage.ChunkIndex == -1 && EntityStorage.IndexInChunk == 0)
{
return true;
}
}
return false;
}
archetype*
GetChunkArchetype(const ecs_world* World, const chunk* Chunk)
{
return &World->Runtime->Archetypes[Chunk->Header.ArchetypeIndex];
}
chunk*
GetChunkAtIndex(ecs_runtime* Runtime, int32_t ChunkIndex)
{
chunk* Chunk = ((chunk*)Runtime->ChunkHeap.GetBase()) + ChunkIndex;
return Chunk;
}
void
RemoveEntityFromChunk(ecs_world* World, entity_storage_info RemovedEntity)
{
// If the entity is stored anywhere (has any components)
if(RemovedEntity.ChunkIndex != -1)
{
chunk* Chunk = GetChunkAtIndex(World->Runtime, RemovedEntity.ChunkIndex);
entity_storage_info LastEntity = {};
LastEntity.ChunkIndex = RemovedEntity.ChunkIndex;
LastEntity.IndexInChunk = Chunk->Header.EntityCount - 1;
// Find the last chunk entity's ID
// TODO(Lukas): Need a hash table for reverse lookup
entity_id LastEntityID = -1;
for(entity_id i = 0; i < World->Entities.Count; i++)
{
if(World->Entities[i].ChunkIndex == LastEntity.ChunkIndex &&
World->Entities[i].IndexInChunk == LastEntity.IndexInChunk)
{
LastEntityID = i;
break;
}
}
assert(LastEntityID != -1);
archetype* Archetype = GetChunkArchetype(World, Chunk);
// Copy data from last entity to the removed spot
if(RemovedEntity.IndexInChunk != LastEntity.IndexInChunk)
{
for(int i = 0; i < Archetype->ComponentTypes.Count; i++)
{
component_id_and_offset OffsetAndID = Archetype->ComponentTypes[i];
component_struct_info ComponentInfo = World->Runtime->ComponentStructInfos[OffsetAndID.ID];
uint8_t* RemovedComponent = GetComponentAddress(Chunk, RemovedEntity.IndexInChunk,
OffsetAndID.Offset, ComponentInfo.Size);
uint8_t* LastComponent = GetComponentAddress(Chunk, LastEntity.IndexInChunk,
OffsetAndID.Offset, ComponentInfo.Size);
memcpy(RemovedComponent, LastComponent, ComponentInfo.Size);
}
World->Entities[LastEntityID] = RemovedEntity;
}
// Remove the last entity in the chunk
Chunk->Header.EntityCount--;
// If the chunk became empty - free it
if(Chunk->Header.EntityCount == 0)
{
chunk** PrevChunkPtrPtr = &Archetype->FirstChunk;
for(chunk* C = Archetype->FirstChunk; C != Chunk; C = C->Header.NextChunk)
{
PrevChunkPtrPtr = &C->Header.NextChunk;
}
*PrevChunkPtrPtr = Chunk->Header.NextChunk;
// If it was the only chunk, remove the archetype
if(Archetype->FirstChunk == NULL)
{
RemoveArchetype(World->Runtime, Archetype);
}
// Free the memory
World->Runtime->ChunkHeap.Dealloc((uint8_t*)Chunk);
}
}
}
// ChunkIndex == -1 && IndexInChunk == 0 means that entity is created but has no components
// ChunkIndex == -1 && IndexInChunk == -1 means that has been destroyed
void
DestroyEntity(ecs_world* World, entity_id RemovedEntityID)
{
assert(DoesEntityExist(World, RemovedEntityID));
entity_storage_info RemovedEntity = World->Entities[RemovedEntityID];
RemoveEntityFromChunk(World, RemovedEntity);
World->Entities[RemovedEntityID] = { -1, -1 };
if(RemovedEntityID == World->Entities.Count - 1)
{
World->Entities.Pop();
}
else
{
World->VacantEntityIndices.Push(RemovedEntityID);
}
}
int32_t
GetComponentIndexInArchetype(const archetype& Archetype, component_id ComponentID)
{
for(int i = 0; i < Archetype.ComponentTypes.Count; i++)
{
if(Archetype.ComponentTypes[i].ID == ComponentID)
{
return i;
}
}
return -1;
}
bool
HasComponent(const ecs_world* World, entity_id EntityID, component_id ComponentID)
{
assert(DoesEntityExist(World, EntityID));
archetype* Archetype = GetEntityArchetype(World, EntityID);
return (Archetype && (GetComponentIndexInArchetype(*Archetype, ComponentID) != -1)) ? true
: false;
}
int32_t
GetChunkEntityCapacity(const ecs_runtime* Runtime, const archetype& Archetype)
{
int32_t ComponentSizeSum = 0;
int32_t MaximalAlignmentOffset = 0;
for(int i = 0; i < Archetype.ComponentTypes.Count; i++)
{
ComponentSizeSum += Runtime->ComponentStructInfos[Archetype.ComponentTypes[i].ID].Size;
MaximalAlignmentOffset +=
Runtime->ComponentStructInfos[Archetype.ComponentTypes[i].ID].Alignment - 1;
}
int32_t EntityCapacity =
(ECS_CHUNK_SIZE - (int32_t)sizeof(chunk_header) - MaximalAlignmentOffset) / ComponentSizeSum;
return EntityCapacity;
}
void
ComputeArchetypeComponentOffsets(archetype* Archetype, const ecs_runtime* Runtime)
{
int32_t EntityCapacity = GetChunkEntityCapacity(Runtime, *Archetype);
uint32_t UnalignedPosition = (uint32_t)sizeof(chunk_header);
for(int i = 0; i < Archetype->ComponentTypes.Count; i++)
{
component_struct_info ComponentStructInfo =
Runtime->ComponentStructInfos[Archetype->ComponentTypes[i].ID];
uint32_t Alignment = Runtime->ComponentStructInfos[Archetype->ComponentTypes[i].ID].Alignment;
uint32_t AlignmentFixup =
(Alignment - (uint32_t)((uint64_t)UnalignedPosition & ((uint64_t)Alignment - 1)));
uint32_t AlignedPosition = UnalignedPosition + AlignmentFixup;
Archetype->ComponentTypes[i].Offset = (uint16_t)AlignedPosition;
UnalignedPosition =
AlignedPosition +
EntityCapacity * Runtime->ComponentStructInfos[Archetype->ComponentTypes[i].ID].Size;
}
}
int32_t
AddComponentWithoutLosingCanonicalForm(archetype* Archetype, component_id NewComponentID,
const ecs_runtime* Runtime)
{
const char* NewComponentName = Runtime->ComponentNames[NewComponentID];
component_id_and_offset NewComponentOffset = {};
NewComponentOffset.ID = NewComponentID;
int32_t NewComponentIndex = -1;
for(int i = 0; i < Archetype->ComponentTypes.Count; i++)
{
int32_t cmp =
strcmp(Runtime->ComponentNames[Archetype->ComponentTypes[i].ID], NewComponentName);
assert(cmp != 0);
if(cmp > 0)
{
Archetype->ComponentTypes.Insert(NewComponentOffset, i);
NewComponentIndex = i;
break;
}
}
if(NewComponentIndex == -1)
{
NewComponentIndex = Archetype->ComponentTypes.Count;
Archetype->ComponentTypes.Push(NewComponentOffset);
}
ComputeArchetypeComponentOffsets(Archetype, Runtime);
return NewComponentIndex;
}
void
RemoveComponentWithoutLosingCanonicalForm(archetype* Archetype, component_id RemoveComponentID,
const ecs_runtime* Runtime)
{
for(int i = 0; i < Archetype->ComponentTypes.Count; i++)
{
if(Archetype->ComponentTypes[i].ID == RemoveComponentID)
{
Archetype->ComponentTypes.Remove(i);
break;
}
}
if(Archetype->ComponentTypes.Count == 0)
{
return;
}
ComputeArchetypeComponentOffsets(Archetype, Runtime);
}
archetype*
AddArchetype(ecs_runtime* Runtime, const archetype& Archetype)
{
assert(!Runtime->Archetypes.Full() || !Runtime->VacantArchetypeIndices.Empty());
int32_t NewArchetypeIndex = -1;
if(!Runtime->VacantArchetypeIndices.Empty())
{
NewArchetypeIndex = Runtime->VacantArchetypeIndices.Pop();
}
else
{
NewArchetypeIndex = (entity_id)Runtime->Archetypes.Count;
Runtime->Archetypes.Push(Archetype);
}
archetype* NewArchetype = &Runtime->Archetypes[NewArchetypeIndex];
*NewArchetype = Archetype;
NewArchetype->FirstChunk = NULL;
return NewArchetype;
}
void
RemoveArchetypeAtIndex(ecs_runtime* Runtime, int32_t RemoveIndex)
{
assert(0 <= RemoveIndex && RemoveIndex < Runtime->Archetypes.Count);
archetype* RemovedArchetype = &Runtime->Archetypes[RemoveIndex];
assert(RemovedArchetype->FirstChunk == NULL);
RemovedArchetype->ComponentTypes.Clear();
if(RemoveIndex != Runtime->Archetypes.Count - 1)
{
Runtime->VacantArchetypeIndices.Push(RemoveIndex);
}
else
{
Runtime->Archetypes.Pop();
}
}
void
RemoveArchetype(ecs_runtime* Runtime, archetype* Archetype)
{
int32_t ArchetypeIndex = (int32_t)(Archetype - &Runtime->Archetypes[0]);
RemoveArchetypeAtIndex(Runtime, ArchetypeIndex);
}
int32_t
GetChunkIndex(const ecs_runtime* Runtime, const chunk* C)
{
int32_t ChunkIndex = (int32_t)(C - (chunk*)Runtime->ChunkHeap.GetBase());
return ChunkIndex;
}
int32_t
GetArchetypeIndex(const ecs_runtime* Runtime, const archetype* Archetype)
{
int32_t ArchetypeIndex = (int32_t)(Archetype - Runtime->Archetypes.Elements);
return ArchetypeIndex;
}
entity_storage_info
CreateNewArchetypeInstance(ecs_runtime* Runtime, archetype* Archetype)
{
assert(Archetype);
entity_storage_info NewEntityStorage = {};
chunk** PrevCPtrPtr = &Archetype->FirstChunk;
chunk* C = Archetype->FirstChunk;
for(; C != NULL; C = C->Header.NextChunk)
{
PrevCPtrPtr = &C->Header.NextChunk;
if(C->Header.EntityCount < C->Header.EntityCapacity)
{
NewEntityStorage.ChunkIndex = (int16_t)GetChunkIndex(Runtime, C);
NewEntityStorage.IndexInChunk = C->Header.EntityCount;
C->Header.EntityCount++;
break;
}
}
if(C == NULL)
{
// If no vacancies were found
chunk* NewChunk = (chunk*)Runtime->ChunkHeap.Alloc(sizeof(chunk));
if(Archetype->FirstChunk)
{
NewChunk->Header = Archetype->FirstChunk->Header;
NewChunk->Header.NextChunk = NULL;
}
else
{
NewChunk->Header = {};
NewChunk->Header.EntityCapacity = (int16_t)GetChunkEntityCapacity(Runtime, *Archetype);
NewChunk->Header.ArchetypeIndex = (int16_t)GetArchetypeIndex(Runtime, Archetype);
}
NewChunk->Header.EntityCount = 1;
*PrevCPtrPtr = NewChunk;
NewEntityStorage.ChunkIndex = (int16_t)GetChunkIndex(Runtime, NewChunk);
NewEntityStorage.IndexInChunk = 0;
}
return NewEntityStorage;
}
void
CopyMatchingComponentValues(ecs_world* World, entity_storage_info DstStorage,
entity_storage_info SrcStorage, const archetype& DstArchetype,
const archetype& SrcArchetype)
{
chunk* DstChunk = GetChunkAtIndex(World->Runtime, DstStorage.ChunkIndex);
chunk* SrcChunk = GetChunkAtIndex(World->Runtime, SrcStorage.ChunkIndex);
int SrcStartIndex = 0;
for(int i = 0; i < DstArchetype.ComponentTypes.Count; i++)
{
int j = SrcStartIndex;
for(; j < SrcArchetype.ComponentTypes.Count; j++)
{
if(DstArchetype.ComponentTypes[i].ID == SrcArchetype.ComponentTypes[j].ID)
{
SrcStartIndex++;
component_struct_info ComponentInfo =
World->Runtime->ComponentStructInfos[DstArchetype.ComponentTypes[i].ID];
uint8_t* DstComponentAddress =
GetComponentAddress(DstChunk, DstStorage.IndexInChunk,
DstArchetype.ComponentTypes[i].Offset, ComponentInfo.Size);
uint8_t* SrcComponentAddress =
GetComponentAddress(SrcChunk, SrcStorage.IndexInChunk,
SrcArchetype.ComponentTypes[j].Offset, ComponentInfo.Size);
memcpy(DstComponentAddress, SrcComponentAddress, ComponentInfo.Size);
break;
}
}
}
}
archetype*
GetMatchingArchetype(ecs_runtime* Runtime, const archetype& Archetype)
{
archetype* MatchingArchetype = NULL;
for(int i = 0; i < Runtime->Archetypes.Count; i++)
{
if(Archetype.ComponentTypes.Count == Runtime->Archetypes[i].ComponentTypes.Count &&
Runtime->Archetypes[i].FirstChunk &&
memcmp(Archetype.ComponentTypes.Elements, Runtime->Archetypes[i].ComponentTypes.Elements,
Archetype.ComponentTypes.Count * sizeof(component_id_and_offset)) == 0)
{
MatchingArchetype = &Runtime->Archetypes[i];
break;
}
}
return MatchingArchetype;
}
void
AddComponent(ecs_world* World, entity_id EntityID, component_id ComponentID)
{
assert(DoesEntityExist(World, EntityID));
const archetype* PreviousArchetype = GetEntityArchetype(World, EntityID);
archetype TempArchetype = {};
TempArchetype.ComponentTypes.Clear();
if(PreviousArchetype) // If entity had any components
{
int32_t ComponentIndex = GetComponentIndexInArchetype(*PreviousArchetype, ComponentID);
assert(ComponentIndex == -1 && "assert: trying to add a component for the second time");
TempArchetype.ComponentTypes = PreviousArchetype->ComponentTypes;
}
int32_t NewComponentIndex =
AddComponentWithoutLosingCanonicalForm(&TempArchetype, ComponentID, World->Runtime);
archetype* MatchedArchetype = GetMatchingArchetype(World->Runtime, TempArchetype);
archetype* NewArchetype = NULL;
if(MatchedArchetype)
{
NewArchetype = MatchedArchetype;
}
else
{
NewArchetype = AddArchetype(World->Runtime, TempArchetype);
}
entity_storage_info NewEntityStorage = CreateNewArchetypeInstance(World->Runtime, NewArchetype);
if(PreviousArchetype)
{
CopyMatchingComponentValues(World, NewEntityStorage, World->Entities[EntityID], *NewArchetype,
*PreviousArchetype);
}
// Zero out the new component
{
component_id_and_offset NewComponentIDAndOffset =
NewArchetype->ComponentTypes[NewComponentIndex];
component_struct_info ComponentInfo =
World->Runtime->ComponentStructInfos[NewComponentIDAndOffset.ID];
uint8_t* NewComponentAddress =
GetComponentAddress(World, NewEntityStorage, NewComponentIDAndOffset.Offset, ComponentInfo);
memset(NewComponentAddress, 0, (size_t)ComponentInfo.Size);
}
RemoveEntityFromChunk(World, World->Entities[EntityID]);
World->Entities[EntityID] = NewEntityStorage;
}
void
RemoveComponent(ecs_world* World, entity_id EntityID, component_id ComponentID)
{
assert(DoesEntityExist(World, EntityID));
const archetype* PreviousArchetype = GetEntityArchetype(World, EntityID);
archetype TempArchetype = {};
TempArchetype.ComponentTypes.Clear();
if(PreviousArchetype) // If entity had any components
{
int32_t ComponentIndex = GetComponentIndexInArchetype(*PreviousArchetype, ComponentID);
assert(ComponentIndex != -1 && "assert: trying to remove a not-added component which");
TempArchetype.ComponentTypes = PreviousArchetype->ComponentTypes;
}
RemoveComponentWithoutLosingCanonicalForm(&TempArchetype, ComponentID, World->Runtime);
entity_storage_info NewEntityStorage = { -1, 0 };
if(0 < TempArchetype.ComponentTypes.Count)
{
archetype* MatchedArchetype = GetMatchingArchetype(World->Runtime, TempArchetype);
archetype* NewArchetype = NULL;
if(MatchedArchetype)
{
NewArchetype = MatchedArchetype;
}
else
{
NewArchetype = AddArchetype(World->Runtime, TempArchetype);
}
NewEntityStorage = CreateNewArchetypeInstance(World->Runtime, NewArchetype);
if(PreviousArchetype)
{
CopyMatchingComponentValues(World, NewEntityStorage, World->Entities[EntityID], *NewArchetype,
*PreviousArchetype);
}
}
RemoveEntityFromChunk(World, World->Entities[EntityID]);
World->Entities[EntityID] = NewEntityStorage;
}
void*
GetComponent_(ecs_world* World, entity_id EntityID, size_t ComponentSize, component_id ComponentID)
{
assert(DoesEntityExist(World, EntityID));
component_struct_info ComponentInfo = World->Runtime->ComponentStructInfos[ComponentID];
assert(ComponentInfo.Size == ComponentSize);
const archetype* Archetype = GetEntityArchetype(World, EntityID);
int32_t ComponentIndex = GetComponentIndexInArchetype(*Archetype, ComponentID);
entity_storage_info EntityStorage = World->Entities[EntityID];
uint8_t* DesiredComponent =
GetComponentAddress(GetChunkAtIndex(World->Runtime, EntityStorage.ChunkIndex),
EntityStorage.IndexInChunk,
Archetype->ComponentTypes[ComponentIndex].Offset, ComponentInfo.Size);
return DesiredComponent;
}
void
SetComponent_(ecs_world* World, entity_id EntityID, const void* ComponentValue,
size_t ComponentSize, component_id ComponentID)
{
void* DesiredComponent = GetComponent_(World, EntityID, ComponentSize, ComponentID);
memcpy(DesiredComponent, ComponentValue, ComponentSize);
}