Octo's patch from bug 523.
[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 <assert.h>
21 #include <iostream>
22 #include <sstream>
23 #include <stdexcept>
24
25 #include "math/rect.hpp"
26 #include "physfs/physfs_sdl.hpp"
27 #include "util/file_system.hpp"
28 #include "util/log.hpp"
29 #include "video/sdl_surface_ptr.hpp"
30 #include "video/texture.hpp"
31 #include "video/video_systems.hpp"
32
33 #ifdef HAVE_OPENGL
34 #include "video/gl/gl_texture.hpp"
35 #endif
36
37 TextureManager::TextureManager() :
38   image_textures()
39 #ifdef HAVE_OPENGL
40   ,textures(),
41   saved_textures()
42 #endif
43 {
44 }
45
46 TextureManager::~TextureManager()
47 {
48   for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i)
49   {
50     if(i->second.lock())
51     {
52       log_warning << "Texture '" << i->first << "' not freed" << std::endl;
53     }
54   }
55   image_textures.clear();
56 }
57
58 TexturePtr
59 TextureManager::get(const std::string& _filename)
60 {
61   std::string filename = FileSystem::normalize(_filename);
62   ImageTextures::iterator i = image_textures.find(filename);
63
64   TexturePtr texture;
65   if(i != image_textures.end())
66     texture = i->second.lock();
67
68   if(!texture) {
69     texture = create_image_texture(filename);
70     image_textures[texture->get_filename()] = texture;
71   }
72
73   return texture;
74 }
75
76 TexturePtr
77 TextureManager::get(const std::string& filename, const Rect& rect)
78 {
79   // FIXME: implement caching
80   return create_image_texture(filename, rect);
81 }
82
83 void
84 TextureManager::release(Texture* texture)
85 {
86   image_textures.erase(texture->get_filename());
87 }
88
89 #ifdef HAVE_OPENGL
90 void
91 TextureManager::register_texture(GLTexture* texture)
92 {
93   textures.insert(texture);
94 }
95
96 void
97 TextureManager::remove_texture(GLTexture* texture)
98 {
99   textures.erase(texture);
100 }
101 #endif
102
103 TexturePtr
104 TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
105 {
106   try 
107   {
108     return create_image_texture_raw(filename, rect);
109   }
110   catch(const std::exception& err)
111   {
112     log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
113     TexturePtr texture = create_dummy_texture();
114     texture->set_filename(filename);
115     return texture;
116   }
117 }
118
119 TexturePtr
120 TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
121 {
122   SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
123   if (!image)
124   {
125     std::ostringstream msg;
126     msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
127     throw std::runtime_error(msg.str());
128   }
129   else
130   {
131     SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) + 
132                                                     rect.top * image->pitch + 
133                                                     rect.left * image->format->BytesPerPixel, 
134
135                                                     rect.get_width(), rect.get_height(),
136                                                     image->format->BitsPerPixel,
137                                                     image->pitch,
138                                                     image->format->Rmask,
139                                                     image->format->Gmask,
140                                                     image->format->Bmask,
141                                                     image->format->Amask));
142     if (!subimage)
143     {
144       throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
145     }
146     else
147     {
148       if (image->format->palette)
149       { // copy the image palette to subimage if present
150         SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
151       }
152
153       TexturePtr result = VideoSystem::new_texture(subimage.get());
154       result->set_filename(filename);
155       return result;
156     }
157   }
158 }
159
160 TexturePtr
161 TextureManager::create_image_texture(const std::string& filename)
162 {
163   try 
164   {
165     return create_image_texture_raw(filename);
166   }
167   catch (const std::exception& err)
168   {
169     log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
170     TexturePtr texture = create_dummy_texture();
171     texture->set_filename(filename);
172     return texture;
173   }
174 }
175
176 TexturePtr
177 TextureManager::create_image_texture_raw(const std::string& filename)
178 {
179   SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
180   if (!image) 
181   {
182     std::ostringstream msg;
183     msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
184     throw std::runtime_error(msg.str());
185   }
186   else
187   {
188     TexturePtr result = VideoSystem::new_texture(image.get());
189     result->set_filename(filename);
190     return result;
191   }
192 }
193
194 TexturePtr
195 TextureManager::create_dummy_texture()
196 {
197   const std::string dummy_texture_fname = "images/engine/missing.png";
198  
199   // on error, try loading placeholder file
200   try 
201   {
202     TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
203     return tex;
204   }
205   catch (const std::exception& err) 
206   {
207     // on error (when loading placeholder), try using empty surface,
208     // when that fails to, just give up
209     SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
210     if (!image)
211     {
212       throw err;
213     }
214     else
215     {
216       TexturePtr result = VideoSystem::new_texture(image.get());
217       result->set_filename("-dummy-texture-.png");
218       log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
219       return result;
220     }
221   }
222 }
223
224 #ifdef HAVE_OPENGL
225 void
226 TextureManager::save_textures()
227 {
228 #ifdef GL_PACK_ROW_LENGTH
229   /* all this stuff is not support by OpenGL ES */
230   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
231   glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
232   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
233   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
234   glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
235 #endif
236
237   glPixelStorei(GL_PACK_ALIGNMENT, 1);
238   for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
239     save_texture(*i);
240   }
241   for(ImageTextures::iterator i = image_textures.begin();
242       i != image_textures.end(); ++i) {
243     save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
244   }
245 }
246
247 void
248 TextureManager::save_texture(GLTexture* texture)
249 {
250   SavedTexture saved_texture;
251   saved_texture.texture = texture;
252   glBindTexture(GL_TEXTURE_2D, texture->get_handle());
253
254   //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
255 #ifndef GL_VERSION_ES_CM_1_0
256   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
257                            &saved_texture.width);
258   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
259                            &saved_texture.height);
260   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
261                            &saved_texture.border);
262   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
263                       &saved_texture.min_filter);
264   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
265                       &saved_texture.mag_filter);
266   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
267                       &saved_texture.wrap_s);
268   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
269                       &saved_texture.wrap_t);
270
271   size_t pixelssize = saved_texture.width * saved_texture.height * 4;
272   saved_texture.pixels = new char[pixelssize];
273
274   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
275                 saved_texture.pixels);
276 #endif
277
278   saved_textures.push_back(saved_texture);
279
280   glDeleteTextures(1, &(texture->get_handle()));
281   texture->set_handle(0);
282
283   assert_gl("retrieving texture for save");
284 }
285
286 void
287 TextureManager::reload_textures()
288 {
289 #ifdef GL_UNPACK_ROW_LENGTH
290   /* OpenGL ES doesn't support these */
291   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
292   glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
293   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
294   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
295   glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
296 #endif
297   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
298
299   for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
300       i != saved_textures.end(); ++i) {
301     SavedTexture& saved_texture = *i;
302
303     GLuint handle;
304     glGenTextures(1, &handle);
305     assert_gl("creating texture handle");
306
307     glBindTexture(GL_TEXTURE_2D, handle);
308     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
309                  saved_texture.width, saved_texture.height,
310                  saved_texture.border, GL_RGBA,
311                  GL_UNSIGNED_BYTE, saved_texture.pixels);
312     delete[] saved_texture.pixels;
313     assert_gl("uploading texture pixel data");
314
315     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
316                     saved_texture.min_filter);
317     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
318                     saved_texture.mag_filter);
319     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
320                     saved_texture.wrap_s);
321     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
322                     saved_texture.wrap_t);
323
324     assert_gl("setting texture_params");
325     saved_texture.texture->set_handle(handle);
326   }
327
328   saved_textures.clear();
329 }
330 #endif
331
332 /* EOF */