polyadvent

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

commit e2f5d340817a184e9ba04ed10bbfbdca349f87b2
parent d46337ec1b2bf722e315d6534df25dfab11d9d02
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 20 Nov 2018 00:52:51 -0800

workingish but memory corrupted

Diffstat:
MMakefile | 1+
Msrc/game.c | 4++--
Msrc/geometry.c | 28+++++++++-------------------
Msrc/geometry.h | 5+++--
Msrc/model.c | 1-
Msrc/resource.c | 12++++++++++--
Msrc/resource.h | 1+
Msrc/terrain.c | 2+-
Msrc/ui.c | 4++--
Msrc/ui.h | 2+-
10 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/Makefile b/Makefile @@ -45,6 +45,7 @@ OBJS += $(SRC)/update.o OBJS += $(SRC)/util.o OBJS += $(SRC)/vec3.o OBJS += $(SRC)/scene.o +OBJS += $(SRC)/resource.o SRCS=$(OBJS:.o=.c) diff --git a/src/game.c b/src/game.c @@ -52,6 +52,7 @@ static void init_user_settings(struct user_settings *settings) { void game_init(struct game *game, int width, int height) { init_gl(&game->test_resources, width, height); init_entity_system(); + init_geometry_manager(); init_user_settings(&game->user_settings); check_gl(); @@ -125,8 +126,7 @@ void game_init(struct game *game, int width, int height) { // player entity player = new_entity(&res->player_id); assert(res->player_id.index == 1); - ok = load_model(&player->model, "pirate-officer"); - assert(ok); + player->model = get_model(model_pirateofficer); player->node.label = "player"; node_attach(&player->node, root); node_translate(&player->node, V3(terrain->size/2.,terrain->size/2.,0.0)); diff --git a/src/geometry.c b/src/geometry.c @@ -1,9 +1,11 @@ #include "geometry.h" #include "util.h" +#include "resource.h" #include <assert.h> -static struct geometry_manager geom_manager; + +static struct resource_manager geom_manager; void destroy_buffer_geometry(geometry_id_t *geom_id) { @@ -186,31 +188,19 @@ void geometry_centroid(struct geometry *geom, float *dest) { }; void init_geometry_manager() { - geom_manager.num_geometry = 0; + init_resource_manager(&geom_manager, sizeof(struct geometry), + DEF_NUM_GEOMETRY, MAX_GEOMETRY); } struct geometry *get_geometry(geometry_id_t *geom_id) { - assert(geom_id->index != -1); - assert(geom_id->index < geom_manager.num_geometry); - return &geom_manager.geoms[geom_id->index]; + return get_resource(&geom_manager, geom_id); } struct geometry *new_geometry(geometry_id_t *geom_id) { - u32 index = geom_manager.num_geometry++; - struct geometry *g = &geom_manager.geoms[index]; - init_geometry(g); - if (geom_id) - geom_id->index = index; - - return g; -} - -void init_geometry_id(struct geometry_id *id) { - id->index = -1; + return new_resource(&geom_manager, geom_id); } -struct geometry *get_all_geometry(u32 *count) { - *count = geom_manager.num_geometry; - return &geom_manager.geoms[0]; +struct geometry *get_all_geometry(u32 *count, geometry_id_t **ids) { + return get_all_resources(&geom_manager, count, ids); } diff --git a/src/geometry.h b/src/geometry.h @@ -7,7 +7,8 @@ #include "shader.h" #include "resource.h" -#define MAX_GEOMETRY 64 +#define DEF_NUM_GEOMETRY 32 +#define MAX_GEOMETRY 1024 // -1 is uninitialized typedef struct resource_id geometry_id_t; @@ -58,6 +59,6 @@ void init_geometry_manager(); void init_geometry_id(geometry_id_t *); struct geometry *new_geometry(geometry_id_t *); struct geometry *get_geometry(geometry_id_t *); -struct geometry *get_all_geometry(u32 *count); +struct geometry *get_all_geometry(u32 *count, geometry_id_t **ids); #endif /* GEOMETRY_H */ diff --git a/src/model.c b/src/model.c @@ -16,7 +16,6 @@ static int static_models_initialized = 0; void init_model(struct model *model) { model->shading = SHADING_VERT_COLOR; model->texture = 0; - init_geometry_id(&model->geom_id); } static void initialize_static_models() { diff --git a/src/resource.c b/src/resource.c @@ -2,6 +2,7 @@ #include "resource.h" #include <stdlib.h> #include <string.h> +#include <assert.h> static u64 resource_uuids = 0; @@ -17,6 +18,12 @@ void *get_all_resources(struct resource_manager *r, u32 *count, struct resource_ return r->resources; } +void init_resource_id(struct resource_id *id) { + id->index = -1; + id->generation = -1; + id->uuid = -1; +} + void init_resource_manager(struct resource_manager *r, u32 elem_size, u32 initial_elements, u32 max_elements) { r->generation = 1; @@ -74,8 +81,9 @@ void destroy_resource(struct resource_manager *r, struct resource_id *id) { // generation 0 means destroyed id->generation = 0; - memmove(index_resource(r, id->index), - index_resource(r, id->index+1), r->resource_count -) + assert(!"implement destroy resource"); + /* memmove(index_resource(r, id->index), */ + /* index_resource(r, id->index+1), r->resource_count -) */ } struct resource_id new_id(struct resource_manager *r) { diff --git a/src/resource.h b/src/resource.h @@ -19,6 +19,7 @@ struct resource_manager { u32 max_elements; }; +void init_resource_id(struct resource_id *id); void *get_resource(struct resource_manager *r, struct resource_id *id); void *get_all_resources(struct resource_manager *, u32 *count, struct resource_id **ids); void destroy_resource(struct resource_manager *, struct resource_id *id); diff --git a/src/terrain.c b/src/terrain.c @@ -55,7 +55,7 @@ void init_terrain(struct terrain *terrain, float size) { struct entity *ent; ent = new_entity(&terrain->entity_id); - assert(terrain->entity_id.uuid == 0); + assert(terrain->entity_id.index == 0); terrain->model.shading = SHADING_TERRAIN; ent->model = &terrain->model; ent->node.label = "terrain_node"; diff --git a/src/ui.c b/src/ui.c @@ -70,7 +70,7 @@ void render_ui(struct ui *ui, float *view) { check_gl(); // render quad - render_geometry(&ui->quad, &ui->attrs, ui->shader); + render_geometry(get_geometry(&ui->quad_geom_id), &ui->attrs, ui->shader); check_gl(); } @@ -93,7 +93,7 @@ void create_ui(struct ui *ui, int width, int height, struct gpu_program *shader) int ok = 0; ui->shader = shader; - create_quad(&ui->quad); + create_quad(&ui->quad_geom_id); check_gl(); ok = make_shader(GL_VERTEX_SHADER, SHADER("ui.v.glsl"), &vertex); diff --git a/src/ui.h b/src/ui.h @@ -8,7 +8,7 @@ struct ui { struct gpu_program *shader; - struct geometry quad; + geometry_id_t quad_geom_id; struct attributes attrs; struct ui_uniforms {