summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/dimension/map.h11
-rw-r--r--libdimension/dimension/pigments.h5
-rw-r--r--libdimension/map.c34
-rw-r--r--libdimension/pigment_map.c5
-rw-r--r--libdimension/tests/render.c8
5 files changed, 28 insertions, 35 deletions
diff --git a/libdimension/dimension/map.h b/libdimension/dimension/map.h
index 7e12e7e..77b1fe9 100644
--- a/libdimension/dimension/map.h
+++ b/libdimension/dimension/map.h
@@ -1,5 +1,5 @@
/*************************************************************************
- * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> *
+ * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> *
* *
* This file is part of The Dimension Library. *
* *
@@ -32,16 +32,11 @@ typedef struct dmnsn_map {
/**
* Create an empty map.
+ * @param[in] pool The memory pool to allocate from.
* @param[in] size The size of the objects to store in the map.
* @return A map with no entries.
*/
-dmnsn_map *dmnsn_new_map(size_t size);
-
-/**
- * Delete a map.
- * @param[in,out] map The map to delete.
- */
-void dmnsn_delete_map(dmnsn_map *map);
+dmnsn_map *dmnsn_new_map(dmnsn_pool *pool, size_t size);
/**
* Add an entry (a scalar-object pair) to a map.
diff --git a/libdimension/dimension/pigments.h b/libdimension/dimension/pigments.h
index 383e38b..e2ea274 100644
--- a/libdimension/dimension/pigments.h
+++ b/libdimension/dimension/pigments.h
@@ -1,5 +1,5 @@
/*************************************************************************
- * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> *
+ * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> *
* *
* This file is part of The Dimension Library. *
* *
@@ -48,9 +48,10 @@ typedef enum dmnsn_pigment_map_flags {
/**
* Construct a pigment map.
+ * @param[in] pool The memory pool to allocate from.
* @return An empty pigment map.
*/
-dmnsn_map *dmnsn_new_pigment_map(void);
+dmnsn_map *dmnsn_new_pigment_map(dmnsn_pool *pool);
/**
* A pigment-mapped pigment.
diff --git a/libdimension/map.c b/libdimension/map.c
index 963f170..df07ec8 100644
--- a/libdimension/map.c
+++ b/libdimension/map.c
@@ -31,10 +31,24 @@ typedef struct dmnsn_map_entry {
char object[];
} dmnsn_map_entry;
+static void
+dmnsn_map_cleanup(void *ptr)
+{
+ dmnsn_map *map = ptr;
+ if (map->free_fn) {
+ for (size_t i = 0; i < dmnsn_array_size(map->array); ++i) {
+ dmnsn_map_entry *entry = dmnsn_array_at(map->array, i);
+ map->free_fn(entry->object);
+ }
+ }
+
+ dmnsn_delete_array(map->array);
+}
+
dmnsn_map *
-dmnsn_new_map(size_t size)
+dmnsn_new_map(dmnsn_pool *pool, size_t size)
{
- dmnsn_map *map = DMNSN_MALLOC(dmnsn_map);
+ dmnsn_map *map = DMNSN_PALLOC_TIDY(pool, dmnsn_map, dmnsn_map_cleanup);
map->free_fn = NULL;
map->obj_size = size;
map->array = dmnsn_new_array(sizeof(dmnsn_map_entry) + size);
@@ -42,22 +56,6 @@ dmnsn_new_map(size_t size)
}
void
-dmnsn_delete_map(dmnsn_map *map)
-{
- if (map) {
- if (map->free_fn) {
- for (size_t i = 0; i < dmnsn_array_size(map->array); ++i) {
- dmnsn_map_entry *entry = dmnsn_array_at(map->array, i);
- map->free_fn(entry->object);
- }
- }
-
- dmnsn_delete_array(map->array);
- dmnsn_free(map);
- }
-}
-
-void
dmnsn_map_add_entry(dmnsn_map *map, double n, const void *obj)
{
dmnsn_map_entry *entry
diff --git a/libdimension/pigment_map.c b/libdimension/pigment_map.c
index 6f07f15..a8a4b75 100644
--- a/libdimension/pigment_map.c
+++ b/libdimension/pigment_map.c
@@ -42,9 +42,9 @@ dmnsn_delete_mapped_pigment(void *ptr)
}
dmnsn_map *
-dmnsn_new_pigment_map(void)
+dmnsn_new_pigment_map(dmnsn_pool *pool)
{
- dmnsn_map *pigment_map = dmnsn_new_map(sizeof(dmnsn_pigment *));
+ dmnsn_map *pigment_map = dmnsn_new_map(pool, sizeof(dmnsn_pigment *));
pigment_map->free_fn = dmnsn_delete_mapped_pigment;
return pigment_map;
}
@@ -62,7 +62,6 @@ static void
dmnsn_pigment_map_free_fn(dmnsn_pigment *pigment)
{
dmnsn_pigment_map *pigment_map = (dmnsn_pigment_map *)pigment;
- dmnsn_delete_map(pigment_map->map);
dmnsn_free(pigment_map);
}
diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c
index 4c230e1..2906717 100644
--- a/libdimension/tests/render.c
+++ b/libdimension/tests/render.c
@@ -72,7 +72,7 @@ static void
dmnsn_test_scene_add_background(dmnsn_pool *pool, dmnsn_scene *scene)
{
dmnsn_pattern *sky_gradient = dmnsn_new_gradient_pattern(pool, dmnsn_y);
- dmnsn_map *sky_gradient_pigment_map = dmnsn_new_pigment_map();
+ dmnsn_map *sky_gradient_pigment_map = dmnsn_new_pigment_map(pool);
dmnsn_canvas *png_canvas = NULL;
dmnsn_pigment *png_pigment;
@@ -173,7 +173,7 @@ dmnsn_test_scene_add_spike(dmnsn_pool *pool, dmnsn_scene *scene)
dmnsn_object *arrow = dmnsn_new_csg_union(arrow_array);
dmnsn_delete_array(arrow_array);
dmnsn_pattern *gradient = dmnsn_new_gradient_pattern(pool, dmnsn_y);
- dmnsn_map *gradient_pigment_map = dmnsn_new_pigment_map();
+ dmnsn_map *gradient_pigment_map = dmnsn_new_pigment_map(pool);
dmnsn_pigment_map_add_color(gradient_pigment_map, 0.0, dmnsn_red);
dmnsn_pigment_map_add_color(gradient_pigment_map, 1.0/6.0, dmnsn_orange);
dmnsn_pigment_map_add_color(gradient_pigment_map, 2.0/6.0, dmnsn_yellow);
@@ -266,7 +266,7 @@ dmnsn_test_scene_add_ground(dmnsn_pool *pool, dmnsn_scene *scene)
dmnsn_object *plane = dmnsn_new_plane(dmnsn_new_vector(0.0, 1.0, 0.0));
plane->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -2.0, 0.0));
dmnsn_pattern *checker = dmnsn_new_checker_pattern(pool);
- dmnsn_map *small_map = dmnsn_new_pigment_map();
+ dmnsn_map *small_map = dmnsn_new_pigment_map(pool);
dmnsn_pigment_map_add_color(small_map, 0.0, dmnsn_black);
dmnsn_pigment_map_add_color(small_map, 1.0, dmnsn_white);
dmnsn_pigment *small_pigment =
@@ -274,7 +274,7 @@ dmnsn_test_scene_add_ground(dmnsn_pool *pool, dmnsn_scene *scene)
DMNSN_PIGMENT_MAP_REGULAR);
small_pigment->trans =
dmnsn_scale_matrix(dmnsn_new_vector(1.0/3.0, 1.0/3.0, 1.0/3.0));
- dmnsn_map *big_map = dmnsn_new_pigment_map();
+ dmnsn_map *big_map = dmnsn_new_pigment_map(pool);
dmnsn_pigment_map_add_color(big_map, 0.0, dmnsn_white);
dmnsn_map_add_entry(big_map, 1.0, &small_pigment);
plane->texture = dmnsn_new_texture();