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() :
46 TextureManager::~TextureManager()
48 for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i)
52 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
55 image_textures.clear();
59 TextureManager::get(const std::string& _filename)
61 std::string filename = FileSystem::normalize(_filename);
62 ImageTextures::iterator i = image_textures.find(filename);
65 if(i != image_textures.end())
66 texture = i->second.lock();
69 texture = create_image_texture(filename);
70 image_textures[filename] = texture;
77 TextureManager::get(const std::string& filename, const Rect& rect)
79 // FIXME: implement caching
80 return create_image_texture(filename, rect);
84 TextureManager::release(Texture* texture)
86 image_textures.erase(texture->get_filename());
91 TextureManager::register_texture(GLTexture* texture)
93 textures.insert(texture);
97 TextureManager::remove_texture(GLTexture* texture)
99 textures.erase(texture);
104 TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
108 return create_image_texture_raw(filename, rect);
110 catch(const std::exception& err)
112 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
113 TexturePtr texture = create_dummy_texture();
119 TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
121 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
124 std::ostringstream msg;
125 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
126 throw std::runtime_error(msg.str());
130 SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
131 rect.top * image->pitch +
132 rect.left * image->format->BytesPerPixel,
134 rect.get_width(), rect.get_height(),
135 image->format->BitsPerPixel,
137 image->format->Rmask,
138 image->format->Gmask,
139 image->format->Bmask,
140 image->format->Amask));
143 throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
147 if (image->format->palette)
148 { // copy the image palette to subimage if present
149 SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
152 TexturePtr result = VideoSystem::new_texture(subimage.get());
153 result->set_filename(filename);
160 TextureManager::create_image_texture(const std::string& filename)
164 return create_image_texture_raw(filename);
166 catch (const std::exception& err)
168 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
169 TexturePtr texture = create_dummy_texture();
175 TextureManager::create_image_texture_raw(const std::string& filename)
177 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
180 std::ostringstream msg;
181 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
182 throw std::runtime_error(msg.str());
186 TexturePtr result = VideoSystem::new_texture(image.get());
187 result->set_filename(filename);
193 TextureManager::create_dummy_texture()
195 const std::string dummy_texture_fname = "images/engine/missing.png";
197 // on error, try loading placeholder file
200 TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
203 catch (const std::exception& err)
205 // on error (when loading placeholder), try using empty surface,
206 // when that fails to, just give up
207 SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
214 TexturePtr result = VideoSystem::new_texture(image.get());
215 result->set_filename("-dummy-texture-.png");
216 log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
224 TextureManager::save_textures()
226 #ifdef GL_PACK_ROW_LENGTH
227 /* all this stuff is not support by OpenGL ES */
228 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
229 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
230 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
231 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
232 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
235 glPixelStorei(GL_PACK_ALIGNMENT, 1);
236 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
239 for(ImageTextures::iterator i = image_textures.begin();
240 i != image_textures.end(); ++i) {
241 save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
246 TextureManager::save_texture(GLTexture* texture)
248 SavedTexture saved_texture;
249 saved_texture.texture = texture;
250 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
252 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
253 #ifndef GL_VERSION_ES_CM_1_0
254 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
255 &saved_texture.width);
256 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
257 &saved_texture.height);
258 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
259 &saved_texture.border);
260 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
261 &saved_texture.min_filter);
262 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
263 &saved_texture.mag_filter);
264 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
265 &saved_texture.wrap_s);
266 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
267 &saved_texture.wrap_t);
269 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
270 saved_texture.pixels = new char[pixelssize];
272 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
273 saved_texture.pixels);
276 saved_textures.push_back(saved_texture);
278 glDeleteTextures(1, &(texture->get_handle()));
279 texture->set_handle(0);
281 assert_gl("retrieving texture for save");
285 TextureManager::reload_textures()
287 #ifdef GL_UNPACK_ROW_LENGTH
288 /* OpenGL ES doesn't support these */
289 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
290 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
291 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
292 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
293 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
295 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
297 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
298 i != saved_textures.end(); ++i) {
299 SavedTexture& saved_texture = *i;
302 glGenTextures(1, &handle);
303 assert_gl("creating texture handle");
305 glBindTexture(GL_TEXTURE_2D, handle);
306 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
307 saved_texture.width, saved_texture.height,
308 saved_texture.border, GL_RGBA,
309 GL_UNSIGNED_BYTE, saved_texture.pixels);
310 delete[] saved_texture.pixels;
311 assert_gl("uploading texture pixel data");
313 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
314 saved_texture.min_filter);
315 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
316 saved_texture.mag_filter);
317 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
318 saved_texture.wrap_s);
319 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
320 saved_texture.wrap_t);
322 assert_gl("setting texture_params");
323 saved_texture.texture->set_handle(handle);
326 saved_textures.clear();