vrheights

diff src/texman.cc @ 8:3f221bdc9bab

mesh loading walk polys
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 03 Oct 2014 04:16:16 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/texman.cc	Fri Oct 03 04:16:16 2014 +0300
     1.3 @@ -0,0 +1,51 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string>
     1.6 +#include <map>
     1.7 +#include <imago2.h>
     1.8 +#include "opengl.h"
     1.9 +#include "texman.h"
    1.10 +
    1.11 +static std::string find_datafile(const std::string &fname);
    1.12 +
    1.13 +static std::map<std::string, unsigned int> textures;
    1.14 +static const char *paths[] = {
    1.15 +	"data",
    1.16 +	0
    1.17 +};
    1.18 +
    1.19 +unsigned int get_texture(const char *fname)
    1.20 +{
    1.21 +	unsigned int tex;
    1.22 +
    1.23 +	if(!fname || !*fname) {
    1.24 +		return 0;
    1.25 +	}
    1.26 +
    1.27 +	std::map<std::string, unsigned int>::const_iterator it = textures.find(fname);
    1.28 +	if(it != textures.end()) {
    1.29 +		tex = it->second;
    1.30 +	} else {
    1.31 +		if(!(tex = img_gltexture_load(find_datafile(fname).c_str()))) {
    1.32 +			fprintf(stderr, "failed to load image: %s\n", fname);
    1.33 +		} else {
    1.34 +			textures[fname] = tex;
    1.35 +		}
    1.36 +	}
    1.37 +
    1.38 +	return tex;
    1.39 +}
    1.40 +
    1.41 +static std::string find_datafile(const std::string &fname)
    1.42 +{
    1.43 +	FILE *fp;
    1.44 +
    1.45 +	for(int i=0; paths[i]; i++) {
    1.46 +		std::string path = paths[i] + std::string("/") + fname;
    1.47 +		if((fp = fopen(path.c_str(), "rb"))) {
    1.48 +			fclose(fp);
    1.49 +			return path;
    1.50 +		}
    1.51 +	}
    1.52 +
    1.53 +	return fname;
    1.54 +}