goat3d
view src/goat3d_write.cc @ 45:8da36540e2e9
fixed memory leak in readxml and reversed error return
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 08 Dec 2013 02:27:32 +0200 |
parents | |
children | 498ca7ac7047 |
line source
1 #include "goat3d_impl.h"
2 #include "chunk.h"
4 /*
5 static long save_env(const Scene *scn, long offset, goat3d_io *io);
6 static long save_materials(const Scene *scn, long offset, goat3d_io *io);
7 static long save_material(const Material *mat, long offset, goat3d_io *io);
8 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io);
9 static long save_meshes(const Scene *scn, long offset, goat3d_io *io);
10 static long save_lights(const Scene *scn, long offset, goat3d_io *io);
11 static long save_cameras(const Scene *scn, long offset, goat3d_io *io);
12 static long save_nodes(const Scene *scn, long offset, goat3d_io *io);
14 static long write_chunk_float(int id, float val, long offs, goat3d_io *io);
15 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io);
16 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io);
17 */
19 bool Scene::save(goat3d_io *io) const
20 {
21 /*
22 long res;
24 ChunkHeader hdr;
25 hdr.id = CNK_SCENE;
26 hdr.size = sizeof hdr;
28 if((res = save_env(this, hdr.size, io)) < 0) {
29 return false;
30 }
31 hdr.size += res;
33 if((res = save_materials(this, hdr.size, io)) < 0) {
34 return false;
35 }
36 hdr.size += res;
38 if((res = save_meshes(this, hdr.size, io)) < 0) {
39 return false;
40 }
41 hdr.size += res;
43 if((res = save_lights(this, hdr.size, io)) < 0) {
44 return false;
45 }
46 hdr.size += res;
48 if((res = save_cameras(this, hdr.size, io)) < 0) {
49 return false;
50 }
51 hdr.size += res;
53 if((res = save_nodes(this, hdr.size, io)) < 0) {
54 return false;
55 }
56 hdr.size += res;
58 // now go back and write the root chunk
59 io->seek(0, SEEK_SET, io->cls);
60 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
61 return false;
62 }
64 return true;
65 */
66 return false;
67 }
70 #if 0
71 static long save_env(const Scene *scn, long offset, goat3d_io *io)
72 {
73 long res;
75 ChunkHeader hdr;
76 hdr.id = CNK_ENV;
77 hdr.size = sizeof hdr;
79 if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) {
80 return -1;
81 }
82 hdr.size += res;
84 // TODO add fog chunk
86 io->seek(offset, SEEK_SET, io->cls);
87 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
88 return -1;
89 }
90 return hdr.size;
91 }
93 static long save_materials(const Scene *scn, long offset, goat3d_io *io)
94 {
95 long res;
97 ChunkHeader hdr;
98 hdr.id = CNK_MTL_LIST;
99 hdr.size = sizeof hdr;
101 for(int i=0; i<scn->get_material_count(); i++) {
102 if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) {
103 return -1;
104 }
105 hdr.size += res;
106 }
108 io->seek(offset, SEEK_SET, io->cls);
109 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
110 return -1;
111 }
112 return hdr.size;
113 }
115 static long save_material(const Material *mat, long offset, goat3d_io *io)
116 {
117 long res;
119 ChunkHeader hdr;
120 hdr.id = CNK_MTL;
121 hdr.size = sizeof hdr;
123 for(int i=0; i<mat->get_attrib_count(); i++) {
124 const char *name = mat->get_attrib_name(i);
125 if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) {
126 return -1;
127 }
128 hdr.size += res;
129 }
131 io->seek(offset, SEEK_SET, io->cls);
132 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
133 return -1;
134 }
135 return hdr.size;
136 }
138 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io)
139 {
140 long res;
142 ChunkHeader hdr;
143 hdr.id = CNK_MTL_ATTR;
144 hdr.size = sizeof hdr;
146 // TODO cont.
147 return -1;
148 }
150 static long save_meshes(const Scene *scn, long offset, goat3d_io *io)
151 {
152 return 0;
153 }
155 static long save_lights(const Scene *scn, long offset, goat3d_io *io)
156 {
157 return 0;
158 }
160 static long save_cameras(const Scene *scn, long offset, goat3d_io *io)
161 {
162 return 0;
163 }
165 static long save_nodes(const Scene *scn, long offset, goat3d_io *io)
166 {
167 return 0;
168 }
170 static long write_chunk_float(int id, float val, long offs, goat3d_io *io)
171 {
172 int size = sizeof(ChunkHeader) + sizeof val;
173 char *buf = (char*)alloca(size);
175 Chunk *c = (Chunk*)buf;
176 c->hdr.id = id;
177 c->hdr.size = size;
178 *(float*)c->data = val;
180 io->seek(offs, SEEK_SET, io->cls);
181 if(io->write(buf, size, io->cls) < size) {
182 return -1;
183 }
184 return size;
185 }
187 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io)
188 {
189 int size = sizeof(ChunkHeader) + sizeof vec;
190 char *buf = (char*)alloca(size);
192 Chunk *c = (Chunk*)buf;
193 c->hdr.id = id;
194 c->hdr.size = size;
195 *(Vector3*)c->data = vec;
197 io->seek(offs, SEEK_SET, io->cls);
198 if(io->write(buf, size, io->cls) < size) {
199 return -1;
200 }
201 return size;
202 }
204 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io)
205 {
206 int size = sizeof(ChunkHeader) + sizeof vec;
207 char *buf = (char*)alloca(size);
209 Chunk *c = (Chunk*)buf;
210 c->hdr.id = id;
211 c->hdr.size = size;
212 *(Vector4*)c->data = vec;
214 io->seek(offs, SEEK_SET, io->cls);
215 if(io->write(buf, size, io->cls) < size) {
216 return -1;
217 }
218 return size;
219 }
220 #endif