]> git.jsancho.org Git - lugaru.git/blob - Source/Texture.cpp
Removed ununused field hasAlpha
[lugaru.git] / Source / Texture.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "gamegl.h"
22 #include "Texture.h"
23 #include "ImageIO.h"
24
25 using namespace std;
26
27 extern bool trilinear;
28
29 vector<TextureRes*> TextureRes::list;
30
31 void TextureRes::load()
32 {
33     ImageRec texture;
34
35     //load image into 'texture'
36     load_image(ConvertFileName(filename.c_str()), texture);
37
38     skinsize = texture.sizeX;
39     GLuint type = GL_RGBA;
40     if (texture.bpp == 24)
41         type = GL_RGB;
42
43     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
44
45     glDeleteTextures(1, &id);
46     glGenTextures(1, &id);
47     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
48
49     glBindTexture(GL_TEXTURE_2D, id);
50     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
51     if (hasMipmap) {
52         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
53         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
54     } else {
55         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
56     }
57
58     if (isSkin) {
59         free(data);
60         const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
61         data = (GLubyte*)malloc(nb * sizeof(GLubyte));
62         datalen = 0;
63         for (int i = 0; i < nb; i++)
64             if ((i + 1) % 4 || type == GL_RGB)
65                 data[datalen++] = texture.data[i];
66         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
67     } else {
68         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
69     }
70 }
71
72 void TextureRes::bind()
73 {
74     glBindTexture(GL_TEXTURE_2D, id);
75 }
76
77 TextureRes::TextureRes(const string& _filename, bool _hasMipmap):
78     id(0), filename(_filename), hasMipmap(_hasMipmap), isSkin(false),
79     skinsize(0), data(NULL), datalen(0)
80 {
81     load();
82     list.push_back(this);
83 }
84
85 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
86     id(0), filename(_filename), hasMipmap(_hasMipmap), isSkin(true),
87     skinsize(0), data(NULL), datalen(0)
88 {
89     load();
90     *skinsizep = skinsize;
91     for (int i = 0; i < datalen; i++)
92         array[i] = data[i];
93     list.push_back(this);
94 }
95
96 TextureRes::~TextureRes()
97 {
98     free(data);
99     glDeleteTextures(1, &id);
100     for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
101         if (*it == this) {
102             list.erase(it);
103             break;
104         }
105 }
106
107 void Texture::load(const string& filename, bool hasMipmap)
108 {
109     destroy();
110     tex = new TextureRes(filename, hasMipmap);
111 }
112
113 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
114 {
115     destroy();
116     tex = new TextureRes(filename, hasMipmap, array, skinsizep);
117 }
118
119 void Texture::destroy()
120 {
121     if (tex) {
122         delete tex;
123         tex = NULL;
124     }
125 }
126
127 void Texture::bind()
128 {
129     if (tex)
130         tex->bind();
131     else
132         glBindTexture(GL_TEXTURE_2D, 0);
133 }