Further cleanups to texture caching, from bug 523:
[supertux.git] / src / video / texture_manager.cpp
index a7c32e4..7343a45 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()
 #ifdef HAVE_OPENGL
@@ -41,7 +47,7 @@ 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;
     }
@@ -61,6 +67,7 @@ TextureManager::get(const std::string& _filename)
 
   if(!texture) {
     texture = create_image_texture(filename);
+    texture->cache_filename = filename;
     image_textures[filename] = texture;
   }
 
@@ -75,9 +82,12 @@ TextureManager::get(const std::string& filename, const Rect& 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
@@ -104,8 +114,7 @@ TextureManager::create_image_texture(const std::string& filename, const Rect& re
   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();
   }
 }
 
@@ -143,9 +152,7 @@ TextureManager::create_image_texture_raw(const std::string& filename, const Rect
         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;
+      return VideoSystem::new_texture(subimage.get());
     }
   }
 }
@@ -160,8 +167,7 @@ TextureManager::create_image_texture(const std::string& 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();
   }
 }
 
@@ -177,9 +183,7 @@ TextureManager::create_image_texture_raw(const std::string& filename)
   }
   else
   {
-    TexturePtr result = VideoSystem::new_texture(image.get());
-    result->set_filename(filename);
-    return result;
+    return VideoSystem::new_texture(image.get());
   }
 }
 
@@ -205,10 +209,8 @@ 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;
+      return VideoSystem::new_texture(image.get());
     }
   }
 }