2022-10-09 17:00:33 +00:00
|
|
|
/*
|
|
|
|
component.c
|
|
|
|
|
|
|
|
Component handling
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-10-16 09:29:25 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2022-10-09 17:00:33 +00:00
|
|
|
#define IMPLEMENT_COMPONENT_Funcs
|
|
|
|
#include "QF/scene/component.h"
|
2022-10-16 09:29:25 +00:00
|
|
|
|
|
|
|
VISIBLE ecs_registry_t *
|
|
|
|
ECS_NewRegistry (void)
|
|
|
|
{
|
|
|
|
ecs_registry_t *reg = calloc (1, sizeof (ecs_registry_t));
|
2022-10-21 10:12:20 +00:00
|
|
|
reg->next = Ent_Index (nullent);
|
2022-10-16 09:29:25 +00:00
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
ECS_DelRegistry (ecs_registry_t *registry)
|
|
|
|
{
|
|
|
|
free (registry->entities);
|
|
|
|
for (uint32_t i = 0; i < registry->num_components; i++) {
|
|
|
|
free (registry->comp_pools[i].sparse);
|
|
|
|
free (registry->comp_pools[i].dense);
|
|
|
|
free (registry->comp_pools[i].data);
|
|
|
|
}
|
|
|
|
free (registry->comp_pools);
|
|
|
|
free (registry);
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
ECS_RegisterComponents (ecs_registry_t *registry,
|
|
|
|
const component_t *components, uint32_t count)
|
|
|
|
{
|
|
|
|
registry->num_components = count;
|
|
|
|
registry->components = components;
|
|
|
|
registry->comp_pools = calloc (count, sizeof (ecs_pool_t));
|
|
|
|
size_t size = registry->max_entities * sizeof (uint32_t);
|
|
|
|
for (uint32_t i = 0; i < registry->num_components; i++) {
|
|
|
|
registry->comp_pools[i].sparse = malloc (size);
|
2022-10-21 10:12:20 +00:00
|
|
|
memset (registry->comp_pools[i].sparse, nullent, size);
|
2022-10-16 09:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 00:50:14 +00:00
|
|
|
VISIBLE void *
|
2022-10-16 09:29:25 +00:00
|
|
|
Ent_AddComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
|
|
|
|
{
|
|
|
|
uint32_t id = Ent_Index (ent);
|
|
|
|
ecs_pool_t *pool = ®istry->comp_pools[comp];
|
2022-10-25 00:50:14 +00:00
|
|
|
if (pool->sparse[id] < pool->count
|
|
|
|
&& pool->dense[pool->sparse[id]] == ent) {
|
|
|
|
return Ent_GetComponent (ent, comp, registry);
|
2022-10-16 09:29:25 +00:00
|
|
|
}
|
|
|
|
if (pool->count == pool->max_count) {
|
|
|
|
pool->max_count += COMP_GROW;
|
|
|
|
pool->dense = realloc (pool->dense,
|
|
|
|
pool->max_count * sizeof (uint32_t));
|
|
|
|
Component_ResizeArray (®istry->components[comp], &pool->data,
|
|
|
|
pool->max_count);
|
|
|
|
}
|
|
|
|
uint32_t ind = pool->count++;
|
|
|
|
pool->sparse[id] = ind;
|
|
|
|
pool->dense[ind] = ent;
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
return Component_CreateElements (®istry->components[comp], pool->data,
|
|
|
|
ind, 1);
|
2022-10-16 09:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
Ent_RemoveComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
|
|
|
|
{
|
|
|
|
uint32_t id = Ent_Index (ent);
|
|
|
|
ecs_pool_t *pool = ®istry->comp_pools[comp];
|
|
|
|
uint32_t ind = pool->sparse[id];
|
2022-10-25 00:55:08 +00:00
|
|
|
if (ind < pool->count && pool->dense[ind] == ent) {
|
2022-10-16 09:29:25 +00:00
|
|
|
uint32_t last = pool->count - 1;
|
|
|
|
Component_DestroyElements (®istry->components[comp], pool->data,
|
|
|
|
ind, 1);
|
|
|
|
if (last > ind) {
|
|
|
|
pool->sparse[Ent_Index (pool->dense[last])] = ind;
|
|
|
|
pool->dense[ind] = pool->dense[last];
|
|
|
|
Component_MoveElements (®istry->components[comp], pool->data,
|
|
|
|
ind, last, 1);
|
|
|
|
}
|
|
|
|
pool->count--;
|
2022-10-21 10:12:20 +00:00
|
|
|
pool->sparse[id] = nullent;
|
2022-10-16 09:29:25 +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 {
|
2022-10-21 10:12:20 +00:00
|
|
|
if (registry->num_entities == Ent_Index (nullent)) {
|
2022-10-16 09:29:25 +00:00
|
|
|
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);
|
|
|
|
for (uint32_t i = 0; i < registry->num_components; i++) {
|
|
|
|
uint32_t *sparse = registry->comp_pools[i].sparse;
|
|
|
|
sparse = realloc (sparse, size);
|
2022-10-21 10:12:20 +00:00
|
|
|
memset (sparse + registry->max_entities - ENT_GROW, nullent,
|
2022-10-16 09:29:25 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
uint32_t next = registry->next | Ent_NextGen (Ent_Generation (ent));
|
|
|
|
uint32_t id = Ent_Index (ent);
|
|
|
|
registry->entities[id] = next;
|
|
|
|
registry->next = id;
|
|
|
|
registry->available++;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < registry->num_components; i++) {
|
|
|
|
Ent_RemoveComponent (ent, i, registry);
|
|
|
|
}
|
|
|
|
}
|