summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/canvas.c17
-rw-r--r--libdimension/dimension/canvas.h2
-rw-r--r--libdimension/dimension/scene.h9
-rw-r--r--libdimension/scene.c9
-rw-r--r--libdimension/tests/render.c2
5 files changed, 28 insertions, 11 deletions
diff --git a/libdimension/canvas.c b/libdimension/canvas.c
index 975686d..b1b65e1 100644
--- a/libdimension/canvas.c
+++ b/libdimension/canvas.c
@@ -30,18 +30,13 @@
dmnsn_canvas *
dmnsn_new_canvas(size_t width, size_t height)
{
- /* Allocate the dmnsn_canvas struct */
dmnsn_canvas *canvas = dmnsn_malloc(sizeof(dmnsn_canvas));
- /* Set the width and height */
- canvas->width = width;
- canvas->height = height;
-
- /* Allocate room for the optimizers */
+ canvas->width = width;
+ canvas->height = height;
canvas->optimizers = dmnsn_new_array(sizeof(dmnsn_canvas_optimizer));
-
- /* Allocate the pixels */
- canvas->pixels = dmnsn_malloc(sizeof(dmnsn_color)*width*height);
+ canvas->pixels = dmnsn_malloc(sizeof(dmnsn_color)*width*height);
+ canvas->refcount = dmnsn_new_refcount();
return canvas;
}
@@ -50,7 +45,9 @@ dmnsn_new_canvas(size_t width, size_t height)
void
dmnsn_delete_canvas(dmnsn_canvas *canvas)
{
- if (canvas) {
+ if (canvas && DMNSN_DECREF(canvas)) {
+ dmnsn_delete_refcount(canvas->refcount);
+
/* Free the optimizers */
DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i, canvas->optimizers) {
if (i->free_fn) {
diff --git a/libdimension/dimension/canvas.h b/libdimension/dimension/canvas.h
index 6938c0e..f33fc88 100644
--- a/libdimension/dimension/canvas.h
+++ b/libdimension/dimension/canvas.h
@@ -39,6 +39,8 @@ typedef struct {
* at (a,b) is accessible as pixels[b*width + a].
*/
dmnsn_color *pixels;
+
+ dmnsn_refcount *refcount; /**< @internal Reference count. */
} dmnsn_canvas;
/* Forward-declare dmnsn_canvas_optimizer */
diff --git a/libdimension/dimension/scene.h b/libdimension/dimension/scene.h
index c182d85..ffe5fb3 100644
--- a/libdimension/dimension/scene.h
+++ b/libdimension/dimension/scene.h
@@ -93,6 +93,15 @@ void dmnsn_delete_scene(dmnsn_scene *scene);
void dmnsn_initialize_scene(dmnsn_scene *scene);
/**
+ * Set the output canvas for a scene.
+ * @param[in,out] scene The scene for which to set the canvas.
+ * @param[in] canvas The canvas to set.
+ */
+void dmnsn_scene_set_canvas(dmnsn_scene *scene, dmnsn_canvas *canvas);
+
+/**
* 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.
*/
void dmnsn_scene_add_object(dmnsn_scene *scene, dmnsn_object *object);
diff --git a/libdimension/scene.c b/libdimension/scene.c
index f5b76e7..51b8215 100644
--- a/libdimension/scene.c
+++ b/libdimension/scene.c
@@ -84,8 +84,17 @@ dmnsn_initialize_scene(dmnsn_scene *scene)
}
void
+dmnsn_scene_set_canvas(dmnsn_scene *scene, dmnsn_canvas *canvas)
+{
+ DMNSN_INCREF(canvas);
+ dmnsn_delete_canvas(scene->canvas);
+ scene->canvas = canvas;
+}
+
+void
dmnsn_scene_add_object(dmnsn_scene *scene, dmnsn_object *object)
{
+ DMNSN_INCREF(object);
dmnsn_initialize_object(object);
dmnsn_array_push(scene->objects, &object);
}
diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c
index a487224..f35f5bb 100644
--- a/libdimension/tests/render.c
+++ b/libdimension/tests/render.c
@@ -40,7 +40,7 @@ dmnsn_new_test_scene(void)
);
/* Allocate a canvas */
- scene->canvas = dmnsn_new_canvas(768, 480);
+ dmnsn_scene_set_canvas(scene, dmnsn_new_canvas(768, 480));
/* Set up the transformation matrix for the perspective camera */
dmnsn_matrix trans = dmnsn_scale_matrix(