projects
/
supertux.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Further cleanups to texture caching, from bug 523:
[supertux.git]
/
src
/
video
/
texture_manager.cpp
diff --git
a/src/video/texture_manager.cpp
b/src/video/texture_manager.cpp
index
3831132
..
7343a45
100644
(file)
--- a/
src/video/texture_manager.cpp
+++ b/
src/video/texture_manager.cpp
@@
-19,15
+19,21
@@
#include <SDL_image.h>
#include <assert.h>
#include <iostream>
#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 "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/sdl_surface_ptr.hpp"
+#include "video/texture.hpp"
#include "video/video_systems.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() :
image_textures()
#ifdef HAVE_OPENGL
@@
-39,34
+45,36
@@
TextureManager::TextureManager() :
TextureManager::~TextureManager()
{
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.expired()
)
+ {
+
log_warning << "Texture '" << i->first << "' not freed" << std::endl;
+ }
}
}
+ image_textures.clear();
}
}
-Texture
*
+Texture
Ptr
TextureManager::get(const std::string& _filename)
{
std::string filename = FileSystem::normalize(_filename);
ImageTextures::iterator i = image_textures.find(filename);
TextureManager::get(const std::string& _filename)
{
std::string filename = FileSystem::normalize(_filename);
ImageTextures::iterator i = image_textures.find(filename);
- Texture
* texture = NULL
;
+ Texture
Ptr texture
;
if(i != image_textures.end())
if(i != image_textures.end())
- texture = i->second;
+ texture = i->second
.lock()
;
- if(
texture == NULL
) {
+ if(
!texture
) {
texture = create_image_texture(filename);
texture = create_image_texture(filename);
+ texture->cache_filename = filename;
image_textures[filename] = texture;
}
return texture;
}
image_textures[filename] = texture;
}
return texture;
}
-Texture
*
+Texture
Ptr
TextureManager::get(const std::string& filename, const Rect& rect)
{
// FIXME: implement caching
TextureManager::get(const std::string& filename, const Rect& rect)
{
// FIXME: implement caching
@@
-74,10
+82,12
@@
TextureManager::get(const std::string& filename, const Rect& rect)
}
void
}
void
-TextureManager::re
lease(Texture* textur
e)
+TextureManager::re
ap_cache_entry(const std::string& filenam
e)
{
{
- image_textures.erase(texture->get_filename());
- delete texture;
+ ImageTextures::iterator i = image_textures.find(filename);
+ assert(i != image_textures.end());
+ assert(i->second.expired());
+ image_textures.erase(i);
}
#ifdef HAVE_OPENGL
}
#ifdef HAVE_OPENGL
@@
-94,7
+104,7
@@
TextureManager::remove_texture(GLTexture* texture)
}
#endif
}
#endif
-Texture
*
+Texture
Ptr
TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
{
try
TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
{
try
@@
-104,12
+114,11
@@
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;
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();
- return texture;
+ return create_dummy_texture();
}
}
}
}
-Texture
*
+Texture
Ptr
TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
{
SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
{
SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
@@
-143,14
+152,12
@@
TextureManager::create_image_texture_raw(const std::string& filename, const Rect
SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
}
SDL_SetColors(subimage.get(), image->format->palette->colors, 0, image->format->palette->ncolors);
}
- Texture* result = VideoSystem::new_texture(subimage.get());
- result->set_filename(filename);
- return result;
+ return VideoSystem::new_texture(subimage.get());
}
}
}
}
}
}
-Texture
*
+Texture
Ptr
TextureManager::create_image_texture(const std::string& filename)
{
try
TextureManager::create_image_texture(const std::string& filename)
{
try
@@
-160,12
+167,11
@@
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;
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();
- return texture;
+ return create_dummy_texture();
}
}
}
}
-Texture
*
+Texture
Ptr
TextureManager::create_image_texture_raw(const std::string& filename)
{
SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
TextureManager::create_image_texture_raw(const std::string& filename)
{
SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
@@
-177,13
+183,11
@@
TextureManager::create_image_texture_raw(const std::string& filename)
}
else
{
}
else
{
- Texture* result = VideoSystem::new_texture(image.get());
- result->set_filename(filename);
- return result;
+ return VideoSystem::new_texture(image.get());
}
}
}
}
-Texture
*
+Texture
Ptr
TextureManager::create_dummy_texture()
{
const std::string dummy_texture_fname = "images/engine/missing.png";
TextureManager::create_dummy_texture()
{
const std::string dummy_texture_fname = "images/engine/missing.png";
@@
-191,7
+195,7
@@
TextureManager::create_dummy_texture()
// on error, try loading placeholder file
try
{
// on error, try loading placeholder file
try
{
- Texture
*
tex = create_image_texture_raw(dummy_texture_fname);
+ Texture
Ptr
tex = create_image_texture_raw(dummy_texture_fname);
return tex;
}
catch (const std::exception& err)
return tex;
}
catch (const std::exception& err)
@@
-205,10
+209,8
@@
TextureManager::create_dummy_texture()
}
else
{
}
else
{
- Texture* 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;
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())
;
}
}
}
}
}
}
@@
-232,7
+234,7
@@
TextureManager::save_textures()
}
for(ImageTextures::iterator i = image_textures.begin();
i != image_textures.end(); ++i) {
}
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()
));
}
}
}
}