scenefile

view src/scene.c @ 2:c15992cedec9

scene
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 15 Jan 2012 08:32:19 +0200
parents
children b30f83409769
line source
1 #include <stdlib.h>
2 #include <string.h>
3 #include "scene.h"
4 #include "dynarr.h"
6 struct scenefile {
7 struct mesh **mesh;
8 };
10 int scnfile_init(struct scenefile *scn)
11 {
12 if(!(scn->mesh = dynarr_alloc(0, sizeof *scn->mesh))) {
13 return -1;
14 }
15 return 0;
16 }
18 void scnfile_destroy(struct scenefile *scn)
19 {
20 int i, num_meshes = scnfile_count(scn);
22 for(i=0; i<num_meshes; i++) {
23 mesh_destroy(scn->mesh[i]);
24 free(scn->mesh[i]);
25 }
26 dynarr_free(scn->mesh);
27 }
29 struct scenefile *scnfile_create(void)
30 {
31 struct scenefile *scn;
33 if(!(scn = malloc(sizeof *scn))) {
34 return 0;
35 }
36 if(scnfile_init(scn) == -1) {
37 free(scn);
38 return 0;
39 }
40 return scn;
41 }
43 void scnfile_free(struct scenefile *scn)
44 {
45 scnfile_destroy(scn);
46 free(scn);
47 }
50 int scnfile_add_mesh(struct scenefile *scn, struct mesh *m)
51 {
52 void *tmp;
53 if(!(tmp = dynarr_push(scn->mesh, m))) {
54 return -1;
55 }
56 scn->mesh = tmp;
57 return 0;
58 }
60 int scnfile_load(struct scenefile *scn, const char *fname)
61 {
62 return -1; /* TODO */
63 }
65 int scnfile_find_mesh(struct scenefile *scn, const char *fname)
66 {
67 int i, num = scnfile_count(scn);
69 for(i=0; i<num; i++) {
70 if(strcmp(mesh_get_name(scn->mesh[i]), fname) == 0) {
71 return i;
72 }
73 }
74 return -1;
75 }
77 struct mesh *scnfile_mesh(struct scenefile *scn, int idx)
78 {
79 if(idx < 0 || idx >= scnfile_count(scn)) {
80 return 0;
81 }
82 return scn->mesh[idx];
83 }
85 int scnfile_count(struct scenefile *scn)
86 {
87 return dynarr_size(scn->mesh);
88 }