2021-07-24 05:19:52 +00:00
|
|
|
/*
|
|
|
|
scene.c
|
|
|
|
|
|
|
|
General scene handling
|
|
|
|
|
|
|
|
Copyright (C) 2021 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
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-04 09:00:35 +00:00
|
|
|
#include "QF/mathlib.h"
|
2021-07-24 05:19:52 +00:00
|
|
|
#include "QF/progs.h" // for PR_RESMAP
|
2022-02-14 07:41:38 +00:00
|
|
|
#include "QF/sys.h"
|
2022-05-05 12:30:14 +00:00
|
|
|
#include "QF/model.h"
|
2021-07-24 05:19:52 +00:00
|
|
|
|
|
|
|
#include "QF/scene/entity.h"
|
|
|
|
#include "QF/scene/scene.h"
|
|
|
|
#include "QF/scene/transform.h"
|
|
|
|
|
2022-02-14 11:01:36 +00:00
|
|
|
#include "scn_internal.h"
|
2021-07-24 05:19:52 +00:00
|
|
|
|
2022-05-05 14:45:21 +00:00
|
|
|
static byte empty_visdata[] = { 0x01 };
|
|
|
|
|
2022-05-05 12:30:14 +00:00
|
|
|
static mleaf_t empty_leafs[] = {
|
|
|
|
[1] = {
|
|
|
|
.contents = CONTENTS_EMPTY,
|
|
|
|
.mins = {-INFINITY, -INFINITY, -INFINITY},
|
|
|
|
.maxs = { INFINITY, INFINITY, INFINITY},
|
2022-05-05 14:45:21 +00:00
|
|
|
.compressed_vis = empty_visdata,
|
2022-05-05 12:30:14 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static mnode_t *empty_leaf_parents[] = {
|
|
|
|
[1] = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int empty_leaf_flags[] = {
|
2022-05-05 14:45:21 +00:00
|
|
|
[1] = SURF_DRAWSKY,
|
2022-05-05 12:30:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static char empty_entities[] = { 0 };
|
|
|
|
|
|
|
|
static model_t empty_world = {
|
|
|
|
.type = mod_brush,
|
|
|
|
.radius = INFINITY,
|
|
|
|
.mins = {-INFINITY, -INFINITY, -INFINITY},
|
|
|
|
.maxs = { INFINITY, INFINITY, INFINITY},
|
|
|
|
.brush = {
|
|
|
|
.modleafs = 2,
|
|
|
|
.visleafs = 1,
|
|
|
|
.nodes = (mnode_t *) &empty_leafs[1],
|
|
|
|
.leafs = empty_leafs,
|
|
|
|
.entities = empty_entities,
|
2022-05-05 14:45:21 +00:00
|
|
|
.visdata = empty_visdata,
|
2022-05-05 12:30:14 +00:00
|
|
|
.leaf_parents = empty_leaf_parents,
|
|
|
|
.leaf_flags = empty_leaf_flags,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-07-24 05:19:52 +00:00
|
|
|
scene_t *
|
|
|
|
Scene_NewScene (void)
|
|
|
|
{
|
|
|
|
scene_t *scene;
|
|
|
|
scene_resources_t *res;
|
|
|
|
|
|
|
|
scene = calloc (1, sizeof (scene_t));
|
|
|
|
res = calloc (1, sizeof (scene_resources_t));
|
|
|
|
*(scene_resources_t **)&scene->resources = res;
|
|
|
|
|
2022-05-05 12:30:14 +00:00
|
|
|
scene->worldmodel = &empty_world;
|
|
|
|
|
2021-07-24 05:19:52 +00:00
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
2022-02-14 07:41:38 +00:00
|
|
|
void
|
|
|
|
Scene_DeleteScene (scene_t *scene)
|
|
|
|
{
|
|
|
|
Scene_FreeAllEntities (scene);
|
|
|
|
|
|
|
|
scene_resources_t *res = scene->resources;
|
|
|
|
for (unsigned i = 0; i < res->entities._size; i++) {
|
|
|
|
free (res->entities._map[i]);
|
|
|
|
}
|
|
|
|
free (res->entities._map);
|
|
|
|
|
|
|
|
free (scene->resources);
|
|
|
|
free (scene);
|
|
|
|
}
|
|
|
|
|
2021-07-24 05:19:52 +00:00
|
|
|
entity_t *
|
|
|
|
Scene_CreateEntity (scene_t *scene)
|
|
|
|
{
|
|
|
|
scene_resources_t *res = scene->resources;
|
|
|
|
|
2022-05-06 15:51:44 +00:00
|
|
|
entity_t *ent = PR_RESNEW (res->entities);
|
2022-02-14 11:01:36 +00:00
|
|
|
ent->transform = Transform_New (scene, 0);
|
2022-02-14 07:41:38 +00:00
|
|
|
ent->id = PR_RESINDEX (res->entities, ent);
|
|
|
|
|
|
|
|
hierarchy_t *h = ent->transform->hierarchy;
|
|
|
|
h->entity.a[ent->transform->index] = ent;
|
|
|
|
|
2022-05-04 09:00:35 +00:00
|
|
|
QuatSet (1, 1, 1, 1, ent->renderer.colormod);
|
|
|
|
|
2021-07-24 05:19:52 +00:00
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
2022-02-14 07:41:38 +00:00
|
|
|
entity_t *
|
|
|
|
Scene_GetEntity (scene_t *scene, int id)
|
2021-07-24 05:19:52 +00:00
|
|
|
{
|
|
|
|
scene_resources_t *res = scene->resources;
|
2022-02-14 07:41:38 +00:00
|
|
|
return PR_RESGET (res->entities, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy_entity (scene_t *scene, entity_t *ent)
|
|
|
|
{
|
|
|
|
scene_resources_t *res = scene->resources;
|
|
|
|
// ent->transform will be trampled by the loop below
|
|
|
|
transform_t *transform = ent->transform;
|
|
|
|
|
|
|
|
// Transform_Delete takes care of all hierarchy stuff (transforms
|
|
|
|
// themselves, name strings, hierarchy table)
|
|
|
|
hierarchy_t *h = transform->hierarchy;
|
|
|
|
for (size_t i = 0; i < h->entity.size; i++) {
|
|
|
|
entity_t *e = h->entity.a[0];
|
|
|
|
e->transform = 0;
|
|
|
|
PR_RESFREE (res->entities, ent);
|
|
|
|
}
|
|
|
|
Transform_Delete (transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Scene_DestroyEntity (scene_t *scene, entity_t *ent)
|
|
|
|
{
|
|
|
|
scene_resources_t *res = scene->resources;
|
|
|
|
|
|
|
|
if (PR_RESGET (res->entities, ent->id) != ent) {
|
|
|
|
Sys_Error ("Scene_DestroyEntity: entity not owned by scene");
|
|
|
|
}
|
|
|
|
// pull the transform out of the hierarchy to make it easier to destory
|
|
|
|
// all the child entities
|
|
|
|
Transform_SetParent (ent->transform, 0);
|
|
|
|
destroy_entity (scene, ent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Scene_FreeAllEntities (scene_t *scene)
|
|
|
|
{
|
2022-03-03 21:43:30 +00:00
|
|
|
while (scene->hierarchies) {
|
|
|
|
hierarchy_t *h = scene->hierarchies;
|
2022-02-14 07:41:38 +00:00
|
|
|
// deleting the root entity deletes all child entities
|
|
|
|
entity_t *ent = h->entity.a[0];
|
|
|
|
destroy_entity (scene, ent);
|
|
|
|
}
|
|
|
|
scene_resources_t *res = scene->resources;
|
2021-07-24 05:19:52 +00:00
|
|
|
PR_RESRESET (res->entities);
|
|
|
|
}
|
2022-02-14 15:06:39 +00:00
|
|
|
|
|
|
|
transform_t *
|
|
|
|
Scene_GetTransform (scene_t *scene, int id)
|
|
|
|
{
|
|
|
|
scene_resources_t *res = scene->resources;
|
|
|
|
return PR_RESGET (res->transforms, id);
|
|
|
|
}
|