#include <SDL_image.h>
#include <assert.h>
#include <iostream>
+#include <sstream>
+#include <stdexcept>
+#include "math/rect.hpp"
#include "physfs/physfs_sdl.hpp"
#include "util/file_system.hpp"
#include "util/log.hpp"
-#include "video/gl/gl_texture.hpp"
#include "video/sdl_surface_ptr.hpp"
+#include "video/texture.hpp"
#include "video/video_systems.hpp"
+#ifdef HAVE_OPENGL
+#include "video/gl/gl_texture.hpp"
+#endif
+
TextureManager::TextureManager() :
image_textures()
#ifdef HAVE_OPENGL
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.lock())
+ {
+ 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);
- image_textures[filename] = texture;
+ image_textures[texture->get_filename()] = texture;
}
return texture;
}
-Texture*
+TexturePtr
TextureManager::get(const std::string& filename, const Rect& rect)
{
- return 0;
+ // FIXME: implement caching
+ return create_image_texture(filename, rect);
}
void
TextureManager::release(Texture* texture)
{
image_textures.erase(texture->get_filename());
- delete texture;
}
#ifdef HAVE_OPENGL
}
#endif
-Texture*
+TexturePtr
TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
{
- assert(!"not implemented");
- return 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;
+ TexturePtr texture = create_dummy_texture();
+ texture->set_filename(filename);
+ return texture;
+ }
+}
+
+TexturePtr
+TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
+{
+ 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
+ {
+ SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(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");
+ }
+ else
+ {
+ if (image->format->palette)
+ { // copy the image palette to subimage if present
+ SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
+ }
+
+ TexturePtr result = VideoSystem::new_texture(subimage.get());
+ result->set_filename(filename);
+ return result;
+ }
+ }
}
-Texture*
+TexturePtr
TextureManager::create_image_texture(const std::string& filename)
{
try
catch (const std::exception& err)
{
log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
- Texture* texture = create_dummy_texture();
+ TexturePtr texture = create_dummy_texture();
+ texture->set_filename(filename);
return texture;
}
}
-Texture*
+TexturePtr
TextureManager::create_image_texture_raw(const std::string& filename)
{
SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
}
else
{
- Texture* result = VideoSystem::new_texture(image.get());
+ TexturePtr result = VideoSystem::new_texture(image.get());
result->set_filename(filename);
return result;
}
}
-Texture*
+TexturePtr
TextureManager::create_dummy_texture()
{
const std::string dummy_texture_fname = "images/engine/missing.png";
// on error, try loading placeholder file
try
{
- Texture* tex = create_image_texture_raw(dummy_texture_fname);
+ TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
return tex;
}
catch (const std::exception& err)
}
else
{
- Texture* result = VideoSystem::new_texture(image.get());
+ TexturePtr result = VideoSystem::new_texture(image.get());
result->set_filename("-dummy-texture-.png");
log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
return result;
}
for(ImageTextures::iterator i = image_textures.begin();
i != image_textures.end(); ++i) {
- save_texture(dynamic_cast<GLTexture *>(i->second));
+ save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
}
}