Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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 <iostream>
21
22 #include "physfs/physfs_sdl.hpp"
23 #include "util/file_system.hpp"
24 #include "util/log.hpp"
25 #include "video/gl/gl_texture.hpp"
26 #include "video/video_systems.hpp"
27
28 TextureManager* texture_manager = NULL;
29
30 TextureManager::TextureManager()
31 {
32 }
33
34 TextureManager::~TextureManager()
35 {
36   for(ImageTextures::iterator i = image_textures.begin();
37       i != image_textures.end(); ++i) {
38     if(i->second == NULL)
39       continue;
40     log_warning << "Texture '" << i->first << "' not freed" << std::endl;
41     delete i->second;
42   }
43 }
44
45 Texture*
46 TextureManager::get(const std::string& _filename)
47 {
48   std::string filename = FileSystem::normalize(_filename);
49   ImageTextures::iterator i = image_textures.find(filename);
50
51   Texture* texture = NULL;
52   if(i != image_textures.end())
53     texture = i->second;
54
55   if(texture == NULL) {
56     texture = create_image_texture(filename);
57     image_textures[filename] = texture;
58   }
59
60   return texture;
61 }
62
63 void
64 TextureManager::release(Texture* texture)
65 {
66   image_textures.erase(texture->get_filename());
67   delete texture;
68 }
69
70 #ifdef HAVE_OPENGL
71 void
72 TextureManager::register_texture(GLTexture* texture)
73 {
74   textures.insert(texture);
75 }
76
77 void
78 TextureManager::remove_texture(GLTexture* texture)
79 {
80   textures.erase(texture);
81 }
82 #endif
83
84 Texture*
85 TextureManager::create_image_texture(const std::string& filename)
86 {
87   try {
88
89     SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
90     if(image == 0) {
91       std::ostringstream msg;
92       msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
93       throw std::runtime_error(msg.str());
94     }
95
96     Texture* result = 0;
97     try {
98       result = new_texture(image);
99       result->set_filename(filename);
100     } catch(...) {
101       delete result;
102       SDL_FreeSurface(image);
103       throw;
104     }
105
106     SDL_FreeSurface(image);
107     return result;
108
109   } catch (const std::runtime_error& err) {
110     const std::string dummy_texture_fname = "images/engine/missing.png";
111     if (filename == dummy_texture_fname) throw err;
112
113     // on error, try loading placeholder file
114     try {
115
116       Texture* tex = create_image_texture(dummy_texture_fname);
117       log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
118       return tex;
119
120     } catch (...) {
121
122       // on error (when loading placeholder), try using empty surface
123       try {
124
125         SDL_Surface* image = SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0);
126         if(image == 0) {
127           throw err;
128         }
129
130         Texture* result = 0;
131         try {
132           result = new_texture(image);
133           result->set_filename("-dummy-texture-.png");
134         } catch(...) {
135           delete result;
136           SDL_FreeSurface(image);
137           throw err;
138         }
139
140         SDL_FreeSurface(image);
141         log_warning << "Couldn't load texture '" << filename << "' (now using empty one): " << err.what() << std::endl;
142         return result;
143
144         // on error (when trying to use empty surface), give up
145       } catch (const std::runtime_error& err) {
146         throw err;
147       }
148     }
149   }
150 }
151
152 #ifdef HAVE_OPENGL
153 void
154 TextureManager::save_textures()
155 {
156 #ifdef GL_PACK_ROW_LENGTH
157   /* all this stuff is not support by OpenGL ES */
158   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
159   glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
160   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
161   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
162   glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
163 #endif
164
165   glPixelStorei(GL_PACK_ALIGNMENT, 1);
166   for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
167     save_texture(*i);
168   }
169   for(ImageTextures::iterator i = image_textures.begin();
170       i != image_textures.end(); ++i) {
171     save_texture(dynamic_cast<GLTexture *>(i->second));
172   }
173 }
174
175 void
176 TextureManager::save_texture(GLTexture* texture)
177 {
178   SavedTexture saved_texture;
179   saved_texture.texture = texture;
180   glBindTexture(GL_TEXTURE_2D, texture->get_handle());
181
182   //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
183 #ifndef GL_VERSION_ES_CM_1_0
184   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
185                            &saved_texture.width);
186   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
187                            &saved_texture.height);
188   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
189                            &saved_texture.border);
190   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
191                       &saved_texture.min_filter);
192   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
193                       &saved_texture.mag_filter);
194   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
195                       &saved_texture.wrap_s);
196   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
197                       &saved_texture.wrap_t);
198
199   size_t pixelssize = saved_texture.width * saved_texture.height * 4;
200   saved_texture.pixels = new char[pixelssize];
201
202   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
203                 saved_texture.pixels);
204 #endif
205
206   saved_textures.push_back(saved_texture);
207
208   glDeleteTextures(1, &(texture->get_handle()));
209   texture->set_handle(0);
210
211   assert_gl("retrieving texture for save");
212 }
213
214 void
215 TextureManager::reload_textures()
216 {
217 #ifdef GL_UNPACK_ROW_LENGTH
218   /* OpenGL ES doesn't support these */
219   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
220   glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
221   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
222   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
223   glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
224 #endif
225   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
226
227   for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
228       i != saved_textures.end(); ++i) {
229     SavedTexture& saved_texture = *i;
230
231     GLuint handle;
232     glGenTextures(1, &handle);
233     assert_gl("creating texture handle");
234
235     glBindTexture(GL_TEXTURE_2D, handle);
236     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
237                  saved_texture.width, saved_texture.height,
238                  saved_texture.border, GL_RGBA,
239                  GL_UNSIGNED_BYTE, saved_texture.pixels);
240     delete[] saved_texture.pixels;
241     assert_gl("uploading texture pixel data");
242
243     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
244                     saved_texture.min_filter);
245     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
246                     saved_texture.mag_filter);
247     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
248                     saved_texture.wrap_s);
249     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
250                     saved_texture.wrap_t);
251
252     assert_gl("setting texture_params");
253     saved_texture.texture->set_handle(handle);
254   }
255
256   saved_textures.clear();
257 }
258 #endif
259
260 /* EOF */