commit ecca662a2c4afb0c5be1b7befd9c277a42a247e8
parent 16811635a54c0eb4f849442ce311c8d0d298ca4d
Author: William Casarin <jb55@jb55.com>
Date: Mon, 19 Nov 2018 23:39:57 -0800
resource system
Diffstat:
15 files changed, 70 insertions(+), 55 deletions(-)
diff --git a/src/entity.c b/src/entity.c
@@ -2,27 +2,17 @@
#include "entity.h"
#include "node.h"
#include "model.h"
+#include "resource.h"
#include <assert.h>
#define DEF_NUM_ENTITIES 1024
-struct entity_system {
- struct entity *entities;
- struct entity_id *ids;
- u32 entity_count;
- u32 generation;
-};
+static struct resource_manager esys;
static u64 entity_uuids = 0;
-static struct entity_system esys;
-
-struct entity *get_all_entities(u32 *count, struct entity_id **ids) {
- if (count != NULL)
- *count = esys.entity_count;
- if (ids != NULL)
- *ids = esys.ids;
- return esys.entities;
+struct entity *get_all_entities(u32 *count, entity_id_t **ids) {
+ return (struct entity*)get_all_resources(count, ids);
}
struct entity *init_entity(struct entity *ent) {
@@ -64,7 +54,7 @@ struct entity *get_entity(struct entity_id *ent_id) {
static inline struct entity_id new_id() {
return (struct entity_id){
.id = (struct id) {
- .index = esys.entity_count,
+ .index = esys.resource_count,
.uuid = entity_uuids++,
.generation = esys.generation,
}
@@ -98,12 +88,13 @@ void destroy_entities() {
esys.entity_count = RESERVED_ENTITIES;
};
-void destroy_entity(struct entity *ent) {
+void destroy_entity(entity_id_t id) {
node_detach_from_parent(&ent->node);
+ destroy_resource(&esys, id);
}
void destroy_entity_system() {
- free(esys.entities);
+ destroy_resource_manager(&esys);
}
void init_entity_system() {
diff --git a/src/entity.h b/src/entity.h
@@ -22,17 +22,15 @@ struct entity {
int casts_shadows;
};
-struct entity_id {
- struct id id;
-};
+typedef struct resource_id entity_id_t;
struct entity *init_entity(struct entity *);
void destroy_entities();
void destroy_entity(struct entity *);
void init_entity_system();
-struct entity *get_entity(struct entity_id *);
-struct entity *get_all_entities(u32 *count, struct entity_id **ids);
-struct entity *new_entity(struct entity_id *id);
+struct entity *get_entity(entity_id_t *);
+struct entity *get_all_entities(u32 *count, entity_id_t **ids);
+struct entity *new_entity(entity_id_t *id);
#endif /* ENTITY_H */
diff --git a/src/game.h b/src/game.h
@@ -59,7 +59,7 @@ struct resources {
struct attributes attributes;
struct node root;
- struct entity_id player_id;
+ entity_id_t player_id;
struct orbit orbit_camera;
struct node free_camera;
const struct node *camera_node;
diff --git a/src/geometry.c b/src/geometry.c
@@ -6,7 +6,7 @@
static struct geometry_manager geom_manager;
void
-destroy_buffer_geometry(struct geometry_id geom_id) {
+destroy_buffer_geometry(geometry_id_t *geom_id) {
struct geometry *geom = get_geometry(geom_id);
gpu_addr buffers[] = {
@@ -28,7 +28,6 @@ destroy_buffer_geometry(struct geometry_id geom_id) {
check_gl();
-
geom->has_vbos = 0;
}
@@ -172,13 +171,11 @@ make_buffer_geometry_(struct make_geometry *mkgeom, struct geometry *geom) {
geom->has_vbos = 1;
}
-struct geometry_id
-make_buffer_geometry(struct make_geometry *mkgeom) {
- struct geometry_id geom_id;
- struct geometry *geom = new_geometry(&geom_id);
+void
+make_buffer_geometry(struct make_geometry *mkgeom, geometry_id_t *geom_id) {
+ struct geometry *geom = new_geometry(geom_id);
make_buffer_geometry_(mkgeom, geom);
- return geom_id;
}
diff --git a/src/geometry.h b/src/geometry.h
@@ -5,14 +5,12 @@
#include "common.h"
#include "vbo.h"
#include "shader.h"
-#include "id.h"
+#include "resource.h"
#define MAX_GEOMETRY 64
// -1 is uninitialized
-struct geometry_id {
- struct id id;
-};
+typedef struct resource_id geometry_id_t;
struct geometry {
struct vbo vertex;
@@ -53,13 +51,13 @@ void render_geometry(struct geometry *geom, struct attributes *,
void bind_geometry(struct geometry *geom, struct attributes *);
void init_geometry(struct geometry *geom);
void init_make_geometry(struct make_geometry *mkgeom);
-struct geometry_id make_buffer_geometry(struct make_geometry *mkgeom);
-void destroy_buffer_geometry(struct geometry *geom);
+void make_buffer_geometry(struct make_geometry *mkgeom, geometry_id_t *);
+void destroy_buffer_geometry(geometry_id_t *geom);
void geometry_centroid(struct geometry *geom, float *v3);
void init_geometry_manager();
-void init_geometry_id(struct geometry_id *);
-struct geometry *new_geometry(struct geometry_id *);
-struct geometry *get_geometry(struct geometry_id);
+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);
#endif /* GEOMETRY_H */
diff --git a/src/id.h b/src/id.h
@@ -4,10 +4,4 @@
#include "common.h"
-struct id {
- u64 uuid;
- u32 index;
- u32 generation;
-};
-
#endif /* ID_H */
diff --git a/src/model.c b/src/model.c
@@ -36,7 +36,7 @@ struct model *get_model(enum static_model m) {
struct model *model = &static_models[m].model;
- if (model->geom_id.index >= 0)
+ if (get_geometry(&model->geom_id))
return model;
int ok = 0;
diff --git a/src/model.h b/src/model.h
@@ -24,7 +24,7 @@ enum shading {
};
struct model {
- struct geometry_id geom_id;
+ geometry_id_t geom_id;
enum shading shading;
u32 texture;
};
diff --git a/src/ply.c b/src/ply.c
@@ -105,7 +105,7 @@ static int parse_magic(const char **cursor) {
}
-int parse_ply(const char *filename, struct geometry_id *geom_id) {
+int parse_ply(const char *filename, geometry_id_t *geom_id) {
size_t len;
int success = 0;
int nverts = 0;
diff --git a/src/ply.h b/src/ply.h
@@ -5,6 +5,6 @@
#include "geometry.h"
-int parse_ply(const char *filename, struct geometry_id *geom_id);
+int parse_ply(const char *filename, geometry_id_t *geom_id);
#endif /* PLYPARSER_H */
diff --git a/src/resource.c b/src/resource.c
@@ -0,0 +1,14 @@
+
+#include "resource.h"
+
+void *get_all_resources(struct resource_manager *r, u32 *count, struct resource_id **ids) {
+ if (count != 0)
+ *count = r->resource_count;
+ if (ids != 0)
+ *ids = r->ids;
+ return r->resources;
+}
+
+void destroy_resource(struct resource_manager *r) {
+
+}
diff --git a/src/resource.h b/src/resource.h
@@ -0,0 +1,24 @@
+
+#ifndef RESOURCE_H
+#define RESOURCE_H
+
+#include "common.h"
+
+struct resource_id {
+ u64 uuid;
+ u32 index;
+ u32 generation;
+};
+
+struct resource_manager {
+ void *resources;
+ struct resource_id *ids;
+ u32 resource_count;
+ u32 generation;
+};
+
+void *get_all_resources(struct resource_manager *, u32 *count, struct resource_id **ids);
+void destroy_resource(struct resource_manager *);
+void destroy_resource_manager(struct resource_manager *);
+
+#endif /* RESOURCE_H */
diff --git a/src/terrain.c b/src/terrain.c
@@ -214,8 +214,7 @@ void create_terrain(struct terrain *terrain, float scale) {
mkgeom.indices = (u32*)del_indices;
mkgeom.num_indices = num_verts;
- terrain->model.geom_id =
- make_buffer_geometry(&mkgeom);
+ make_buffer_geometry(&mkgeom, &terrain->model.geom_id);
delaunay2d_release(del);
tri_delaunay2d_release(tri);
diff --git a/src/terrain.h b/src/terrain.h
@@ -21,7 +21,7 @@ struct perlin_settings {
struct terrain {
- struct entity_id entity_id;
+ entity_id_t entity_id;
struct perlin_settings settings;
struct point *samples;
struct model model;
diff --git a/src/ui.c b/src/ui.c
@@ -34,7 +34,7 @@ static GLfloat quad_uvs[] =
};
-static struct geometry_id create_quad()
+static geometry_id_t create_quad()
{
struct make_geometry mkgeom = {
.indices = quad_indices,
@@ -45,7 +45,7 @@ static struct geometry_id create_quad()
.num_verts = ARRAY_SIZE(quad_vertices),
.num_uv_components = 2
};
- struct geometry_id id = make_buffer_geometry(&mkgeom);
+ geometry_id_t id = make_buffer_geometry(&mkgeom);
check_gl();
return id;