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
|
#ifndef world_hpp
#define world_hpp
struct Arena;
#include <stdint.h>
#include <new>
#include <utility>
using Component_Mask = uint64_t;
using Entity_Id = uint32_t;
#define max_entities 1024
#define pool_cap 128
#define max_components 64
struct Pool {
Component_Mask mask;
Pool* next;
void* data;
Entity_Id entities[pool_cap];
int mapping[max_entities];
int offsets[max_components];
int size, count;
void init(Arena* a, Component_Mask m);
void* add(Arena* a, Entity_Id eid);
void* get(Entity_Id eid, int cid);
void* get(Entity_Id eid);
void remove(Entity_Id eid);
};
int entity_version(Entity_Id e);
int entity_index(Entity_Id e);
Entity_Id entity_id(int index, int version);
int get_new_component_id(int size);
struct World {
static constexpr int max_pools = 32;
Pool pools[max_pools];
Component_Mask masks[max_entities];
uint8_t versions[max_entities];
Entity_Id entities[max_entities];
Entity_Id freelist[max_entities];
Arena* arena;
int count, free_count;
void init(Arena* a);
uint64_t hash_mask(Component_Mask m);
Pool& get_pool(Component_Mask m);
template <typename T>
int get_component_id() {
static int id = get_new_component_id(sizeof(T));
return id;
}
Entity_Id create_entity();
template <typename... T>
Component_Mask get_mask() {
return (... | ((Component_Mask)1 << get_component_id<T>()));
}
template <typename... T>
std::tuple<T&...> add(Entity_Id e) {
Component_Mask m = get_mask<T...>();
add(e, m);
Pool& p = get_pool(masks[entity_index(e)]);
return { *(T*)p.get(e, get_component_id<T>())... };
}
template <typename T>
T& get(Entity_Id e) {
int id = get_component_id<T>();
return *(T*)get(e, id);
}
template <typename T>
void remove(Entity_Id e) {
int id = get_component_id<T>();
remove(e, id);
}
template <typename T>
bool has(Entity_Id e) {
int id = get_component_id<T>();
Component_Mask m = masks[entity_index(e)];
return (m & ((Component_Mask)1 << id)) != 0;
}
void* add(Entity_Id eid, Component_Mask m);
void* get(Entity_Id eid, int cid);
void remove(Entity_Id eid, int cid);
void destroy(Entity_Id e);
struct View {
World* w;
Pool* pools[max_pools];
int pool_count;
struct Iter {
View* v;
int ptr, ind;
bool equals(const Iter& other) {
return ptr == other.ptr && ind == other.ind;
}
bool operator==(const Iter& other) {
return equals(other);
}
bool operator!=(const Iter& other) {
return !equals(other);
}
Iter operator++() {
Pool* p = v->pools[ind];
ptr++;
if (ptr == p->count) {
ptr = 0;
ind++;
}
return *this;
}
template <typename C>
C& get() {
Pool* p = v->pools[ind];
return *(C*)(
(char*)p->data + ptr * p->size +
p->offsets[v->w->get_component_id<C>()]
);
}
Iter& operator*() {
return *this;
}
Entity_Id entity() {
Pool* p = v->pools[ind];
return p->entities[ptr];
}
};
Iter begin() {
Iter i;
i.v = this;
i.ind = 0;
i.ptr = 0;
return i;
}
Iter end() {
Iter i;
i.v = this;
i.ind = pool_count;
i.ptr = 0;
return i;
}
};
template <typename... T>
View view() {
int i;
auto m = get_mask<T...>();
View v;
v.w = this;
v.pool_count = 0;
for (i = 0; i < max_pools; i++) {
Pool& p = pools[i];
if (p.count && m == (p.mask & m))
v.pools[v.pool_count++] = &p;
}
return v;
}
};
#endif
|