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