tinygi

view src/scene.c @ 1:bc64090fe3d1

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 20 Jul 2015 04:38:53 +0300
parents 16fdca2a1ef5
children
line source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "tgi_impl.h"
5 #include "object.h"
6 #include "dynarr.h"
8 struct tinygi *tgi_init(void)
9 {
10 struct tinygi *tgi;
12 if(!(tgi = malloc(sizeof *tgi))) {
13 tgi_log("failed to allocate memory\n");
14 return 0;
15 }
16 if(!(tgi->objects = dynarr_alloc(0, sizeof *tgi->objects))) {
17 tgi_log("failed to allocate objects array\n");
18 free(tgi);
19 return 0;
20 }
22 return tgi;
23 }
25 void tgi_destroy(struct tinygi *tgi)
26 {
27 tgi_clear_scene(tgi);
28 free(tgi);
29 }
31 void tgi_clear_scene(struct tinygi *tgi)
32 {
33 int i, nobj = dynarr_size(tgi->objects);
35 for(i=0; i<nobj; i++) {
36 tgi_destroy_object(tgi->objects[i]);
37 }
38 tgi->objects = dynarr_resize(tgi->objects, 0);
39 assert(tgi->objects);
40 }
42 int tgi_load_scene(struct tinygi *tgi, const char *fname)
43 {
44 return -1; /* TODO implement later */
45 }
47 void tgi_add_object(struct tinygi *tgi, struct tgi_object *o)
48 {
49 tgi->objects = dynarr_push(tgi->objects, o);
50 assert(tgi->objects);
51 }
53 int tgi_remove_object(struct tinygi *tgi, struct tgi_object *o)
54 {
55 int i, idx = -1, sz = dynarr_size(tgi->objects);
57 if(!sz) return -1;
59 for(i=0; i<sz; i++) {
60 if(tgi->objects[i] == o) {
61 idx = i;
62 break;
63 }
64 }
65 if(idx == -1) {
66 return -1;
67 }
69 tgi->objects[idx] = tgi->objects[sz - 1];
70 tgi->objects = dynarr_pop(tgi->objects);
71 assert(tgi->objects);
72 return 0;
73 }
75 struct tgi_object *tgi_find_object(struct tinygi *tgi, const char *name)
76 {
77 int i, sz = dynarr_size(tgi->objects);
79 for(i=0; i<sz; i++) {
80 if(strcmp(tgi->objects[i]->name, name) == 0) {
81 return tgi->objects[i];
82 }
83 }
84 return 0;
85 }
88 struct tgi_object *tgi_get_object(struct tinygi *tgi, int idx)
89 {
90 return tgi->objects[idx];
91 }
93 int tgi_get_object_count(struct tinygi *tgi)
94 {
95 return dynarr_size(tgi->objects);
96 }