istereo

changeset 4:14bbdfcb9030

resource path find code
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 06:25:05 +0300
parents 2c5620f0670c
children 76ad575d72d0
files src/istereo.c src/respath.c src/respath.h src/sdr.c
diffstat 4 files changed, 118 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/src/istereo.c	Wed Sep 07 05:33:19 2011 +0300
     1.2 +++ b/src/istereo.c	Wed Sep 07 06:25:05 2011 +0300
     1.3 @@ -5,16 +5,17 @@
     1.4  #include "istereo.h"
     1.5  #include "sanegl.h"
     1.6  #include "sdr.h"
     1.7 -#include "objcsucks.h"
     1.8 +#include "respath.h"
     1.9 +
    1.10 +static unsigned int get_shader_program(const char *vfile, const char *pfile);
    1.11  
    1.12  unsigned int prog;
    1.13  
    1.14  int init(void)
    1.15  {
    1.16 -	char *path = ocs_get_path(OCS_PATH_RESOURCES);
    1.17 -	chdir(path);
    1.18 +	add_resource_path("sdr");
    1.19  
    1.20 -	if(!(prog = create_program_load("test.v.glsl", "test.p.glsl"))) {
    1.21 +	if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
    1.22  		fprintf(stderr, "failed to load shader program\n");
    1.23  		return -1;
    1.24  	}
    1.25 @@ -59,3 +60,20 @@
    1.26  	gl_load_identity();
    1.27  	glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
    1.28  }
    1.29 +
    1.30 +static unsigned int get_shader_program(const char *vfile, const char *pfile)
    1.31 +{
    1.32 +	unsigned int prog, vs, ps;
    1.33 +
    1.34 +	if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
    1.35 +		return -1;
    1.36 +	}
    1.37 +	if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
    1.38 +		return -1;
    1.39 +	}
    1.40 +
    1.41 +	if(!(prog = create_program_link(vs, ps))) {
    1.42 +		return -1;
    1.43 +	}
    1.44 +	return prog;
    1.45 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/respath.c	Wed Sep 07 06:25:05 2011 +0300
     2.3 @@ -0,0 +1,88 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <string.h>
     2.7 +#include <errno.h>
     2.8 +#include <unistd.h>
     2.9 +#include "respath.h"
    2.10 +
    2.11 +#if defined(__IPHONE_3_0) || defined(__IPHONE_3_2) || defined(__IPHONE_4_0)
    2.12 +#define IPHONE
    2.13 +#include <CoreFoundation/CoreFoundation.h>
    2.14 +#endif
    2.15 +
    2.16 +
    2.17 +#ifndef IPHONE
    2.18 +struct path_node {
    2.19 +	char *path;
    2.20 +	struct path_node *next;
    2.21 +};
    2.22 +
    2.23 +static struct path_node *pathlist;
    2.24 +
    2.25 +void add_resource_path(const char *path)
    2.26 +{
    2.27 +	struct path_node *node = 0;
    2.28 +
    2.29 +	if(!(node = malloc(sizeof *node)) || !(node->path = malloc(strlen(path) + 1))) {
    2.30 +		free(node);
    2.31 +		fprintf(stderr, "failed to add path: %s: %s\n", path, strerror(errno));
    2.32 +		return;
    2.33 +	}
    2.34 +	strcpy(node->path, path);
    2.35 +	node->next = pathlist;
    2.36 +	pathlist = node;
    2.37 +}
    2.38 +
    2.39 +char *find_resource(const char *fname, char *path, size_t sz)
    2.40 +{
    2.41 +	static char buffer[1024];
    2.42 +	struct path_node *node;
    2.43 +
    2.44 +	if(!path) {
    2.45 +		path = buffer;
    2.46 +		sz = sizeof buffer;
    2.47 +	}
    2.48 +
    2.49 +	node = pathlist;
    2.50 +	while(node) {
    2.51 +		snprintf(path, sz, "%s/%s", node->path, fname);
    2.52 +		if(access(path, F_OK) != -1) {
    2.53 +			return path;
    2.54 +		}
    2.55 +		node = node->next;
    2.56 +	}
    2.57 +	return 0;
    2.58 +}
    2.59 +
    2.60 +#else	/* IPHONE */
    2.61 +
    2.62 +void add_resource_path(const char *path)
    2.63 +{
    2.64 +}
    2.65 +
    2.66 +
    2.67 +char *find_resource(const char *fname, char *path, size_t sz)
    2.68 +{
    2.69 +	static char buffer[1024];
    2.70 +	CFBundleRef bundle;
    2.71 +	CFURLRef url;
    2.72 +	CFStringRef cfname;
    2.73 +
    2.74 +	cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
    2.75 +
    2.76 +	bundle = CFBundleGetMainBundle();
    2.77 +	if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
    2.78 +		return 0;
    2.79 +	}
    2.80 +
    2.81 +	if(!path) {
    2.82 +		path = buffer;
    2.83 +		sz = sizeof buffer;
    2.84 +	}
    2.85 +
    2.86 +	if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) {
    2.87 +		return 0;
    2.88 +	}
    2.89 +	return path;
    2.90 +}
    2.91 +#endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/respath.h	Wed Sep 07 06:25:05 2011 +0300
     3.3 @@ -0,0 +1,7 @@
     3.4 +#ifndef RESPATH_H_
     3.5 +#define RESPATH_H_
     3.6 +
     3.7 +void add_resource_path(const char *path);
     3.8 +char *find_resource(const char *fname, char *path, size_t sz);
     3.9 +
    3.10 +#endif	/* RESPATH_H_ */
     4.1 --- a/src/sdr.c	Wed Sep 07 05:33:19 2011 +0300
     4.2 +++ b/src/sdr.c	Wed Sep 07 06:25:05 2011 +0300
     4.3 @@ -131,7 +131,7 @@
     4.4  {
     4.5  	unsigned int sdr;
     4.6  
     4.7 -	if(!(sdr = load_shader(fname, sdr_type))) {
     4.8 +	if(!fname || !(sdr = load_shader(fname, sdr_type))) {
     4.9  		return 0;
    4.10  	}
    4.11  	return sdr;