TextureManager Class Reference

#include <texture_manager.hpp>

List of all members.

Public Member Functions

 TextureManager ()
 ~TextureManager ()
TexturePtr get (const std::string &filename)
TexturePtr get (const std::string &filename, const Rect &rect)

Private Types

typedef std::map< std::string,
boost::weak_ptr< Texture > > 
ImageTextures

Private Member Functions

void reap_cache_entry (const std::string &filename)
TexturePtr create_image_texture (const std::string &filename, const Rect &rect)
TexturePtr create_image_texture (const std::string &filename)
 on failure a dummy texture is returned and no exception is thrown
TexturePtr create_image_texture_raw (const std::string &filename)
 throw an exception on error
TexturePtr create_image_texture_raw (const std::string &filename, const Rect &rect)
TexturePtr create_dummy_texture ()

Private Attributes

ImageTextures image_textures

Friends

class Texture


Detailed Description

Definition at line 35 of file texture_manager.hpp.


Member Typedef Documentation

typedef std::map<std::string, boost::weak_ptr<Texture> > TextureManager::ImageTextures [private]

Definition at line 56 of file texture_manager.hpp.


Constructor & Destructor Documentation

TextureManager::TextureManager (  ) 

Definition at line 37 of file texture_manager.cpp.

00037                                :
00038   image_textures()
00039 #ifdef HAVE_OPENGL
00040   ,textures(),
00041   saved_textures()
00042 #endif
00043 {
00044 }

TextureManager::~TextureManager (  ) 

Definition at line 46 of file texture_manager.cpp.

References image_textures, and log_warning.

00047 {
00048   for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i)
00049   {
00050     if(!i->second.expired())
00051     {
00052       log_warning << "Texture '" << i->first << "' not freed" << std::endl;
00053     }
00054   }
00055   image_textures.clear();
00056 }


Member Function Documentation

TexturePtr TextureManager::get ( const std::string &  filename  ) 

Definition at line 59 of file texture_manager.cpp.

References create_image_texture(), image_textures, and FileSystem::normalize().

00060 {
00061   std::string filename = FileSystem::normalize(_filename);
00062   ImageTextures::iterator i = image_textures.find(filename);
00063 
00064   TexturePtr texture;
00065   if(i != image_textures.end())
00066     texture = i->second.lock();
00067 
00068   if(!texture) {
00069     texture = create_image_texture(filename);
00070     texture->cache_filename = filename;
00071     image_textures[filename] = texture;
00072   }
00073 
00074   return texture;
00075 }

TexturePtr TextureManager::get ( const std::string &  filename,
const Rect rect 
)

Definition at line 78 of file texture_manager.cpp.

References create_image_texture().

00079 {
00080   // FIXME: implement caching
00081   return create_image_texture(filename, rect);
00082 }

void TextureManager::reap_cache_entry ( const std::string &  filename  )  [private]

Definition at line 85 of file texture_manager.cpp.

References image_textures.

Referenced by Texture::~Texture().

00086 {
00087   ImageTextures::iterator i = image_textures.find(filename);
00088   assert(i != image_textures.end());
00089   assert(i->second.expired());
00090   image_textures.erase(i);
00091 }

TexturePtr TextureManager::create_image_texture ( const std::string &  filename,
const Rect rect 
) [private]

Definition at line 108 of file texture_manager.cpp.

References create_dummy_texture(), create_image_texture_raw(), and log_warning.

Referenced by get().

00109 {
00110   try 
00111   {
00112     return create_image_texture_raw(filename, rect);
00113   }
00114   catch(const std::exception& err)
00115   {
00116     log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
00117     return create_dummy_texture();
00118   }
00119 }

TexturePtr TextureManager::create_image_texture ( const std::string &  filename  )  [private]

on failure a dummy texture is returned and no exception is thrown

Definition at line 161 of file texture_manager.cpp.

References create_dummy_texture(), create_image_texture_raw(), and log_warning.

00162 {
00163   try 
00164   {
00165     return create_image_texture_raw(filename);
00166   }
00167   catch (const std::exception& err)
00168   {
00169     log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
00170     return create_dummy_texture();
00171   }
00172 }

TexturePtr TextureManager::create_image_texture_raw ( const std::string &  filename  )  [private]

throw an exception on error

Definition at line 175 of file texture_manager.cpp.

References SDLSurfacePtr::get(), get_physfs_SDLRWops(), and VideoSystem::new_texture().

Referenced by create_dummy_texture(), and create_image_texture().

00176 {
00177   SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
00178   if (!image) 
00179   {
00180     std::ostringstream msg;
00181     msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
00182     throw std::runtime_error(msg.str());
00183   }
00184   else
00185   {
00186     return VideoSystem::new_texture(image.get());
00187   }
00188 }

TexturePtr TextureManager::create_image_texture_raw ( const std::string &  filename,
const Rect rect 
) [private]

Definition at line 122 of file texture_manager.cpp.

References SDLSurfacePtr::get(), Rect::get_height(), get_physfs_SDLRWops(), Rect::get_width(), Rect::left, VideoSystem::new_texture(), and Rect::top.

00123 {
00124   SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
00125   if (!image)
00126   {
00127     std::ostringstream msg;
00128     msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
00129     throw std::runtime_error(msg.str());
00130   }
00131   else
00132   {
00133     SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) + 
00134                                                     rect.top * image->pitch + 
00135                                                     rect.left * image->format->BytesPerPixel, 
00136 
00137                                                     rect.get_width(), rect.get_height(),
00138                                                     image->format->BitsPerPixel,
00139                                                     image->pitch,
00140                                                     image->format->Rmask,
00141                                                     image->format->Gmask,
00142                                                     image->format->Bmask,
00143                                                     image->format->Amask));
00144     if (!subimage)
00145     {
00146       throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
00147     }
00148     else
00149     {
00150       if (image->format->palette)
00151       { // copy the image palette to subimage if present
00152         SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
00153       }
00154 
00155       return VideoSystem::new_texture(subimage.get());
00156     }
00157   }
00158 }

TexturePtr TextureManager::create_dummy_texture (  )  [private]

Definition at line 191 of file texture_manager.cpp.

References create_image_texture_raw(), SDLSurfacePtr::get(), log_warning, and VideoSystem::new_texture().

Referenced by create_image_texture().

00192 {
00193   const std::string dummy_texture_fname = "images/engine/missing.png";
00194  
00195   // on error, try loading placeholder file
00196   try 
00197   {
00198     TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
00199     return tex;
00200   }
00201   catch (const std::exception& err) 
00202   {
00203     // on error (when loading placeholder), try using empty surface,
00204     // when that fails to, just give up
00205     SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
00206     if (!image)
00207     {
00208       throw err;
00209     }
00210     else
00211     {
00212       log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
00213       return VideoSystem::new_texture(image.get());
00214     }
00215   }
00216 }


Friends And Related Function Documentation

friend class Texture [friend]

Definition at line 53 of file texture_manager.hpp.


Member Data Documentation

ImageTextures TextureManager::image_textures [private]

Definition at line 57 of file texture_manager.hpp.

Referenced by get(), reap_cache_entry(), and ~TextureManager().


The documentation for this class was generated from the following files:
Generated on Mon Apr 14 03:38:46 2014 for SuperTux by  doxygen 1.5.1