vrheights

annotate src/texman.cc @ 16:7f6d68d95c22

updated to new version of goatvr
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 30 Oct 2015 06:34:31 +0200
parents
children
rev   line source
nuclear@8 1 #include <stdio.h>
nuclear@8 2 #include <string>
nuclear@8 3 #include <map>
nuclear@8 4 #include <imago2.h>
nuclear@8 5 #include "opengl.h"
nuclear@8 6 #include "texman.h"
nuclear@8 7
nuclear@8 8 static std::string find_datafile(const std::string &fname);
nuclear@8 9
nuclear@8 10 static std::map<std::string, unsigned int> textures;
nuclear@8 11 static const char *paths[] = {
nuclear@8 12 "data",
nuclear@8 13 0
nuclear@8 14 };
nuclear@8 15
nuclear@8 16 unsigned int get_texture(const char *fname)
nuclear@8 17 {
nuclear@8 18 unsigned int tex;
nuclear@8 19
nuclear@8 20 if(!fname || !*fname) {
nuclear@8 21 return 0;
nuclear@8 22 }
nuclear@8 23
nuclear@8 24 std::map<std::string, unsigned int>::const_iterator it = textures.find(fname);
nuclear@8 25 if(it != textures.end()) {
nuclear@8 26 tex = it->second;
nuclear@8 27 } else {
nuclear@8 28 if(!(tex = img_gltexture_load(find_datafile(fname).c_str()))) {
nuclear@8 29 fprintf(stderr, "failed to load image: %s\n", fname);
nuclear@8 30 } else {
nuclear@8 31 textures[fname] = tex;
nuclear@8 32 }
nuclear@8 33 }
nuclear@8 34
nuclear@8 35 return tex;
nuclear@8 36 }
nuclear@8 37
nuclear@8 38 static std::string find_datafile(const std::string &fname)
nuclear@8 39 {
nuclear@8 40 FILE *fp;
nuclear@8 41
nuclear@8 42 for(int i=0; paths[i]; i++) {
nuclear@8 43 std::string path = paths[i] + std::string("/") + fname;
nuclear@8 44 if((fp = fopen(path.c_str(), "rb"))) {
nuclear@8 45 fclose(fp);
nuclear@8 46 return path;
nuclear@8 47 }
nuclear@8 48 }
nuclear@8 49
nuclear@8 50 return fname;
nuclear@8 51 }