X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fvideo%2Ftexture_manager.cpp;h=e7c392f6f581e50dddf0f30dcd638afb66b27532;hb=dfaf28adf2756d68091c7e54ab16fcb174dfcc5c;hp=5cb962be56f1f171a997b9d9f01a607df6da1caf;hpb=d78fbc721f773f410a373ca1f060dfe223cf45d2;p=supertux.git diff --git a/src/video/texture_manager.cpp b/src/video/texture_manager.cpp index 5cb962be5..e7c392f6f 100644 --- a/src/video/texture_manager.cpp +++ b/src/video/texture_manager.cpp @@ -17,137 +17,221 @@ #include "video/texture_manager.hpp" #include +#include #include +#include +#include +#include "math/rect.hpp" #include "physfs/physfs_sdl.hpp" #include "util/file_system.hpp" #include "util/log.hpp" +#include "video/sdl_surface_ptr.hpp" +#include "video/texture.hpp" +#include "video/video_system.hpp" + +#ifdef HAVE_OPENGL #include "video/gl/gl_texture.hpp" -#include "video/video_systems.hpp" +#endif TextureManager::TextureManager() : - image_textures() + m_image_textures() + ,m_surfaces() #ifdef HAVE_OPENGL - ,textures(), - saved_textures() + ,m_textures(), + m_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 = m_image_textures.begin(); i != m_image_textures.end(); ++i) + { + if(!i->second.expired()) + { + log_warning << "Texture '" << i->first << "' not freed" << std::endl; + } + } + m_image_textures.clear(); + + for(auto& surface : m_surfaces) + { + SDL_FreeSurface(surface.second); } + m_surfaces.clear(); } -Texture* +TexturePtr TextureManager::get(const std::string& _filename) { std::string filename = FileSystem::normalize(_filename); - ImageTextures::iterator i = image_textures.find(filename); + ImageTextures::iterator i = m_image_textures.find(filename); - Texture* texture = NULL; - if(i != image_textures.end()) - texture = i->second; + TexturePtr texture; + if(i != m_image_textures.end()) + texture = i->second.lock(); - if(texture == NULL) { + if(!texture) { texture = create_image_texture(filename); - image_textures[filename] = texture; + texture->cache_filename = filename; + m_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 = m_image_textures.find(filename); + assert(i != m_image_textures.end()); + assert(i->second.expired()); + m_image_textures.erase(i); } #ifdef HAVE_OPENGL void TextureManager::register_texture(GLTexture* texture) { - textures.insert(texture); + m_textures.insert(texture); } void TextureManager::remove_texture(GLTexture* texture) { - textures.erase(texture); + m_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) { - try { + 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 = nullptr; - SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1); - if(image == 0) { + Surfaces::iterator i = m_surfaces.find(filename); + if (i != m_surfaces.end()) + { + image = i->second; + } + else + { + 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 = VideoSystem::new_texture(image); - result->set_filename(filename); - } catch(...) { - delete result; - SDL_FreeSurface(image); - throw; - } - - SDL_FreeSurface(image); - return result; - - } catch (const std::runtime_error& err) { - const std::string dummy_texture_fname = "images/engine/missing.png"; - if (filename == dummy_texture_fname) throw err; - - // on error, try loading placeholder file - try { + m_surfaces[filename] = image; + } - Texture* tex = create_image_texture(dummy_texture_fname); - log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl; - return tex; + 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"); + } - } catch (...) { +#ifdef OLD_SDL + if (image->format->palette) + { // copy the image palette to subimage if present + SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors); + } +#endif - // on error (when loading placeholder), try using empty surface - try { + return VideoSystem::current()->new_texture(subimage.get()); +} - SDL_Surface* image = SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0); - if(image == 0) { - throw err; - } +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(); + } +} - Texture* result = 0; - try { - result = VideoSystem::new_texture(image); - result->set_filename("-dummy-texture-.png"); - } catch(...) { - delete result; - SDL_FreeSurface(image); - throw err; - } +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 + { + TexturePtr texture = VideoSystem::current()->new_texture(image.get()); + image.reset(NULL); + return texture; + } +} - SDL_FreeSurface(image); - log_warning << "Couldn't load texture '" << filename << "' (now using empty one): " << err.what() << std::endl; - return result; +TexturePtr +TextureManager::create_dummy_texture() +{ + const std::string dummy_texture_fname = "images/engine/missing.png"; - // on error (when trying to use empty surface), give up - } catch (const std::runtime_error& err) { - throw err; - } + // 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; + } + else + { + log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl; + TexturePtr texture = VideoSystem::current()->new_texture(image.get()); + image.reset(NULL); + return texture; } } } @@ -156,7 +240,7 @@ TextureManager::create_image_texture(const std::string& filename) void TextureManager::save_textures() { -#ifdef GL_PACK_ROW_LENGTH +#if defined(GL_PACK_ROW_LENGTH) || defined(USE_GLBINDING) /* all this stuff is not support by OpenGL ES */ glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0); @@ -166,12 +250,16 @@ TextureManager::save_textures() #endif glPixelStorei(GL_PACK_ALIGNMENT, 1); - for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) { + + for(Textures::iterator i = m_textures.begin(); i != m_textures.end(); ++i) + { save_texture(*i); } - for(ImageTextures::iterator i = image_textures.begin(); - i != image_textures.end(); ++i) { - save_texture(dynamic_cast(i->second)); + + for(ImageTextures::iterator i = m_image_textures.begin(); + i != m_image_textures.end(); ++i) + { + save_texture(dynamic_cast(i->second.lock().get())); } } @@ -206,7 +294,7 @@ TextureManager::save_texture(GLTexture* texture) saved_texture.pixels); #endif - saved_textures.push_back(saved_texture); + m_saved_textures.push_back(saved_texture); glDeleteTextures(1, &(texture->get_handle())); texture->set_handle(0); @@ -217,7 +305,7 @@ TextureManager::save_texture(GLTexture* texture) void TextureManager::reload_textures() { -#ifdef GL_UNPACK_ROW_LENGTH +#if defined(GL_UNPACK_ROW_LENGTH) || defined(USE_GLBINDING) /* OpenGL ES doesn't support these */ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0); @@ -227,8 +315,8 @@ TextureManager::reload_textures() #endif glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - for(std::vector::iterator i = saved_textures.begin(); - i != saved_textures.end(); ++i) { + for(std::vector::iterator i = m_saved_textures.begin(); + i != m_saved_textures.end(); ++i) { SavedTexture& saved_texture = *i; GLuint handle; @@ -236,7 +324,7 @@ TextureManager::reload_textures() assert_gl("creating texture handle"); glBindTexture(GL_TEXTURE_2D, handle); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + glTexImage2D(GL_TEXTURE_2D, 0, static_cast(GL_RGBA), saved_texture.width, saved_texture.height, saved_texture.border, GL_RGBA, GL_UNSIGNED_BYTE, saved_texture.pixels); @@ -256,7 +344,7 @@ TextureManager::reload_textures() saved_texture.texture->set_handle(handle); } - saved_textures.clear(); + m_saved_textures.clear(); } #endif