tinygi

diff src/scene.c @ 0:16fdca2a1ef5

initial tinygi commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Jul 2015 18:30:29 +0300
parents
children bc64090fe3d1
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/scene.c	Sun Jul 19 18:30:29 2015 +0300
     1.3 @@ -0,0 +1,98 @@
     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 +	return tgi;
    1.26 +}
    1.27 +
    1.28 +void tgi_destroy(struct tinygi *tgi)
    1.29 +{
    1.30 +	tgi_clear_scene(tgi);
    1.31 +	free(tgi);
    1.32 +}
    1.33 +
    1.34 +void tgi_clear_scene(struct tinygi *tgi)
    1.35 +{
    1.36 +	int i, nobj = dynarr_size(tgi->objects);
    1.37 +
    1.38 +	for(i=0; i<nobj; i++) {
    1.39 +		tgi_destroy_object(tgi->objects[i]);
    1.40 +	}
    1.41 +	tgi->objects = dynarr_resize(tgi->objects, 0);
    1.42 +	assert(tgi->objects);
    1.43 +}
    1.44 +
    1.45 +int tgi_load_scene(struct tinygi *tgi, const char *fname)
    1.46 +{
    1.47 +	return -1;	/* TODO implement later */
    1.48 +}
    1.49 +
    1.50 +void tgi_add_object(struct tinygi *tgi, struct tgi_object *o)
    1.51 +{
    1.52 +	tgi->objects = dynarr_push(tgi->objects, o);
    1.53 +	assert(tgi->objects);
    1.54 +}
    1.55 +
    1.56 +int tgi_del_object(struct tinygi *tgi, struct tgi_object *o)
    1.57 +{
    1.58 +	int i, idx = -1, sz = dynarr_size(tgi->objects);
    1.59 +
    1.60 +	if(!sz) return -1;
    1.61 +
    1.62 +	for(i=0; i<sz; i++) {
    1.63 +		if(tgi->objects[i] == o) {
    1.64 +			idx = i;
    1.65 +			break;
    1.66 +		}
    1.67 +	}
    1.68 +	if(idx == -1) {
    1.69 +		return -1;
    1.70 +	}
    1.71 +
    1.72 +	tgi->objects[idx] = tgi->objects[sz - 1];
    1.73 +	tgi->objects = dynarr_pop(tgi->objects);
    1.74 +	assert(tgi->objects);
    1.75 +	return 0;
    1.76 +}
    1.77 +
    1.78 +struct tgi_object *tgi_find_object(struct tinygi *tgi, const char *name)
    1.79 +{
    1.80 +	int i, sz = dynarr_size(tgi->objects);
    1.81 +
    1.82 +	for(i=0; i<sz; i++) {
    1.83 +		if(strcmp(tgi->objects[i]->name, name) == 0) {
    1.84 +			return tgi->objects[i];
    1.85 +		}
    1.86 +	}
    1.87 +	return 0;
    1.88 +}
    1.89 +
    1.90 +
    1.91 +struct tgi_object *tgi_get_object(struct tinygi *tgi, int idx)
    1.92 +{
    1.93 +	return tgi->objects[idx];
    1.94 +}
    1.95 +
    1.96 +int tgi_get_object_count(struct tinygi *tgi)
    1.97 +{
    1.98 +	return dynarr_size(tgi->objects);
    1.99 +}
   1.100 +
   1.101 +