]> git.jsancho.org Git - lugaru.git/blob - Source/Texture.cpp
License: Update GPLv2+ header to match current FSF recommendation
[lugaru.git] / Source / Texture.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "gamegl.h"
21 #include "Texture.h"
22 #include "TGALoader.h"
23
24 using namespace std;
25
26 extern TGAImageRec texture;
27 extern bool trilinear;
28
29
30 class TextureRes
31 {
32 private:
33     static vector<TextureRes*> list;
34
35     GLuint id;
36     string filename;
37     bool hasMipmap;
38     bool hasAlpha;
39     bool isSkin;
40     int skinsize;
41     GLubyte* data;
42     int datalen;
43     GLubyte* skindata;
44
45     void load();
46
47 public:
48     TextureRes(const string& filename, bool hasMipmap, bool hasAlpha);
49     TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize);
50     ~TextureRes();
51     void bind();
52
53     static void reloadAll();
54 };
55
56
57 vector<TextureRes*> TextureRes::list;
58
59 void TextureRes::load()
60 {
61     //load image into 'texture' global var
62     if (!skindata) {
63         unsigned char filenamep[256];
64         CopyCStringToPascal(ConvertFileName(filename.c_str()), filenamep);
65         upload_image(filenamep, hasAlpha);
66     }
67
68     skinsize = texture.sizeX;
69     GLuint type = GL_RGBA;
70     if (texture.bpp == 24)
71         type = GL_RGB;
72
73     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
74
75     glDeleteTextures(1, &id);
76     glGenTextures(1, &id);
77     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
78
79     glBindTexture(GL_TEXTURE_2D, id);
80     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
81     if (hasMipmap) {
82         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
83         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
84     } else {
85         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
86     }
87
88     if (isSkin) {
89         if (skindata) {
90             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, skinsize, skinsize, 0, GL_RGB, GL_UNSIGNED_BYTE, skindata);
91         } else {
92             free(data);
93             const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
94             data = (GLubyte*)malloc(nb * sizeof(GLubyte));
95             datalen = 0;
96             for (int i = 0; i < nb; i++)
97                 if ((i + 1) % 4 || type == GL_RGB)
98                     data[datalen++] = texture.data[i];
99             glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
100         }
101     } else {
102         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
103     }
104 }
105
106 void TextureRes::bind()
107 {
108     glBindTexture(GL_TEXTURE_2D, id);
109 }
110
111 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, bool _hasAlpha):
112     id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(_hasAlpha), isSkin(false),
113     skinsize(0), data(NULL), datalen(0), skindata(NULL)
114 {
115     load();
116     list.push_back(this);
117 }
118
119 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
120     id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(false), isSkin(true),
121     skinsize(0), data(NULL), datalen(0), skindata(NULL)
122 {
123     load();
124     *skinsizep = skinsize;
125     for (int i = 0; i < datalen; i++)
126         array[i] = data[i];
127     skindata = array;
128     list.push_back(this);
129 }
130
131 TextureRes::~TextureRes()
132 {
133     free(data);
134     glDeleteTextures(1, &id);
135     for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
136         if (*it == this) {
137             list.erase(it);
138             break;
139         }
140 }
141
142 void TextureRes::reloadAll()
143 {
144     for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++) {
145         (*it)->id = 0;
146         (*it)->load();
147     }
148 }
149
150
151
152
153 void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha)
154 {
155     destroy();
156     tex = new TextureRes(filename, hasMipmap, hasAlpha);
157 }
158
159 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
160 {
161     destroy();
162     tex = new TextureRes(filename, hasMipmap, array, skinsizep);
163 }
164
165 void Texture::destroy()
166 {
167     if (tex) {
168         delete tex;
169         tex = NULL;
170     }
171 }
172
173 void Texture::bind()
174 {
175     if (tex)
176         tex->bind();
177     else
178         glBindTexture(GL_TEXTURE_2D, 0);
179 }
180
181 void Texture::reloadAll()
182 {
183     TextureRes::reloadAll();
184 }
185