graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 1bf4df5febd6735dff817a2e6daa02b2987d9f3b
parent 1c57f2c3264bf319e76c0a9a6097694c8949bf20
Author: Brian Swetland <swetland@frotz.net>
Date:   Sun, 16 Jun 2013 14:57:59 -0700

factor out asset loading, provide fopen_asset(), implement basic asset paths

Diffstat:
Mcommon/io.cc | 46+++++++++++++++++++++++++++++++++++-----------
Mcommon/loadfile.cc | 11++---------
Mcommon/loadobj.cc | 8+-------
Mcommon/loadpng.cc | 10++--------
Mcommon/util.h | 1+
5 files changed, 41 insertions(+), 35 deletions(-)

diff --git a/common/io.cc b/common/io.cc @@ -21,9 +21,35 @@ #include "util.h" -extern const char *load_file_base_path; +static char base_path[1024 + 8] = ""; -static char base_path[1024 + 8]; +static const char *search[] = { +#ifdef _WIN32 + "assets\\", +#else + "assets/", + "../common/assets/", +#endif + NULL, +}; + +FILE *fopen_asset(const char *fn, const char *kind) { + char path[2048 + 64]; + FILE *fp; + int i; + if (strlen(fn) > 1024) + return NULL; + for (i = 0; search[i]; i++) { + sprintf(path, "%s%s%s", base_path, search[i], fn); + fp = fopen(path, "rb"); + if (fp != NULL) { + printx("Loading %s from '%s'...\n", kind, path); + return fp; + } + } + printx("Cannot find %s '%s'.\n", kind, fn); + return NULL; +} #ifdef _WIN32 #include <windows.h> @@ -32,11 +58,10 @@ void init_io(void) { GetModuleFileName(NULL, base_path, 1024); base_path[1023] = 0; x = strrchr(base_path, '\\'); - if (x) { + if (x) x[1] = 0; - strcat(base_path,"assets\\"); - load_file_base_path = base_path; - } + else + strcpy(base_path,".\\"); } #else #include <unistd.h> @@ -47,18 +72,17 @@ void init_io(void) { if (r < 0) return; x = strrchr(base_path, '/'); - if (x) { + if (x) x[1] = 0; - strcat(base_path,"assets/"); - load_file_base_path = base_path; - } + else + strcpy(base_path,"./"); } #endif int file_get_mtime(const char *fn) { struct stat s; char buf[1024]; - snprintf(buf, 1024, "%s%s", load_file_base_path, fn); + snprintf(buf, 1024, "%s%s", base_path, fn); if (stat(buf, &s)) return -1; return s.st_mtime; diff --git a/common/loadfile.cc b/common/loadfile.cc @@ -19,19 +19,12 @@ #include "util.h" -const char *load_file_base_path = ""; - void *load_file(const char *fn, unsigned *_sz) { - char tmp[1024]; void *data = 0; long sz; FILE *fp; - snprintf(tmp, 1024, "%s%s", load_file_base_path, fn); - tmp[1023] = 0; - - printx("Loading '%s'...\n", tmp); - if(!(fp = fopen(tmp, "rb"))) + if (!(fp = fopen_asset(fn, "file"))) goto exit; if (fseek(fp, 0, SEEK_END)) @@ -59,6 +52,6 @@ close_and_exit: fclose(fp); exit: if (!data) - error("Failed to load '%s'", tmp); + error("Failed to load '%s'", fn); return data; } diff --git a/common/loadobj.cc b/common/loadobj.cc @@ -46,18 +46,12 @@ struct obj { vector<i3> triangles; }; -extern const char *load_file_base_path; - struct obj *load_obj(const char *fn) { - char tmp[1024]; char buf[128]; FILE *fp; struct obj *o = 0; - snprintf(tmp, 1024, "%s%s", load_file_base_path, fn); - tmp[1023] = 0; - printx("Loading Model '%s'...\n", tmp); - if (!(fp = fopen(tmp, "r"))) + if (!(fp = fopen_asset(fn, "model"))) goto exit; o = new(obj); diff --git a/common/loadpng.cc b/common/loadpng.cc @@ -21,21 +21,15 @@ #include "util.h" -extern const char *load_file_base_path; - void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int ch, int inverty) { png_structp png; png_infop info; png_uint_32 w, h; int depth, ctype, itype, i; png_byte *data = 0; - char tmp[1024]; FILE *fp; - snprintf(tmp, 1024, "%s%s", load_file_base_path, fn); - tmp[1023] = 0; - - if ((fp = fopen(tmp, "rb")) == NULL) + if ((fp = fopen_asset(fn, "image")) == NULL) goto exit; if (!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) @@ -113,7 +107,7 @@ close_and_exit: fclose(fp); exit: if (!data) - error("Failed to load '%s'", tmp); + error("Failed to load '%s'", fn); return data; } diff --git a/common/util.h b/common/util.h @@ -51,5 +51,6 @@ void printmtx(float *m, const char *name); int error(const char *fmt, ...); void die(const char *fmt, ...); +FILE *fopen_asset(const char *fn, const char *kind); #endif