summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/dimension/scene.h6
-rw-r--r--libdimension/scene.c32
-rw-r--r--libdimension/tests/render.c36
3 files changed, 29 insertions, 45 deletions
diff --git a/libdimension/dimension/scene.h b/libdimension/dimension/scene.h
index 321fac2..dd0c1ba 100644
--- a/libdimension/dimension/scene.h
+++ b/libdimension/dimension/scene.h
@@ -89,12 +89,6 @@ typedef struct dmnsn_scene {
dmnsn_scene *dmnsn_new_scene(dmnsn_pool *pool);
/**
- * Delete a scene.
- * @param[in,out] scene The scene to delete.
- */
-void dmnsn_delete_scene(dmnsn_scene *scene);
-
-/**
* Initialize a scene.
* @param[in,out] scene The scene to initalize.
*/
diff --git a/libdimension/scene.c b/libdimension/scene.c
index 6500707..610b8f9 100644
--- a/libdimension/scene.c
+++ b/libdimension/scene.c
@@ -26,11 +26,25 @@
#include "dimension-internal.h"
#include <stdlib.h>
+static void
+dmnsn_scene_cleanup(void *ptr)
+{
+ dmnsn_scene *scene = ptr;
+ DMNSN_ARRAY_FOREACH (dmnsn_object **, object, scene->objects) {
+ dmnsn_delete_object(*object);
+ }
+
+ dmnsn_delete_array(scene->lights);
+ dmnsn_delete_array(scene->objects);
+ dmnsn_delete_texture(scene->default_texture);
+ dmnsn_delete_pigment(scene->background);
+}
+
/* Allocate an empty scene */
dmnsn_scene *
dmnsn_new_scene(dmnsn_pool *pool)
{
- dmnsn_scene *scene = DMNSN_PALLOC(pool, dmnsn_scene);
+ dmnsn_scene *scene = DMNSN_PALLOC_TIDY(pool, dmnsn_scene, dmnsn_scene_cleanup);
scene->background = NULL;
scene->default_texture = dmnsn_new_texture();
@@ -52,22 +66,6 @@ dmnsn_new_scene(dmnsn_pool *pool)
return scene;
}
-/* Free a scene */
-void
-dmnsn_delete_scene(dmnsn_scene *scene)
-{
- if (scene) {
- DMNSN_ARRAY_FOREACH (dmnsn_object **, object, scene->objects) {
- dmnsn_delete_object(*object);
- }
-
- dmnsn_delete_array(scene->lights);
- dmnsn_delete_array(scene->objects);
- dmnsn_delete_texture(scene->default_texture);
- dmnsn_delete_pigment(scene->background);
- }
-}
-
void
dmnsn_scene_initialize(dmnsn_scene *scene)
{
diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c
index 2906717..c6dc537 100644
--- a/libdimension/tests/render.c
+++ b/libdimension/tests/render.c
@@ -316,9 +316,13 @@ dmnsn_new_test_scene(dmnsn_pool *pool)
int
main(void)
{
+ int ret = EXIT_FAILURE;
+
/* Treat warnings as errors for tests */
dmnsn_die_on_warnings(true);
+ dmnsn_display *display = NULL;
+
/* Create the test scene */
dmnsn_pool *pool = dmnsn_new_pool();
dmnsn_scene *scene = dmnsn_new_test_scene(pool);
@@ -330,9 +334,8 @@ main(void)
if (errno == ENOSYS) {
have_png = false;
} else {
- dmnsn_delete_scene(scene);
fprintf(stderr, "--- Couldn't optimize canvas for PNG! ---\n");
- return EXIT_FAILURE;
+ goto exit;
}
}
@@ -343,16 +346,14 @@ main(void)
if (errno == ENOSYS) {
have_gl = false;
} else {
- dmnsn_delete_scene(scene);
fprintf(stderr, "--- Couldn't optimize canvas for GL! ---\n");
- return EXIT_FAILURE;
+ goto exit;
}
}
dmnsn_canvas_clear(scene->canvas, DMNSN_TCOLOR(dmnsn_black));
/* Create a new glX display */
- dmnsn_display *display = NULL;
if (have_gl) {
display = dmnsn_new_display(scene->canvas);
if (!display) {
@@ -370,10 +371,8 @@ main(void)
while (!dmnsn_future_is_done(future)) {
dmnsn_future_pause(future);
if (dmnsn_gl_write_canvas(scene->canvas) != 0) {
- dmnsn_delete_display(display);
- dmnsn_delete_scene(scene);
fprintf(stderr, "--- Drawing to OpenGL failed! ---\n");
- return EXIT_FAILURE;
+ goto exit;
}
dmnsn_future_resume(future);
@@ -382,20 +381,16 @@ main(void)
}
if (dmnsn_future_join(future) != 0) {
- dmnsn_delete_display(display);
- dmnsn_delete_scene(scene);
fprintf(stderr, "--- Raytracing failed! ---\n");
- return EXIT_FAILURE;
+ goto exit;
}
/* Make sure we show the completed rendering */
if (display) {
printf("Drawing to OpenGL\n");
if (dmnsn_gl_write_canvas(scene->canvas) != 0) {
- dmnsn_delete_display(display);
- dmnsn_delete_scene(scene);
fprintf(stderr, "--- Drawing to OpenGL failed! ---\n");
- return EXIT_FAILURE;
+ goto exit;
}
dmnsn_display_flush(display);
}
@@ -404,25 +399,22 @@ main(void)
printf("Writing scene to PNG\n");
FILE *ofile = fopen("render.png", "wb");
if (!ofile) {
- dmnsn_delete_display(display);
- dmnsn_delete_scene(scene);
fprintf(stderr, "--- Couldn't open 'render.png' for writing! ---\n");
- return EXIT_FAILURE;
+ goto exit;
}
if (dmnsn_png_write_canvas(scene->canvas, ofile) != 0) {
fclose(ofile);
- dmnsn_delete_display(display);
- dmnsn_delete_scene(scene);
fprintf(stderr, "--- Writing canvas to PNG failed! ---\n");
- return EXIT_FAILURE;
+ goto exit;
}
fclose(ofile);
}
+ ret = EXIT_SUCCESS;
+ exit:
dmnsn_delete_display(display);
- dmnsn_delete_scene(scene);
dmnsn_delete_pool(pool);
- return EXIT_SUCCESS;
+ return ret;
}