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() :
47 TextureManager::~TextureManager()
49 for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i)
51 if(!i->second.expired())
53 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
56 image_textures.clear();
58 for(auto& surface : surfaces)
60 SDL_FreeSurface(surface.second);
66 TextureManager::get(const std::string& _filename)
68 std::string filename = FileSystem::normalize(_filename);
69 ImageTextures::iterator i = image_textures.find(filename);
72 if(i != image_textures.end())
73 texture = i->second.lock();
76 texture = create_image_texture(filename);
77 texture->cache_filename = filename;
78 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 = image_textures.find(filename);
96 assert(i != image_textures.end());
97 assert(i->second.expired());
98 image_textures.erase(i);
103 TextureManager::register_texture(GLTexture* texture)
105 textures.insert(texture);
109 TextureManager::remove_texture(GLTexture* texture)
111 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 = surfaces.find(filename);
135 if (i != 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 surfaces[filename] = image;
152 SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
153 rect.top * image->pitch +
154 rect.left * image->format->BytesPerPixel,
155 rect.get_width(), rect.get_height(),
156 image->format->BitsPerPixel,
158 image->format->Rmask,
159 image->format->Gmask,
160 image->format->Bmask,
161 image->format->Amask));
164 throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
168 if (image->format->palette)
169 { // copy the image palette to subimage if present
170 SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors);
174 return VideoSystem::new_texture(subimage.get());
178 TextureManager::create_image_texture(const std::string& filename)
182 return create_image_texture_raw(filename);
184 catch (const std::exception& err)
186 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
187 return create_dummy_texture();
192 TextureManager::create_image_texture_raw(const std::string& filename)
194 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
197 std::ostringstream msg;
198 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
199 throw std::runtime_error(msg.str());
203 TexturePtr texture = VideoSystem::new_texture(image.get());
210 TextureManager::create_dummy_texture()
212 const std::string dummy_texture_fname = "images/engine/missing.png";
214 // on error, try loading placeholder file
217 TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
220 catch (const std::exception& err)
222 // on error (when loading placeholder), try using empty surface,
223 // when that fails to, just give up
224 SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
231 log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
232 TexturePtr texture = VideoSystem::new_texture(image.get());
241 TextureManager::save_textures()
243 #ifdef GL_PACK_ROW_LENGTH
244 /* all this stuff is not support by OpenGL ES */
245 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
246 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
247 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
248 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
249 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
252 glPixelStorei(GL_PACK_ALIGNMENT, 1);
253 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
256 for(ImageTextures::iterator i = image_textures.begin();
257 i != image_textures.end(); ++i) {
258 save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
263 TextureManager::save_texture(GLTexture* texture)
265 SavedTexture saved_texture;
266 saved_texture.texture = texture;
267 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
269 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
270 #ifndef GL_VERSION_ES_CM_1_0
271 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
272 &saved_texture.width);
273 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
274 &saved_texture.height);
275 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
276 &saved_texture.border);
277 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
278 &saved_texture.min_filter);
279 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
280 &saved_texture.mag_filter);
281 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
282 &saved_texture.wrap_s);
283 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
284 &saved_texture.wrap_t);
286 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
287 saved_texture.pixels = new char[pixelssize];
289 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
290 saved_texture.pixels);
293 saved_textures.push_back(saved_texture);
295 glDeleteTextures(1, &(texture->get_handle()));
296 texture->set_handle(0);
298 assert_gl("retrieving texture for save");
302 TextureManager::reload_textures()
304 #ifdef GL_UNPACK_ROW_LENGTH
305 /* OpenGL ES doesn't support these */
306 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
307 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
308 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
309 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
310 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
312 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
314 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
315 i != saved_textures.end(); ++i) {
316 SavedTexture& saved_texture = *i;
319 glGenTextures(1, &handle);
320 assert_gl("creating texture handle");
322 glBindTexture(GL_TEXTURE_2D, handle);
323 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
324 saved_texture.width, saved_texture.height,
325 saved_texture.border, GL_RGBA,
326 GL_UNSIGNED_BYTE, saved_texture.pixels);
327 delete[] saved_texture.pixels;
328 assert_gl("uploading texture pixel data");
330 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
331 saved_texture.min_filter);
332 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
333 saved_texture.mag_filter);
334 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
335 saved_texture.wrap_s);
336 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
337 saved_texture.wrap_t);
339 assert_gl("setting texture_params");
340 saved_texture.texture->set_handle(handle);
343 saved_textures.clear();