polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit 240e9581a202f39037bc124f0bf4457ee110974c
parent addcea9881d5d29c905a7af5a7a2a3953a3925bb
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 23 Sep 2021 14:29:15 -0700

remove static resources

Diffstat:
M.rgignore | 4++--
MMakefile | 1+
Mdefault.nix | 2+-
Mmain.c | 1-
Msrc/chess.c | 14+++++++-------
Msrc/entity.c | 24+++++++-----------------
Msrc/entity.h | 7+------
Msrc/game.c | 10++++------
Msrc/geometry.c | 8+-------
Msrc/model.c | 127++++++++++++++++++++++++++++++++++++++++---------------------------------------
Msrc/model.h | 25++++++++++++-------------
Msrc/node.c | 6+-----
Msrc/render.c | 1-
Msrc/resource.c | 65++++++++++++++---------------------------------------------------
Msrc/resource.h | 22+---------------------
Msrc/scene.c | 8++++----
Msrc/skybox.c | 2+-
Msrc/skybox.h | 2+-
Dsrc/static_resources.h | 28----------------------------
Msrc/terrain.c | 6++----
Msrc/update.c | 2+-
Mtest/test_dae.c | 2+-
Mtest/test_resource.c | 24++++++++++++------------
Mtest/test_scene.c | 9++++-----
24 files changed, 142 insertions(+), 258 deletions(-)

diff --git a/.rgignore b/.rgignore @@ -1,3 +1,4 @@ *.dae *.ply -gamecontrollerdb.txt- \ No newline at end of file +/tags +gamecontrollerdb.txt diff --git a/Makefile b/Makefile @@ -8,6 +8,7 @@ DEFS= -DGLFW_INCLUDE_NONE -DDEBUG # CFLAGS = $(DEFS) -ggdb -O0 -I src -Wall -Wextra -std=c99 \ BASE_CFLAGS = $(DEFS) -O2 -g -I src -Wall -Werror -Wextra -std=c99 \ + -DSTBI_ONLY_TGA \ -Wno-unused-function \ -Wno-unused-parameter \ -Wno-unused-variable \ diff --git a/default.nix b/default.nix @@ -14,7 +14,7 @@ stdenv.mkDerivation rec { makeFlags = "PREFIX=$(out)"; - nativeBuildInputs = with pkgs; [ tinycc pkg-config gdb emscripten binaryen ]; + nativeBuildInputs = with pkgs; [ tinycc pkg-config gdb emscripten binaryen wabt ]; buildInputs = with pkgs; [ SDL2 mesa libglvnd ] ++ (with xorg; [ libX11 libxcb libXau libXdmcp libXext libXcursor diff --git a/main.c b/main.c @@ -45,7 +45,6 @@ int main(void) int hello; int height = 480; game_init(&game, width, height); - reset_scene(&game); default_scene(&game); //chess_scene(&game); diff --git a/src/chess.c b/src/chess.c @@ -131,11 +131,11 @@ static void position_piece(struct entity *piece, int x, int y) static void setup_pieces(node_id *chessboard) { struct model *pirate_officer; - get_static_model(model_pirate_officer, &pirate_officer); + get_model_by_name("pirate_officer", &pirate_officer); // make pawn model - model_id pawn_model_id; - init_id(&pawn_model_id); + struct model_id pawn_model_id; + init_id(&pawn_model_id.id); struct model *pawn_model = new_model(&pawn_model_id); pawn_model->geom_id = pirate_officer->geom_id; pawn_model->shader = CHESS_PIECE_PROGRAM; @@ -162,7 +162,7 @@ static void setup_pieces(node_id *chessboard) { struct entity *rook = new_entity(NULL); struct model *rook_model; struct node *rook_node = get_node(&rook->node_id); - rook->model_id = get_static_model(model_tower, &rook_model); + rook->model_id = get_model_by_name("tower", &rook_model); rook->flags |= ENT_CASTS_SHADOWS | ENT_IS_WHITE; rook_model->shader = CHESS_PIECE_PROGRAM; node_attach(&rook->node_id, chessboard); @@ -185,7 +185,7 @@ void chess_scene(struct game *game) struct node *node = get_node(&ent->node_id); assert(node); struct entity *player = get_player(&game->test_resources); - ent->model_id = get_static_model(model_icosphere, NULL); + ent->model_id = get_model_by_name("icosphere", NULL); node_set_label(node, "sphere"); // @@ -194,8 +194,8 @@ void chess_scene(struct game *game) geometry_id chessboard_geom; make_chessboard_geom(&chessboard_geom); - model_id chessboard_model_id; - init_id(&chessboard_model_id); + struct model_id chessboard_model_id; + init_id(&chessboard_model_id.id); struct model *chessboard_model = new_model(&chessboard_model_id); chessboard_model->shader = DEFAULT_PROGRAM; diff --git a/src/entity.c b/src/entity.c @@ -21,7 +21,7 @@ struct entity *get_all_entities(u32 *count, entity_id **ids) { struct entity *init_entity(struct entity *ent, node_id *id) { node_id new_id; - init_id(&ent->model_id); + init_id(&ent->model_id.id); if (id == NULL) { init_id(&new_id); new_node(&new_id); @@ -67,12 +67,13 @@ struct entity *add_entity(struct entity *e, entity_id *id) { return new; } -void destroy_entities() { +void destroy_entities() +{ entity_id *ids; u32 count; get_all_resources(&esys, &count, &ids); - for (u32 i = RESERVED_ENTITIES; i < count; i++) { - destroy_entity(&ids[i]); + for (u32 i = 0; i < count; i++) { + destroy_entity(&ids[i]); } }; @@ -87,11 +88,6 @@ void destroy_entity(entity_id *id) if (node == NULL) return; - if (id->index < RESERVED_ENTITIES) { - unusual("trying to destroy reserved entity with node %s\n", node->label); - return; - } - node_detach_from_parent(node); destroy_resource(&esys, id); } @@ -108,20 +104,14 @@ void destroy_entity_system() { void init_entity_system() { init_resource_manager(&esys, sizeof(struct entity), DEF_NUM_ENTITIES, - MAX_ENTITIES, "entity", RESERVED_ENTITIES); - - for (int i = 0; i < RESERVED_ENTITIES; i++) { - node_id node_id = make_static_id(i); - struct entity *ent = &static_entities()[i]; - init_entity(ent, &node_id); - } + MAX_ENTITIES, "entity"); } struct entity *new_debug_entity(entity_id *ent_id, float *pos) { init_id(ent_id); struct entity *ent = new_entity(ent_id); - ent->model_id = get_static_model(model_barrel, NULL); + ent->model_id = get_model_by_name("barrel", NULL); struct node *enode = get_node(&ent->node_id); node_set_label(enode, "debug"); vec3_copy(pos, enode->pos); diff --git a/src/entity.h b/src/entity.h @@ -20,7 +20,7 @@ enum entity_flags { struct entity { node_id node_id; - model_id model_id; + struct model_id model_id; u32 flags; float velocity[3]; float accel[3]; @@ -41,11 +41,6 @@ struct entity *new_entity_with_node(entity_id *, node_id *); struct entity *new_debug_entity(entity_id *, float *pos); void destroy_entity_system(); -static inline struct entity *static_entities() -{ - return (struct entity*)_internal_get_entity_system()->resources; -} - static inline struct entity *new_entity(entity_id *id) { if (id) diff --git a/src/game.c b/src/game.c @@ -157,15 +157,13 @@ void init_misc(struct game *game, int width, int height) { // player entity init_id(&res->player_id); - player = &static_entities()[entity_player]; + player = new_entity(&res->player_id); player->flags |= ENT_CASTS_SHADOWS; - struct node *pnode = &static_nodes()[node_player]; + struct node *pnode = get_node(&player->node_id); assert(pnode); - res->player_id = make_static_id(entity_player); - assert(res->player_id.index == entity_player); - struct model *pmodel; - player->model_id = get_static_model(model_pirate_officer, &pmodel); + player->model_id = get_model_by_name("pirate_officer", NULL); + assert(!is_null_id(&player->model_id.id)); node_set_label(pnode, "player"); /* node_rotate(pnode, V3(-5.0,0,0)); */ diff --git a/src/geometry.c b/src/geometry.c @@ -3,7 +3,6 @@ #include "util.h" #include "resource.h" #include "debug.h" -#include "static_resources.h" #include <assert.h> struct resource_manager geom_manager; @@ -165,12 +164,7 @@ void geometry_centroid(struct geometry *geom, float *dest) { void init_geometry_manager() { init_resource_manager(&geom_manager, sizeof(struct geometry), - DEF_NUM_GEOMETRY, MAX_GEOMETRY, "geometry", - NUM_STATIC_MODELS); - - for (int i = 0; i < NUM_STATIC_MODELS; i++) { - init_geometry(&static_geometry()[i]); - } + DEF_NUM_GEOMETRY, MAX_GEOMETRY, "geometry"); } struct geometry *get_geometry(geometry_id *geom_id) { diff --git a/src/model.c b/src/model.c @@ -8,14 +8,6 @@ #define MODELDEF(name) { .id = model_##name, .loaded = 0, .file = #name } -static struct model_def static_model_defs[NUM_STATIC_MODELS] = { - MODELDEF(cube), - MODELDEF(tower), - MODELDEF(icosphere), - MODELDEF(pirate_officer), - MODELDEF(barrel), -}; - static struct resource_manager dyn_modelman; struct model *init_model(struct model *model) { @@ -29,25 +21,15 @@ struct model *init_model(struct model *model) { return model; } -static inline struct model *static_models() -{ - return (struct model *)dyn_modelman.resources; -} - - -static void initialize_static_models() { - struct model *models = static_models(); - for (int i = 0; i < NUM_STATIC_MODELS; i++) { - struct model *sm = &models[i]; - init_model(sm); - } +struct model *get_all_models(u32 *count, struct model_id **ids) { + return get_all_resources(&dyn_modelman, count, (struct resource_id **)ids); } -static inline struct model *new_uninitialized_model(struct resource_id *id) { - return new_resource(&dyn_modelman, id); +static inline struct model *new_uninitialized_model(struct model_id *id) { + return new_resource(&dyn_modelman, &id->id); } -static struct model *new_model_resource(model_id *model_id) +static struct model *new_model_resource(struct model_id *model_id) { struct model *model = new_uninitialized_model(model_id); /* debug("new model %llu\n", model_id->dyn_model_id.uuid); */ @@ -59,46 +41,63 @@ static struct model *new_model_resource(model_id *model_id) void init_model_manager() { init_resource_manager(&dyn_modelman, sizeof(struct model), - DEF_DYNAMIC_MODELS, MAX_DYNAMIC_MODELS, "model", - NUM_STATIC_MODELS); - - initialize_static_models(); + DEF_DYNAMIC_MODELS, MAX_DYNAMIC_MODELS, "model"); } -struct model *new_model(model_id *id) +struct model *new_model(struct model_id *id) { return new_model_resource(id); } +struct model *get_model(struct model_id *model_id) +{ + return get_resource(&dyn_modelman, &model_id->id); +} + +// TODO: move hash to it's own file +#define FNV_32_PRIME ((u32)0x01000193) -struct model *get_model(model_id *model_id) +static u32 hash_str(const char *str) { - return get_resource(&dyn_modelman, model_id); + u8 *s = (u8 *)str; /* unsigned string */ + u32 hval = 0; + + /* + * FNV-1a hash each octet in the buffer + */ + while (*s) { + hval ^= (u32)*s++; + hval *= FNV_32_PRIME; + } + + /* return our new hash value */ + return hval; } -static struct model *load_static_model(enum static_model m) +static struct model *load_model(const char *name, struct model_id *mid) { static char path[128] = {0}; - struct model *model = &static_models()[m]; - struct geometry *geom = &static_geometry()[m]; + struct model *model = new_model_resource(mid); + struct geometry *geom = get_geometry(&model->geom_id); struct mdl_geometry lgeom; + init_mdl_geometry(&lgeom); init_geometry(geom); - model->geom_id = make_static_id(m); + model->name = name; + model->name_hash = hash_str(name); - if (get_geometry(&model->geom_id)->has_vbos) { - debug("model %s already loaded\n", static_model_defs[m].file); + if (geom->has_vbos) { + debug("model %s already loaded\n", name); return model; } // Load mesh - debug("loading %s model with geom_id\n", static_model_defs[m].file); + debug("loading %s model with geom_id\n", name); - assert(m < NUM_STATIC_MODELS); - snprintf(path, 128, "data/models/%s.mdl", static_model_defs[m].file); + snprintf(path, 128, "data/models/%s.mdl", name); load_mdl(path, model, &lgeom); vec3_copy(lgeom.min, geom->min); @@ -110,41 +109,43 @@ static struct model *load_static_model(enum static_model m) return model; } -void destroy_model(model_id *model_id) +struct model_id get_model_by_name(const char *name, struct model **found) { - if (is_static_resource(model_id)) - return; - - struct model *model = get_model(model_id); + struct model_id *ids; + struct model_id new_id; + struct model *model; + struct model *models; + u32 count, i, name_hash; - destroy_geometry(&model->geom_id); - destroy_resource(&dyn_modelman, model_id); -} + init_id(&new_id.id); + if (found) *found = NULL; -model_id get_static_model(enum static_model m, struct model **model) -{ - model_id model_id; + name_hash = hash_str(name); + models = get_all_models(&count, &ids); - if (!is_static_model_loaded(m)) { - load_static_model(m); - } + for (i = 0; i < count; i++) { + model = &models[i]; + if (model->name_hash == name_hash) { + if (found) + *found = model; + return ids[i]; + } + } - model_id = make_static_id(m); + model = load_model(name, &new_id); - if (model) { - *model = get_model(&model_id); - } + if (found) *found = model; - return model_id; + return new_id; } - -bool is_static_model_loaded(enum static_model m) +void destroy_model(struct model_id *model_id) { - geometry_id geom_id = make_static_id(m); - struct geometry *geom = get_geometry(&geom_id); - return geom->has_vbos; + struct model *model = get_model(model_id); + + destroy_geometry(&model->geom_id); + destroy_resource(&dyn_modelman, &model_id->id); } diff --git a/src/model.h b/src/model.h @@ -8,14 +8,14 @@ #include "geometry.h" #include "common.h" #include "animation.h" -#include "static_resources.h" #define DEF_DYNAMIC_MODELS 128 -#define MAX_STATIC_MODELS 128 #define MAX_DYNAMIC_MODELS 2048 #define MAX_POSES 2 -typedef struct resource_id model_id; +struct model_id { + struct resource_id id; +}; struct model { /* geometry_id geom_id; */ @@ -24,6 +24,8 @@ struct model { struct pose poses[MAX_POSES]; // TODO: separate animated_model buffer? int nposes; u32 texture; + u32 name_hash; + const char *name; }; struct model_def { @@ -33,16 +35,13 @@ struct model_def { }; void init_model_manager(); -void init_model_id(model_id *id); +void init_model_id(struct model_id *id); struct model *init_model(struct model *model); -struct model *get_model(model_id *); -struct model *get_model_geom(model_id *, struct geometry **); -struct model *new_model(model_id *); -struct model *new_static_model(model_id *); -void destroy_model(model_id *); - -bool is_static_model_loaded(enum static_model); - -model_id get_static_model(enum static_model, struct model**); +struct model_id get_model_by_name(const char *name, struct model **); +struct model *get_model(struct model_id *model_id); +struct model *get_model_geom(struct model_id *, struct geometry **); +struct model *new_model(struct model_id *); +struct model *new_static_model(struct model_id *); +void destroy_model(struct model_id *); #endif /* MODEL_H */ diff --git a/src/node.c b/src/node.c @@ -3,7 +3,6 @@ #include "node.h" #include "mat_util.h" #include "debug.h" -#include "static_resources.h" #include <string.h> #include <stdio.h> #include <assert.h> @@ -42,10 +41,7 @@ void destroy_node(node_id *id) void init_node_manager() { init_resource_manager(&node_manager, sizeof(struct node), 4096, - 0xFFFF, "node", N_STATIC_NODES); - - for (int i = 0; i < N_STATIC_NODES; i++) - node_init(&static_nodes()[i]); + 0xFFFF, "node"); } struct node *node_init(struct node *node) { diff --git a/src/render.c b/src/render.c @@ -10,7 +10,6 @@ #include "vbo.h" #include "shader.h" #include "geometry.h" -#include "static_resources.h" #include "debug.h" #include "render.h" #include "skybox.h" diff --git a/src/resource.c b/src/resource.c @@ -44,31 +44,21 @@ void null_id(struct resource_id *id) id->index = -1; } -static void init_static_ids(struct resource_manager *r) -{ - for (u32 i = 0; i < r->static_elems; i++) - r->ids[i] = make_static_id(i); -} - void init_resource_manager(struct resource_manager *r, u32 elem_size, u32 initial_elements, u32 max_elements, - const char *name, - int static_elems) + const char *name) { r->generation = 1; - r->resource_count = static_elems; + r->resource_count = 0; r->elem_size = elem_size; r->max_capacity = max_elements; - r->current_capacity = initial_elements + static_elems; + r->current_capacity = initial_elements; r->name = name; - r->static_elems = static_elems; assert(initial_elements != 0); r->resources = calloc(r->current_capacity, elem_size); r->ids = calloc(r->current_capacity, sizeof(struct resource_id)); - - init_static_ids(r); } void destroy_resource_manager(struct resource_manager *r) { @@ -85,7 +75,7 @@ static int refresh_id(struct resource_manager *r, struct resource_id *id, /* debug("id %llu gen %d != res gen %d, refreshing\n", */ /* id->uuid, id->generation, r->generation); */ // try to find uuid in new memory layout - for (u32 i = r->static_elems; i < r->resource_count; i++) { + for (u32 i = 0; i < r->resource_count; i++) { struct resource_id *newer_id = &r->ids[i]; if (newer_id->uuid == id->uuid) { /* debug("found %llu, ind %d -> %d\n", new_id->uuid, new_id->index, new->index); */ @@ -147,13 +137,6 @@ static void resize(struct resource_manager *r) void print_id(struct resource_id *id, int nl) { - int is_static = is_static_resource(id); - - if (is_static) { - printf("sid(%d)%s", id->index, nl?"\n":""); - return; - } - printf("id(u:%llu i:%d g:%d)%s", id->uuid, id->index, id->generation, nl?"\n":""); } @@ -162,9 +145,7 @@ void print_id(struct resource_id *id, int nl) void *new_resource(struct resource_manager *r, struct resource_id *id) { assert(id); - assert(id->uuid != STATIC_UUID && "called new_resource with a static id"); assert(id->index == 0xFFFFFFFF && "res_id is uninitialized"); - assert(id->index >= r->static_elems); struct resource_id *fresh_id; @@ -184,18 +165,7 @@ void *new_resource(struct resource_manager *r, struct resource_id *id) } -void *get_static_resource(struct resource_manager *r, - struct resource_id *id) -{ - assert(id->index < r->static_elems && "oob static index"); - - return index_resource(r, id->index); -} - void *get_resource(struct resource_manager *r, struct resource_id *id) { - if (id->uuid == STATIC_UUID) - return get_static_resource(r, id); - assert((int64_t)id->generation != -1 && "id intialized but not allocated (needs new_ call)"); if (id->generation == 0) { @@ -215,13 +185,6 @@ void *get_resource(struct resource_manager *r, struct resource_id *id) { void destroy_resource(struct resource_manager *r, struct resource_id *id) { - assert(id->index >= r->static_elems && "cant destroy a static resource"); - - if (id->index < r->static_elems) { - unusual("trying to destroy a static %s resource #%d\n", r->name, id->index); - return; - } - if (is_resource_destroyed(id)) { unusual("trying to destroy resource %llu which was already destroyed\n", id->uuid); return; @@ -243,20 +206,20 @@ void destroy_resource(struct resource_manager *r, struct resource_id *id) { /* r->name, id->uuid, id->index, r->resource_count); */ r->resource_count--; - assert(r->resource_count >= r->static_elems); r->generation++; - assert((int)r->resource_count - (int)id->index >= 0); + if (r->resource_count > 0) { + assert((int)r->resource_count - (int)id->index >= 0); - // TODO: we're copying OOB here - memmove(index_resource(r, id->index), - index_resource(r, id->index+1), - r->elem_size * (r->resource_count - id->index)); - - memmove(&r->ids[id->index], - &r->ids[id->index+1], - sizeof(struct resource_id) * (r->resource_count - id->index)); + // TODO: we're copying OOB here + memmove(index_resource(r, id->index), + index_resource(r, id->index+1), + r->elem_size * (r->resource_count - id->index)); + memmove(&r->ids[id->index], + &r->ids[id->index+1], + sizeof(struct resource_id) * (r->resource_count - id->index)); + } for (u32 i = id->index; i < r->resource_count; i++) { r->ids[i].index--; diff --git a/src/resource.h b/src/resource.h @@ -4,8 +4,6 @@ #include "common.h" -#define STATIC_UUID 0x5A51C00000000000 - enum refresh_status { RESOURCE_DELETED, REFRESH_NOT_NEEDED, @@ -21,7 +19,6 @@ struct resource_id { struct resource_manager { u8 *resources; struct resource_id *ids; - u32 static_elems; u32 resource_count; u32 generation; u32 elem_size; @@ -40,29 +37,12 @@ void *get_all_resources(struct resource_manager *, u32 *count, struct resource_i void destroy_resource(struct resource_manager *, struct resource_id *id); void destroy_resource_manager(struct resource_manager *); void *new_resource(struct resource_manager *, struct resource_id *id); -void *new_static_resource(struct resource_manager *, struct resource_id *id); void print_id(struct resource_id *, int nl); void null_id(struct resource_id *id); void init_resource_manager(struct resource_manager *r, u32 elem_size, - u32 initial_elements, u32 max_elements, const char *name, - int static_resources - ); - + u32 initial_elements, u32 max_elements, const char *name); -static inline struct resource_id make_static_id(u32 index) -{ - return (struct resource_id){ - .uuid = STATIC_UUID, - .index = index, - .generation = -1 - }; -} - -static inline int is_static_resource(struct resource_id *id) -{ - return id->uuid == STATIC_UUID; -} static inline int is_null_id(struct resource_id *id) { diff --git a/src/scene.c b/src/scene.c @@ -33,7 +33,7 @@ void default_scene(struct game *game) { struct node *tnode = get_node(&tower->node_id); assert(tnode); - tower->model_id = get_static_model(model_tower, NULL); + tower->model_id = get_model_by_name("tower", NULL); node_set_label(tnode, "tower"); //node_attach(&tower->node_id, &player->node_id); node_translate(tnode, V3(0.0, 50.0, 0.0)); @@ -52,8 +52,8 @@ void entity_test_scene(struct game *game) { struct terrain *terrain = &game->terrain; - model_id rock_model; - init_id(&rock_model); + struct model_id rock_model; + init_id(&rock_model.id); /* model_id rock_model = get_static_model(model_tower, NULL); */ struct model *pmodel = new_model(&rock_model); assert(pmodel); @@ -86,6 +86,6 @@ void pbr_scene(struct game *game) struct node *node = get_node(&ent->node_id); assert(node); struct entity *player = get_player(&game->test_resources); - ent->model_id = get_static_model(model_icosphere, NULL); + ent->model_id = get_model_by_name("icosphere", NULL); node_set_label(node, "sphere"); } diff --git a/src/skybox.c b/src/skybox.c @@ -42,7 +42,7 @@ void create_skybox(struct skybox *skybox, struct gpu_program *program) { (void)ok; node_init(&skybox->node); - init_id(&skybox->model_id); + init_id(&skybox->model_id.id); skybox->program = program; diff --git a/src/skybox.h b/src/skybox.h @@ -9,7 +9,7 @@ struct skybox { struct gpu_program *program; - model_id model_id; + struct model_id model_id; gpu_addr attrs[MAX_VERTEX_ATTRS]; struct node node; struct { diff --git a/src/static_resources.h b/src/static_resources.h @@ -1,28 +0,0 @@ - -#ifndef STATIC_RESOURCES_H -#define STATIC_RESOURCES_H - -enum static_model { - model_cube, - model_tower, - model_icosphere, - model_pirate_officer, - model_barrel, - NUM_STATIC_MODELS -}; - -enum static_entities { - entity_terrain, - entity_player, - RESERVED_ENTITIES -}; - -// this should align with static entities -enum static_nodes { - node_terrain, - node_player, - N_STATIC_NODES -}; - - -#endif /* STATIC_RESOURCES_H */ diff --git a/src/terrain.c b/src/terrain.c @@ -65,13 +65,11 @@ double offset_fn(struct terrain* terrain, double x, double y) { void init_terrain(struct terrain *terrain, float size) { init_id(&terrain->entity_id); - struct entity *ent = &static_entities()[entity_terrain]; - struct node *node = &static_nodes()[node_terrain]; + struct entity *ent = new_entity(&terrain->entity_id); + struct node *node = get_node(&ent->node_id); struct model *model = new_model(&ent->model_id); assert(model); /* struct model *model = init_model(&ent->model_id); assert(model); */ - terrain->entity_id = make_static_id(entity_terrain); - assert(terrain->entity_id.index == 0); model->shader = TERRAIN_PROGRAM; diff --git a/src/update.c b/src/update.c @@ -345,7 +345,7 @@ void orbit_update_from_mouse(struct orbit *camera, struct input *input, static void camera_keep_above_ground(struct terrain *terrain, struct node *camera) { - if (static_entities()[entity_terrain].flags & ENT_INVISIBLE) + if (get_entity(&terrain->entity_id)->flags & ENT_INVISIBLE) return; float move[3]; float *camworld = node_world(camera); diff --git a/test/test_dae.c b/test/test_dae.c @@ -67,7 +67,7 @@ static void test_save_mdl() load_dae("data/models/pirate_officer.dae", &model, mk1); const char *tmpfile = "/tmp/test.mdl"; - save_mdl(tmpfile, &model, &mg); + save_mdl(tmpfile, &mg); load_mdl(tmpfile, &model2, &mg2); debug("poses %d %d\n", model.nposes, model2.nposes); diff --git a/test/test_resource.c b/test/test_resource.c @@ -37,7 +37,7 @@ static void test_compact() int *p; u32 i; - init_resource_manager(&r, sizeof(int), 2, 6, "int", 0); + init_resource_manager(&r, sizeof(int), 2, 6, "int"); for (i = 0; i < (int)ARRAY_SIZE(ids); i++) init_id(&ids[i]); @@ -78,7 +78,7 @@ static void test_int_resource_manager() struct resource_id id, first_id; int *p; // 2 item case - init_resource_manager(&r, sizeof(int), 1, 2, "int", 0); + init_resource_manager(&r, sizeof(int), 1, 2, "int"); init_id(&id); init_id(&first_id); @@ -116,16 +116,16 @@ static void test_entity_system() init_id(&ent_id); ents = get_all_entities(&count, &ids); - assert(count == RESERVED_ENTITIES); + assert(count == 0); ent = new_entity(&ent_id); ents = get_all_entities(&count, &ids); assert(ent != NULL); - assert(count == 1+RESERVED_ENTITIES); - assert(&ents[RESERVED_ENTITIES] == ent); + assert(count == 1); + assert(&ents[0] == ent); - assert(ideq(&ids[RESERVED_ENTITIES], &ent_id)); + assert(ideq(&ids[0], &ent_id)); destroy_entity_system(); } @@ -137,8 +137,8 @@ void test_geometry() init_model_manager(); struct model *model; - model_id player_model_id = - get_static_model(model_pirate_officer, &model); + struct model_id player_model_id = + get_model_by_name("pirate_officer", &model); struct geometry *geom = get_geometry(&model->geom_id); assert(geom); @@ -146,15 +146,15 @@ void test_geometry() debug("pirate verts %d\n", geom->num_verts); /* assert(geom->num_verts == 2676); */ - model_id rock_model; - init_id(&rock_model); + struct model_id rock_model; + init_id(&rock_model.id); struct model *pmodel = new_model(&rock_model); assert(pmodel); struct geometry *pgeom = get_geometry(&pmodel->geom_id); assert(geom); proc_sphere(pgeom); - model_id player_model_id2 = make_static_id(model_pirate_officer); + struct model_id player_model_id2 = get_model_by_name("pirate_officer", NULL); - assert(ideq(&player_model_id, &player_model_id2)); + assert(ideq(&player_model_id.id, &player_model_id2.id)); pmodel = get_model(&player_model_id2); pgeom = get_geometry(&pmodel->geom_id); diff --git a/test/test_scene.c b/test/test_scene.c @@ -17,7 +17,7 @@ void delete_every_other_entity() u32 count; entity_id *ids; - for (u32 i = RESERVED_ENTITIES; i < 1000; i++) { + for (u32 i = 0; i < 1000; i++) { get_all_entities(&count, &ids); if (i >= count) @@ -46,19 +46,18 @@ int scene_tests(struct game *game) { "scene: node count doesn't match initial after reset_scene"); get_all_entities(&ent_count, NULL); - t_assert(ent_count == RESERVED_ENTITIES, + t_assert(ent_count == 0, "scene: entity count isn't reset after reset_scene"); - default_scene(game); get_all_entities(&ent_count, NULL); - t_assert(ent_count > RESERVED_ENTITIES, + t_assert(ent_count > 0, "scene: entity count isn't larger after loading default scene"); reset_scene(game); get_all_entities(&ent_count, NULL); - t_assert(ent_count == RESERVED_ENTITIES, + t_assert(ent_count == 0, "scene: entity count isn't reset after reset_scene"); t_assert(node_count(root) == initial_node_count,