include fixes from ohnobinki, video_systems.cpp should fall back to SDL now if GL...
[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[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     return texture;
115   }
116 }
117
118 TexturePtr
119 TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
120 {
121   SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
122   if (!image)
123   {
124     std::ostringstream msg;
125     msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
126     throw std::runtime_error(msg.str());
127   }
128   else
129   {
130     SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) + 
131                                                     rect.top * image->pitch + 
132                                                     rect.left * image->format->BytesPerPixel, 
133
134                                                     rect.get_width(), rect.get_height(),
135                                                     image->format->BitsPerPixel,
136                                                     image->pitch,
137                                                     image->format->Rmask,
138                                                     image->format->Gmask,
139                                                     image->format->Bmask,
140                                                     image->format->Amask));
141     if (!subimage)
142     {
143       throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
144     }
145     else
146     {
147       if (image->format->palette)
148       { // copy the image palette to subimage if present
149         SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
150       }
151
152       TexturePtr result = VideoSystem::new_texture(subimage.get());
153       result->set_filename(filename);
154       return result;
155     }
156   }
157 }
158
159 TexturePtr
160 TextureManager::create_image_texture(const std::string& filename)
161 {
162   try 
163   {
164     return create_image_texture_raw(filename);
165   }
166   catch (const std::exception& err)
167   {
168     log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
169     TexturePtr texture = create_dummy_texture();
170     return texture;
171   }
172 }
173
174 TexturePtr
175 TextureManager::create_image_texture_raw(const std::string& filename)
176 {
177   SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
178   if (!image) 
179   {
180     std::ostringstream msg;
181     msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
182     throw std::runtime_error(msg.str());
183   }
184   else
185   {
186     TexturePtr result = VideoSystem::new_texture(image.get());
187     result->set_filename(filename);
188     return result;
189   }
190 }
191
192 TexturePtr
193 TextureManager::create_dummy_texture()
194 {
195   const std::string dummy_texture_fname = "images/engine/missing.png";
196  
197   // on error, try loading placeholder file
198   try 
199   {
200     TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
201     return tex;
202   }
203   catch (const std::exception& err) 
204   {
205     // on error (when loading placeholder), try using empty surface,
206     // when that fails to, just give up
207     SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
208     if (!image)
209     {
210       throw err;
211     }
212     else
213     {
214       TexturePtr result = VideoSystem::new_texture(image.get());
215       result->set_filename("-dummy-texture-.png");
216       log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
217       return result;
218     }
219   }
220 }
221
222 #ifdef HAVE_OPENGL
223 void
224 TextureManager::save_textures()
225 {
226 #ifdef GL_PACK_ROW_LENGTH
227   /* all this stuff is not support by OpenGL ES */
228   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
229   glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
230   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
231   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
232   glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
233 #endif
234
235   glPixelStorei(GL_PACK_ALIGNMENT, 1);
236   for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
237     save_texture(*i);
238   }
239   for(ImageTextures::iterator i = image_textures.begin();
240       i != image_textures.end(); ++i) {
241     save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
242   }
243 }
244
245 void
246 TextureManager::save_texture(GLTexture* texture)
247 {
248   SavedTexture saved_texture;
249   saved_texture.texture = texture;
250   glBindTexture(GL_TEXTURE_2D, texture->get_handle());
251
252   //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
253 #ifndef GL_VERSION_ES_CM_1_0
254   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
255                            &saved_texture.width);
256   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
257                            &saved_texture.height);
258   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
259                            &saved_texture.border);
260   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
261                       &saved_texture.min_filter);
262   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
263                       &saved_texture.mag_filter);
264   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
265                       &saved_texture.wrap_s);
266   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
267                       &saved_texture.wrap_t);
268
269   size_t pixelssize = saved_texture.width * saved_texture.height * 4;
270   saved_texture.pixels = new char[pixelssize];
271
272   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
273                 saved_texture.pixels);
274 #endif
275
276   saved_textures.push_back(saved_texture);
277
278   glDeleteTextures(1, &(texture->get_handle()));
279   texture->set_handle(0);
280
281   assert_gl("retrieving texture for save");
282 }
283
284 void
285 TextureManager::reload_textures()
286 {
287 #ifdef GL_UNPACK_ROW_LENGTH
288   /* OpenGL ES doesn't support these */
289   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
290   glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
291   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
292   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
293   glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
294 #endif
295   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
296
297   for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
298       i != saved_textures.end(); ++i) {
299     SavedTexture& saved_texture = *i;
300
301     GLuint handle;
302     glGenTextures(1, &handle);
303     assert_gl("creating texture handle");
304
305     glBindTexture(GL_TEXTURE_2D, handle);
306     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
307                  saved_texture.width, saved_texture.height,
308                  saved_texture.border, GL_RGBA,
309                  GL_UNSIGNED_BYTE, saved_texture.pixels);
310     delete[] saved_texture.pixels;
311     assert_gl("uploading texture pixel data");
312
313     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
314                     saved_texture.min_filter);
315     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
316                     saved_texture.mag_filter);
317     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
318                     saved_texture.wrap_s);
319     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
320                     saved_texture.wrap_t);
321
322     assert_gl("setting texture_params");
323     saved_texture.texture->set_handle(handle);
324   }
325
326   saved_textures.clear();
327 }
328 #endif
329
330 /* EOF */