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
|
#include "storage.h"
#include "bitset.h"
component_pair_t *
findPair(storage_t *this, const char *name)
{
for (int i = 0; i < this->componentsStored; ++i)
{
component_pair_t *pair = &this->objects[i];
if (strcmp(name, pair->name) == 0)
{
return pair;
}
}
return NULL;
}
entity_t
createEntity(storage_t *this)
{
entity_t idx = this->objectsStored++;
for (int i = 0; i < this->componentsStored; ++i)
{
component_pair_t pair = this->objects[i];
memset(&pair.objects[idx * pair.objectSize], 0, pair.objectSize);
}
return idx;
}
void
registerSystem(storage_t *storage, system_t system, bitset_t components)
{
int idx = storage->systemsStored++;
storage->systems[idx] = system;
bitarena_copy(&storage->systemSignatures, components, idx);
}
bitset_t
createSignature(storage_t *storage, char **components)
{
bitset_t bitset = bitset_create();
for (int i = 0; components[i] != NULL; ++i)
{
const char *componentName = components[i];
component_pair_t *pair = findPair(storage, componentName);
if (pair == NULL)
{
return NULL;
}
else
{
bitset_set(bitset, pair->idx);
}
}
return bitset;
}
// Not intended for use outside of macros
void
internal_registerComponent(storage_t *storage, const char *name, size_t size)
{
unsigned int componentIdx = storage->componentsStored++;
storage->objects[componentIdx] = (component_pair_t) {
.idx = componentIdx,
.name = name,
.objectSize = size,
.objects = malloc(sizeof(char) * size * MAX_OBJECTS)
};
}
char * // NULL if component with such name is not found
internal_addComponent(storage_t *this, entity_t idx, const char *name)
{
component_pair_t *pair = findPair(this, name);
if (pair == NULL)
{
return NULL;
}
// TODO: add bitset implementation
bitset_t bitset = bitarena_at(&this->objectSignatures, idx);
bitset_set(bitset, pair->idx);
return &pair->objects[idx];
}
// Not intended for use outside of macros
char * // NULL if component is not found
internal_getComponent(storage_t *this, entity_t idx, const char *name)
{
for (int i = 0; i < this->componentsStored; ++i)
{
component_pair_t *pair = &this->objects[i];
if (strcmp(name, pair->name) == 0)
{
return &pair->objects[idx * pair->objectSize];
}
}
return NULL;
}
|