2022-12-18 12:11:21 +00:00
|
|
|
/*
|
|
|
|
subpool.c
|
|
|
|
|
|
|
|
ECS subpool management
|
|
|
|
|
|
|
|
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/sys.h"
|
|
|
|
|
|
|
|
#include "QF/ecs.h"
|
|
|
|
|
|
|
|
VISIBLE uint32_t
|
|
|
|
ECS_NewSubpoolRange (ecs_registry_t *registry, uint32_t component)
|
|
|
|
{
|
|
|
|
ecs_subpool_t *subpool = ®istry->subpools[component];
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t num_ranges = subpool->num_ranges - subpool->available;
|
|
|
|
if (subpool->available) {
|
|
|
|
subpool->available--;
|
|
|
|
uint32_t next = subpool->next;
|
|
|
|
id = next | Ent_Generation (subpool->rangeids[next]);
|
|
|
|
subpool->next = Ent_Index (subpool->rangeids[next]);
|
|
|
|
subpool->rangeids[next] = id;
|
|
|
|
subpool->sorted[next] = num_ranges;
|
|
|
|
} else {
|
|
|
|
if (subpool->num_ranges == Ent_Index (nullent)) {
|
|
|
|
Sys_Error ("ECS_NewEntity: out of rangeids");
|
|
|
|
}
|
|
|
|
if (subpool->num_ranges == subpool->max_ranges) {
|
|
|
|
subpool->max_ranges += RANGE_GROW;
|
|
|
|
size_t idsize = subpool->max_ranges * sizeof (uint32_t);
|
|
|
|
size_t rsize = subpool->max_ranges * sizeof (ecs_range_t);
|
|
|
|
subpool->rangeids = realloc (subpool->rangeids, idsize);
|
|
|
|
subpool->sorted = realloc (subpool->sorted, idsize);
|
|
|
|
subpool->ranges = realloc (subpool->ranges, rsize);
|
|
|
|
}
|
|
|
|
id = subpool->num_ranges++;
|
|
|
|
// id starts out with generation 0
|
|
|
|
subpool->rangeids[id] = id;
|
|
|
|
subpool->sorted[id] = num_ranges;
|
|
|
|
}
|
|
|
|
uint32_t end = 0;
|
|
|
|
if (num_ranges) {
|
2022-12-20 08:56:08 +00:00
|
|
|
end = subpool->ranges[num_ranges - 1];
|
2022-12-18 12:11:21 +00:00
|
|
|
}
|
2022-12-20 08:56:08 +00:00
|
|
|
subpool->ranges[subpool->sorted[Ent_Index (id)]] = end;
|
2022-12-18 12:11:21 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
ECS_DelSubpoolRange (ecs_registry_t *registry, uint32_t component, uint32_t id)
|
|
|
|
{
|
|
|
|
ecs_subpool_t *subpool = ®istry->subpools[component];
|
|
|
|
uint32_t next = subpool->next | Ent_NextGen (Ent_Generation (id));
|
|
|
|
uint32_t ind = Ent_Index (id);
|
|
|
|
uint32_t count = subpool->num_ranges - subpool->available;
|
2023-07-09 02:52:11 +00:00
|
|
|
uint32_t range = subpool->sorted[ind];
|
2022-12-18 12:11:21 +00:00
|
|
|
subpool->rangeids[ind] = next;
|
|
|
|
subpool->next = ind;
|
|
|
|
subpool->available++;
|
2023-07-10 15:06:21 +00:00
|
|
|
memmove (subpool->ranges + range, subpool->ranges + range + 1,
|
|
|
|
(subpool->num_ranges - 1 - range) * sizeof (ecs_range_t));
|
2022-12-18 12:11:21 +00:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2023-07-09 02:52:11 +00:00
|
|
|
if (subpool->sorted[i] > range) {
|
2022-12-18 12:11:21 +00:00
|
|
|
subpool->sorted[i]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-08 10:55:37 +00:00
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
ECS_MoveSubpoolLast (ecs_registry_t *registry, uint32_t component, uint32_t id)
|
|
|
|
{
|
|
|
|
ecs_pool_t *pool = ®istry->comp_pools[component];
|
|
|
|
ecs_subpool_t *subpool = ®istry->subpools[component];
|
|
|
|
uint32_t ind = Ent_Index (id);
|
|
|
|
component_t *c = ®istry->components.a[component];
|
|
|
|
uint32_t range = subpool->sorted[ind];
|
|
|
|
uint32_t num_ranges = subpool->num_ranges - subpool->available;
|
|
|
|
uint32_t last_range = num_ranges - 1;
|
|
|
|
|
|
|
|
uint32_t start = range ? subpool->ranges[range - 1] : 0;
|
|
|
|
uint32_t end = subpool->ranges[range];
|
|
|
|
uint32_t last = subpool->ranges[last_range];
|
|
|
|
uint32_t count = end - start;
|
|
|
|
uint32_t srcIndex = start;
|
|
|
|
uint32_t dstIndex = last - count;
|
|
|
|
for (uint32_t i = range; i < last_range; i++) {
|
|
|
|
subpool->ranges[i] = subpool->ranges[i + 1] - count;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < num_ranges; i++) {
|
|
|
|
if (subpool->sorted[i] > range) {
|
|
|
|
subpool->sorted[i]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
subpool->sorted[ind] = last_range;
|
|
|
|
Component_RotateElements (c, pool->data, dstIndex, srcIndex, count);
|
|
|
|
}
|