[scene] Manage scene roots in transform

Since transforms now know the scene to which they belong, and they know
when they are root and when not, getting the transform code to manage
the scene roots is the best way to keep the list of root transforms
consistent.
This commit is contained in:
Bill Currie 2022-03-02 10:43:52 +09:00
parent 54c3b4cc53
commit b210f01837
3 changed files with 17 additions and 4 deletions

View file

@ -11,4 +11,7 @@ typedef struct scene_resources_s {
PR_RESMAP (transform_t) transforms;
} scene_resources_t;
void scene_add_root (scene_t *scene, transform_t *transform);
void scene_del_root (scene_t *scene, transform_t *transform);
#endif//__scn_internal_h

View file

@ -88,7 +88,6 @@ Scene_CreateEntity (scene_t *scene)
hierarchy_t *h = ent->transform->hierarchy;
h->entity.a[ent->transform->index] = ent;
DARRAY_APPEND (&scene->roots, ent->transform);
return ent;
}
@ -99,8 +98,16 @@ Scene_GetEntity (scene_t *scene, int id)
return PR_RESGET (res->entities, id);
}
static void
unroot_transform (scene_t *scene, transform_t *transform)
void
scene_add_root (scene_t *scene, transform_t *transform)
{
if (!Transform_GetParent (transform)) {
DARRAY_APPEND (&scene->roots, transform);
}
}
void
scene_del_root (scene_t *scene, transform_t *transform)
{
if (!Transform_GetParent (transform)) {
for (size_t i = 0; i < scene->roots.size; i++) {
@ -138,7 +145,6 @@ Scene_DestroyEntity (scene_t *scene, entity_t *ent)
if (PR_RESGET (res->entities, ent->id) != ent) {
Sys_Error ("Scene_DestroyEntity: entity not owned by scene");
}
unroot_transform (scene, ent->transform);
// pull the transform out of the hierarchy to make it easier to destory
// all the child entities
Transform_SetParent (ent->transform, 0);

View file

@ -58,6 +58,7 @@ Transform_New (scene_t *scene, transform_t *parent)
} else {
transform->hierarchy = Hierarchy_New (16, 1);//FIXME should be config
transform->index = 0;
scene_add_root (scene, transform);
}
transform->hierarchy->transform.a[transform->index] = transform;
Hierarchy_UpdateMatrices (transform->hierarchy);
@ -72,6 +73,7 @@ Transform_Delete (transform_t *transform)
// hierarchy so deleting it is easier
Transform_SetParent (transform, 0);
}
scene_del_root (transform->scene, transform);
Hierarchy_Delete (transform->hierarchy);
}
@ -87,6 +89,7 @@ void
Transform_SetParent (transform_t *transform, transform_t *parent)
{
if (parent) {
scene_del_root (transform->scene, transform);
hierarchy_t *hierarchy = transform->hierarchy;
uint32_t index = transform->index;
Hierarchy_InsertHierarchy (parent->hierarchy, hierarchy,
@ -108,6 +111,7 @@ Transform_SetParent (transform_t *transform, transform_t *parent)
Hierarchy_InsertHierarchy (new_hierarchy, hierarchy, null_transform,
index);
Hierarchy_RemoveHierarchy (hierarchy, index);
scene_add_root (transform->scene, transform);
}
}