summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-07-16 01:16:09 +0000
committerTavian Barnes <tavianator@gmail.com>2009-07-16 01:16:09 +0000
commit8f01c5394dcce8f5d4e7102dacfcdea9d1f7b021 (patch)
tree4092a281882e34ac53ec15d582dcbf2cb0d408c0 /libdimension
parent1928016fe7aa439d4bfb61d3a7e7b7399ca7a229 (diff)
downloaddimension-8f01c5394dcce8f5d4e7102dacfcdea9d1f7b021.tar.xz
Add destructor callbacks for polymorphic C types, and use their base
dmnsn_delete_*() function.
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/camera.c13
-rw-r--r--libdimension/cameras.c15
-rw-r--r--libdimension/canvas.c1
-rw-r--r--libdimension/dimension.h3
-rw-r--r--libdimension/dimension/camera.h13
-rw-r--r--libdimension/dimension/cameras.h1
-rw-r--r--libdimension/dimension/canvas.h6
-rw-r--r--libdimension/dimension/object.h5
-rw-r--r--libdimension/dimension/objects.h2
-rw-r--r--libdimension/dimension/pigments.h1
-rw-r--r--libdimension/dimension/texture.h4
-rw-r--r--libdimension/object.c10
-rw-r--r--libdimension/objects.c14
-rw-r--r--libdimension/pigments.c9
-rw-r--r--libdimension/texture.c12
15 files changed, 53 insertions, 56 deletions
diff --git a/libdimension/camera.c b/libdimension/camera.c
index 5a321ef..dfa14e6 100644
--- a/libdimension/camera.c
+++ b/libdimension/camera.c
@@ -25,12 +25,21 @@
dmnsn_camera *
dmnsn_new_camera()
{
- return malloc(sizeof(dmnsn_camera));
+ dmnsn_camera *camera = malloc(sizeof(dmnsn_camera));
+ if (camera) {
+ camera->free_fn = NULL;
+ }
+ return camera;
}
/* Free a dummy camera */
void
dmnsn_delete_camera(dmnsn_camera *camera)
{
- free(camera);
+ if (camera) {
+ if (camera->free_fn) {
+ (*camera->free_fn)(camera->ptr);
+ }
+ free(camera);
+ }
}
diff --git a/libdimension/cameras.c b/libdimension/cameras.c
index 1ab1360..dfe4708 100644
--- a/libdimension/cameras.c
+++ b/libdimension/cameras.c
@@ -39,8 +39,6 @@ dmnsn_new_perspective_camera(dmnsn_matrix trans)
dmnsn_matrix *ptr;
dmnsn_camera *camera = dmnsn_new_camera();
if (camera) {
- camera->ray_fn = &dmnsn_perspective_camera_ray_fn;
-
/* Allocate room for the transformation matrix */
ptr = malloc(sizeof(dmnsn_matrix));
if (!ptr) {
@@ -49,18 +47,11 @@ dmnsn_new_perspective_camera(dmnsn_matrix trans)
}
*ptr = trans;
camera->ptr = ptr;
- }
- return camera;
-}
-/* Delete a perspective camera */
-void
-dmnsn_delete_perspective_camera(dmnsn_camera *camera)
-{
- if (camera) {
- free(camera->ptr);
- dmnsn_delete_camera(camera);
+ camera->ray_fn = &dmnsn_perspective_camera_ray_fn;
+ camera->free_fn = &free;
}
+ return camera;
}
/* Get the transformation matrix */
diff --git a/libdimension/canvas.c b/libdimension/canvas.c
index 2933259..9329a07 100644
--- a/libdimension/canvas.c
+++ b/libdimension/canvas.c
@@ -64,6 +64,7 @@ dmnsn_delete_canvas(dmnsn_canvas *canvas)
(*optimizer.free_fn)(optimizer.ptr);
}
}
+ dmnsn_delete_array(canvas->optimizers);
/* Free the pixels and canvas */
free(canvas->pixels);
diff --git a/libdimension/dimension.h b/libdimension/dimension.h
index 0c6ae26..4cc29ab 100644
--- a/libdimension/dimension.h
+++ b/libdimension/dimension.h
@@ -56,6 +56,9 @@
extern "C" {
#endif
+/* Common types */
+typedef void dmnsn_free_fn(void *ptr);
+
/* Include all the libdimension headers */
#include <dimension/error.h>
#include <dimension/array.h>
diff --git a/libdimension/dimension/camera.h b/libdimension/dimension/camera.h
index 13c03b0..3e6da4f 100644
--- a/libdimension/dimension/camera.h
+++ b/libdimension/dimension/camera.h
@@ -34,20 +34,15 @@ typedef dmnsn_line dmnsn_camera_ray_fn(const dmnsn_camera *camera,
unsigned int x, unsigned int y);
struct dmnsn_camera {
+ /* Callback functions */
+ dmnsn_camera_ray_fn *ray_fn;
+ dmnsn_free_fn *free_fn;
+
/* Generic pointer for camera info */
void *ptr;
-
- /* Callback function */
- dmnsn_camera_ray_fn *ray_fn;
};
dmnsn_camera *dmnsn_new_camera();
void dmnsn_delete_camera(dmnsn_camera *camera);
-/* A perspective camera, at the origin, looking at (0, 0, 1). The feild of view
- is the section of the plane z = 1 from (-0.5, -0.5) to (0.5, 0.5). Rays are
- transformed by the transformation matrix `trans'. */
-dmnsn_camera *dmnsn_new_perspective_camera(dmnsn_matrix trans);
-void dmnsn_delete_perspective_camera(dmnsn_camera *camera);
-
#endif /* DIMENSION_CAMERA_H */
diff --git a/libdimension/dimension/cameras.h b/libdimension/dimension/cameras.h
index 1793bbd..b135693 100644
--- a/libdimension/dimension/cameras.h
+++ b/libdimension/dimension/cameras.h
@@ -29,7 +29,6 @@
is the section of the plane z = 1 from (-0.5, -0.5) to (0.5, 0.5). Rays are
transformed by the transformation matrix `trans'. */
dmnsn_camera *dmnsn_new_perspective_camera(dmnsn_matrix trans);
-void dmnsn_delete_perspective_camera(dmnsn_camera *camera);
/* Get or set the transformation matrix */
dmnsn_matrix dmnsn_get_perspective_camera_trans(const dmnsn_camera *camera);
diff --git a/libdimension/dimension/canvas.h b/libdimension/dimension/canvas.h
index 05a9a62..0d37acf 100644
--- a/libdimension/dimension/canvas.h
+++ b/libdimension/dimension/canvas.h
@@ -47,12 +47,14 @@ typedef struct dmnsn_canvas_optimizer dmnsn_canvas_optimizer;
typedef void dmnsn_canvas_optimizer_fn(dmnsn_canvas *canvas,
dmnsn_canvas_optimizer optimizer,
unsigned int x, unsigned int y);
-typedef void dmnsn_canvas_optimizer_free_fn(void *ptr);
/* Canvas optimizer */
struct dmnsn_canvas_optimizer {
+ /* Callback types */
dmnsn_canvas_optimizer_fn *optimizer_fn;
- dmnsn_canvas_optimizer_free_fn *free_fn;
+ dmnsn_free_fn *free_fn;
+
+ /* Generic pointer */
void *ptr;
};
diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h
index 2da3ac7..901b366 100644
--- a/libdimension/dimension/object.h
+++ b/libdimension/dimension/object.h
@@ -34,7 +34,9 @@ typedef dmnsn_array *dmnsn_object_intersections_fn(const dmnsn_object *object,
typedef int dmnsn_object_inside_fn(const dmnsn_object *object,
dmnsn_vector point);
+/* dmnsn_object definition */
struct dmnsn_object {
+ /* Surface properties */
dmnsn_texture *texture;
/* Transformation matrix */
@@ -43,6 +45,7 @@ struct dmnsn_object {
/* Callback functions */
dmnsn_object_intersections_fn *intersections_fn;
dmnsn_object_inside_fn *inside_fn;
+ dmnsn_free_fn *free_fn;
/* Generic pointer for object info */
void *ptr;
@@ -50,6 +53,8 @@ struct dmnsn_object {
/* Allocate a dummy object */
dmnsn_object *dmnsn_new_object();
+
+/* Free an object */
void dmnsn_delete_object(dmnsn_object *object);
#endif /* DIMENSION_OBJECT_H */
diff --git a/libdimension/dimension/objects.h b/libdimension/dimension/objects.h
index d8c4144..4a24ca7 100644
--- a/libdimension/dimension/objects.h
+++ b/libdimension/dimension/objects.h
@@ -27,10 +27,8 @@
/* A sphere object, of radius 1, centered at the origin. */
dmnsn_object *dmnsn_new_sphere();
-void dmnsn_delete_sphere(dmnsn_object *sphere);
/* A cube, axis-aligned, from (-1, -1, -1) to (1, 1, 1) */
dmnsn_object *dmnsn_new_cube();
-void dmnsn_delete_cube(dmnsn_object *cube);
#endif /* DIMENSION_OBJECTS_H */
diff --git a/libdimension/dimension/pigments.h b/libdimension/dimension/pigments.h
index ed0c3b2..61d50d8 100644
--- a/libdimension/dimension/pigments.h
+++ b/libdimension/dimension/pigments.h
@@ -27,6 +27,5 @@
/* A solid color */
dmnsn_pigment *dmnsn_new_solid_pigment(dmnsn_color color);
-void dmnsn_delete_solid_pigment(dmnsn_pigment *pigment);
#endif /* DIMENSION_PIGMENTS_H */
diff --git a/libdimension/dimension/texture.h b/libdimension/dimension/texture.h
index 2eabe84..c26e840 100644
--- a/libdimension/dimension/texture.h
+++ b/libdimension/dimension/texture.h
@@ -38,7 +38,11 @@ typedef dmnsn_color dmnsn_pigment_fn(const dmnsn_pigment *pigment,
/* dmnsn_pigment definition */
struct dmnsn_pigment {
+ /* Callbacks */
dmnsn_pigment_fn *pigment_fn;
+ dmnsn_free_fn *free_fn;
+
+ /* Generic pointer */
void *ptr;
};
diff --git a/libdimension/object.c b/libdimension/object.c
index 0556921..6b248ff 100644
--- a/libdimension/object.c
+++ b/libdimension/object.c
@@ -28,7 +28,8 @@ dmnsn_new_object()
dmnsn_object *object = malloc(sizeof(dmnsn_object));
if (object) {
object->texture = NULL;
- object->trans = dmnsn_identity_matrix();
+ object->trans = dmnsn_identity_matrix();
+ object->free_fn = NULL;
}
return object;
}
@@ -37,5 +38,10 @@ dmnsn_new_object()
void
dmnsn_delete_object(dmnsn_object *object)
{
- free(object);
+ if (object) {
+ if (object->free_fn) {
+ (*object->free_fn)(object->ptr);
+ }
+ free(object);
+ }
}
diff --git a/libdimension/objects.c b/libdimension/objects.c
index 8f45c0e..32d3fdd 100644
--- a/libdimension/objects.c
+++ b/libdimension/objects.c
@@ -44,13 +44,6 @@ dmnsn_new_sphere()
return sphere;
}
-/* Free a sphere */
-void
-dmnsn_delete_sphere(dmnsn_object *sphere)
-{
- dmnsn_delete_object(sphere);
-}
-
/* Return a list of insersections of `line' with a sphere */
static dmnsn_array *
dmnsn_sphere_intersections_fn(const dmnsn_object *sphere, dmnsn_line line)
@@ -103,13 +96,6 @@ dmnsn_new_cube()
return cube;
}
-/* Delete a cube */
-void
-dmnsn_delete_cube(dmnsn_object *cube)
-{
- dmnsn_delete_object(cube);
-}
-
/* Intersections callback for a cube */
static dmnsn_array *
dmnsn_cube_intersections_fn(const dmnsn_object *cube, dmnsn_line line)
diff --git a/libdimension/pigments.c b/libdimension/pigments.c
index 3b417f6..22f75e8 100644
--- a/libdimension/pigments.c
+++ b/libdimension/pigments.c
@@ -46,15 +46,6 @@ dmnsn_new_solid_pigment(dmnsn_color color)
return pigment;
}
-/* Destroy a solid color */
-void dmnsn_delete_solid_pigment(dmnsn_pigment *pigment)
-{
- if (pigment) {
- free(pigment->ptr);
- dmnsn_delete_pigment(pigment);
- }
-}
-
/* Solid color callback */
static dmnsn_color
dmnsn_solid_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v)
diff --git a/libdimension/texture.c b/libdimension/texture.c
index 511c6b0..f232cbc 100644
--- a/libdimension/texture.c
+++ b/libdimension/texture.c
@@ -25,7 +25,11 @@
dmnsn_pigment *
dmnsn_new_pigment()
{
- return malloc(sizeof(dmnsn_pigment));
+ dmnsn_pigment *pigment = malloc(sizeof(dmnsn_pigment));
+ if (pigment) {
+ pigment->free_fn = NULL;
+ }
+ return pigment;
}
/* Free a dummy pigment */
@@ -39,7 +43,11 @@ dmnsn_delete_pigment(dmnsn_pigment *pigment)
dmnsn_texture *
dmnsn_new_texture()
{
- return malloc(sizeof(dmnsn_texture));
+ dmnsn_texture *texture = malloc(sizeof(dmnsn_texture));
+ if (texture) {
+ texture->pigment = NULL;
+ }
+ return texture;
}
/* Free a dummy texture */