scenefile

view src/scene.c @ 3:b30f83409769

foo
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sat, 21 Jan 2012 04:14:24 +0200
parents c15992cedec9
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "scene.h"
6 #include "dynarr.h"
8 struct scenefile {
9 struct mesh **mesh;
10 };
12 struct scnfile_io {
13 void *uptr; /* user-data */
15 size_t (*read)(void *buf, size_t bytes, void *uptr);
16 size_t (*write)(void *buf, size_t bytes, void *uptr);
17 long (*seek)(long offs, int whence, void *uptr);
18 };
20 static size_t def_read(void *buf, size_t bytes, void *uptr);
21 static size_t def_write(void *buf, size_t bytes, void *uptr);
22 static long def_seek(long offset, int whence, void *uptr);
25 int scnfile_init(struct scenefile *scn)
26 {
27 if(!(scn->mesh = dynarr_alloc(0, sizeof *scn->mesh))) {
28 return -1;
29 }
30 return 0;
31 }
33 void scnfile_destroy(struct scenefile *scn)
34 {
35 int i, num_meshes = scnfile_count(scn);
37 for(i=0; i<num_meshes; i++) {
38 mesh_destroy(scn->mesh[i]);
39 free(scn->mesh[i]);
40 }
41 dynarr_free(scn->mesh);
42 }
44 struct scenefile *scnfile_create(void)
45 {
46 struct scenefile *scn;
48 if(!(scn = malloc(sizeof *scn))) {
49 return 0;
50 }
51 if(scnfile_init(scn) == -1) {
52 free(scn);
53 return 0;
54 }
55 return scn;
56 }
58 void scnfile_free(struct scenefile *scn)
59 {
60 scnfile_destroy(scn);
61 free(scn);
62 }
65 int scnfile_add_mesh(struct scenefile *scn, struct mesh *m)
66 {
67 void *tmp;
68 if(!(tmp = dynarr_push(scn->mesh, m))) {
69 return -1;
70 }
71 scn->mesh = tmp;
72 return 0;
73 }
75 int scnfile_load(struct scenefile *scn, const char *fname)
76 {
77 FILE *fp;
78 int res;
80 if(!(fp = fopen(fname, "rb"))) {
81 fprintf(stderr, "scenefile: failed to load: %s: %s\n", fname, strerror(errno));
82 return -1;
83 }
84 res = scnfile_read_file(scn, fp);
85 fclose(fp);
86 return res;
87 }
89 int scnfile_read_file(struct scenefile *scn, FILE *fp)
90 {
91 struct scnfile_io io = {0, def_read, def_write, def_seek};
92 io.uptr = fp;
93 return scnfile_read(scn, &io);
94 }
96 int scnfile_read(struct scenefile *scn, struct scnfile_io *io)
97 {
98 return -1; /* TODO */
99 }
101 int scnfile_find_mesh(struct scenefile *scn, const char *fname)
102 {
103 int i, num = scnfile_count(scn);
105 for(i=0; i<num; i++) {
106 if(strcmp(mesh_get_name(scn->mesh[i]), fname) == 0) {
107 return i;
108 }
109 }
110 return -1;
111 }
113 struct mesh *scnfile_mesh(struct scenefile *scn, int idx)
114 {
115 if(idx < 0 || idx >= scnfile_count(scn)) {
116 return 0;
117 }
118 return scn->mesh[idx];
119 }
121 int scnfile_count(struct scenefile *scn)
122 {
123 return dynarr_size(scn->mesh);
124 }
127 void scnfile_io_user_data(struct scnfile_io *io, void *uptr)
128 {
129 io->uptr = uptr;
130 }
132 void scnfile_io_read_func(struct scnfile_io *io, size_t (*read)(void*, size_t, void*))
133 {
134 io->read = read;
135 }
137 void scnfile_io_write_func(struct scnfile_io *io, size_t (*write)(void*, size_t, void*))
138 {
139 io->write = write;
140 }
142 void scnfile_io_seek_func(struct scnfile_io *io, long (*seek)(long, int, void*))
143 {
144 io->seek = seek;
145 }
148 static size_t def_read(void *buf, size_t bytes, void *uptr)
149 {
150 return uptr ? fread(buf, 1, bytes, uptr) : 0;
151 }
153 static size_t def_write(void *buf, size_t bytes, void *uptr)
154 {
155 return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
156 }
158 static long def_seek(long offset, int whence, void *uptr)
159 {
160 if(!uptr || fseek(uptr, offset, whence) == -1) {
161 return -1;
162 }
163 return ftell(uptr);
164 }