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_system.hpp"
34 #include "video/gl/gl_texture.hpp"
37 TextureManager::TextureManager() :
47 TextureManager::~TextureManager()
49 for(ImageTextures::iterator i = m_image_textures.begin(); i != m_image_textures.end(); ++i)
51 if(!i->second.expired())
53 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
56 m_image_textures.clear();
58 for(auto& surface : m_surfaces)
60 SDL_FreeSurface(surface.second);
66 TextureManager::get(const std::string& _filename)
68 std::string filename = FileSystem::normalize(_filename);
69 ImageTextures::iterator i = m_image_textures.find(filename);
72 if(i != m_image_textures.end())
73 texture = i->second.lock();
76 texture = create_image_texture(filename);
77 texture->cache_filename = filename;
78 m_image_textures[filename] = texture;
85 TextureManager::get(const std::string& _filename, const Rect& rect)
87 std::string filename = FileSystem::normalize(_filename);
88 // FIXME: implement caching
89 return create_image_texture(filename, rect);
93 TextureManager::reap_cache_entry(const std::string& filename)
95 ImageTextures::iterator i = m_image_textures.find(filename);
96 assert(i != m_image_textures.end());
97 assert(i->second.expired());
98 m_image_textures.erase(i);
103 TextureManager::register_texture(GLTexture* texture)
105 m_textures.insert(texture);
109 TextureManager::remove_texture(GLTexture* texture)
111 m_textures.erase(texture);
116 TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
120 return create_image_texture_raw(filename, rect);
122 catch(const std::exception& err)
124 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
125 return create_dummy_texture();
130 TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
132 SDL_Surface *image = nullptr;
134 Surfaces::iterator i = m_surfaces.find(filename);
135 if (i != m_surfaces.end())
141 image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
144 std::ostringstream msg;
145 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
146 throw std::runtime_error(msg.str());
149 m_surfaces[filename] = image;
152 SDL_PixelFormat* format = image->format;
153 if(format->Rmask == 0 && format->Gmask == 0 && format->Bmask == 0 && format->Amask == 0) {
154 log_debug << "Wrong surface format for image " << filename << ". Compensating." << std::endl;
155 image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_RGBA8888, 0);
158 SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
159 rect.top * image->pitch +
160 rect.left * image->format->BytesPerPixel,
161 rect.get_width(), rect.get_height(),
162 image->format->BitsPerPixel,
164 image->format->Rmask,
165 image->format->Gmask,
166 image->format->Bmask,
167 image->format->Amask));
170 throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
174 if (image->format->palette)
175 { // copy the image palette to subimage if present
176 SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors);
180 return VideoSystem::current()->new_texture(subimage.get());
184 TextureManager::create_image_texture(const std::string& filename)
188 return create_image_texture_raw(filename);
190 catch (const std::exception& err)
192 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
193 return create_dummy_texture();
198 TextureManager::create_image_texture_raw(const std::string& filename)
200 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
203 std::ostringstream msg;
204 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
205 throw std::runtime_error(msg.str());
209 TexturePtr texture = VideoSystem::current()->new_texture(image.get());
216 TextureManager::create_dummy_texture()
218 const std::string dummy_texture_fname = "images/engine/missing.png";
220 // on error, try loading placeholder file
223 TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
226 catch (const std::exception& err)
228 // on error (when loading placeholder), try using empty surface,
229 // when that fails to, just give up
230 SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
237 log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
238 TexturePtr texture = VideoSystem::current()->new_texture(image.get());
247 TextureManager::save_textures()
249 #if defined(GL_PACK_ROW_LENGTH) || defined(USE_GLBINDING)
250 /* all this stuff is not support by OpenGL ES */
251 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
252 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
253 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
254 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
255 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
258 glPixelStorei(GL_PACK_ALIGNMENT, 1);
260 for(Textures::iterator i = m_textures.begin(); i != m_textures.end(); ++i)
265 for(ImageTextures::iterator i = m_image_textures.begin();
266 i != m_image_textures.end(); ++i)
268 GLTexture* texture = dynamic_cast<GLTexture*>(i->second.lock().get());
272 save_texture(texture);
277 TextureManager::save_texture(GLTexture* texture)
279 SavedTexture saved_texture;
280 saved_texture.texture = texture;
281 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
283 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
284 #ifndef GL_VERSION_ES_CM_1_0
285 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
286 &saved_texture.width);
287 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
288 &saved_texture.height);
289 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
290 &saved_texture.border);
291 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
292 &saved_texture.min_filter);
293 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
294 &saved_texture.mag_filter);
295 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
296 &saved_texture.wrap_s);
297 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
298 &saved_texture.wrap_t);
300 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
301 saved_texture.pixels = new char[pixelssize];
303 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
304 saved_texture.pixels);
307 m_saved_textures.push_back(saved_texture);
309 glDeleteTextures(1, &(texture->get_handle()));
310 texture->set_handle(0);
312 assert_gl("retrieving texture for save");
316 TextureManager::reload_textures()
318 #if defined(GL_UNPACK_ROW_LENGTH) || defined(USE_GLBINDING)
319 /* OpenGL ES doesn't support these */
320 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
321 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
322 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
323 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
324 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
326 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
328 for(std::vector<SavedTexture>::iterator i = m_saved_textures.begin();
329 i != m_saved_textures.end(); ++i) {
330 SavedTexture& saved_texture = *i;
333 glGenTextures(1, &handle);
334 assert_gl("creating texture handle");
336 glBindTexture(GL_TEXTURE_2D, handle);
337 glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(GL_RGBA),
338 saved_texture.width, saved_texture.height,
339 saved_texture.border, GL_RGBA,
340 GL_UNSIGNED_BYTE, saved_texture.pixels);
341 delete[] saved_texture.pixels;
342 assert_gl("uploading texture pixel data");
344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
345 saved_texture.min_filter);
346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
347 saved_texture.mag_filter);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
349 saved_texture.wrap_s);
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
351 saved_texture.wrap_t);
353 assert_gl("setting texture_params");
354 saved_texture.texture->set_handle(handle);
357 m_saved_textures.clear();