2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program 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.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "TGALoader.h"
28 extern TGAImageRec texture;
29 extern bool trilinear;
35 static vector<TextureRes*> list;
50 TextureRes(const string& filename, bool hasMipmap, bool hasAlpha);
51 TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize);
55 static void reloadAll();
59 vector<TextureRes*> TextureRes::list;
61 void TextureRes::load()
63 //load image into 'texture' global var
65 unsigned char filenamep[256];
66 CopyCStringToPascal(ConvertFileName(filename.c_str()), filenamep);
67 upload_image(filenamep, hasAlpha);
70 skinsize = texture.sizeX;
71 GLuint type = GL_RGBA;
72 if (texture.bpp == 24)
75 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
77 glDeleteTextures(1, &id);
78 glGenTextures(1, &id);
79 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
81 glBindTexture(GL_TEXTURE_2D, id);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
85 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
92 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, skinsize, skinsize, 0, GL_RGB, GL_UNSIGNED_BYTE, skindata);
95 const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
96 data = (GLubyte*)malloc(nb * sizeof(GLubyte));
98 for (int i = 0; i < nb; i++)
99 if ((i + 1) % 4 || type == GL_RGB)
100 data[datalen++] = texture.data[i];
101 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
104 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
108 void TextureRes::bind()
110 glBindTexture(GL_TEXTURE_2D, id);
113 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, bool _hasAlpha):
114 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(_hasAlpha), isSkin(false),
115 skinsize(0), data(NULL), datalen(0), skindata(NULL)
118 list.push_back(this);
121 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
122 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(false), isSkin(true),
123 skinsize(0), data(NULL), datalen(0), skindata(NULL)
126 *skinsizep = skinsize;
127 for (int i = 0; i < datalen; i++)
130 list.push_back(this);
133 TextureRes::~TextureRes()
136 glDeleteTextures(1, &id);
137 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
144 void TextureRes::reloadAll()
146 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++) {
155 void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha)
158 tex = new TextureRes(filename, hasMipmap, hasAlpha);
161 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
164 tex = new TextureRes(filename, hasMipmap, array, skinsizep);
167 void Texture::destroy()
180 glBindTexture(GL_TEXTURE_2D, 0);
183 void Texture::reloadAll()
185 TextureRes::reloadAll();