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