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