More -Weffc++ cleanup
[supertux.git] / src / video / texture_manager.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "video/texture_manager.hpp"
18
19 #include <SDL_image.h>
20 #include <iostream>
21
22 #include "physfs/physfs_sdl.hpp"
23 #include "util/file_system.hpp"
24 #include "util/log.hpp"
25 #include "video/gl/gl_texture.hpp"
26 #include "video/video_systems.hpp"
27
28 TextureManager* texture_manager = NULL;
29
30 TextureManager::TextureManager() :
31   image_textures()
32 #ifdef HAVE_OPENGL
33   ,textures(),
34   saved_textures()
35 #endif
36 {
37 }
38
39 TextureManager::~TextureManager()
40 {
41   for(ImageTextures::iterator i = image_textures.begin();
42       i != image_textures.end(); ++i) {
43     if(i->second == NULL)
44       continue;
45     log_warning << "Texture '" << i->first << "' not freed" << std::endl;
46     delete i->second;
47   }
48 }
49
50 Texture*
51 TextureManager::get(const std::string& _filename)
52 {
53   std::string filename = FileSystem::normalize(_filename);
54   ImageTextures::iterator i = image_textures.find(filename);
55
56   Texture* texture = NULL;
57   if(i != image_textures.end())
58     texture = i->second;
59
60   if(texture == NULL) {
61     texture = create_image_texture(filename);
62     image_textures[filename] = texture;
63   }
64
65   return texture;
66 }
67
68 void
69 TextureManager::release(Texture* texture)
70 {
71   image_textures.erase(texture->get_filename());
72   delete texture;
73 }
74
75 #ifdef HAVE_OPENGL
76 void
77 TextureManager::register_texture(GLTexture* texture)
78 {
79   textures.insert(texture);
80 }
81
82 void
83 TextureManager::remove_texture(GLTexture* texture)
84 {
85   textures.erase(texture);
86 }
87 #endif
88
89 Texture*
90 TextureManager::create_image_texture(const std::string& filename)
91 {
92   try {
93
94     SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
95     if(image == 0) {
96       std::ostringstream msg;
97       msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
98       throw std::runtime_error(msg.str());
99     }
100
101     Texture* result = 0;
102     try {
103       result = new_texture(image);
104       result->set_filename(filename);
105     } catch(...) {
106       delete result;
107       SDL_FreeSurface(image);
108       throw;
109     }
110
111     SDL_FreeSurface(image);
112     return result;
113
114   } catch (const std::runtime_error& err) {
115     const std::string dummy_texture_fname = "images/engine/missing.png";
116     if (filename == dummy_texture_fname) throw err;
117
118     // on error, try loading placeholder file
119     try {
120
121       Texture* tex = create_image_texture(dummy_texture_fname);
122       log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
123       return tex;
124
125     } catch (...) {
126
127       // on error (when loading placeholder), try using empty surface
128       try {
129
130         SDL_Surface* image = SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0);
131         if(image == 0) {
132           throw err;
133         }
134
135         Texture* result = 0;
136         try {
137           result = new_texture(image);
138           result->set_filename("-dummy-texture-.png");
139         } catch(...) {
140           delete result;
141           SDL_FreeSurface(image);
142           throw err;
143         }
144
145         SDL_FreeSurface(image);
146         log_warning << "Couldn't load texture '" << filename << "' (now using empty one): " << err.what() << std::endl;
147         return result;
148
149         // on error (when trying to use empty surface), give up
150       } catch (const std::runtime_error& err) {
151         throw err;
152       }
153     }
154   }
155 }
156
157 #ifdef HAVE_OPENGL
158 void
159 TextureManager::save_textures()
160 {
161 #ifdef GL_PACK_ROW_LENGTH
162   /* all this stuff is not support by OpenGL ES */
163   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
164   glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
165   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
166   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
167   glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
168 #endif
169
170   glPixelStorei(GL_PACK_ALIGNMENT, 1);
171   for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
172     save_texture(*i);
173   }
174   for(ImageTextures::iterator i = image_textures.begin();
175       i != image_textures.end(); ++i) {
176     save_texture(dynamic_cast<GLTexture *>(i->second));
177   }
178 }
179
180 void
181 TextureManager::save_texture(GLTexture* texture)
182 {
183   SavedTexture saved_texture;
184   saved_texture.texture = texture;
185   glBindTexture(GL_TEXTURE_2D, texture->get_handle());
186
187   //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
188 #ifndef GL_VERSION_ES_CM_1_0
189   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
190                            &saved_texture.width);
191   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
192                            &saved_texture.height);
193   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
194                            &saved_texture.border);
195   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
196                       &saved_texture.min_filter);
197   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
198                       &saved_texture.mag_filter);
199   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
200                       &saved_texture.wrap_s);
201   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
202                       &saved_texture.wrap_t);
203
204   size_t pixelssize = saved_texture.width * saved_texture.height * 4;
205   saved_texture.pixels = new char[pixelssize];
206
207   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
208                 saved_texture.pixels);
209 #endif
210
211   saved_textures.push_back(saved_texture);
212
213   glDeleteTextures(1, &(texture->get_handle()));
214   texture->set_handle(0);
215
216   assert_gl("retrieving texture for save");
217 }
218
219 void
220 TextureManager::reload_textures()
221 {
222 #ifdef GL_UNPACK_ROW_LENGTH
223   /* OpenGL ES doesn't support these */
224   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
225   glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
226   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
227   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
228   glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
229 #endif
230   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
231
232   for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
233       i != saved_textures.end(); ++i) {
234     SavedTexture& saved_texture = *i;
235
236     GLuint handle;
237     glGenTextures(1, &handle);
238     assert_gl("creating texture handle");
239
240     glBindTexture(GL_TEXTURE_2D, handle);
241     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
242                  saved_texture.width, saved_texture.height,
243                  saved_texture.border, GL_RGBA,
244                  GL_UNSIGNED_BYTE, saved_texture.pixels);
245     delete[] saved_texture.pixels;
246     assert_gl("uploading texture pixel data");
247
248     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
249                     saved_texture.min_filter);
250     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
251                     saved_texture.mag_filter);
252     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
253                     saved_texture.wrap_s);
254     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
255                     saved_texture.wrap_t);
256
257     assert_gl("setting texture_params");
258     saved_texture.texture->set_handle(handle);
259   }
260
261   saved_textures.clear();
262 }
263 #endif
264
265 /* EOF */