]> git.jsancho.org Git - lugaru.git/blob - Source/Texture.cpp
glDeleteTextures safely ignores zeroes
[lugaru.git] / Source / Texture.cpp
1 #include "gamegl.h"
2 #include "Texture.h"
3 #include "TGALoader.h"
4
5 using namespace std;
6
7 map<string,Texture> Texture::textures;
8
9 extern TGAImageRec texture;
10 extern bool trilinear;
11
12 Texture::~Texture()  {
13         free(array);
14     glDeleteTextures(1,&id);
15 }
16
17 void Texture::load()  {
18         GLuint type;
19
20         LOGFUNC;
21
22         LOG(std::string("Loading texture...") + fileName);
23
24         unsigned char fileNamep[256];
25         CopyCStringToPascal(ConvertFileName(fileName.c_str()), fileNamep);
26         //Load Image
27         upload_image( fileNamep ,hasalpha);
28         
29         //Alpha channel?
30         if ( texture.bpp == 24 )
31                 type = GL_RGB;
32         else
33                 type = GL_RGBA;
34
35         glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
36
37         if(!id)
38                 glGenTextures( 1, &id );
39         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
40
41         glBindTexture( GL_TEXTURE_2D, id);
42         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
43         if(mipmap)
44                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear?GL_LINEAR_MIPMAP_LINEAR:GL_LINEAR_MIPMAP_NEAREST) );
45         else
46                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
47         
48         skinsize=texture.sizeX;
49         
50         if(isSkin) {
51                 int tempnum=0;
52                 int nb = (texture.sizeY*texture.sizeX*(texture.bpp/8));
53                 array = (GLubyte*)malloc(nb*sizeof(GLubyte));
54                 for(int i=0;i<nb;i++) {
55                         if((i+1)%4||type==GL_RGB) {
56                                 array[tempnum]=texture.data[i];
57                                 tempnum++;
58                         }
59                 }
60                 arraySize=tempnum;
61
62                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, GL_RGB, GL_UNSIGNED_BYTE, array );
63         } else {
64                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
65         }
66 }
67
68 GLuint Texture::Load(const string& fileName, bool mipmap, bool hasalpha) {
69         map<string,Texture>::iterator it = textures.find(fileName);
70         if(it==textures.end()) {
71                 textures.insert(make_pair(fileName,Texture(fileName,mipmap,hasalpha)));
72                 textures[fileName].load();
73                 return textures[fileName].getId();
74         } else {
75                 return it->second.getId();
76         }
77 }
78
79 GLuint Texture::Load(const string& fileName, bool mipmap, bool hasalpha, GLubyte* array, int* skinsize) {
80         map<string,Texture>::iterator it = textures.find(fileName);
81         if(it==textures.end()) {
82                 textures.insert(make_pair(fileName,Texture(fileName,mipmap,hasalpha,true)));
83                 textures[fileName].load();
84                 *skinsize = textures[fileName].skinsize;
85                 for(int i=0;i<textures[fileName].arraySize;i++) {
86                         array[i] = textures[fileName].array[i];
87                 }
88                 return textures[fileName].getId();
89         } else {
90                 *skinsize = it->second.skinsize;
91                 for(int i=0;i<it->second.arraySize;i++) {
92                         array[i] = it->second.array[i];
93                 }
94                 return it->second.getId();
95         }
96 }
97