This repository was archived by the owner on Apr 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecs.h
483 lines (424 loc) · 14.6 KB
/
secs.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
/*
secs.h -- https://github.com/takeiteasy/secs
Copyright (C) 2024 George Watson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SECS_HEAD
#define SECS_HEAD
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
#ifdef SECS_ENABLE_BLOCKS
#ifndef BLOCKS
#error "This platform doesn't support BLOCKS"
#endif
#endif
#if defined(_WIN32) || defined(_WIN64)
#define SECS_ON_WINDOWS
#endif
#if defined(SECS_ON_WINDOWS) && !defined(SECS_NO_EXPORT)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
typedef union entity {
struct {
uint32_t id;
uint16_t version;
uint8_t alive;
uint8_t type;
};
uint64_t value;
} entity_t;
extern const uint64_t ecs_nil;
extern const entity_t ecs_nil_entity;
typedef struct world world_t;
#ifdef SECS_ENABLE_BLOCKS
typedef void(^system_t)(entity_t);
typedef int(^filter_system_t)(entity_t);
#else
typedef void(*system_t)(entity_t);
typedef int(*filter_system_t)(entity_t);
#endif
enum {
ECS_ENTITY,
ECS_COMPONENT,
ECS_SYSTEM
};
EXPORT world_t* ecs_world(void);
EXPORT void ecs_world_destroy(world_t **world);
EXPORT entity_t ecs_spawn(world_t *world);
EXPORT entity_t ecs_component(world_t *world, size_t size_of_component);
EXPORT entity_t ecs_system(world_t *world, system_t fn, int component_count, ...);
EXPORT void ecs_delete(world_t *world, entity_t e);
EXPORT int entity_isvalid(world_t *world, entity_t e);
EXPORT int entity_isa(world_t *world, entity_t e, int type);
EXPORT int entity_cmp(entity_t a, entity_t b);
EXPORT int entity_isnil(entity_t e);
EXPORT void* entity_give(world_t *world, entity_t e, entity_t c);
EXPORT void entity_remove(world_t *world, entity_t e, entity_t c);
EXPORT int entity_has(world_t *world, entity_t e, entity_t c);
EXPORT void* entity_get(world_t *world, entity_t e, entity_t c);
EXPORT void entity_set(world_t *world, entity_t e, entity_t c, void *data);
EXPORT void ecs_step(world_t *world);
EXPORT entity_t* ecs_find(world_t *world, filter_system_t filter, int *result_count, int component_count, ...);
EXPORT void ecs_query(world_t *world, system_t fn, filter_system_t filter, int component_count, ...);
#ifdef __cplusplus
}
#endif
#endif // SECS_HEAD
#ifdef SECS_IMPLEMENTATION
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
const uint64_t ecs_nil = 0xFFFFFFFFull;
const entity_t ecs_nil_entity = {.id=ecs_nil};
typedef struct sparse {
entity_t *sparse;
entity_t *dense;
size_t sizeOfSparse;
size_t sizeOfDense;
} sparse_t;
static sparse_t* sparse(void) {
sparse_t *result = malloc(sizeof(sparse_t));
memset(result, 0, sizeof(sparse_t));
return result;
}
static void sparse_destroy(sparse_t **sparse) {
sparse_t *s = *sparse;
if (s->sparse)
free(s->sparse);
if (s->dense)
free(s->dense);
free(s);
*sparse = NULL;
}
static int sparse_has(sparse_t *sparse, entity_t e) {
return e.id < sparse->sizeOfSparse && !entity_isnil(sparse->sparse[e.id]);
}
static void sparse_emplace(sparse_t *sparse, entity_t e) {
if (e.id > sparse->sizeOfSparse) {
size_t size = e.id + 1;
sparse->sparse = realloc(sparse->sparse, size * sizeof * sparse->sparse);
for (size_t i = sparse->sizeOfSparse; i < size; i++)
sparse->sparse[i] = ecs_nil_entity;
sparse->sizeOfSparse = size;
}
sparse->sparse[e.id] = (entity_t){.id=(uint32_t)sparse->sizeOfDense};
sparse->dense = realloc(sparse->dense, (sparse->sizeOfDense + 1) * sizeof * sparse->dense);
sparse->dense[sparse->sizeOfDense++] = e;
}
static size_t sparse_at(sparse_t *sparse, entity_t e) {
return sparse->sparse[e.id].id;
}
static size_t sparse_remove(sparse_t *sparse, entity_t e) {
assert(sparse_has(sparse, e));
uint32_t pos = sparse_at(sparse, e);
entity_t other = sparse->dense[sparse->sizeOfDense-1];
sparse->sparse[other.id] = (entity_t){.id=pos};
sparse->dense[pos] = other;
sparse->sparse[e.id] = ecs_nil_entity;
sparse->dense = realloc(sparse->dense, --sparse->sizeOfDense * sizeof * sparse->dense);
return pos;
}
typedef struct storage {
entity_t componentId;
void *data;
size_t sizeOfData;
size_t sizeOfComponent;
sparse_t *sparse;
} storage_t;
static storage_t* storage(entity_t e, size_t sz) {
storage_t *storage = malloc(sizeof(storage_t));
*storage = (storage_t) {
.componentId = e,
.sizeOfComponent = sz,
.sizeOfData = 0,
.data = NULL,
.sparse = sparse()
};
return storage;
}
static void storage_destroy(storage_t **strg) {
storage_t* s = *strg;
if (s->sparse)
sparse_destroy(&s->sparse);
if (s->data)
free(s->data);
free(s);
*strg = NULL;
}
static int storage_has(storage_t *strg, entity_t e) {
return sparse_has(strg->sparse, e);
}
static void* storage_emplace(storage_t *strg, entity_t e) {
strg->data = realloc(strg->data, ++strg->sizeOfData * sizeof(char) * strg->sizeOfComponent);
void *result = &((char*)strg->data)[(strg->sizeOfData - 1) * sizeof(char) * strg->sizeOfComponent];
sparse_emplace(strg->sparse, e);
return result;
}
static void storage_remove(storage_t *strg, entity_t e) {
size_t pos = sparse_remove(strg->sparse, e);
memmove(&((char*)strg->data)[pos * sizeof(char) * strg->sizeOfComponent],
&((char*)strg->data)[(strg->sizeOfData - 1) * sizeof(char) * strg->sizeOfComponent],
strg->sizeOfComponent);
strg->data = realloc(strg->data, --strg->sizeOfData * sizeof(char) * strg->sizeOfComponent);
}
static void* storage_get(storage_t *strg, entity_t e) {
uint32_t pos = sparse_at(strg->sparse, e);
return &((char*)strg->data)[pos * sizeof(char) * strg->sizeOfComponent];
}
struct world {
storage_t **storages;
storage_t *systems;
size_t sizeOfStorages;
entity_t *entities;
size_t sizeOfEntities;
uint32_t *recyclable;
size_t sizeOfRecyclable;
};
typedef struct {
uint32_t id;
uint32_t component_count;
entity_t *components;
system_t callback;
} system_component_t;
static entity_t make_entity(world_t *world, uint8_t type) {
if (world->sizeOfRecyclable) {
uint32_t id = world->recyclable[world->sizeOfRecyclable-1];
entity_t old = world->entities[id];
entity_t new = (entity_t) {
.id = old.id,
.version = old.version,
.alive = 1,
.type = type
};
world->entities[id] = new;
world->recyclable = realloc(world->recyclable, --world->sizeOfRecyclable * sizeof(uint32_t));
return new;
} else {
world->entities = realloc(world->entities, ++world->sizeOfEntities * sizeof(entity_t));
entity_t e = (entity_t) {
.id = (uint32_t)world->sizeOfEntities - 1,
.version = 0,
.alive = 1,
.type = type
};
world->entities[e.id] = e;
return e;
}
}
world_t* ecs_world(void) {
world_t *result = malloc(sizeof(world_t));
memset(result, 0, sizeof(world_t));
entity_t e = make_entity(result, ECS_COMPONENT); // doesn't matter will always be first entity
result->systems = storage(e, sizeof(system_component_t));
return result;
}
void ecs_world_destroy(world_t **_world) {
world_t *world = *_world;
if (world->storages) {
for (int i = 0; i < world->sizeOfStorages; i++)
storage_destroy(&world->storages[i]);
free(world->storages);
}
if (world->entities)
free(world->entities);
if (world->recyclable)
free(world->recyclable);
if (world->systems)
storage_destroy(&world->systems);
free(world);
*_world = NULL;
}
static storage_t* find_storage(world_t *world, entity_t e) {
for (int i = 0; i < world->sizeOfStorages; i++)
if (entity_cmp(e, world->storages[i]->componentId))
return world->storages[i];
return NULL;
}
entity_t ecs_spawn(world_t *world) {
return make_entity(world, ECS_ENTITY);
}
entity_t ecs_component(world_t *world, size_t sizeOfComponent) {
entity_t e = make_entity(world, ECS_COMPONENT);
storage_t *strg = find_storage(world, e);
if (strg)
return e;
strg = storage(e, sizeOfComponent);
world->storages = realloc(world->storages, (world->sizeOfStorages + 1) * sizeof * world->storages);
world->storages[world->sizeOfStorages++] = strg;
return e;
}
static entity_t* vargs_components(world_t *world, int n, va_list args) {
entity_t *result = malloc(n * sizeof(entity_t));
for (int i = 0; i < n; i++) {
entity_t component = va_arg(args, entity_t);
if (!entity_isa(world, component, ECS_COMPONENT)) {
free(result);
result = NULL;
goto BAIL;
}
result[i] = component;
}
BAIL:
va_end(args);
return result;
}
entity_t ecs_system(world_t *world, system_t system, int n, ...) {
entity_t e = make_entity(world, ECS_SYSTEM);
va_list args;
va_start(args, n);
system_component_t *system_data = malloc(sizeof(system_component_t));
system_data->id = e.id;
system_data->component_count = n;
system_data->components = vargs_components(world, n, args);
system_data->callback = system;
void *ptr = storage_emplace(world->systems, e);
memcpy(ptr, system_data, sizeof(system_component_t));
return e;
}
void ecs_delete(world_t *world, entity_t e) {
switch (e.type) {
case ECS_ENTITY:
for (size_t i = world->sizeOfStorages; i; --i)
if (world->storages[i - 1] && sparse_has(world->storages[i - 1]->sparse, e))
storage_remove(world->storages[i - 1], e);
world->entities[e.id] = (entity_t) {
.id = e.id,
.version = e.version + 1,
.alive = 0,
.type = 255
};
world->recyclable = realloc(world->recyclable, ++world->sizeOfRecyclable * sizeof(uint32_t));
world->recyclable[world->sizeOfRecyclable-1] = e.id;
break;
case ECS_COMPONENT:
// TODO: !
break;
case ECS_SYSTEM:
// TODO: !
break;
}
}
int entity_isvalid(world_t *world, entity_t e) {
return world->sizeOfEntities > e.id && entity_cmp(world->entities[e.id], e);
}
int entity_isa(world_t *world, entity_t e, int type) {
return entity_isvalid(world, e) && e.type == type;
}
int entity_cmp(entity_t a, entity_t b) {
return a.value == b.value;
}
int entity_isnil(entity_t e) {
return e.id == ecs_nil;
}
static storage_t* find_entity_storage(world_t *world, entity_t e, entity_t c) {
assert(entity_isa(world, e, ECS_ENTITY));
assert(entity_isa(world, c, ECS_COMPONENT));
storage_t *strg = find_storage(world, c);
assert(strg);
return strg;
}
void* entity_give(world_t *world, entity_t e, entity_t c) {
return storage_emplace(find_entity_storage(world, e, c), e);
}
void entity_remove(world_t *world, entity_t e, entity_t c) {
storage_t *strg = find_entity_storage(world, e, c);
assert(storage_has(strg, e));
storage_remove(strg, e);
}
void* entity_get(world_t *world, entity_t e, entity_t c) {
storage_t *strg = find_entity_storage(world, e, c);
return storage_has(strg, e) ? storage_get(strg, e) : NULL;
}
void entity_set(world_t *world, entity_t e, entity_t c, void *data) {
storage_t *strg = find_entity_storage(world, e, c);
memcpy(storage_has(strg, e) ? storage_get(strg, e) : storage_emplace(strg, e),
data,
strg->sizeOfComponent);
}
int entity_has(world_t *world, entity_t e, entity_t c) {
storage_t *strg = find_storage(world, c);
if (!strg)
return 0;
return storage_has(strg, e);
}
entity_t* ecs_find(world_t *world, filter_system_t filter, int *result_count, int n, ...) {
va_list args;
va_start(args, n);
entity_t *components = vargs_components(world, n, args);
entity_t *result = NULL;
int count = 0;
for (int i = 0; i < world->sizeOfEntities; i++) {
int match = 1;
for (int j = 0; j < n; j++) {
storage_t *strg = find_storage(world, components[j]);
assert(strg);
if (!storage_has(strg, world->entities[i])) {
match = 0;
break;
}
}
if (match && !(filter && !filter(world->entities[i]))) {
result = realloc(result, count + 1 * sizeof(entity_t));
result[count++] = world->entities[i];
}
}
if (result_count)
*result_count = count;
return result;
}
void ecs_query(world_t *world, system_t fn, filter_system_t filter, int n, ...) {
va_list args;
va_start(args, n);
entity_t *components = vargs_components(world, n, args);
for (int i = 0; i < world->sizeOfEntities; i++) {
int match = 1;
for (int j = 0; j < n; j++) {
storage_t *strg = find_storage(world, components[j]);
assert(strg);
if (!storage_has(strg, world->entities[i])) {
match = 0;
break;
}
}
if (match && !(filter && !filter(world->entities[i])))
fn(world->entities[i]);
}
}
void ecs_step(world_t *world) {
for (int i = 0; i < world->systems->sparse->sizeOfDense; i++) {
system_component_t *system_data = storage_get(world->systems, world->systems->sparse->dense[i]);
entity_t system_entity = world->entities[system_data->id];
assert(entity_isa(world, system_entity, ECS_SYSTEM));
if (!system_entity.alive)
continue;
for (int j = 0; j < world->sizeOfEntities; j++) {
int match = 1;
for (int k = 0; k < system_data->component_count; k++) {
storage_t *strg = find_storage(world, system_data->components[k]);
assert(strg);
if (!storage_has(strg, world->entities[j])) {
match = 0;
break;
}
}
if (match)
system_data->callback(world->entities[j]);
}
}
}
#endif