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();
60 TextureManager::get(const std::string& _filename)
62 std::string filename = FileSystem::normalize(_filename);
63 ImageTextures::iterator i = image_textures.find(filename);
66 if(i != image_textures.end())
67 texture = i->second.lock();
70 texture = create_image_texture(filename);
71 texture->cache_filename = filename;
72 image_textures[filename] = texture;
79 TextureManager::get(const std::string& _filename, const Rect& rect)
81 std::string filename = FileSystem::normalize(_filename);
82 // FIXME: implement caching
83 return create_image_texture(filename, rect);
87 TextureManager::reap_cache_entry(const std::string& filename)
89 ImageTextures::iterator i = image_textures.find(filename);
90 assert(i != image_textures.end());
91 assert(i->second.expired());
92 image_textures.erase(i);
97 TextureManager::register_texture(GLTexture* texture)
99 textures.insert(texture);
103 TextureManager::remove_texture(GLTexture* texture)
105 textures.erase(texture);
110 TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
114 return create_image_texture_raw(filename, rect);
116 catch(const std::exception& err)
118 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
119 return create_dummy_texture();
124 TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
126 SDL_Surface *image = NULL;
128 Surfaces::iterator i = surfaces.find(filename);
129 if (i != surfaces.end())
133 image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
138 std::ostringstream msg;
139 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
140 throw std::runtime_error(msg.str());
143 surfaces[filename] = image;
145 SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
146 rect.top * image->pitch +
147 rect.left * image->format->BytesPerPixel,
149 rect.get_width(), rect.get_height(),
150 image->format->BitsPerPixel,
152 image->format->Rmask,
153 image->format->Gmask,
154 image->format->Bmask,
155 image->format->Amask));
158 throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
161 /* if (image->format->palette)
162 { // copy the image palette to subimage if present
163 SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors); //edited by giby
166 return VideoSystem::new_texture(subimage.get());
170 TextureManager::create_image_texture(const std::string& filename)
174 return create_image_texture_raw(filename);
176 catch (const std::exception& err)
178 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
179 return create_dummy_texture();
184 TextureManager::create_image_texture_raw(const std::string& filename)
186 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
189 std::ostringstream msg;
190 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
191 throw std::runtime_error(msg.str());
195 return VideoSystem::new_texture(image.get());
200 TextureManager::create_dummy_texture()
202 const std::string dummy_texture_fname = "images/engine/missing.png";
204 // on error, try loading placeholder file
207 TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
210 catch (const std::exception& err)
212 // on error (when loading placeholder), try using empty surface,
213 // when that fails to, just give up
214 SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
221 log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
222 return VideoSystem::new_texture(image.get());
229 TextureManager::save_textures()
231 #ifdef GL_PACK_ROW_LENGTH
232 /* all this stuff is not support by OpenGL ES */
233 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
234 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
235 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
236 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
237 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
240 glPixelStorei(GL_PACK_ALIGNMENT, 1);
241 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
244 for(ImageTextures::iterator i = image_textures.begin();
245 i != image_textures.end(); ++i) {
246 save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
251 TextureManager::save_texture(GLTexture* texture)
253 SavedTexture saved_texture;
254 saved_texture.texture = texture;
255 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
257 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
258 #ifndef GL_VERSION_ES_CM_1_0
259 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
260 &saved_texture.width);
261 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
262 &saved_texture.height);
263 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
264 &saved_texture.border);
265 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
266 &saved_texture.min_filter);
267 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
268 &saved_texture.mag_filter);
269 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
270 &saved_texture.wrap_s);
271 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
272 &saved_texture.wrap_t);
274 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
275 saved_texture.pixels = new char[pixelssize];
277 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
278 saved_texture.pixels);
281 saved_textures.push_back(saved_texture);
283 glDeleteTextures(1, &(texture->get_handle()));
284 texture->set_handle(0);
286 assert_gl("retrieving texture for save");
290 TextureManager::reload_textures()
292 #ifdef GL_UNPACK_ROW_LENGTH
293 /* OpenGL ES doesn't support these */
294 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
295 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
296 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
297 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
298 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
300 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
302 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
303 i != saved_textures.end(); ++i) {
304 SavedTexture& saved_texture = *i;
307 glGenTextures(1, &handle);
308 assert_gl("creating texture handle");
310 glBindTexture(GL_TEXTURE_2D, handle);
311 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
312 saved_texture.width, saved_texture.height,
313 saved_texture.border, GL_RGBA,
314 GL_UNSIGNED_BYTE, saved_texture.pixels);
315 delete[] saved_texture.pixels;
316 assert_gl("uploading texture pixel data");
318 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
319 saved_texture.min_filter);
320 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
321 saved_texture.mag_filter);
322 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
323 saved_texture.wrap_s);
324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
325 saved_texture.wrap_t);
327 assert_gl("setting texture_params");
328 saved_texture.texture->set_handle(handle);
331 saved_textures.clear();