X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fvideo%2Ftexture_manager.cpp;h=b64fcee3ac67a24eea4f90eacd8f3d1aff7ce327;hb=40ddc3f1e77c473f7f96ba32c30d0cf584ee941e;hp=d9ff2a74be2ee1c1c1af3fac2184980033117ff5;hpb=d8aaa588d7a774d8eb81861367f84b1a34512457;p=supertux.git diff --git a/src/video/texture_manager.cpp b/src/video/texture_manager.cpp index d9ff2a74b..b64fcee3a 100644 --- a/src/video/texture_manager.cpp +++ b/src/video/texture_manager.cpp @@ -1,12 +1,10 @@ -// $Id$ -// // SuperTux // Copyright (C) 2006 Matthias Braun // -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,133 +12,250 @@ // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -#include +// along with this program. If not, see . -#include "texture_manager.hpp" +#include "video/texture_manager.hpp" -#include -#include #include +#include #include #include #include + +#include "math/rect.hpp" #include "physfs/physfs_sdl.hpp" -#include "video_systems.hpp" -#include "gl_texture.hpp" -#include "glutil.hpp" -#include "gameconfig.hpp" -#include "file_system.hpp" -#include "log.hpp" -#include "texture.hpp" +#include "util/file_system.hpp" +#include "util/log.hpp" +#include "video/sdl_surface_ptr.hpp" +#include "video/texture.hpp" +#include "video/video_systems.hpp" -TextureManager* texture_manager = NULL; +#ifdef HAVE_OPENGL +#include "video/gl/gl_texture.hpp" +#endif -TextureManager::TextureManager() +TextureManager::TextureManager() : + image_textures() + ,surfaces() +#ifdef HAVE_OPENGL + ,textures(), + saved_textures() +#endif { } TextureManager::~TextureManager() { - for(ImageTextures::iterator i = image_textures.begin(); - i != image_textures.end(); ++i) { - if(i->second == NULL) - continue; - log_warning << "Texture '" << i->first << "' not freed" << std::endl; - delete i->second; + for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i) + { + if(!i->second.expired()) + { + log_warning << "Texture '" << i->first << "' not freed" << std::endl; + } } + image_textures.clear(); } -Texture* +TexturePtr TextureManager::get(const std::string& _filename) { std::string filename = FileSystem::normalize(_filename); ImageTextures::iterator i = image_textures.find(filename); - Texture* texture = NULL; + TexturePtr texture; if(i != image_textures.end()) - texture = i->second; + texture = i->second.lock(); - if(texture == NULL) { + if(!texture) { texture = create_image_texture(filename); + texture->cache_filename = filename; image_textures[filename] = texture; } return texture; } +TexturePtr +TextureManager::get(const std::string& _filename, const Rect& rect) +{ + std::string filename = FileSystem::normalize(_filename); + // FIXME: implement caching + return create_image_texture(filename, rect); +} + void -TextureManager::release(Texture* texture) +TextureManager::reap_cache_entry(const std::string& filename) { - image_textures.erase(texture->get_filename()); - delete texture; + ImageTextures::iterator i = image_textures.find(filename); + assert(i != image_textures.end()); + assert(i->second.expired()); + image_textures.erase(i); } #ifdef HAVE_OPENGL void -TextureManager::register_texture(GL::Texture* texture) +TextureManager::register_texture(GLTexture* texture) { textures.insert(texture); } void -TextureManager::remove_texture(GL::Texture* texture) +TextureManager::remove_texture(GLTexture* texture) { textures.erase(texture); } #endif -Texture* -TextureManager::create_image_texture(const std::string& filename) +TexturePtr +TextureManager::create_image_texture(const std::string& filename, const Rect& rect) { - SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1); - if(image == 0) { + try + { + return create_image_texture_raw(filename, rect); + } + catch(const std::exception& err) + { + log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl; + return create_dummy_texture(); + } +} + +TexturePtr +TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect) +{ + SDL_Surface *image = NULL; + + Surfaces::iterator i = surfaces.find(filename); + if (i != surfaces.end()) + image = i->second; + + if (!image) { + image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1); + } + + if (!image) + { std::ostringstream msg; msg << "Couldn't load image '" << filename << "' :" << SDL_GetError(); throw std::runtime_error(msg.str()); } - Texture* result = 0; - try { - result = new_texture(image); - result->set_filename(filename); - } catch(...) { - delete result; - SDL_FreeSurface(image); - throw; + surfaces[filename] = image; + + SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast(image->pixels) + + rect.top * image->pitch + + rect.left * image->format->BytesPerPixel, + + rect.get_width(), rect.get_height(), + image->format->BitsPerPixel, + image->pitch, + image->format->Rmask, + image->format->Gmask, + image->format->Bmask, + image->format->Amask)); + if (!subimage) + { + throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed"); + } + + /* if (image->format->palette) + { // copy the image palette to subimage if present + SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors); //edited by giby + } */ + + return VideoSystem::new_texture(subimage.get()); +} + +TexturePtr +TextureManager::create_image_texture(const std::string& filename) +{ + try + { + return create_image_texture_raw(filename); + } + catch (const std::exception& err) + { + log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl; + return create_dummy_texture(); } +} - SDL_FreeSurface(image); - return result; +TexturePtr +TextureManager::create_image_texture_raw(const std::string& filename) +{ + SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1)); + if (!image) + { + std::ostringstream msg; + msg << "Couldn't load image '" << filename << "' :" << SDL_GetError(); + throw std::runtime_error(msg.str()); + } + else + { + return VideoSystem::new_texture(image.get()); + } +} + +TexturePtr +TextureManager::create_dummy_texture() +{ + const std::string dummy_texture_fname = "images/engine/missing.png"; + + // on error, try loading placeholder file + try + { + TexturePtr tex = create_image_texture_raw(dummy_texture_fname); + return tex; + } + catch (const std::exception& err) + { + // on error (when loading placeholder), try using empty surface, + // when that fails to, just give up + SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0)); + if (!image) + { + throw err; + } + else + { + log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl; + return VideoSystem::new_texture(image.get()); + } + } } #ifdef HAVE_OPENGL void TextureManager::save_textures() { +#ifdef GL_PACK_ROW_LENGTH + /* all this stuff is not support by OpenGL ES */ glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0); glPixelStorei(GL_PACK_SKIP_PIXELS, 0); glPixelStorei(GL_PACK_SKIP_ROWS, 0); glPixelStorei(GL_PACK_SKIP_IMAGES, 0); +#endif + glPixelStorei(GL_PACK_ALIGNMENT, 1); for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) { save_texture(*i); } for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i) { - save_texture(dynamic_cast(i->second)); + save_texture(dynamic_cast(i->second.lock().get())); } } void -TextureManager::save_texture(GL::Texture* texture) +TextureManager::save_texture(GLTexture* texture) { SavedTexture saved_texture; saved_texture.texture = texture; glBindTexture(GL_TEXTURE_2D, texture->get_handle()); + + //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway) +#ifndef GL_VERSION_ES_CM_1_0 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &saved_texture.width); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, @@ -161,6 +276,7 @@ TextureManager::save_texture(GL::Texture* texture) glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, saved_texture.pixels); +#endif saved_textures.push_back(saved_texture); @@ -173,11 +289,14 @@ TextureManager::save_texture(GL::Texture* texture) void TextureManager::reload_textures() { +#ifdef GL_UNPACK_ROW_LENGTH + /* OpenGL ES doesn't support these */ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0); +#endif glPixelStorei(GL_UNPACK_ALIGNMENT, 1); for(std::vector::iterator i = saved_textures.begin(); @@ -212,3 +331,5 @@ TextureManager::reload_textures() saved_textures.clear(); } #endif + +/* EOF */