summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libdimension/csg.c8
-rw-r--r--libdimension/dimension/object.h3
-rw-r--r--libdimension/dimension/scene.h5
-rw-r--r--libdimension/interior.c2
-rw-r--r--libdimension/object.c4
-rw-r--r--libdimension/scene.c9
-rw-r--r--libdimension/tests/prtree.c2
-rw-r--r--libdimension/tests/render.c8
-rw-r--r--libdimension/texture.c2
9 files changed, 32 insertions, 11 deletions
diff --git a/libdimension/csg.c b/libdimension/csg.c
index caecee0..0222b8b 100644
--- a/libdimension/csg.c
+++ b/libdimension/csg.c
@@ -92,9 +92,11 @@ dmnsn_new_csg_union(const dmnsn_array *objects)
dmnsn_object *csg = dmnsn_new_object();
DMNSN_ARRAY_FOREACH (dmnsn_object **, object, objects) {
+ DMNSN_INCREF(*object);
dmnsn_array_push(csg->children, object);
}
+ csg->ptr = NULL;
csg->intersection_fn = dmnsn_csg_union_intersection_fn;
csg->inside_fn = dmnsn_csg_union_inside_fn;
csg->initialize_fn = dmnsn_csg_union_initialize_fn;
@@ -234,6 +236,8 @@ dmnsn_new_csg_intersection(dmnsn_object *A, dmnsn_object *B)
{
dmnsn_object *csg = dmnsn_new_object();
+ DMNSN_INCREF(A);
+ DMNSN_INCREF(B);
dmnsn_object **params = dmnsn_malloc(2*sizeof(dmnsn_object *));
params[0] = A;
params[1] = B;
@@ -292,6 +296,8 @@ dmnsn_new_csg_difference(dmnsn_object *A, dmnsn_object *B)
{
dmnsn_object *csg = dmnsn_new_object();
+ DMNSN_INCREF(A);
+ DMNSN_INCREF(B);
dmnsn_object **params = dmnsn_malloc(2*sizeof(dmnsn_object *));
params[0] = A;
params[1] = B;
@@ -353,6 +359,8 @@ dmnsn_new_csg_merge(dmnsn_object *A, dmnsn_object *B)
{
dmnsn_object *csg = dmnsn_new_object();
+ DMNSN_INCREF(A);
+ DMNSN_INCREF(B);
dmnsn_object **params = dmnsn_malloc(2*sizeof(dmnsn_object *));
params[0] = A;
params[1] = B;
diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h
index ea8a6a8..1e10c3f 100644
--- a/libdimension/dimension/object.h
+++ b/libdimension/dimension/object.h
@@ -90,6 +90,9 @@ struct dmnsn_object {
/** Generic pointer for object info. */
void *ptr;
+
+ /** @internal Reference count. */
+ dmnsn_refcount *refcount;
};
/**
diff --git a/libdimension/dimension/scene.h b/libdimension/dimension/scene.h
index 2d9840b..c182d85 100644
--- a/libdimension/dimension/scene.h
+++ b/libdimension/dimension/scene.h
@@ -91,3 +91,8 @@ void dmnsn_delete_scene(dmnsn_scene *scene);
* @param[in,out] scene The scene to initalize.
*/
void dmnsn_initialize_scene(dmnsn_scene *scene);
+
+/**
+ * Add an object to a scene.
+ */
+void dmnsn_scene_add_object(dmnsn_scene *scene, dmnsn_object *object);
diff --git a/libdimension/interior.c b/libdimension/interior.c
index 0eec274..87b5059 100644
--- a/libdimension/interior.c
+++ b/libdimension/interior.c
@@ -42,10 +42,10 @@ void
dmnsn_delete_interior(dmnsn_interior *interior)
{
if (interior && DMNSN_DECREF(interior)) {
+ dmnsn_delete_refcount(interior->refcount);
if (interior->free_fn) {
interior->free_fn(interior->ptr);
}
- dmnsn_delete_refcount(interior->refcount);
dmnsn_free(interior);
}
}
diff --git a/libdimension/object.c b/libdimension/object.c
index 7c4a714..f73ea02 100644
--- a/libdimension/object.c
+++ b/libdimension/object.c
@@ -39,6 +39,7 @@ dmnsn_new_object(void)
object->inside_fn = NULL;
object->initialize_fn = NULL;
object->free_fn = NULL;
+ object->refcount = dmnsn_new_refcount();
return object;
}
@@ -46,7 +47,8 @@ dmnsn_new_object(void)
void
dmnsn_delete_object(dmnsn_object *object)
{
- if (object) {
+ if (object && DMNSN_DECREF(object)) {
+ dmnsn_delete_refcount(object->refcount);
DMNSN_ARRAY_FOREACH (dmnsn_object **, child, object->children) {
dmnsn_delete_object(*child);
}
diff --git a/libdimension/scene.c b/libdimension/scene.c
index fadd077..f5b76e7 100644
--- a/libdimension/scene.c
+++ b/libdimension/scene.c
@@ -81,8 +81,11 @@ dmnsn_initialize_scene(dmnsn_scene *scene)
if (scene->sky_sphere) {
dmnsn_initialize_sky_sphere(scene->sky_sphere);
}
+}
- DMNSN_ARRAY_FOREACH (dmnsn_object **, object, scene->objects) {
- dmnsn_initialize_object(*object);
- }
+void
+dmnsn_scene_add_object(dmnsn_scene *scene, dmnsn_object *object)
+{
+ dmnsn_initialize_object(object);
+ dmnsn_array_push(scene->objects, &object);
}
diff --git a/libdimension/tests/prtree.c b/libdimension/tests/prtree.c
index cb36153..86aaa4e 100644
--- a/libdimension/tests/prtree.c
+++ b/libdimension/tests/prtree.c
@@ -69,7 +69,7 @@ main(void)
dmnsn_randomize_bounding_box(object);
object->intersection_fn = dmnsn_fake_intersection_fn;
dmnsn_initialize_object(object);
- dmnsn_array_push(scene->objects, &object);
+ dmnsn_scene_add_object(scene, object);
}
dmnsn_prtree *prtree = dmnsn_new_prtree(scene->objects);
diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c
index 5b03423..a487224 100644
--- a/libdimension/tests/render.c
+++ b/libdimension/tests/render.c
@@ -112,7 +112,7 @@ dmnsn_new_test_scene(void)
sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25));
dmnsn_object *csg = dmnsn_new_csg_difference(cube, sphere);
- dmnsn_array_push(scene->objects, &csg);
+ dmnsn_scene_add_object(scene, csg);
dmnsn_array *arrow_array = dmnsn_new_array(sizeof(dmnsn_object *));
@@ -149,7 +149,7 @@ dmnsn_new_test_scene(void)
dmnsn_translation_matrix(dmnsn_new_vector(0.0, -1.25, 0.0)),
dmnsn_scale_matrix(dmnsn_new_vector(1.0, 2.75, 1.0))
);
- dmnsn_array_push(scene->objects, &arrow);
+ dmnsn_scene_add_object(scene, arrow);
dmnsn_delete_array(arrow_array);
dmnsn_array *torus_array = dmnsn_new_array(sizeof(dmnsn_object *));
@@ -172,7 +172,7 @@ dmnsn_new_test_scene(void)
torii->texture = dmnsn_new_texture();
torii->texture->pigment = dmnsn_new_solid_pigment(dmnsn_blue);
torii->texture->finish = dmnsn_new_ambient_finish(dmnsn_white);
- dmnsn_array_push(scene->objects, &torii);
+ dmnsn_scene_add_object(scene, torii);
dmnsn_delete_array(torus_array);
dmnsn_object *plane = dmnsn_new_plane(dmnsn_new_vector(0.0, 1.0, 0.0));
@@ -194,7 +194,7 @@ dmnsn_new_test_scene(void)
plane->texture->pigment
= dmnsn_new_pigment_map_pigment(checker2, checker_pigment_map);
plane->texture->pigment->quick_color = dmnsn_new_color(1.0, 0.5, 0.75);
- dmnsn_array_push(scene->objects, &plane);
+ dmnsn_scene_add_object(scene, plane);
return scene;
}
diff --git a/libdimension/texture.c b/libdimension/texture.c
index 5f3b98d..2199103 100644
--- a/libdimension/texture.c
+++ b/libdimension/texture.c
@@ -43,9 +43,9 @@ void
dmnsn_delete_texture(dmnsn_texture *texture)
{
if (texture && DMNSN_DECREF(texture)) {
+ dmnsn_delete_refcount(texture->refcount);
dmnsn_delete_finish(texture->finish);
dmnsn_delete_pigment(texture->pigment);
- dmnsn_delete_refcount(texture->refcount);
dmnsn_free(texture);
}
}