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