mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-04-08 10:21:40 +00:00
[ecs] Add support for sorting component pools
As the bookkeeping data is spread between three arrays, sorting a component pool is not trivial and thus not something to duplicate around the codebase.
This commit is contained in:
parent
962319af09
commit
cf911884c6
2 changed files with 78 additions and 0 deletions
|
@ -84,6 +84,8 @@ COMPINLINE void Component_ResizeArray (const component_t *component,
|
|||
COMPINLINE void *Component_MoveElements (const component_t *component,
|
||||
void *array, uint32_t dstIndex,
|
||||
uint32_t srcIndex, uint32_t count);
|
||||
COMPINLINE void Component_SwapElements (const component_t *component,
|
||||
void *a, void *b);
|
||||
COMPINLINE void *Component_CopyElements (const component_t *component,
|
||||
void *dstArray, uint32_t dstIndex,
|
||||
const void *srcArray,
|
||||
|
@ -131,6 +133,16 @@ Component_MoveElements (const component_t *component,
|
|||
return memmove (dst, src, count * component->size);
|
||||
}
|
||||
|
||||
COMPINLINE void
|
||||
Component_SwapElements (const component_t *component, void *a, void *b)
|
||||
{
|
||||
size_t size = component->size;
|
||||
byte tmp[size];
|
||||
memcpy (tmp, a, size);
|
||||
memcpy (a, b, size);
|
||||
memcpy (b, tmp, size);
|
||||
}
|
||||
|
||||
COMPINLINE void *
|
||||
Component_CopyElements (const component_t *component,
|
||||
void *dstArray, uint32_t dstIndex,
|
||||
|
@ -217,6 +229,13 @@ void ECS_DelRegistry (ecs_registry_t *registry);
|
|||
void ECS_RegisterComponents (ecs_registry_t *registry,
|
||||
const component_t *components, uint32_t count);
|
||||
|
||||
#ifndef __compar_d_fn_t_defined
|
||||
#define __compar_d_fn_t_defined
|
||||
typedef int (*__compar_d_fn_t)(const void *, const void *, void *);
|
||||
#endif
|
||||
void ECS_SortComponentPool (ecs_registry_t *registry, uint32_t component,
|
||||
__compar_d_fn_t cmp, void *arg);
|
||||
|
||||
uint32_t ECS_NewEntity (ecs_registry_t *registry);
|
||||
void ECS_DelEntity (ecs_registry_t *registry, uint32_t ent);
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#include "QF/heapsort.h"
|
||||
#include "QF/sys.h"
|
||||
|
||||
#define IMPLEMENT_COMPONENT_Funcs
|
||||
|
@ -69,6 +70,64 @@ ECS_RegisterComponents (ecs_registry_t *registry,
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
ECS_SortComponentPool (ecs_registry_t *registry, uint32_t component,
|
||||
__compar_d_fn_t cmp, void *arg)
|
||||
{
|
||||
if (component >= registry->num_components) {
|
||||
Sys_Error ("ECS_SortComponentPool: invalid component: %u", component);
|
||||
}
|
||||
ecs_pool_t *pool = ®istry->comp_pools[component];
|
||||
if (!pool->count) {
|
||||
return;
|
||||
}
|
||||
__auto_type comp = ®istry->components[component];
|
||||
ecs_sort_t sortctx = { .cmp = cmp, .arg = arg, .pool = pool, .comp = comp };
|
||||
heapsort_s (pool->dense, pool->count, sizeof (uint32_t),
|
||||
ecs_compare, ecs_swap, &sortctx);
|
||||
}
|
||||
|
||||
VISIBLE void *
|
||||
Ent_AddComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue