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