vrheights
view 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 source
1 #include <stdio.h>
2 #include <string>
3 #include <map>
4 #include <imago2.h>
5 #include "opengl.h"
6 #include "texman.h"
8 static std::string find_datafile(const std::string &fname);
10 static std::map<std::string, unsigned int> textures;
11 static const char *paths[] = {
12 "data",
13 0
14 };
16 unsigned int get_texture(const char *fname)
17 {
18 unsigned int tex;
20 if(!fname || !*fname) {
21 return 0;
22 }
24 std::map<std::string, unsigned int>::const_iterator it = textures.find(fname);
25 if(it != textures.end()) {
26 tex = it->second;
27 } else {
28 if(!(tex = img_gltexture_load(find_datafile(fname).c_str()))) {
29 fprintf(stderr, "failed to load image: %s\n", fname);
30 } else {
31 textures[fname] = tex;
32 }
33 }
35 return tex;
36 }
38 static std::string find_datafile(const std::string &fname)
39 {
40 FILE *fp;
42 for(int i=0; paths[i]; i++) {
43 std::string path = paths[i] + std::string("/") + fname;
44 if((fp = fopen(path.c_str(), "rb"))) {
45 fclose(fp);
46 return path;
47 }
48 }
50 return fname;
51 }