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>
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"
31 TextureManager::TextureManager() :
40 TextureManager::~TextureManager()
42 for(ImageTextures::iterator i = image_textures.begin();
43 i != image_textures.end(); ++i) {
46 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
52 TextureManager::get(const std::string& _filename)
54 std::string filename = FileSystem::normalize(_filename);
55 ImageTextures::iterator i = image_textures.find(filename);
57 Texture* texture = NULL;
58 if(i != image_textures.end())
62 texture = create_image_texture(filename);
63 image_textures[filename] = texture;
70 TextureManager::get(const std::string& filename, const Rect& rect)
72 // FIXME: implement caching
73 return create_image_texture(filename, rect);
77 TextureManager::release(Texture* texture)
79 image_textures.erase(texture->get_filename());
85 TextureManager::register_texture(GLTexture* texture)
87 textures.insert(texture);
91 TextureManager::remove_texture(GLTexture* texture)
93 textures.erase(texture);
98 TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
102 return create_image_texture_raw(filename, rect);
104 catch(const std::exception& err)
106 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
107 Texture* texture = create_dummy_texture();
113 TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
115 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
118 std::ostringstream msg;
119 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
120 throw std::runtime_error(msg.str());
124 SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
125 rect.top * image->pitch +
126 rect.left * image->format->BytesPerPixel,
128 rect.get_width(), rect.get_height(),
129 image->format->BitsPerPixel,
131 image->format->Rmask,
132 image->format->Gmask,
133 image->format->Bmask,
134 image->format->Amask));
137 throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
141 if (image->format->palette)
142 { // copy the image palette to subimage if present
143 SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
146 Texture* result = VideoSystem::new_texture(subimage.get());
147 result->set_filename(filename);
154 TextureManager::create_image_texture(const std::string& filename)
158 return create_image_texture_raw(filename);
160 catch (const std::exception& err)
162 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
163 Texture* texture = create_dummy_texture();
169 TextureManager::create_image_texture_raw(const std::string& filename)
171 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
174 std::ostringstream msg;
175 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
176 throw std::runtime_error(msg.str());
180 Texture* result = VideoSystem::new_texture(image.get());
181 result->set_filename(filename);
187 TextureManager::create_dummy_texture()
189 const std::string dummy_texture_fname = "images/engine/missing.png";
191 // on error, try loading placeholder file
194 Texture* tex = create_image_texture_raw(dummy_texture_fname);
197 catch (const std::exception& err)
199 // on error (when loading placeholder), try using empty surface,
200 // when that fails to, just give up
201 SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
208 Texture* result = VideoSystem::new_texture(image.get());
209 result->set_filename("-dummy-texture-.png");
210 log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
218 TextureManager::save_textures()
220 #ifdef GL_PACK_ROW_LENGTH
221 /* all this stuff is not support by OpenGL ES */
222 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
223 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
224 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
225 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
226 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
229 glPixelStorei(GL_PACK_ALIGNMENT, 1);
230 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
233 for(ImageTextures::iterator i = image_textures.begin();
234 i != image_textures.end(); ++i) {
235 save_texture(dynamic_cast<GLTexture *>(i->second));
240 TextureManager::save_texture(GLTexture* texture)
242 SavedTexture saved_texture;
243 saved_texture.texture = texture;
244 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
246 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
247 #ifndef GL_VERSION_ES_CM_1_0
248 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
249 &saved_texture.width);
250 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
251 &saved_texture.height);
252 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
253 &saved_texture.border);
254 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
255 &saved_texture.min_filter);
256 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
257 &saved_texture.mag_filter);
258 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
259 &saved_texture.wrap_s);
260 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
261 &saved_texture.wrap_t);
263 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
264 saved_texture.pixels = new char[pixelssize];
266 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
267 saved_texture.pixels);
270 saved_textures.push_back(saved_texture);
272 glDeleteTextures(1, &(texture->get_handle()));
273 texture->set_handle(0);
275 assert_gl("retrieving texture for save");
279 TextureManager::reload_textures()
281 #ifdef GL_UNPACK_ROW_LENGTH
282 /* OpenGL ES doesn't support these */
283 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
284 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
285 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
286 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
287 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
289 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
291 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
292 i != saved_textures.end(); ++i) {
293 SavedTexture& saved_texture = *i;
296 glGenTextures(1, &handle);
297 assert_gl("creating texture handle");
299 glBindTexture(GL_TEXTURE_2D, handle);
300 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
301 saved_texture.width, saved_texture.height,
302 saved_texture.border, GL_RGBA,
303 GL_UNSIGNED_BYTE, saved_texture.pixels);
304 delete[] saved_texture.pixels;
305 assert_gl("uploading texture pixel data");
307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
308 saved_texture.min_filter);
309 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
310 saved_texture.mag_filter);
311 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
312 saved_texture.wrap_s);
313 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
314 saved_texture.wrap_t);
316 assert_gl("setting texture_params");
317 saved_texture.texture->set_handle(handle);
320 saved_textures.clear();