tinygi
diff src/tinygi.c @ 2:72752a1b3dbe
images and shapes
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 21 Jul 2015 04:30:00 +0300 |
parents | src/scene.c@bc64090fe3d1 |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/tinygi.c Tue Jul 21 04:30:00 2015 +0300 1.3 @@ -0,0 +1,103 @@ 1.4 +#include <stdlib.h> 1.5 +#include <string.h> 1.6 +#include <assert.h> 1.7 +#include "tgi_impl.h" 1.8 +#include "object.h" 1.9 +#include "dynarr.h" 1.10 + 1.11 +struct tinygi *tgi_init(void) 1.12 +{ 1.13 + struct tinygi *tgi; 1.14 + 1.15 + if(!(tgi = malloc(sizeof *tgi))) { 1.16 + tgi_log("failed to allocate memory\n"); 1.17 + return 0; 1.18 + } 1.19 + if(!(tgi->objects = dynarr_alloc(0, sizeof *tgi->objects))) { 1.20 + tgi_log("failed to allocate objects array\n"); 1.21 + free(tgi); 1.22 + return 0; 1.23 + } 1.24 + 1.25 + tgi_init_image(&tgi->fb); 1.26 + tgi_init_image(&tgi->fbfinal); 1.27 + tgi->fb_nsamples = 0; 1.28 + 1.29 + return tgi; 1.30 +} 1.31 + 1.32 +void tgi_destroy(struct tinygi *tgi) 1.33 +{ 1.34 + tgi_clear_scene(tgi); 1.35 + tgi_destroy_image(&tgi->fb); 1.36 + tgi_destroy_image(&tgi->fbfinal); 1.37 + free(tgi->fb_nsamples); 1.38 + free(tgi); 1.39 +} 1.40 + 1.41 +void tgi_clear_scene(struct tinygi *tgi) 1.42 +{ 1.43 + int i, nobj = dynarr_size(tgi->objects); 1.44 + 1.45 + for(i=0; i<nobj; i++) { 1.46 + tgi_destroy_object(tgi->objects[i]); 1.47 + } 1.48 + tgi->objects = dynarr_resize(tgi->objects, 0); 1.49 + assert(tgi->objects); 1.50 +} 1.51 + 1.52 +int tgi_load_scene(struct tinygi *tgi, const char *fname) 1.53 +{ 1.54 + return -1; /* TODO implement later */ 1.55 +} 1.56 + 1.57 +void tgi_add_object(struct tinygi *tgi, struct tgi_object *o) 1.58 +{ 1.59 + tgi->objects = dynarr_push(tgi->objects, o); 1.60 + assert(tgi->objects); 1.61 +} 1.62 + 1.63 +int tgi_remove_object(struct tinygi *tgi, struct tgi_object *o) 1.64 +{ 1.65 + int i, idx = -1, sz = dynarr_size(tgi->objects); 1.66 + 1.67 + if(!sz) return -1; 1.68 + 1.69 + for(i=0; i<sz; i++) { 1.70 + if(tgi->objects[i] == o) { 1.71 + idx = i; 1.72 + break; 1.73 + } 1.74 + } 1.75 + if(idx == -1) { 1.76 + return -1; 1.77 + } 1.78 + 1.79 + tgi->objects[idx] = tgi->objects[sz - 1]; 1.80 + tgi->objects = dynarr_pop(tgi->objects); 1.81 + assert(tgi->objects); 1.82 + return 0; 1.83 +} 1.84 + 1.85 +struct tgi_object *tgi_find_object(struct tinygi *tgi, const char *name) 1.86 +{ 1.87 + int i, sz = dynarr_size(tgi->objects); 1.88 + 1.89 + for(i=0; i<sz; i++) { 1.90 + if(strcmp(tgi->objects[i]->name, name) == 0) { 1.91 + return tgi->objects[i]; 1.92 + } 1.93 + } 1.94 + return 0; 1.95 +} 1.96 + 1.97 + 1.98 +struct tgi_object *tgi_get_object(struct tinygi *tgi, int idx) 1.99 +{ 1.100 + return tgi->objects[idx]; 1.101 +} 1.102 + 1.103 +int tgi_get_object_count(struct tinygi *tgi) 1.104 +{ 1.105 + return dynarr_size(tgi->objects); 1.106 +}