2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "video/texture_manager.hpp"
19 #include <SDL_image.h>
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"
34 #include "video/gl/gl_texture.hpp"
37 TextureManager::TextureManager() :
46 TextureManager::~TextureManager()
48 for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i)
52 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
55 image_textures.clear();
59 TextureManager::get(const std::string& _filename)
61 std::string filename = FileSystem::normalize(_filename);
62 ImageTextures::iterator i = image_textures.find(filename);
65 if(i != image_textures.end())
66 texture = i->second.lock();
69 texture = create_image_texture(filename);
70 image_textures[texture->get_filename()] = texture;
77 TextureManager::get(const std::string& filename, const Rect& rect)
79 // FIXME: implement caching
80 return create_image_texture(filename, rect);
84 TextureManager::release(Texture* texture)
86 image_textures.erase(texture->get_filename());
91 TextureManager::register_texture(GLTexture* texture)
93 textures.insert(texture);
97 TextureManager::remove_texture(GLTexture* texture)
99 textures.erase(texture);
104 TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
108 return create_image_texture_raw(filename, rect);
110 catch(const std::exception& err)
112 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
113 TexturePtr texture = create_dummy_texture();
114 texture->set_filename(filename);
120 TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
122 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
125 std::ostringstream msg;
126 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
127 throw std::runtime_error(msg.str());
131 SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
132 rect.top * image->pitch +
133 rect.left * image->format->BytesPerPixel,
135 rect.get_width(), rect.get_height(),
136 image->format->BitsPerPixel,
138 image->format->Rmask,
139 image->format->Gmask,
140 image->format->Bmask,
141 image->format->Amask));
144 throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
148 if (image->format->palette)
149 { // copy the image palette to subimage if present
150 SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
153 TexturePtr result = VideoSystem::new_texture(subimage.get());
154 result->set_filename(filename);
161 TextureManager::create_image_texture(const std::string& filename)
165 return create_image_texture_raw(filename);
167 catch (const std::exception& err)
169 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
170 TexturePtr texture = create_dummy_texture();
171 texture->set_filename(filename);
177 TextureManager::create_image_texture_raw(const std::string& filename)
179 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
182 std::ostringstream msg;
183 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
184 throw std::runtime_error(msg.str());
188 TexturePtr result = VideoSystem::new_texture(image.get());
189 result->set_filename(filename);
195 TextureManager::create_dummy_texture()
197 const std::string dummy_texture_fname = "images/engine/missing.png";
199 // on error, try loading placeholder file
202 TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
205 catch (const std::exception& err)
207 // on error (when loading placeholder), try using empty surface,
208 // when that fails to, just give up
209 SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
216 TexturePtr result = VideoSystem::new_texture(image.get());
217 result->set_filename("-dummy-texture-.png");
218 log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
226 TextureManager::save_textures()
228 #ifdef GL_PACK_ROW_LENGTH
229 /* all this stuff is not support by OpenGL ES */
230 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
231 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
232 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
233 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
234 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
237 glPixelStorei(GL_PACK_ALIGNMENT, 1);
238 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
241 for(ImageTextures::iterator i = image_textures.begin();
242 i != image_textures.end(); ++i) {
243 save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
248 TextureManager::save_texture(GLTexture* texture)
250 SavedTexture saved_texture;
251 saved_texture.texture = texture;
252 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
254 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
255 #ifndef GL_VERSION_ES_CM_1_0
256 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
257 &saved_texture.width);
258 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
259 &saved_texture.height);
260 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
261 &saved_texture.border);
262 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
263 &saved_texture.min_filter);
264 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
265 &saved_texture.mag_filter);
266 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
267 &saved_texture.wrap_s);
268 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
269 &saved_texture.wrap_t);
271 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
272 saved_texture.pixels = new char[pixelssize];
274 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
275 saved_texture.pixels);
278 saved_textures.push_back(saved_texture);
280 glDeleteTextures(1, &(texture->get_handle()));
281 texture->set_handle(0);
283 assert_gl("retrieving texture for save");
287 TextureManager::reload_textures()
289 #ifdef GL_UNPACK_ROW_LENGTH
290 /* OpenGL ES doesn't support these */
291 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
292 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
293 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
294 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
295 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
297 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
299 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
300 i != saved_textures.end(); ++i) {
301 SavedTexture& saved_texture = *i;
304 glGenTextures(1, &handle);
305 assert_gl("creating texture handle");
307 glBindTexture(GL_TEXTURE_2D, handle);
308 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
309 saved_texture.width, saved_texture.height,
310 saved_texture.border, GL_RGBA,
311 GL_UNSIGNED_BYTE, saved_texture.pixels);
312 delete[] saved_texture.pixels;
313 assert_gl("uploading texture pixel data");
315 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
316 saved_texture.min_filter);
317 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
318 saved_texture.mag_filter);
319 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
320 saved_texture.wrap_s);
321 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
322 saved_texture.wrap_t);
324 assert_gl("setting texture_params");
325 saved_texture.texture->set_handle(handle);
328 saved_textures.clear();