goat3d

annotate src/goat3d.cc @ 17:1d85d7dd0038

goatprim done
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Sep 2013 02:29:52 +0300
parents cb6c1a945a11
children b35427826b60
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@16 246 #if __cplusplus >= 201103L
nuclear@16 247 #define MOVE(x) std::move(x)
nuclear@16 248 #else
nuclear@16 249 #define MOVE(x) x
nuclear@16 250 #endif
nuclear@16 251
nuclear@16 252 #define VECDATA(type, data, num) \
nuclear@16 253 MOVE(std::vector<type>((type*)(data), (type*)(data) + (num)))
nuclear@16 254
nuclear@16 255 void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
nuclear@16 256 {
nuclear@16 257 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
nuclear@16 258 mesh->vertices = VECDATA(Vector3, data, vnum);
nuclear@16 259 return;
nuclear@16 260 }
nuclear@16 261
nuclear@16 262 if(vnum != (int)mesh->vertices.size()) {
nuclear@16 263 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
nuclear@16 264 return;
nuclear@16 265 }
nuclear@16 266
nuclear@16 267 switch(attrib) {
nuclear@16 268 case GOAT3D_MESH_ATTR_NORMAL:
nuclear@16 269 mesh->normals = VECDATA(Vector3, data, vnum);
nuclear@16 270 break;
nuclear@16 271 case GOAT3D_MESH_ATTR_TANGENT:
nuclear@16 272 mesh->tangents = VECDATA(Vector3, data, vnum);
nuclear@16 273 break;
nuclear@16 274 case GOAT3D_MESH_ATTR_TEXCOORD:
nuclear@16 275 mesh->texcoords = VECDATA(Vector2, data, vnum);
nuclear@16 276 break;
nuclear@16 277 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
nuclear@16 278 mesh->skin_weights = VECDATA(Vector4, data, vnum);
nuclear@16 279 break;
nuclear@16 280 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
nuclear@16 281 mesh->skin_matrices = VECDATA(Int4, data, vnum);
nuclear@16 282 break;
nuclear@16 283 case GOAT3D_MESH_ATTR_COLOR:
nuclear@16 284 mesh->colors = VECDATA(Vector4, data, vnum);
nuclear@16 285 default:
nuclear@16 286 break;
nuclear@16 287 }
nuclear@16 288 }
nuclear@16 289
nuclear@16 290 void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
nuclear@16 291 {
nuclear@16 292 return goat3d_get_mesh_attrib(mesh, attrib, 0);
nuclear@16 293 }
nuclear@16 294
nuclear@16 295 void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
nuclear@16 296 {
nuclear@16 297 switch(attrib) {
nuclear@16 298 case GOAT3D_MESH_ATTR_VERTEX:
nuclear@16 299 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
nuclear@16 300 case GOAT3D_MESH_ATTR_NORMAL:
nuclear@16 301 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
nuclear@16 302 case GOAT3D_MESH_ATTR_TANGENT:
nuclear@16 303 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
nuclear@16 304 case GOAT3D_MESH_ATTR_TEXCOORD:
nuclear@16 305 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
nuclear@16 306 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
nuclear@16 307 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
nuclear@16 308 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
nuclear@16 309 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
nuclear@16 310 case GOAT3D_MESH_ATTR_COLOR:
nuclear@16 311 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
nuclear@16 312 default:
nuclear@16 313 break;
nuclear@16 314 }
nuclear@16 315 return 0;
nuclear@16 316 }
nuclear@16 317
nuclear@16 318
nuclear@16 319 void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
nuclear@16 320 {
nuclear@16 321 mesh->faces = VECDATA(Face, data, num);
nuclear@16 322 }
nuclear@16 323
nuclear@16 324 int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
nuclear@16 325 {
nuclear@16 326 return goat3d_get_mesh_face(mesh, 0);
nuclear@16 327 }
nuclear@16 328
nuclear@16 329 int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
nuclear@16 330 {
nuclear@16 331 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
nuclear@16 332 }
nuclear@16 333
nuclear@16 334 // immedate mode state
nuclear@16 335 static enum goat3d_im_primitive im_prim;
nuclear@16 336 static struct goat3d_mesh *im_mesh;
nuclear@16 337 static Vector3 im_norm, im_tang;
nuclear@16 338 static Vector2 im_texcoord;
nuclear@16 339 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
nuclear@16 340 static Int4 im_skinmat;
nuclear@16 341 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
nuclear@16 342
nuclear@16 343
nuclear@16 344 void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
nuclear@16 345 {
nuclear@16 346 mesh->vertices.clear();
nuclear@16 347 mesh->normals.clear();
nuclear@16 348 mesh->tangents.clear();
nuclear@16 349 mesh->texcoords.clear();
nuclear@16 350 mesh->skin_weights.clear();
nuclear@16 351 mesh->skin_matrices.clear();
nuclear@16 352 mesh->colors.clear();
nuclear@16 353 mesh->faces.clear();
nuclear@16 354
nuclear@16 355 im_mesh = mesh;
nuclear@16 356 memset(im_use, 0, sizeof im_use);
nuclear@16 357
nuclear@16 358 im_prim = prim;
nuclear@16 359 }
nuclear@16 360
nuclear@16 361 void goat3d_end(void)
nuclear@16 362 {
nuclear@16 363 switch(im_prim) {
nuclear@16 364 case GOAT3D_TRIANGLES:
nuclear@17 365 {
nuclear@17 366 int num_faces = (int)im_mesh->vertices.size() / 3;
nuclear@17 367 im_mesh->faces.resize(num_faces);
nuclear@17 368
nuclear@17 369 int vidx = 0;
nuclear@17 370 for(int i=0; i<num_faces; i++) {
nuclear@17 371 im_mesh->faces[i].v[0] = vidx++;
nuclear@17 372 im_mesh->faces[i].v[1] = vidx++;
nuclear@17 373 im_mesh->faces[i].v[2] = vidx++;
nuclear@17 374 }
nuclear@17 375 }
nuclear@16 376 break;
nuclear@16 377
nuclear@16 378 case GOAT3D_QUADS:
nuclear@17 379 {
nuclear@17 380 int num_quads = (int)im_mesh->vertices.size() / 4;
nuclear@17 381 im_mesh->faces.resize(num_quads * 2);
nuclear@17 382
nuclear@17 383 int vidx = 0;
nuclear@17 384 for(int i=0; i<num_quads; i++) {
nuclear@17 385 im_mesh->faces[i * 2].v[0] = vidx;
nuclear@17 386 im_mesh->faces[i * 2].v[1] = vidx + 1;
nuclear@17 387 im_mesh->faces[i * 2].v[2] = vidx + 2;
nuclear@17 388
nuclear@17 389 im_mesh->faces[i * 2 + 1].v[0] = vidx;
nuclear@17 390 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
nuclear@17 391 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
nuclear@17 392
nuclear@17 393 vidx += 4;
nuclear@17 394 }
nuclear@17 395 }
nuclear@16 396 break;
nuclear@16 397
nuclear@16 398 default:
nuclear@16 399 return;
nuclear@16 400 };
nuclear@16 401 }
nuclear@16 402
nuclear@16 403 void goat3d_vertex3f(float x, float y, float z)
nuclear@16 404 {
nuclear@16 405 im_mesh->vertices.push_back(Vector3(x, y, z));
nuclear@16 406 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
nuclear@16 407 im_mesh->normals.push_back(im_norm);
nuclear@16 408 }
nuclear@16 409 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
nuclear@16 410 im_mesh->tangents.push_back(im_tang);
nuclear@16 411 }
nuclear@16 412 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
nuclear@16 413 im_mesh->texcoords.push_back(im_texcoord);
nuclear@16 414 }
nuclear@16 415 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
nuclear@16 416 im_mesh->skin_weights.push_back(im_skinw);
nuclear@16 417 }
nuclear@16 418 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
nuclear@16 419 im_mesh->skin_matrices.push_back(im_skinmat);
nuclear@16 420 }
nuclear@16 421 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
nuclear@16 422 im_mesh->colors.push_back(im_color);
nuclear@16 423 }
nuclear@16 424 }
nuclear@16 425
nuclear@16 426 void goat3d_normal3f(float x, float y, float z)
nuclear@16 427 {
nuclear@16 428 im_norm = Vector3(x, y, z);
nuclear@17 429 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
nuclear@16 430 }
nuclear@16 431
nuclear@16 432 void goat3d_tangent3f(float x, float y, float z)
nuclear@16 433 {
nuclear@16 434 im_tang = Vector3(x, y, z);
nuclear@17 435 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
nuclear@16 436 }
nuclear@16 437
nuclear@16 438 void goat3d_texcoord2f(float x, float y)
nuclear@16 439 {
nuclear@16 440 im_texcoord = Vector2(x, y);
nuclear@17 441 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
nuclear@16 442 }
nuclear@16 443
nuclear@16 444 void goat3d_skin_weight4f(float x, float y, float z, float w)
nuclear@16 445 {
nuclear@16 446 im_skinw = Vector4(x, y, z, w);
nuclear@17 447 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
nuclear@16 448 }
nuclear@16 449
nuclear@16 450 void goat3d_skin_matrix4i(int x, int y, int z, int w)
nuclear@16 451 {
nuclear@16 452 im_skinmat.x = x;
nuclear@16 453 im_skinmat.y = y;
nuclear@16 454 im_skinmat.z = z;
nuclear@16 455 im_skinmat.w = w;
nuclear@17 456 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
nuclear@16 457 }
nuclear@16 458
nuclear@16 459 void goat3d_color3f(float x, float y, float z)
nuclear@16 460 {
nuclear@17 461 goat3d_color4f(x, y, z, 1.0f);
nuclear@16 462 }
nuclear@16 463
nuclear@16 464 void goat3d_color4f(float x, float y, float z, float w)
nuclear@16 465 {
nuclear@16 466 im_color = Vector4(x, y, z, w);
nuclear@17 467 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
nuclear@16 468 }
nuclear@16 469
nuclear@16 470 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
nuclear@16 471 {
nuclear@16 472 g->scn->add_mesh(mesh);
nuclear@16 473 }
nuclear@16 474
nuclear@16 475
nuclear@16 476 } // extern "C"
nuclear@16 477
nuclear@16 478
nuclear@16 479 static long read_file(void *buf, size_t bytes, void *uptr)
nuclear@16 480 {
nuclear@16 481 return (long)fread(buf, 1, bytes, (FILE*)uptr);
nuclear@16 482 }
nuclear@16 483
nuclear@16 484 static long write_file(const void *buf, size_t bytes, void *uptr)
nuclear@16 485 {
nuclear@16 486 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
nuclear@16 487 }
nuclear@16 488
nuclear@16 489 static long seek_file(long offs, int whence, void *uptr)
nuclear@16 490 {
nuclear@16 491 if(fseek((FILE*)uptr, offs, whence) == -1) {
nuclear@16 492 return -1;
nuclear@16 493 }
nuclear@16 494 return ftell((FILE*)uptr);
nuclear@16 495 }