# HG changeset patch # User John Tsiombikas # Date 1315365905 -10800 # Node ID 14bbdfcb9030e9ec78add7f7559f613dedb99633 # Parent 2c5620f0670c975437e6de8d8e5c57165a9543b9 resource path find code diff -r 2c5620f0670c -r 14bbdfcb9030 src/istereo.c --- a/src/istereo.c Wed Sep 07 05:33:19 2011 +0300 +++ b/src/istereo.c Wed Sep 07 06:25:05 2011 +0300 @@ -5,16 +5,17 @@ #include "istereo.h" #include "sanegl.h" #include "sdr.h" -#include "objcsucks.h" +#include "respath.h" + +static unsigned int get_shader_program(const char *vfile, const char *pfile); unsigned int prog; int init(void) { - char *path = ocs_get_path(OCS_PATH_RESOURCES); - chdir(path); + add_resource_path("sdr"); - if(!(prog = create_program_load("test.v.glsl", "test.p.glsl"))) { + if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) { fprintf(stderr, "failed to load shader program\n"); return -1; } @@ -59,3 +60,20 @@ gl_load_identity(); glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0); } + +static unsigned int get_shader_program(const char *vfile, const char *pfile) +{ + unsigned int prog, vs, ps; + + if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) { + return -1; + } + if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) { + return -1; + } + + if(!(prog = create_program_link(vs, ps))) { + return -1; + } + return prog; +} diff -r 2c5620f0670c -r 14bbdfcb9030 src/respath.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/respath.c Wed Sep 07 06:25:05 2011 +0300 @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include "respath.h" + +#if defined(__IPHONE_3_0) || defined(__IPHONE_3_2) || defined(__IPHONE_4_0) +#define IPHONE +#include +#endif + + +#ifndef IPHONE +struct path_node { + char *path; + struct path_node *next; +}; + +static struct path_node *pathlist; + +void add_resource_path(const char *path) +{ + struct path_node *node = 0; + + if(!(node = malloc(sizeof *node)) || !(node->path = malloc(strlen(path) + 1))) { + free(node); + fprintf(stderr, "failed to add path: %s: %s\n", path, strerror(errno)); + return; + } + strcpy(node->path, path); + node->next = pathlist; + pathlist = node; +} + +char *find_resource(const char *fname, char *path, size_t sz) +{ + static char buffer[1024]; + struct path_node *node; + + if(!path) { + path = buffer; + sz = sizeof buffer; + } + + node = pathlist; + while(node) { + snprintf(path, sz, "%s/%s", node->path, fname); + if(access(path, F_OK) != -1) { + return path; + } + node = node->next; + } + return 0; +} + +#else /* IPHONE */ + +void add_resource_path(const char *path) +{ +} + + +char *find_resource(const char *fname, char *path, size_t sz) +{ + static char buffer[1024]; + CFBundleRef bundle; + CFURLRef url; + CFStringRef cfname; + + cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII); + + bundle = CFBundleGetMainBundle(); + if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) { + return 0; + } + + if(!path) { + path = buffer; + sz = sizeof buffer; + } + + if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) { + return 0; + } + return path; +} +#endif diff -r 2c5620f0670c -r 14bbdfcb9030 src/respath.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/respath.h Wed Sep 07 06:25:05 2011 +0300 @@ -0,0 +1,7 @@ +#ifndef RESPATH_H_ +#define RESPATH_H_ + +void add_resource_path(const char *path); +char *find_resource(const char *fname, char *path, size_t sz); + +#endif /* RESPATH_H_ */ diff -r 2c5620f0670c -r 14bbdfcb9030 src/sdr.c --- a/src/sdr.c Wed Sep 07 05:33:19 2011 +0300 +++ b/src/sdr.c Wed Sep 07 06:25:05 2011 +0300 @@ -131,7 +131,7 @@ { unsigned int sdr; - if(!(sdr = load_shader(fname, sdr_type))) { + if(!fname || !(sdr = load_shader(fname, sdr_type))) { return 0; } return sdr;