summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libdimension/camera.c8
-rw-r--r--libdimension/dimension/camera.h5
-rw-r--r--libdimension/dimension/refcount.h3
-rw-r--r--libdimension/dimension/scene.h7
-rw-r--r--libdimension/scene.c8
-rw-r--r--libdimension/tests/render.c5
6 files changed, 29 insertions, 7 deletions
diff --git a/libdimension/camera.c b/libdimension/camera.c
index 67117f9..e76200a 100644
--- a/libdimension/camera.c
+++ b/libdimension/camera.c
@@ -31,8 +31,9 @@ dmnsn_camera *
dmnsn_new_camera(void)
{
dmnsn_camera *camera = dmnsn_malloc(sizeof(dmnsn_camera));
- camera->free_fn = NULL;
- camera->trans = dmnsn_identity_matrix();
+ camera->free_fn = NULL;
+ camera->trans = dmnsn_identity_matrix();
+ camera->refcount = dmnsn_new_refcount();
return camera;
}
@@ -40,7 +41,8 @@ dmnsn_new_camera(void)
void
dmnsn_delete_camera(dmnsn_camera *camera)
{
- if (camera) {
+ if (camera && DMNSN_DECREF(camera)) {
+ dmnsn_delete_refcount(camera->refcount);
if (camera->free_fn) {
camera->free_fn(camera->ptr);
}
diff --git a/libdimension/dimension/camera.h b/libdimension/dimension/camera.h
index c3ad65b..67fffc0 100644
--- a/libdimension/dimension/camera.h
+++ b/libdimension/dimension/camera.h
@@ -44,8 +44,9 @@ struct dmnsn_camera {
dmnsn_matrix trans; /**< Transformation matrix. */
- /** Generic pointer for camera info. */
- void *ptr;
+ void *ptr; /**< @internal Generic pointer for camera info. */
+
+ dmnsn_refcount *refcount; /**< @internal reference count. */
};
/**
diff --git a/libdimension/dimension/refcount.h b/libdimension/dimension/refcount.h
index 14f31ab..9d2c304 100644
--- a/libdimension/dimension/refcount.h
+++ b/libdimension/dimension/refcount.h
@@ -30,6 +30,7 @@
#define DMNSN_INCREF(obj) ((void)++(*(obj)->refcount))
/**
+ * @internal
* Decrement a reference count.
* @param[in,out] object The reference-counted object to release.
* @return Whether the object is now garbage.
@@ -42,6 +43,7 @@
typedef unsigned int dmnsn_refcount;
/**
+ * @internal
* Create a reference count.
* @return A new reference counter, initialized to zero (a "borrowed" reference,
* which will be garbage-collected the first time it is deleted).
@@ -49,6 +51,7 @@ typedef unsigned int dmnsn_refcount;
dmnsn_refcount *dmnsn_new_refcount(void);
/**
+ * @internal
* Delete a reference count. Raises an error if the reference count wasn't
* zero.
*/
diff --git a/libdimension/dimension/scene.h b/libdimension/dimension/scene.h
index ffe5fb3..b726a77 100644
--- a/libdimension/dimension/scene.h
+++ b/libdimension/dimension/scene.h
@@ -100,6 +100,13 @@ void dmnsn_initialize_scene(dmnsn_scene *scene);
void dmnsn_scene_set_canvas(dmnsn_scene *scene, dmnsn_canvas *canvas);
/**
+ * Set the camera for a scene.
+ * @param[in,out] scene The scene for which to set the canvas.
+ * @param[in] camera The camera to set.
+ */
+void dmnsn_scene_set_camera(dmnsn_scene *scene, dmnsn_camera *camera);
+
+/**
* Add an object to a scene.
* @param[in,out] scene The scene to which to add the object.
* @param[in] object The object to add.
diff --git a/libdimension/scene.c b/libdimension/scene.c
index 51b8215..ec9fdbd 100644
--- a/libdimension/scene.c
+++ b/libdimension/scene.c
@@ -92,6 +92,14 @@ dmnsn_scene_set_canvas(dmnsn_scene *scene, dmnsn_canvas *canvas)
}
void
+dmnsn_scene_set_camera(dmnsn_scene *scene, dmnsn_camera *camera)
+{
+ DMNSN_INCREF(camera);
+ dmnsn_delete_camera(scene->camera);
+ scene->camera = camera;
+}
+
+void
dmnsn_scene_add_object(dmnsn_scene *scene, dmnsn_object *object)
{
DMNSN_INCREF(object);
diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c
index a84ba63..a79c85c 100644
--- a/libdimension/tests/render.c
+++ b/libdimension/tests/render.c
@@ -62,8 +62,9 @@ dmnsn_new_test_scene(void)
);
/* Create a perspective camera */
- scene->camera = dmnsn_new_perspective_camera();
- scene->camera->trans = trans;
+ dmnsn_camera *camera = dmnsn_new_perspective_camera();
+ camera->trans = trans;
+ dmnsn_scene_set_camera(scene, camera);
/* Background color */
scene->background = dmnsn_clear;