dungeon_crawler

view prototype/src/texman.cc @ 46:f3030df27110

debugging the particles
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 13 Sep 2012 06:33:51 +0300
parents 3fef65352b0b
children
line source
1 #include <string.h>
2 #include "opengl.h"
3 #include "imago2.h"
4 #include "texman.h"
5 #include "datapath.h"
7 unsigned int load_texture(const char *fname);
9 TextureSet::~TextureSet()
10 {
11 for(auto iter : textures) {
12 glDeleteTextures(1, &iter.second);
13 }
14 }
16 unsigned int TextureSet::get_texture(const char *fname) const
17 {
18 auto iter = textures.find(fname);
19 if(iter != textures.end()) {
20 return iter->second;
21 }
23 const char *path, *slash;
24 if((slash = strrchr(fname, '/'))) {
25 path = slash + 1;
26 } else {
27 path = fname;
28 }
29 if(!(path = datafile_path(path))) {
30 fprintf(stderr, "can't find texture: %s\n", fname);
31 return 0;
32 }
34 printf("loading texture: %s\n", path);
35 unsigned int tex = load_texture(path);
36 if(tex) {
37 textures[fname] = tex;
38 } else {
39 fprintf(stderr, "failed to load texture: %s\n", path);
40 }
41 return tex;
42 }
44 unsigned int load_texture(const char *fname)
45 {
46 struct img_pixmap img;
48 img_init(&img);
49 if(img_load(&img, fname) == -1) {
50 img_destroy(&img);
51 return 0;
52 }
54 unsigned int intfmt = img_glintfmt(&img);
55 unsigned int fmt = img_glfmt(&img);
56 unsigned int type = img_gltype(&img);
58 unsigned int tex;
59 glGenTextures(1, &tex);
60 glBindTexture(GL_TEXTURE_2D, tex);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
66 if(GLEW_SGIS_generate_mipmap) {
67 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
68 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, img.width, img.height, 0, fmt, type, img.pixels);
69 } else {
70 gluBuild2DMipmaps(GL_TEXTURE_2D, intfmt, img.width, img.height, fmt, type, img.pixels);
71 }
73 img_destroy(&img);
74 return tex;
75 }