[scene] Make Ent_AddComponent more raw

It no longer initializes the new component. For that, use
Ent_SetComponent which will copy in the provided data or call the
component's create function if the data pointer is null (in which case,
Ent_SetComponent acts as Ent_SetComponent used to).
This commit is contained in:
Bill Currie 2022-10-25 11:34:24 +09:00
parent 4759be449f
commit 5b0810f4c2
3 changed files with 49 additions and 33 deletions

View file

@ -76,13 +76,14 @@ typedef struct ecs_registry_s {
COMPINLINE void Component_ResizeArray (const component_t *component,
void **array, uint32_t count);
COMPINLINE void Component_MoveElements (const component_t *component,
void *array, uint32_t dstIndex,
uint32_t srcIndex, uint32_t count);
COMPINLINE void Component_CopyElements (const component_t *component,
void *dstArray, uint32_t dstIndex,
const void *srcArray, uint32_t srcIndex,
uint32_t count);
COMPINLINE void *Component_MoveElements (const component_t *component,
void *array, uint32_t dstIndex,
uint32_t srcIndex, uint32_t count);
COMPINLINE void *Component_CopyElements (const component_t *component,
void *dstArray, uint32_t dstIndex,
const void *srcArray,
uint32_t srcIndex,
uint32_t count);
COMPINLINE void *Component_CreateElements (const component_t *component,
void *array,
uint32_t index, uint32_t count);
@ -98,6 +99,8 @@ COMPINLINE int Ent_HasComponent (uint32_t ent, uint32_t comp,
ecs_registry_t *reg);
COMPINLINE void *Ent_GetComponent (uint32_t ent, uint32_t comp,
ecs_registry_t *reg);
COMPINLINE void *Ent_SetComponent (uint32_t ent, uint32_t comp,
ecs_registry_t *registry, const void *data);
#undef COMPINLINE
#ifndef IMPLEMENT_COMPONENT_Funcs
@ -113,17 +116,17 @@ Component_ResizeArray (const component_t *component,
*array = realloc (*array, count * component->size);
}
COMPINLINE void
COMPINLINE void *
Component_MoveElements (const component_t *component,
void *array, uint32_t dstIndex, uint32_t srcIndex,
uint32_t count)
{
__auto_type dst = (byte *) array + dstIndex * component->size;
__auto_type src = (byte *) array + srcIndex * component->size;
memmove (dst, src, count * component->size);
return memmove (dst, src, count * component->size);
}
COMPINLINE void
COMPINLINE void *
Component_CopyElements (const component_t *component,
void *dstArray, uint32_t dstIndex,
const void *srcArray, uint32_t srcIndex,
@ -131,7 +134,7 @@ Component_CopyElements (const component_t *component,
{
__auto_type dst = (byte *) dstArray + dstIndex * component->size;
__auto_type src = (byte *) srcArray + srcIndex * component->size;
memcpy (dst, src, count * component->size);
return memcpy (dst, src, count * component->size);
}
COMPINLINE void *
@ -215,6 +218,20 @@ void *Ent_AddComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry);
void Ent_RemoveComponent (uint32_t ent, uint32_t comp,
ecs_registry_t *registry);
COMPINLINE void *
Ent_SetComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry,
const void *data)
{
void *dst = Ent_AddComponent (ent, comp, registry);
if (data) {
return Component_CopyElements (&registry->components[comp],
dst, 0, data, 0, 1);
} else {
return Component_CreateElements (&registry->components[comp],
dst, 0, 1);
}
}
///@}
#endif//__QF_scene_component_h

View file

@ -71,24 +71,22 @@ ECS_RegisterComponents (ecs_registry_t *registry,
VISIBLE void *
Ent_AddComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
{
uint32_t id = Ent_Index (ent);
ecs_pool_t *pool = &registry->comp_pools[comp];
if (pool->sparse[id] < pool->count
&& pool->dense[pool->sparse[id]] == ent) {
return Ent_GetComponent (ent, comp, registry);
uint32_t id = Ent_Index (ent);
uint32_t ind = pool->sparse[id];
if (ind >= pool->count || pool->dense[ind] != ent) {
if (pool->count == pool->max_count) {
pool->max_count += COMP_GROW;
pool->dense = realloc (pool->dense,
pool->max_count * sizeof (uint32_t));
Component_ResizeArray (&registry->components[comp], &pool->data,
pool->max_count);
}
uint32_t ind = pool->count++;
pool->sparse[id] = ind;
pool->dense[ind] = ent;
}
if (pool->count == pool->max_count) {
pool->max_count += COMP_GROW;
pool->dense = realloc (pool->dense,
pool->max_count * sizeof (uint32_t));
Component_ResizeArray (&registry->components[comp], &pool->data,
pool->max_count);
}
uint32_t ind = pool->count++;
pool->sparse[id] = ind;
pool->dense[ind] = ent;
return Component_CreateElements (&registry->components[comp], pool->data,
ind, 1);
return Ent_GetComponent (ent, comp, registry);
}
VISIBLE void

View file

@ -176,15 +176,16 @@ Scene_DeleteScene (scene_t *scene)
entity_t
Scene_CreateEntity (scene_t *scene)
{
// Transform_New creates an entity and adds a scene_href component to the
// entity
transform_t trans = Transform_New (scene, nulltransform);
uint32_t id = trans.id;
Ent_AddComponent (id, scene_href, scene->reg);
Ent_AddComponent (id, scene_animation, scene->reg);
Ent_AddComponent (id, scene_visibility, scene->reg);
Ent_AddComponent (id, scene_renderer, scene->reg);
Ent_AddComponent (id, scene_active, scene->reg);
Ent_AddComponent (id, scene_old_origin, scene->reg);
Ent_SetComponent (id, scene_animation, scene->reg, 0);
Ent_SetComponent (id, scene_visibility, scene->reg, 0);
Ent_SetComponent (id, scene_renderer, scene->reg, 0);
Ent_SetComponent (id, scene_active, scene->reg, 0);
Ent_SetComponent (id, scene_old_origin, scene->reg, 0);
renderer_t *renderer = Ent_GetComponent (id, scene_renderer, scene->reg);
QuatSet (1, 1, 1, 1, renderer->colormod);