goat3d

annotate src/goat3d.cc @ 23:b59a3650ed51

mac fixes
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Sep 2013 07:20:22 +0300
parents 1d85d7dd0038
children d0260d80ae09
rev   line source
nuclear@16 1 #include <string.h>
nuclear@16 2 #include <errno.h>
nuclear@16 3 #include "goat3d.h"
nuclear@16 4 #include "goat3d_impl.h"
nuclear@16 5 #include "log.h"
nuclear@16 6
nuclear@16 7 struct goat3d {
nuclear@16 8 Scene *scn;
nuclear@16 9 unsigned int flags;
nuclear@16 10 };
nuclear@16 11
nuclear@16 12 struct goat3d_material : public Material {};
nuclear@16 13 struct goat3d_mesh : public Mesh {};
nuclear@16 14 struct goat3d_light : public Light {};
nuclear@16 15 struct goat3d_camera : public Camera {};
nuclear@16 16 struct goat3d_node : public Node {};
nuclear@16 17
nuclear@16 18
nuclear@16 19 static long read_file(void *buf, size_t bytes, void *uptr);
nuclear@16 20 static long write_file(const void *buf, size_t bytes, void *uptr);
nuclear@16 21 static long seek_file(long offs, int whence, void *uptr);
nuclear@16 22
nuclear@16 23 extern "C" {
nuclear@16 24
nuclear@16 25 struct goat3d *goat3d_create(void)
nuclear@16 26 {
nuclear@16 27 goat3d *goat = new goat3d;
nuclear@16 28 goat->scn = new Scene;
nuclear@16 29 return goat;
nuclear@16 30 }
nuclear@16 31
nuclear@16 32 void goat3d_free(struct goat3d *g)
nuclear@16 33 {
nuclear@16 34 delete g->scn;
nuclear@16 35 delete g;
nuclear@16 36 }
nuclear@16 37
nuclear@16 38 void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
nuclear@16 39 {
nuclear@16 40 if(val) {
nuclear@16 41 g->flags |= (1 << (int)opt);
nuclear@16 42 } else {
nuclear@16 43 g->flags &= ~(1 << (int)opt);
nuclear@16 44 }
nuclear@16 45 }
nuclear@16 46
nuclear@16 47 int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
nuclear@16 48 {
nuclear@16 49 return (g->flags >> (int)opt) & 1;
nuclear@16 50 }
nuclear@16 51
nuclear@16 52 int goat3d_load(struct goat3d *g, const char *fname)
nuclear@16 53 {
nuclear@16 54 FILE *fp = fopen(fname, "rb");
nuclear@16 55 if(!fp) {
nuclear@16 56 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
nuclear@16 57 return -1;
nuclear@16 58 }
nuclear@16 59
nuclear@16 60 int res = goat3d_load_file(g, fp);
nuclear@16 61 fclose(fp);
nuclear@16 62 return res;
nuclear@16 63 }
nuclear@16 64
nuclear@16 65 int goat3d_save(const struct goat3d *g, const char *fname)
nuclear@16 66 {
nuclear@16 67 FILE *fp = fopen(fname, "wb");
nuclear@16 68 if(!fp) {
nuclear@16 69 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
nuclear@16 70 return -1;
nuclear@16 71 }
nuclear@16 72
nuclear@16 73 int res = goat3d_save_file(g, fp);
nuclear@16 74 fclose(fp);
nuclear@16 75 return res;
nuclear@16 76 }
nuclear@16 77
nuclear@16 78 int goat3d_load_file(struct goat3d *g, FILE *fp)
nuclear@16 79 {
nuclear@16 80 goat3d_io io;
nuclear@16 81 io.cls = fp;
nuclear@16 82 io.read = read_file;
nuclear@16 83 io.write = write_file;
nuclear@16 84 io.seek = seek_file;
nuclear@16 85
nuclear@16 86 return goat3d_load_io(g, &io);
nuclear@16 87 }
nuclear@16 88
nuclear@16 89 int goat3d_save_file(const struct goat3d *g, FILE *fp)
nuclear@16 90 {
nuclear@16 91 goat3d_io io;
nuclear@16 92 io.cls = fp;
nuclear@16 93 io.read = read_file;
nuclear@16 94 io.write = write_file;
nuclear@16 95 io.seek = seek_file;
nuclear@16 96
nuclear@16 97 return goat3d_save_io(g, &io);
nuclear@16 98 }
nuclear@16 99
nuclear@16 100 int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
nuclear@16 101 {
nuclear@16 102 if(!g->scn->load(io)) {
nuclear@16 103 if(g->scn->loadxml(io)) {
nuclear@16 104 return -1;
nuclear@16 105 }
nuclear@16 106 }
nuclear@16 107 return 0;
nuclear@16 108 }
nuclear@16 109
nuclear@16 110 int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
nuclear@16 111 {
nuclear@16 112 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
nuclear@16 113 return g->scn->savexml(io) ? 0 : -1;
nuclear@16 114 }
nuclear@16 115 return g->scn->save(io) ? 0 : -1;
nuclear@16 116 }
nuclear@16 117
nuclear@16 118 int goat3d_set_name(struct goat3d *g, const char *name)
nuclear@16 119 {
nuclear@16 120 g->scn->set_name(name);
nuclear@16 121 return 0;
nuclear@16 122 }
nuclear@16 123
nuclear@16 124 const char *goat3d_get_name(const struct goat3d *g)
nuclear@16 125 {
nuclear@16 126 return g->scn->get_name();
nuclear@16 127 }
nuclear@16 128
nuclear@16 129 void goat3d_set_ambient(struct goat3d *g, const float *amb)
nuclear@16 130 {
nuclear@16 131 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
nuclear@16 132 }
nuclear@16 133
nuclear@16 134 void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
nuclear@16 135 {
nuclear@16 136 g->scn->set_ambient(Vector3(ar, ag, ab));
nuclear@16 137 }
nuclear@16 138
nuclear@16 139 const float *goat3d_get_ambient(const struct goat3d *g)
nuclear@16 140 {
nuclear@16 141 return &g->scn->get_ambient().x;
nuclear@16 142 }
nuclear@16 143
nuclear@16 144 // ---- materials ----
nuclear@16 145 struct goat3d_material *goat3d_create_mtl(void)
nuclear@16 146 {
nuclear@16 147 return new goat3d_material;
nuclear@16 148 }
nuclear@16 149
nuclear@16 150 void goat3d_destroy_mtl(struct goat3d_material *mtl)
nuclear@16 151 {
nuclear@16 152 delete mtl;
nuclear@16 153 }
nuclear@16 154
nuclear@16 155 void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
nuclear@16 156 {
nuclear@16 157 mtl->name = std::string(name);
nuclear@16 158 }
nuclear@16 159
nuclear@16 160 const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
nuclear@16 161 {
nuclear@16 162 return mtl->name.c_str();
nuclear@16 163 }
nuclear@16 164
nuclear@16 165 void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
nuclear@16 166 {
nuclear@16 167 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
nuclear@16 168 }
nuclear@16 169
nuclear@16 170 void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
nuclear@16 171 {
nuclear@16 172 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
nuclear@16 173 }
nuclear@16 174
nuclear@16 175 void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
nuclear@16 176 {
nuclear@16 177 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
nuclear@16 178 }
nuclear@16 179
nuclear@16 180 void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
nuclear@16 181 {
nuclear@16 182 (*mtl)[attrib].value = Vector4(r, g, b, a);
nuclear@16 183 }
nuclear@16 184
nuclear@16 185 const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
nuclear@16 186 {
nuclear@16 187 return &(*mtl)[attrib].value.x;
nuclear@16 188 }
nuclear@16 189
nuclear@16 190 void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
nuclear@16 191 {
nuclear@16 192 (*mtl)[attrib].map = std::string(mapname);
nuclear@16 193 }
nuclear@16 194
nuclear@16 195 const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
nuclear@16 196 {
nuclear@16 197 return (*mtl)[attrib].map.c_str();
nuclear@16 198 }
nuclear@16 199
nuclear@16 200 void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
nuclear@16 201 {
nuclear@16 202 g->scn->add_material(mtl);
nuclear@16 203 }
nuclear@16 204
nuclear@16 205 // ---- meshes ----
nuclear@16 206 struct goat3d_mesh *goat3d_create_mesh(void)
nuclear@16 207 {
nuclear@16 208 return new goat3d_mesh;
nuclear@16 209 }
nuclear@16 210
nuclear@16 211 void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
nuclear@16 212 {
nuclear@16 213 delete mesh;
nuclear@16 214 }
nuclear@16 215
nuclear@16 216 void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
nuclear@16 217 {
nuclear@16 218 mesh->name = std::string(name);
nuclear@16 219 }
nuclear@16 220
nuclear@16 221 const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
nuclear@16 222 {
nuclear@16 223 return mesh->name.c_str();
nuclear@16 224 }
nuclear@16 225
nuclear@16 226 void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
nuclear@16 227 {
nuclear@16 228 mesh->material = mtl;
nuclear@16 229 }
nuclear@16 230
nuclear@16 231 struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
nuclear@16 232 {
nuclear@16 233 return (goat3d_material*)mesh->material;
nuclear@16 234 }
nuclear@16 235
nuclear@16 236 int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
nuclear@16 237 {
nuclear@16 238 return (int)mesh->vertices.size();
nuclear@16 239 }
nuclear@16 240
nuclear@16 241 int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
nuclear@16 242 {
nuclear@16 243 return (int)mesh->faces.size();
nuclear@16 244 }
nuclear@16 245
nuclear@19 246 // VECDATA is in goat3d_impl.h
nuclear@16 247 void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
nuclear@16 248 {
nuclear@16 249 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
nuclear@16 250 mesh->vertices = VECDATA(Vector3, data, vnum);
nuclear@16 251 return;
nuclear@16 252 }
nuclear@16 253
nuclear@16 254 if(vnum != (int)mesh->vertices.size()) {
nuclear@16 255 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
nuclear@16 256 return;
nuclear@16 257 }
nuclear@16 258
nuclear@16 259 switch(attrib) {
nuclear@16 260 case GOAT3D_MESH_ATTR_NORMAL:
nuclear@16 261 mesh->normals = VECDATA(Vector3, data, vnum);
nuclear@16 262 break;
nuclear@16 263 case GOAT3D_MESH_ATTR_TANGENT:
nuclear@16 264 mesh->tangents = VECDATA(Vector3, data, vnum);
nuclear@16 265 break;
nuclear@16 266 case GOAT3D_MESH_ATTR_TEXCOORD:
nuclear@16 267 mesh->texcoords = VECDATA(Vector2, data, vnum);
nuclear@16 268 break;
nuclear@16 269 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
nuclear@16 270 mesh->skin_weights = VECDATA(Vector4, data, vnum);
nuclear@16 271 break;
nuclear@16 272 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
nuclear@16 273 mesh->skin_matrices = VECDATA(Int4, data, vnum);
nuclear@16 274 break;
nuclear@16 275 case GOAT3D_MESH_ATTR_COLOR:
nuclear@16 276 mesh->colors = VECDATA(Vector4, data, vnum);
nuclear@16 277 default:
nuclear@16 278 break;
nuclear@16 279 }
nuclear@16 280 }
nuclear@16 281
nuclear@16 282 void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
nuclear@16 283 {
nuclear@16 284 return goat3d_get_mesh_attrib(mesh, attrib, 0);
nuclear@16 285 }
nuclear@16 286
nuclear@16 287 void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
nuclear@16 288 {
nuclear@16 289 switch(attrib) {
nuclear@16 290 case GOAT3D_MESH_ATTR_VERTEX:
nuclear@16 291 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
nuclear@16 292 case GOAT3D_MESH_ATTR_NORMAL:
nuclear@16 293 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
nuclear@16 294 case GOAT3D_MESH_ATTR_TANGENT:
nuclear@16 295 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
nuclear@16 296 case GOAT3D_MESH_ATTR_TEXCOORD:
nuclear@16 297 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
nuclear@16 298 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
nuclear@16 299 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
nuclear@16 300 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
nuclear@16 301 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
nuclear@16 302 case GOAT3D_MESH_ATTR_COLOR:
nuclear@16 303 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
nuclear@16 304 default:
nuclear@16 305 break;
nuclear@16 306 }
nuclear@16 307 return 0;
nuclear@16 308 }
nuclear@16 309
nuclear@16 310
nuclear@16 311 void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
nuclear@16 312 {
nuclear@16 313 mesh->faces = VECDATA(Face, data, num);
nuclear@16 314 }
nuclear@16 315
nuclear@16 316 int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
nuclear@16 317 {
nuclear@16 318 return goat3d_get_mesh_face(mesh, 0);
nuclear@16 319 }
nuclear@16 320
nuclear@16 321 int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
nuclear@16 322 {
nuclear@16 323 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
nuclear@16 324 }
nuclear@16 325
nuclear@16 326 // immedate mode state
nuclear@16 327 static enum goat3d_im_primitive im_prim;
nuclear@16 328 static struct goat3d_mesh *im_mesh;
nuclear@16 329 static Vector3 im_norm, im_tang;
nuclear@16 330 static Vector2 im_texcoord;
nuclear@16 331 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
nuclear@16 332 static Int4 im_skinmat;
nuclear@16 333 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
nuclear@16 334
nuclear@16 335
nuclear@16 336 void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
nuclear@16 337 {
nuclear@16 338 mesh->vertices.clear();
nuclear@16 339 mesh->normals.clear();
nuclear@16 340 mesh->tangents.clear();
nuclear@16 341 mesh->texcoords.clear();
nuclear@16 342 mesh->skin_weights.clear();
nuclear@16 343 mesh->skin_matrices.clear();
nuclear@16 344 mesh->colors.clear();
nuclear@16 345 mesh->faces.clear();
nuclear@16 346
nuclear@16 347 im_mesh = mesh;
nuclear@16 348 memset(im_use, 0, sizeof im_use);
nuclear@16 349
nuclear@16 350 im_prim = prim;
nuclear@16 351 }
nuclear@16 352
nuclear@16 353 void goat3d_end(void)
nuclear@16 354 {
nuclear@16 355 switch(im_prim) {
nuclear@16 356 case GOAT3D_TRIANGLES:
nuclear@17 357 {
nuclear@17 358 int num_faces = (int)im_mesh->vertices.size() / 3;
nuclear@17 359 im_mesh->faces.resize(num_faces);
nuclear@17 360
nuclear@17 361 int vidx = 0;
nuclear@17 362 for(int i=0; i<num_faces; i++) {
nuclear@17 363 im_mesh->faces[i].v[0] = vidx++;
nuclear@17 364 im_mesh->faces[i].v[1] = vidx++;
nuclear@17 365 im_mesh->faces[i].v[2] = vidx++;
nuclear@17 366 }
nuclear@17 367 }
nuclear@16 368 break;
nuclear@16 369
nuclear@16 370 case GOAT3D_QUADS:
nuclear@17 371 {
nuclear@17 372 int num_quads = (int)im_mesh->vertices.size() / 4;
nuclear@17 373 im_mesh->faces.resize(num_quads * 2);
nuclear@17 374
nuclear@17 375 int vidx = 0;
nuclear@17 376 for(int i=0; i<num_quads; i++) {
nuclear@17 377 im_mesh->faces[i * 2].v[0] = vidx;
nuclear@17 378 im_mesh->faces[i * 2].v[1] = vidx + 1;
nuclear@17 379 im_mesh->faces[i * 2].v[2] = vidx + 2;
nuclear@17 380
nuclear@17 381 im_mesh->faces[i * 2 + 1].v[0] = vidx;
nuclear@17 382 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
nuclear@17 383 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
nuclear@17 384
nuclear@17 385 vidx += 4;
nuclear@17 386 }
nuclear@17 387 }
nuclear@16 388 break;
nuclear@16 389
nuclear@16 390 default:
nuclear@16 391 return;
nuclear@16 392 };
nuclear@16 393 }
nuclear@16 394
nuclear@16 395 void goat3d_vertex3f(float x, float y, float z)
nuclear@16 396 {
nuclear@16 397 im_mesh->vertices.push_back(Vector3(x, y, z));
nuclear@16 398 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
nuclear@16 399 im_mesh->normals.push_back(im_norm);
nuclear@16 400 }
nuclear@16 401 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
nuclear@16 402 im_mesh->tangents.push_back(im_tang);
nuclear@16 403 }
nuclear@16 404 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
nuclear@16 405 im_mesh->texcoords.push_back(im_texcoord);
nuclear@16 406 }
nuclear@16 407 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
nuclear@16 408 im_mesh->skin_weights.push_back(im_skinw);
nuclear@16 409 }
nuclear@16 410 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
nuclear@16 411 im_mesh->skin_matrices.push_back(im_skinmat);
nuclear@16 412 }
nuclear@16 413 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
nuclear@16 414 im_mesh->colors.push_back(im_color);
nuclear@16 415 }
nuclear@16 416 }
nuclear@16 417
nuclear@16 418 void goat3d_normal3f(float x, float y, float z)
nuclear@16 419 {
nuclear@16 420 im_norm = Vector3(x, y, z);
nuclear@17 421 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
nuclear@16 422 }
nuclear@16 423
nuclear@16 424 void goat3d_tangent3f(float x, float y, float z)
nuclear@16 425 {
nuclear@16 426 im_tang = Vector3(x, y, z);
nuclear@17 427 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
nuclear@16 428 }
nuclear@16 429
nuclear@16 430 void goat3d_texcoord2f(float x, float y)
nuclear@16 431 {
nuclear@16 432 im_texcoord = Vector2(x, y);
nuclear@17 433 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
nuclear@16 434 }
nuclear@16 435
nuclear@16 436 void goat3d_skin_weight4f(float x, float y, float z, float w)
nuclear@16 437 {
nuclear@16 438 im_skinw = Vector4(x, y, z, w);
nuclear@17 439 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
nuclear@16 440 }
nuclear@16 441
nuclear@16 442 void goat3d_skin_matrix4i(int x, int y, int z, int w)
nuclear@16 443 {
nuclear@16 444 im_skinmat.x = x;
nuclear@16 445 im_skinmat.y = y;
nuclear@16 446 im_skinmat.z = z;
nuclear@16 447 im_skinmat.w = w;
nuclear@17 448 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
nuclear@16 449 }
nuclear@16 450
nuclear@16 451 void goat3d_color3f(float x, float y, float z)
nuclear@16 452 {
nuclear@17 453 goat3d_color4f(x, y, z, 1.0f);
nuclear@16 454 }
nuclear@16 455
nuclear@16 456 void goat3d_color4f(float x, float y, float z, float w)
nuclear@16 457 {
nuclear@16 458 im_color = Vector4(x, y, z, w);
nuclear@17 459 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
nuclear@16 460 }
nuclear@16 461
nuclear@16 462 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
nuclear@16 463 {
nuclear@16 464 g->scn->add_mesh(mesh);
nuclear@16 465 }
nuclear@16 466
nuclear@19 467 int goat3d_get_mesh_count(struct goat3d *g)
nuclear@19 468 {
nuclear@19 469 return g->scn->get_mesh_count();
nuclear@19 470 }
nuclear@19 471
nuclear@19 472 struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
nuclear@19 473 {
nuclear@19 474 return (goat3d_mesh*)g->scn->get_mesh(idx);
nuclear@19 475 }
nuclear@19 476
nuclear@16 477
nuclear@16 478 } // extern "C"
nuclear@16 479
nuclear@16 480
nuclear@16 481 static long read_file(void *buf, size_t bytes, void *uptr)
nuclear@16 482 {
nuclear@16 483 return (long)fread(buf, 1, bytes, (FILE*)uptr);
nuclear@16 484 }
nuclear@16 485
nuclear@16 486 static long write_file(const void *buf, size_t bytes, void *uptr)
nuclear@16 487 {
nuclear@16 488 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
nuclear@16 489 }
nuclear@16 490
nuclear@16 491 static long seek_file(long offs, int whence, void *uptr)
nuclear@16 492 {
nuclear@16 493 if(fseek((FILE*)uptr, offs, whence) == -1) {
nuclear@16 494 return -1;
nuclear@16 495 }
nuclear@16 496 return ftell((FILE*)uptr);
nuclear@16 497 }