diff options
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/dimension/camera.h | 2 | ||||
-rw-r--r-- | libdimension/dimension/light.h | 19 | ||||
-rw-r--r-- | libdimension/dimension/object.h | 2 | ||||
-rw-r--r-- | libdimension/light.c | 27 | ||||
-rw-r--r-- | libdimension/point_light.c | 32 |
5 files changed, 49 insertions, 33 deletions
diff --git a/libdimension/dimension/camera.h b/libdimension/dimension/camera.h index d8e0bdf..e94e227 100644 --- a/libdimension/dimension/camera.h +++ b/libdimension/dimension/camera.h @@ -61,7 +61,7 @@ dmnsn_camera *dmnsn_new_camera(void); /** * Initialize a dmnsn_camera field. - * @param[in,out] camera The camera to initialize. + * @param[out] camera The camera to initialize. */ void dmnsn_init_camera(dmnsn_camera *camera); diff --git a/libdimension/dimension/light.h b/libdimension/dimension/light.h index a1f7ff2..aebb2d8 100644 --- a/libdimension/dimension/light.h +++ b/libdimension/dimension/light.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. * * * @@ -54,16 +54,19 @@ typedef dmnsn_color dmnsn_light_illumination_fn(const dmnsn_light *light, */ typedef bool dmnsn_light_shadow_fn(const dmnsn_light *light, double t); +/** + * Light destruction callback. + * @param[in,out] light The light to free. + */ +typedef void dmnsn_light_free_fn(dmnsn_light *light); + /** A light. */ struct dmnsn_light { /* Callbacks */ dmnsn_light_direction_fn *direction_fn; /**< Direction callback. */ dmnsn_light_illumination_fn *illumination_fn; /**< Illumination callback. */ dmnsn_light_shadow_fn *shadow_fn; /**< Shadow callback. */ - dmnsn_free_fn *free_fn; /**< Desctructor callback. */ - - /** Generic pointer for light info. */ - void *ptr; + dmnsn_light_free_fn *free_fn; /**< Desctructor callback. */ DMNSN_REFCOUNT; /**< Reference count. */ }; @@ -75,6 +78,12 @@ struct dmnsn_light { dmnsn_light *dmnsn_new_light(void); /** + * Initialize a dmnsn_light field. + * @param[out] light The light to initialize. + */ +void dmnsn_init_light(dmnsn_light *light); + +/** * Delete a light. * @param[in,out] light The light to delete. */ diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h index 8d76cc7..75a8aa7 100644 --- a/libdimension/dimension/object.h +++ b/libdimension/dimension/object.h @@ -105,7 +105,7 @@ dmnsn_object *dmnsn_new_object(void); /** * Initialize a dmnsn_object field. - * @param[in,out] object The object to initialize. + * @param[out] object The object to initialize. */ void dmnsn_init_object(dmnsn_object *object); diff --git a/libdimension/light.c b/libdimension/light.c index fdb65bd..daabe6f 100644 --- a/libdimension/light.c +++ b/libdimension/light.c @@ -26,18 +26,30 @@ #include "dimension-internal.h" #include <stdlib.h> +static void +dmnsn_default_light_free_fn(dmnsn_light *light) +{ + dmnsn_free(light); +} + /* Allocate a new dummy light */ dmnsn_light * dmnsn_new_light(void) { dmnsn_light *light = DMNSN_MALLOC(dmnsn_light); - light->direction_fn = NULL; + dmnsn_init_light(light); + return light; +} + +/* Initialize a light */ +void +dmnsn_init_light(dmnsn_light *light) +{ + light->direction_fn = NULL; light->illumination_fn = NULL; - light->shadow_fn = NULL; - light->free_fn = NULL; - light->ptr = NULL; + light->shadow_fn = NULL; + light->free_fn = dmnsn_default_light_free_fn; DMNSN_REFCOUNT_INIT(light); - return light; } /* Free a dummy light */ @@ -45,9 +57,6 @@ void dmnsn_delete_light(dmnsn_light *light) { if (DMNSN_DECREF(light)) { - if (light->free_fn) { - light->free_fn(light->ptr); - } - dmnsn_free(light); + light->free_fn(light); } } diff --git a/libdimension/point_light.c b/libdimension/point_light.c index 6b5639d..2f994b2 100644 --- a/libdimension/point_light.c +++ b/libdimension/point_light.c @@ -26,26 +26,27 @@ #include "dimension.h" #include <stdlib.h> -/** Point light payload type. */ -typedef struct dmnsn_point_light_payload { +/** Point light type. */ +typedef struct dmnsn_point_light { + dmnsn_light light; dmnsn_vector origin; dmnsn_color color; -} dmnsn_point_light_payload; +} dmnsn_point_light; /** Point light direction callback. */ static dmnsn_vector dmnsn_point_light_direction_fn(const dmnsn_light *light, dmnsn_vector v) { - dmnsn_point_light_payload *payload = light->ptr; - return dmnsn_vector_sub(payload->origin, v); + const dmnsn_point_light *point_light = (const dmnsn_point_light *)light; + return dmnsn_vector_sub(point_light->origin, v); } /** Point light illumination callback. */ static dmnsn_color dmnsn_point_light_illumination_fn(const dmnsn_light *light, dmnsn_vector v) { - dmnsn_point_light_payload *payload = light->ptr; - return payload->color; + const dmnsn_point_light *point_light = (const dmnsn_point_light *)light; + return point_light->color; } /** Point light illumination callback. */ @@ -58,17 +59,14 @@ dmnsn_point_light_shadow_fn(const dmnsn_light *light, double t) dmnsn_light * dmnsn_new_point_light(dmnsn_vector x0, dmnsn_color color) { - dmnsn_light *light = dmnsn_new_light(); + dmnsn_point_light *point_light = DMNSN_MALLOC(dmnsn_point_light); + point_light->origin = x0; + point_light->color = color; - dmnsn_point_light_payload *payload = DMNSN_MALLOC(dmnsn_point_light_payload); - payload->origin = x0; - payload->color = color; - light->ptr = payload; - - light->direction_fn = dmnsn_point_light_direction_fn; + dmnsn_light *light = &point_light->light; + dmnsn_init_light(light); + light->direction_fn = dmnsn_point_light_direction_fn; light->illumination_fn = dmnsn_point_light_illumination_fn; - light->shadow_fn = dmnsn_point_light_shadow_fn; - light->free_fn = dmnsn_free; - + light->shadow_fn = dmnsn_point_light_shadow_fn; return light; } |