00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "textures.h"
00026 #include "globals.h"
00027
00028
00029
00030 void TEXTURE::Load()
00031 {
00032 if (file.empty())
00033 cerr << "ERROR: Tried to load an empty texture" << endl;
00034
00035 if (loaded)
00036 cerr << "ERROR: Tried to double load texture: " << file << endl;
00037
00038 bool error = false;
00039 GLuint new_tex_id = utility.TexLoad( file, GL_RGBA, mipmap, w, h, false, error );
00040
00041 if( error )
00042 {
00043 cerr << "error loading texture file \"" << file << "\"..." << endl;
00044 }
00045 else
00046 {
00047 loaded = true;
00048 tex_id = new_tex_id;
00049 }
00050 }
00051
00052 void TEXTURE::Activate()
00053 {
00054
00055
00056
00057 glBindTexture(GL_TEXTURE_2D, tex_id);
00058 }
00059
00060 void TEXTURE::Unload()
00061 {
00062 if (loaded)
00063 glDeleteTextures(1,&tex_id);
00064 loaded = false;
00065 }
00066
00067 void TEXTURE_HANDLE::Activate()
00068 {
00069 if (tex != NULL)
00070 {
00071 tex->Activate();
00072 }
00073 else
00074 {
00075
00076 }
00077 }
00078
00079 bool TEXTURE_HANDLE::Load( string new_tex_file, bool mipmap, int &w, int &h )
00080 {
00081 if (tex != NULL)
00082 {
00083 if (tex->file == new_tex_file)
00084 {
00085 #ifdef DEBUG_TEXTURES
00086 cout << "TEXTURE_HANDLE::Load(" << new_tex_file << "): already loaded" << endl;
00087 #endif
00088 return true;
00089 }
00090 else
00091 {
00092 #ifdef DEBUG_TEXTURES
00093 cout << "TEXTURE_HANDLE::Load(" << new_tex_file << "): unloading previous texture" << endl;
00094 #endif
00095 Unload();
00096 }
00097 }
00098 else
00099 {
00100 #ifdef DEBUG_TEXTURES
00101 cout << "TEXTURE_HANDLE::Load(" << new_tex_file << "): not yet loaded, calling LoadTexture" << endl;
00102 #endif
00103 }
00104
00105 TEXTURE * newtexptr = textures.LoadTexture(new_tex_file, mipmap, w, h);
00106
00107 if (newtexptr != NULL)
00108 {
00109 tex = newtexptr;
00110 tex->references ++;
00111 #ifdef DEBUG_TEXTURES
00112 cout << "TEXTURE_HANDLE::Load(" << new_tex_file << "): loaded, references now " << tex->references << endl;
00113 #endif
00114 }
00115 else
00116 {
00117 cerr << "ERROR: LoadTexture returned NULL pointer for " << new_tex_file << endl;
00118 return false;
00119 }
00120
00121 return true;
00122 }
00123
00124 void TEXTURE_HANDLE::Unload()
00125 {
00126 if (tex != NULL)
00127 {
00128 tex->references--;
00129 if (tex->references <= 0)
00130 {
00131 #ifdef DEBUG_TEXTURES
00132 cout << "1TEXTURE_HANDLE::Unload(" << tex->file << "): references <= 0, unloading now" << endl;
00133 #endif
00134
00135 if (tex->references < 0)
00136 cerr << "texture " + tex->file + " has < 0 references, now " << tex->references << endl;
00137 textures.UnloadTexture(tex);
00138 }
00139 else
00140 {
00141 #ifdef DEBUG_TEXTURES
00142 cout << "2TEXTURE_HANDLE::Unload(" << tex->file << "): references--, now " << tex->references << endl;
00143 #endif
00144 }
00145 }
00146 else
00147 {
00148 #ifdef DEBUG_TEXTURES
00149 cout << "3TEXTURE_HANDLE::Unload(): already unloaded, twiddling thumbs" << endl;
00150 #endif
00151 }
00152
00153 tex = NULL;
00154 }
00155
00156 void TEXTURES::ReloadAll()
00157 {
00158
00159
00160
00161
00162
00163
00164
00165 for (list <TEXTURE>::iterator tex = textures.begin(); tex != textures.end(); tex++)
00166 {
00167 tex->Unload();
00168
00169
00170
00171
00172
00173
00174 tex->Load();
00175 }
00176 }
00177
00178 TEXTURE * TEXTURES::LoadTexture( string new_tex_file, bool mipmap, int &w, int &h )
00179 {
00180 TEXTURE * texptr = FindTexture(new_tex_file);
00181
00182 if (texptr == NULL)
00183 {
00184 #ifdef DEBUG_TEXTURES
00185 cout << "TEXTURES::LoadTexture(" << new_tex_file << "): not yet loaded, loading now" << endl;
00186 #endif
00187
00188
00189 TEXTURE newtex;
00190 textures.push_back(newtex);
00191 texptr = &(textures.back());
00192
00193
00194 texptr->file = new_tex_file;
00195 texptr->mipmap = mipmap;
00196 texptr->Load();
00197 w = texptr->w;
00198 h = texptr->h;
00199 }
00200 else
00201 {
00202 #ifdef DEBUG_TEXTURES
00203 cout << "TEXTURES::LoadTexture(" << new_tex_file << "): already loaded" << endl;
00204 #endif
00205 }
00206
00207 return texptr;
00208 }
00209
00210 void TEXTURES::UnloadTexture(TEXTURE * textodel)
00211 {
00212 for (list <TEXTURE>::iterator i = textures.begin(); i != textures.end(); i++)
00213 {
00214 if (&(*i) == textodel)
00215 {
00216 list <TEXTURE>::iterator tempit = i;
00217 tempit++;
00218 textures.erase(i);
00219 i = tempit;
00220 }
00221 }
00222 }
00223
00224 TEXTURE * TEXTURES::FindTexture(string searchname)
00225 {
00226 TEXTURE * tptr = NULL;
00227
00228 for (list <TEXTURE>::iterator i = textures.begin(); i != textures.end(); i++)
00229 {
00230 if (i->file == searchname)
00231 tptr = &(*i);
00232 }
00233
00234 return tptr;
00235 }
00236
00237 TEXTURE_HANDLE & TEXTURE_HANDLE::CopyFrom(const TEXTURE_HANDLE & other)
00238 {
00239
00240
00241
00242
00243
00244 if (other.tex == NULL)
00245 {
00246 #ifdef DEBUG_TEXTURES
00247 cout << "TEXTURE_HANDLE::CopyFrom(): other texture handle is NULL" << endl;
00248 #endif
00249 Unload();
00250 return *this;
00251 }
00252 else
00253 {
00254 #ifdef DEBUG_TEXTURES
00255 cout << "TEXTURE_HANDLE::CopyFrom() called" << endl;
00256 cout << "TEXTURE_HANDLE::CopyFrom(" + other.tex->file + "): calling Load(" << other.tex->file << ")" << endl;
00257 #endif
00258 Load(other.tex->file, other.tex->mipmap);
00259 #ifdef DEBUG_TEXTURES
00260 cout << "TEXTURE_HANDLE::CopyFrom(" + tex->file + "): loaded, references now " << tex->references << endl;
00261 #endif
00262 }
00263
00264 return *this;
00265 }
00266
00267 TEXTURE_HANDLE::TEXTURE_HANDLE(const TEXTURE_HANDLE & other)
00268 {
00269 tex = NULL;
00270 #ifdef DEBUG_TEXTURES
00271 cout << "TEXTURE_HANDLE Copy constructor called" << endl;
00272 #endif
00273 CopyFrom(other);
00274 }
00275
00276 TEXTURE_HANDLE::~TEXTURE_HANDLE()
00277 {
00278 Unload();
00279 }