2022-12-13 04:36:51 +00:00
|
|
|
/*
|
|
|
|
ecs.c
|
|
|
|
|
|
|
|
Entity Component System
|
|
|
|
|
|
|
|
Copyright (C) 2022 Bill Currke
|
|
|
|
|
|
|
|
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 2
|
|
|
|
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, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/heapsort.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
|
|
|
|
#define IMPLEMENT_ECS_Funcs
|
|
|
|
#include "QF/ecs.h"
|
|
|
|
|
|
|
|
VISIBLE ecs_registry_t *
|
|
|
|
ECS_NewRegistry (void)
|
|
|
|
{
|
|
|
|
ecs_registry_t *reg = calloc (1, sizeof (ecs_registry_t));
|
2022-12-13 13:58:44 +00:00
|
|
|
reg->components = (componentset_t) DARRAY_STATIC_INIT (32);
|
2022-12-13 04:36:51 +00:00
|
|
|
reg->next = Ent_Index (nullent);
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
ECS_DelRegistry (ecs_registry_t *registry)
|
|
|
|
{
|
2023-03-05 12:29:26 +00:00
|
|
|
registry->locked = 1;
|
|
|
|
for (uint32_t i = 0; i < registry->components.size; i++) {
|
|
|
|
__auto_type comp = ®istry->components.a[i];
|
|
|
|
__auto_type pool = ®istry->comp_pools[i];
|
|
|
|
Component_DestroyElements (comp, pool->data, 0, pool->count);
|
|
|
|
}
|
2022-12-13 04:36:51 +00:00
|
|
|
free (registry->entities);
|
2022-12-13 13:58:44 +00:00
|
|
|
for (uint32_t i = 0; i < registry->components.size; i++) {
|
2022-12-13 04:36:51 +00:00
|
|
|
free (registry->comp_pools[i].sparse);
|
|
|
|
free (registry->comp_pools[i].dense);
|
|
|
|
free (registry->comp_pools[i].data);
|
2022-12-18 12:11:21 +00:00
|
|
|
|
2023-03-05 12:29:26 +00:00
|
|
|
free (registry->subpools[i].sorted);
|
2022-12-18 12:11:21 +00:00
|
|
|
free (registry->subpools[i].ranges);
|
|
|
|
free (registry->subpools[i].rangeids);
|
2022-12-13 04:36:51 +00:00
|
|
|
}
|
2023-03-05 12:29:26 +00:00
|
|
|
DARRAY_CLEAR (®istry->components);
|
2022-12-18 12:11:21 +00:00
|
|
|
free (registry->subpools);
|
2022-12-13 04:36:51 +00:00
|
|
|
free (registry->comp_pools);
|
2023-03-05 12:29:26 +00:00
|
|
|
PR_RESDELMAP (registry->hierarchies);
|
2022-12-13 04:36:51 +00:00
|
|
|
free (registry);
|
|
|
|
}
|
|
|
|
|
2022-12-13 13:58:44 +00:00
|
|
|
VISIBLE uint32_t
|
2022-12-13 04:36:51 +00:00
|
|
|
ECS_RegisterComponents (ecs_registry_t *registry,
|
|
|
|
const component_t *components, uint32_t count)
|
|
|
|
{
|
2022-12-13 13:58:44 +00:00
|
|
|
uint32_t base = registry->components.size;
|
|
|
|
DARRAY_RESIZE (®istry->components, base + count);
|
|
|
|
memcpy (registry->components.a + base, components,
|
|
|
|
count * sizeof (component_t));
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
ECS_CreateComponentPools (ecs_registry_t *registry)
|
|
|
|
{
|
|
|
|
uint32_t count = registry->components.size;
|
2022-12-13 04:36:51 +00:00
|
|
|
registry->comp_pools = calloc (count, sizeof (ecs_pool_t));
|
|
|
|
size_t size = registry->max_entities * sizeof (uint32_t);
|
2022-12-13 13:58:44 +00:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2022-12-13 04:36:51 +00:00
|
|
|
registry->comp_pools[i].sparse = malloc (size);
|
|
|
|
memset (registry->comp_pools[i].sparse, nullent, size);
|
|
|
|
}
|
2022-12-18 12:11:21 +00:00
|
|
|
registry->subpools = calloc (count, sizeof (ecs_subpool_t));
|
2022-12-13 04:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
__compar_d_fn_t cmp;
|
|
|
|
void *arg;
|
|
|
|
ecs_pool_t *pool;
|
|
|
|
const component_t *comp;
|
|
|
|
} ecs_sort_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
ecs_compare (const void *a, const void *b, void *arg)
|
|
|
|
{
|
|
|
|
ecs_sort_t *sortctx = arg;
|
|
|
|
return sortctx->cmp (a, b, sortctx->arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
swap_uint32 (uint32_t *a, uint32_t *b)
|
|
|
|
{
|
|
|
|
uint32_t t = *a;
|
|
|
|
*a = *b;
|
|
|
|
*b = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ecs_swap (void *_a, void *_b, void *arg)
|
|
|
|
{
|
|
|
|
ecs_sort_t *sortctx = arg;
|
|
|
|
size_t size = sortctx->comp->size;
|
|
|
|
ecs_pool_t *pool = sortctx->pool;
|
|
|
|
uint32_t *a = _a;
|
|
|
|
uint32_t *b = _b;
|
|
|
|
uint32_t a_ind = a - pool->dense;
|
|
|
|
uint32_t b_ind = b - pool->dense;
|
|
|
|
uint32_t a_ent_ind = Ent_Index (pool->dense[a_ind]);
|
|
|
|
uint32_t b_ent_ind = Ent_Index (pool->dense[b_ind]);
|
|
|
|
__auto_type a_data = (byte *) pool->data + a_ind * size;
|
|
|
|
__auto_type b_data = (byte *) pool->data + b_ind * size;
|
|
|
|
Component_SwapElements (sortctx->comp, a_data, b_data);
|
|
|
|
swap_uint32 (a, b);
|
|
|
|
swap_uint32 (&pool->sparse[a_ent_ind], &pool->sparse[b_ent_ind]);
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
2023-01-16 02:32:12 +00:00
|
|
|
ECS_SortComponentPoolRange (ecs_registry_t *registry, uint32_t component,
|
|
|
|
ecs_range_t range, __compar_d_fn_t cmp, void *arg)
|
2022-12-13 04:36:51 +00:00
|
|
|
{
|
2022-12-13 13:58:44 +00:00
|
|
|
if (component >= registry->components.size) {
|
2023-01-16 02:32:12 +00:00
|
|
|
Sys_Error ("ECS_SortComponentPoolRange: invalid component: %u",
|
|
|
|
component);
|
2022-12-13 04:36:51 +00:00
|
|
|
}
|
|
|
|
ecs_pool_t *pool = ®istry->comp_pools[component];
|
2023-01-16 02:32:12 +00:00
|
|
|
if (!pool->count || range.end <= range.start) {
|
2022-12-13 04:36:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-01-16 02:32:12 +00:00
|
|
|
uint32_t count = range.end - range.start;
|
|
|
|
uint32_t *start = pool->dense + range.start;
|
2022-12-13 13:58:44 +00:00
|
|
|
__auto_type comp = ®istry->components.a[component];
|
2022-12-13 04:36:51 +00:00
|
|
|
ecs_sort_t sortctx = { .cmp = cmp, .arg = arg, .pool = pool, .comp = comp };
|
2023-01-16 02:32:12 +00:00
|
|
|
heapsort_s (start, count, sizeof (uint32_t),
|
2022-12-13 04:36:51 +00:00
|
|
|
ecs_compare, ecs_swap, &sortctx);
|
|
|
|
}
|
|
|
|
|
2023-01-16 02:32:12 +00:00
|
|
|
VISIBLE void
|
|
|
|
ECS_SortComponentPool (ecs_registry_t *registry, uint32_t component,
|
|
|
|
__compar_d_fn_t cmp, void *arg)
|
|
|
|
{
|
|
|
|
if (component >= registry->components.size) {
|
|
|
|
Sys_Error ("ECS_SortComponentPool: invalid component: %u", component);
|
|
|
|
}
|
|
|
|
ecs_pool_t *pool = ®istry->comp_pools[component];
|
|
|
|
ecs_range_t range = { .start = 0, .end = pool->count };
|
|
|
|
ECS_SortComponentPoolRange (registry, component, range, cmp, arg);
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:36:51 +00:00
|
|
|
VISIBLE uint32_t
|
|
|
|
ECS_NewEntity (ecs_registry_t *registry)
|
|
|
|
{
|
|
|
|
uint32_t ent;
|
|
|
|
if (registry->available) {
|
|
|
|
registry->available--;
|
|
|
|
uint32_t next = registry->next;
|
|
|
|
ent = next | Ent_Generation (registry->entities[next]);
|
|
|
|
registry->next = Ent_Index (registry->entities[next]);
|
|
|
|
registry->entities[next] = ent;
|
|
|
|
} else {
|
|
|
|
if (registry->num_entities == Ent_Index (nullent)) {
|
|
|
|
Sys_Error ("ECS_NewEntity: out of entities");
|
|
|
|
}
|
|
|
|
if (registry->num_entities == registry->max_entities) {
|
|
|
|
registry->max_entities += ENT_GROW;
|
|
|
|
size_t size = registry->max_entities * sizeof (uint32_t);
|
|
|
|
registry->entities = realloc (registry->entities, size);
|
2022-12-13 13:58:44 +00:00
|
|
|
for (uint32_t i = 0; i < registry->components.size; i++) {
|
2022-12-13 04:36:51 +00:00
|
|
|
uint32_t *sparse = registry->comp_pools[i].sparse;
|
|
|
|
sparse = realloc (sparse, size);
|
|
|
|
memset (sparse + registry->max_entities - ENT_GROW, nullent,
|
|
|
|
ENT_GROW * sizeof (uint32_t));
|
|
|
|
registry->comp_pools[i].sparse = sparse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ent = registry->num_entities++;
|
|
|
|
// ent starts out with generation 0
|
|
|
|
registry->entities[ent] = ent;
|
|
|
|
}
|
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
ECS_DelEntity (ecs_registry_t *registry, uint32_t ent)
|
|
|
|
{
|
2023-03-05 12:29:26 +00:00
|
|
|
if (registry->locked) {
|
|
|
|
// the registry is being deleted and mass entity and component
|
|
|
|
// deletions are going on
|
|
|
|
return;
|
|
|
|
}
|
2022-12-13 04:36:51 +00:00
|
|
|
uint32_t next = registry->next | Ent_NextGen (Ent_Generation (ent));
|
|
|
|
uint32_t id = Ent_Index (ent);
|
|
|
|
|
2022-12-13 13:58:44 +00:00
|
|
|
for (uint32_t i = 0; i < registry->components.size; i++) {
|
2022-12-13 04:36:51 +00:00
|
|
|
Ent_RemoveComponent (ent, i, registry);
|
|
|
|
}
|
2023-03-05 12:29:26 +00:00
|
|
|
registry->entities[id] = next;
|
|
|
|
registry->next = id;
|
|
|
|
registry->available++;
|
2022-12-13 04:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
ECS_RemoveEntities (ecs_registry_t *registry, uint32_t component)
|
|
|
|
{
|
|
|
|
ecs_pool_t *pool = ®istry->comp_pools[component];
|
2022-12-13 13:58:44 +00:00
|
|
|
const component_t *comp = ®istry->components.a[component];
|
2022-12-13 04:36:51 +00:00
|
|
|
__auto_type destroy = comp->destroy;
|
|
|
|
if (destroy) {
|
|
|
|
byte *data = registry->comp_pools[component].data;
|
|
|
|
for (uint32_t i = 0; i < pool->count; i++) {
|
|
|
|
destroy (data + i * comp->size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pool->count = 0;
|
|
|
|
}
|