summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dimension/realize.c18
-rw-r--r--libdimension/finishes.c44
-rw-r--r--tests/libdimension/tests.c34
3 files changed, 40 insertions, 56 deletions
diff --git a/dimension/realize.c b/dimension/realize.c
index 6ebcdd1..85a4f5a 100644
--- a/dimension/realize.c
+++ b/dimension/realize.c
@@ -547,23 +547,13 @@ dmnsn_realize_astree(const dmnsn_astree *astree)
}
/* Default finish */
- dmnsn_finish *ambient = dmnsn_new_ambient_finish(
- dmnsn_color_mul(0.1, dmnsn_white)
- );
- dmnsn_finish *diffuse = dmnsn_new_diffuse_finish(0.6);
- if (!ambient || !diffuse) {
- dmnsn_delete_finish(diffuse);
- dmnsn_delete_finish(ambient);
- dmnsn_delete_scene(scene);
- return NULL;
- }
scene->default_texture->finish = dmnsn_new_finish_combination(
- ambient,
- diffuse
+ dmnsn_new_ambient_finish(
+ dmnsn_color_mul(0.1, dmnsn_white)
+ ),
+ dmnsn_new_diffuse_finish(0.6)
);
if (!scene->default_texture->finish) {
- dmnsn_delete_finish(diffuse);
- dmnsn_delete_finish(ambient);
dmnsn_delete_scene(scene);
return NULL;
}
diff --git a/libdimension/finishes.c b/libdimension/finishes.c
index a8347bd..a1b10e7 100644
--- a/libdimension/finishes.c
+++ b/libdimension/finishes.c
@@ -78,23 +78,37 @@ dmnsn_finish_combination_free_fn(void *ptr)
dmnsn_finish *
dmnsn_new_finish_combination(dmnsn_finish *f1, dmnsn_finish *f2)
{
- dmnsn_finish *finish = dmnsn_new_finish();
- if (finish) {
- dmnsn_finish **params = malloc(2*sizeof(dmnsn_finish *));
- if (!params) {
- dmnsn_delete_finish(finish);
- return NULL;
+ if (f1 && f2) {
+ dmnsn_finish *finish = dmnsn_new_finish();
+ if (finish) {
+ dmnsn_finish **params = malloc(2*sizeof(dmnsn_finish *));
+ if (!params) {
+ dmnsn_delete_finish(finish);
+ dmnsn_delete_finish(f2);
+ dmnsn_delete_finish(f1);
+ return NULL;
+ }
+
+ params[0] = f1;
+ params[1] = f2;
+
+ finish->ptr = params;
+ finish->finish_fn = &dmnsn_finish_combination_fn;
+ finish->ambient_fn = &dmnsn_finish_combination_ambient_fn;
+ finish->free_fn = &dmnsn_finish_combination_free_fn;
+
+ return finish;
+ } else {
+ dmnsn_delete_finish(f2);
+ dmnsn_delete_finish(f1);
}
-
- params[0] = f1;
- params[1] = f2;
-
- finish->ptr = params;
- finish->finish_fn = &dmnsn_finish_combination_fn;
- finish->ambient_fn = &dmnsn_finish_combination_ambient_fn;
- finish->free_fn = &dmnsn_finish_combination_free_fn;
+ } else if (f1) {
+ dmnsn_delete_finish(f1);
+ } else if (f2) {
+ dmnsn_delete_finish(f2);
}
- return finish;
+
+ return NULL;
}
/*
diff --git a/tests/libdimension/tests.c b/tests/libdimension/tests.c
index bc98cd5..54f506a 100644
--- a/tests/libdimension/tests.c
+++ b/tests/libdimension/tests.c
@@ -29,36 +29,16 @@ dmnsn_new_default_scene()
}
/* Default finish */
- dmnsn_finish *ambient = dmnsn_new_ambient_finish(
- dmnsn_color_mul(0.1, dmnsn_white)
- );
- dmnsn_finish *diffuse = dmnsn_new_diffuse_finish(0.6);
- dmnsn_finish *phong = dmnsn_new_phong_finish(0.2, 40.0);
- if (!ambient || !diffuse || !phong) {
- dmnsn_delete_finish(diffuse);
- dmnsn_delete_finish(ambient);
- dmnsn_delete_finish(phong);
- dmnsn_delete_scene(scene);
- return NULL;
- }
- dmnsn_finish *comb1 = dmnsn_new_finish_combination(
- ambient,
- diffuse
- );
- if (!comb1) {
- dmnsn_delete_finish(diffuse);
- dmnsn_delete_finish(ambient);
- dmnsn_delete_finish(phong);
- dmnsn_delete_scene(scene);
- return NULL;
- }
scene->default_texture->finish = dmnsn_new_finish_combination(
- phong,
- comb1
+ dmnsn_new_finish_combination(
+ dmnsn_new_ambient_finish(
+ dmnsn_color_mul(0.1, dmnsn_white)
+ ),
+ dmnsn_new_diffuse_finish(0.6)
+ ),
+ dmnsn_new_phong_finish(0.2, 40.0)
);
if (!scene->default_texture->finish) {
- dmnsn_delete_finish(comb1);
- dmnsn_delete_finish(phong);
dmnsn_delete_scene(scene);
return NULL;
}