Moved all duplicated drawing code from GLLightmap and GLRenderer into new GLPainter...
[supertux.git] / src / video / texture_manager.cpp
index a7c32e4..7d7830e 100644 (file)
 #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()
+  ,surfaces()
 #ifdef HAVE_OPENGL
   ,textures(),
   saved_textures()
@@ -41,12 +48,18 @@ TextureManager::~TextureManager()
 {
   for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i)
   {
-    if(i->second.lock())
+    if(!i->second.expired())
     {
       log_warning << "Texture '" << i->first << "' not freed" << std::endl;
     }
   }
   image_textures.clear();
+
+  for(auto& surface : surfaces)
+  {
+    SDL_FreeSurface(surface.second);
+  }
+  surfaces.clear();
 }
 
 TexturePtr
@@ -61,6 +74,7 @@ TextureManager::get(const std::string& _filename)
 
   if(!texture) {
     texture = create_image_texture(filename);
+    texture->cache_filename = filename;
     image_textures[filename] = texture;
   }
 
@@ -68,16 +82,20 @@ TextureManager::get(const std::string& _filename)
 }
 
 TexturePtr
-TextureManager::get(const std::string& filename, const Rect& rect)
+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());
+  ImageTextures::iterator i = image_textures.find(filename);
+  assert(i != image_textures.end());
+  assert(i->second.expired());
+  image_textures.erase(i);
 }
 
 #ifdef HAVE_OPENGL
@@ -97,71 +115,76 @@ TextureManager::remove_texture(GLTexture* texture)
 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;
-    TexturePtr texture = create_dummy_texture();
-    return texture;
+    return create_dummy_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)
+  SDL_Surface *image = nullptr;
+
+  Surfaces::iterator i = surfaces.find(filename);
+  if (i != surfaces.end())
   {
-    std::ostringstream msg;
-    msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
-    throw std::runtime_error(msg.str());
+    image = i->second;
   }
   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
+    image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
+    if (!image)
     {
-      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;
+      std::ostringstream msg;
+      msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
+      throw std::runtime_error(msg.str());
     }
+
+    surfaces[filename] = image;
   }
+
+  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");
+  }
+
+#ifdef OLD_SDL
+  if (image->format->palette)
+  { // copy the image palette to subimage if present
+    SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors);
+  }
+#endif
+
+  return VideoSystem::new_texture(subimage.get());
 }
 
 TexturePtr
 TextureManager::create_image_texture(const std::string& filename)
 {
-  try 
+  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;
-    TexturePtr texture = create_dummy_texture();
-    return texture;
+    return create_dummy_texture();
   }
 }
 
@@ -169,7 +192,7 @@ TexturePtr
 TextureManager::create_image_texture_raw(const std::string& filename)
 {
   SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
-  if (!image) 
+  if (!image)
   {
     std::ostringstream msg;
     msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
@@ -177,9 +200,9 @@ TextureManager::create_image_texture_raw(const std::string& filename)
   }
   else
   {
-    TexturePtr result = VideoSystem::new_texture(image.get());
-    result->set_filename(filename);
-    return result;
+    TexturePtr texture = VideoSystem::new_texture(image.get());
+    image.reset(NULL);
+    return texture;
   }
 }
 
@@ -187,14 +210,14 @@ TexturePtr
 TextureManager::create_dummy_texture()
 {
   const std::string dummy_texture_fname = "images/engine/missing.png";
+
   // on error, try loading placeholder file
-  try 
+  try
   {
     TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
     return tex;
   }
-  catch (const std::exception& err) 
+  catch (const std::exception& err)
   {
     // on error (when loading placeholder), try using empty surface,
     // when that fails to, just give up
@@ -205,10 +228,10 @@ TextureManager::create_dummy_texture()
     }
     else
     {
-      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;
+      TexturePtr texture = VideoSystem::new_texture(image.get());
+      image.reset(NULL);
+      return texture;
     }
   }
 }