summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-11-09 11:31:28 -0500
committerTavian Barnes <tavianator@gmail.com>2009-11-09 11:32:25 -0500
commit354f43020d956e7f02dcc81feab13e6fa0e98e10 (patch)
treeb3c6484ce799b1807e7f7b179ecfdead00f56aaa
parentb79e24ded809056b3cd3a338c042d1a794bdcab3 (diff)
downloaddimension-354f43020d956e7f02dcc81feab13e6fa0e98e10.tar.xz
Make dmnsn_delete_scene() delete the scene's elements too.
-rw-r--r--dimension/main.c4
-rw-r--r--dimension/realize.c22
-rw-r--r--dimension/realize.h1
-rw-r--r--libdimension/scene.c17
-rw-r--r--tests/libdimension/gl.c18
-rw-r--r--tests/libdimension/png.c10
-rw-r--r--tests/libdimension/tests.c34
-rw-r--r--tests/libdimension/tests.h1
8 files changed, 44 insertions, 63 deletions
diff --git a/dimension/main.c b/dimension/main.c
index 5d921e7..9f2ab3f 100644
--- a/dimension/main.c
+++ b/dimension/main.c
@@ -150,7 +150,7 @@ main(int argc, char **argv) {
}
if (dmnsn_raytrace_scene(scene) != 0) {
- dmnsn_delete_realized_scene(scene);
+ dmnsn_delete_scene(scene);
dmnsn_error(DMNSN_SEVERITY_HIGH, "Error rendering scene.");
}
@@ -166,6 +166,6 @@ main(int argc, char **argv) {
}
fclose(output_file);
- dmnsn_delete_realized_scene(scene);
+ dmnsn_delete_scene(scene);
return EXIT_SUCCESS;
}
diff --git a/dimension/realize.c b/dimension/realize.c
index f9f5532..b5c4225 100644
--- a/dimension/realize.c
+++ b/dimension/realize.c
@@ -172,7 +172,7 @@ dmnsn_realize(const dmnsn_array *astree)
/* Allocate a canvas */
scene->canvas = dmnsn_new_canvas(768, 480);
if (!scene->canvas) {
- dmnsn_delete_realized_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
@@ -198,7 +198,7 @@ dmnsn_realize(const dmnsn_array *astree)
/* Create a perspective camera */
scene->camera = dmnsn_new_perspective_camera();
if (!scene->camera) {
- dmnsn_delete_realized_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
dmnsn_set_perspective_camera_trans(scene->camera, trans);
@@ -228,26 +228,10 @@ dmnsn_realize(const dmnsn_array *astree)
default:
fprintf(stderr, "Unrecognised syntax element '%s'.\n",
dmnsn_astnode_string(astnode.type));
- dmnsn_delete_realized_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
}
return scene;
}
-
-void
-dmnsn_delete_realized_scene(dmnsn_scene *scene)
-{
- dmnsn_object *object;
- unsigned int i;
-
- for (i = 0; i < dmnsn_array_size(scene->objects); ++i) {
- dmnsn_array_get(scene->objects, i, &object);
- dmnsn_delete_object(object);
- }
-
- dmnsn_delete_camera(scene->camera);
- dmnsn_delete_canvas(scene->canvas);
- dmnsn_delete_scene(scene);
-}
diff --git a/dimension/realize.h b/dimension/realize.h
index 73ea109..d6c7d51 100644
--- a/dimension/realize.h
+++ b/dimension/realize.h
@@ -20,4 +20,3 @@
#include "../libdimension/dimension.h"
dmnsn_scene *dmnsn_realize(const dmnsn_array *astree);
-void dmnsn_delete_realized_scene(dmnsn_scene *scene);
diff --git a/libdimension/scene.c b/libdimension/scene.c
index 6d870fe..65362a5 100644
--- a/libdimension/scene.c
+++ b/libdimension/scene.c
@@ -43,8 +43,25 @@ void
dmnsn_delete_scene(dmnsn_scene *scene)
{
if (scene) {
+ unsigned int i;
+ dmnsn_light *light;
+ dmnsn_object *object;
+
+ for (i = 0; i < dmnsn_array_size(scene->lights); ++i) {
+ dmnsn_array_get(scene->lights, i, &light);
+ dmnsn_delete_light(light);
+ }
+
+ for (i = 0; i < dmnsn_array_size(scene->objects); ++i) {
+ dmnsn_array_get(scene->objects, i, &object);
+ dmnsn_delete_object(object);
+ }
+
dmnsn_delete_array(scene->lights);
dmnsn_delete_array(scene->objects);
+ dmnsn_delete_canvas(scene->canvas);
+ dmnsn_delete_camera(scene->camera);
+ dmnsn_delete_texture(scene->default_texture);
free(scene);
}
}
diff --git a/tests/libdimension/gl.c b/tests/libdimension/gl.c
index 83648cd..7e45c3b 100644
--- a/tests/libdimension/gl.c
+++ b/tests/libdimension/gl.c
@@ -40,7 +40,7 @@ main() {
/* Optimize the canvas for GL drawing */
if (dmnsn_gl_optimize_canvas(scene->canvas) != 0) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Couldn't optimize canvas for GL! ---\n");
return EXIT_FAILURE;
}
@@ -48,7 +48,7 @@ main() {
/* Create a new glX display */
display = dmnsn_new_display(scene->canvas);
if (!display) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Couldn't initialize X or glX! ---\n");
return EXIT_FAILURE;
}
@@ -59,7 +59,7 @@ main() {
progress = dmnsn_raytrace_scene_async(scene);
if (!progress) {
dmnsn_delete_display(display);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Couldn't start raytracing worker thread! ---\n");
return EXIT_FAILURE;
}
@@ -68,7 +68,7 @@ main() {
while (dmnsn_get_progress(progress) < 1.0) {
if (dmnsn_gl_write_canvas(scene->canvas) != 0) {
dmnsn_delete_display(display);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Drawing to openGL failed! ---\n");
return EXIT_FAILURE;
}
@@ -77,7 +77,7 @@ main() {
if (dmnsn_finish_progress(progress) != 0) {
dmnsn_delete_display(display);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Raytracing failed! ---\n");
return EXIT_FAILURE;
}
@@ -86,7 +86,7 @@ main() {
printf("Drawing to OpenGL\n");
if (dmnsn_gl_write_canvas(scene->canvas) != 0) {
dmnsn_delete_display(display);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Drawing to openGL failed! ---\n");
return EXIT_FAILURE;
}
@@ -100,7 +100,7 @@ main() {
canvas = dmnsn_gl_read_canvas(0, 0, scene->canvas->x, scene->canvas->y);
if (!canvas) {
dmnsn_delete_display(display);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Reading canvas from GL buffer failed! ---\n");
return EXIT_FAILURE;
}
@@ -110,7 +110,7 @@ main() {
if (dmnsn_gl_write_canvas(canvas) != 0) {
dmnsn_delete_canvas(canvas);
dmnsn_delete_display(display);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Drawing to openGL failed! ---\n");
return EXIT_FAILURE;
}
@@ -121,6 +121,6 @@ main() {
dmnsn_delete_canvas(canvas);
dmnsn_delete_display(display);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return EXIT_SUCCESS;
}
diff --git a/tests/libdimension/png.c b/tests/libdimension/png.c
index 0937a87..611ab11 100644
--- a/tests/libdimension/png.c
+++ b/tests/libdimension/png.c
@@ -43,7 +43,7 @@ main() {
/* Optimize the canvas for PNG export */
if (dmnsn_png_optimize_canvas(scene->canvas) != 0) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Couldn't optimize canvas for PNG! ---\n");
return EXIT_FAILURE;
}
@@ -52,7 +52,7 @@ main() {
printf("Rendering scene\n");
if (dmnsn_raytrace_scene(scene) != 0) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Raytracing failed! ---\n");
return EXIT_FAILURE;
}
@@ -62,20 +62,20 @@ main() {
printf("Writing scene to PNG\n");
ofile = fopen("png1.png", "wb");
if (!ofile) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Couldn't open 'png1.png' for writing! ---\n");
return EXIT_FAILURE;
}
if (dmnsn_png_write_canvas(scene->canvas, ofile) != 0) {
fclose(ofile);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
fprintf(stderr, "--- Writing canvas to PNG failed! ---\n");
return EXIT_FAILURE;
}
fclose(ofile);
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
/*
* Now test PNG import/export
diff --git a/tests/libdimension/tests.c b/tests/libdimension/tests.c
index 783c0f9..41c7d78 100644
--- a/tests/libdimension/tests.c
+++ b/tests/libdimension/tests.c
@@ -45,7 +45,7 @@ dmnsn_new_default_scene()
/* Allocate a canvas */
scene->canvas = dmnsn_new_canvas(768, 480);
if (!scene->canvas) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
@@ -67,7 +67,7 @@ dmnsn_new_default_scene()
/* Create a perspective camera */
scene->camera = dmnsn_new_perspective_camera();
if (!scene->camera) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
dmnsn_set_perspective_camera_trans(scene->camera, trans);
@@ -76,20 +76,20 @@ dmnsn_new_default_scene()
sphere = dmnsn_new_sphere();
if (!sphere) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
dmnsn_array_push(scene->objects, &sphere);
sphere->texture = dmnsn_new_texture();
if (!sphere->texture) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
sphere->texture->pigment = dmnsn_new_solid_pigment(dmnsn_white);
if (!sphere->texture->pigment) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
@@ -99,20 +99,20 @@ dmnsn_new_default_scene()
cube = dmnsn_new_cube();
if (!cube) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
dmnsn_array_push(scene->objects, &cube);
cube->texture = dmnsn_new_texture();
if (!cube->texture) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
cube->texture->pigment = dmnsn_new_solid_pigment(dmnsn_black);
if (!cube->texture->pigment) {
- dmnsn_delete_default_scene(scene);
+ dmnsn_delete_scene(scene);
return NULL;
}
@@ -123,24 +123,6 @@ dmnsn_new_default_scene()
return scene;
}
-void
-dmnsn_delete_default_scene(dmnsn_scene *scene)
-{
- dmnsn_object *sphere = NULL, *cube = NULL;
-
- if (dmnsn_array_size(scene->objects) >= 1)
- dmnsn_array_get(scene->objects, 0, &sphere);
-
- if (dmnsn_array_size(scene->objects) >= 2)
- dmnsn_array_get(scene->objects, 1, &cube);
-
- dmnsn_delete_object(cube);
- dmnsn_delete_object(sphere);
- dmnsn_delete_camera(scene->camera);
- dmnsn_delete_canvas(scene->canvas);
- dmnsn_delete_scene(scene);
-}
-
/* XIfEvent callback */
static Bool
WaitForNotify(Display *d, XEvent *e, char *arg)
diff --git a/tests/libdimension/tests.h b/tests/libdimension/tests.h
index 68cb726..45b316a 100644
--- a/tests/libdimension/tests.h
+++ b/tests/libdimension/tests.h
@@ -36,7 +36,6 @@ extern "C" {
*/
dmnsn_scene *dmnsn_new_default_scene();
-void dmnsn_delete_default_scene(dmnsn_scene *scene);
/*
* Windowing