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) + rect.top * image->pitch + rect.left * image->format->BytesPerPixel,
125 rect.get_width(), rect.get_height(),
126 image->format->BitsPerPixel,
128 image->format->Rmask,
129 image->format->Gmask,
130 image->format->Bmask,
131 image->format->Amask));
134 throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
138 Texture* result = VideoSystem::new_texture(subimage.get());
139 result->set_filename(filename);
146 TextureManager::create_image_texture(const std::string& filename)
150 return create_image_texture_raw(filename);
152 catch (const std::exception& err)
154 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
155 Texture* texture = create_dummy_texture();
161 TextureManager::create_image_texture_raw(const std::string& filename)
163 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
166 std::ostringstream msg;
167 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
168 throw std::runtime_error(msg.str());
172 Texture* result = VideoSystem::new_texture(image.get());
173 result->set_filename(filename);
179 TextureManager::create_dummy_texture()
181 const std::string dummy_texture_fname = "images/engine/missing.png";
183 // on error, try loading placeholder file
186 Texture* tex = create_image_texture_raw(dummy_texture_fname);
189 catch (const std::exception& err)
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));
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;
210 TextureManager::save_textures()
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);
221 glPixelStorei(GL_PACK_ALIGNMENT, 1);
222 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
225 for(ImageTextures::iterator i = image_textures.begin();
226 i != image_textures.end(); ++i) {
227 save_texture(dynamic_cast<GLTexture *>(i->second));
232 TextureManager::save_texture(GLTexture* texture)
234 SavedTexture saved_texture;
235 saved_texture.texture = texture;
236 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
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);
255 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
256 saved_texture.pixels = new char[pixelssize];
258 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
259 saved_texture.pixels);
262 saved_textures.push_back(saved_texture);
264 glDeleteTextures(1, &(texture->get_handle()));
265 texture->set_handle(0);
267 assert_gl("retrieving texture for save");
271 TextureManager::reload_textures()
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);
281 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
283 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
284 i != saved_textures.end(); ++i) {
285 SavedTexture& saved_texture = *i;
288 glGenTextures(1, &handle);
289 assert_gl("creating texture handle");
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");
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);
308 assert_gl("setting texture_params");
309 saved_texture.texture->set_handle(handle);
312 saved_textures.clear();