int main() { printf("%s\n", "testing sl_cons"); sphere *s1 = sphere_new(vec_new(0.0, 0.0, 0.0), 10.0, rgb_new(0.5, 0.5, 0.5)); sphere *s2 = sphere_new(vec_new(1.0, 1.0, 1.0), 5.0, rgb_new(0.1, 0.1, 0.1)); sphere *s3 = sphere_new(vec_new(100.0, 100.0, 100.0), 100.0, rgb_new(0, 0, 0)); sphere_list *sl = sl_cons(s1, sl_cons(s2, sl_cons(s3, NULL))); sl_print(sl); printf("%s\n", "testing sl_singleton"); sphere_list *sl2 = sl_singleton(sphere_dup(s1)); sl_print(sl2); sphere_list *sl3 = sl_cons(sphere_dup(s2), sl_singleton(sphere_dup(s1))); sl_print(sl3); printf("%s\n", "testing sl_dup"); sphere_list *sl4 = sl_dup(sl); sl_print(sl4); printf("%s\n", "FOR VALGRIND TESTING"); sl_free(sl); sl_free(sl2); sl_free(sl3); sl_free(sl4); }
/* constructor for a sphere_list */ sphere_list *sl_singleton(sphere *s) { if (s == NULL) { fprintf(stderr, "%s\n", "sl_singleton: sphere parameter is NULL"); exit(1); } return sl_cons(s, NULL); }
/* duplicates a sphere list */ sphere_list *sl_dup(sphere_list *sl) { if (sl == NULL) { fprintf(stderr, "%s\n", "sl_dup: parameter is NULL"); exit(1); } sphere_list *p = NULL; while (sl != NULL) { p = sl_cons(sphere_dup(sl->s), p); sl = sl->next; } return p; }
scene *scene_new(rgb *bg, sphere_list *ss, light *light, rgb *amb) { scene *news = malloc(sizeof(scene)); if(news == NULL) { printf("scene_new: Cannot allocate\n"); exit (1); } news->bg = rgb_dup(bg); sphere_list *sl = NULL; while (ss != NULL) { sl = sl_cons(ss->s, sl); ss= ss->next; } news->spheres = sl; news->light = light_dup(light); news->amb = rgb_dup(amb); return news; }