- Texture::filename was used for only one purpose, so we might as well
make that clear.
- Set the filename in just one place: before a texture is added to the
cache. This is intended to be obviously correct.
- Other minor adjustments.
Thanks to Matt McCutchen for this patch.
git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6294
837edb03-e0f3-0310-88ca-
d4d4e8b29345
-protected:
- std::string filename;
+private:
+ friend class TextureManager;
+ /* The name under which this texture is cached by the texture manager,
+ * or the empty string if not. */
+ std::string cache_filename;
- Texture() : filename() {}
+ Texture() : cache_filename() {}
- if (texture_manager)
- texture_manager->release(this);
+ if (texture_manager && cache_filename != "")
+ /* The cache entry is now useless: its weak pointer to us has been
+ * cleared. Remove the entry altogether to save memory. */
+ texture_manager->reap_cache_entry(cache_filename);
}
virtual unsigned int get_texture_width() const = 0;
}
virtual unsigned int get_texture_width() const = 0;
virtual unsigned int get_image_width() const = 0;
virtual unsigned int get_image_height() const = 0;
virtual unsigned int get_image_width() const = 0;
virtual unsigned int get_image_height() const = 0;
- std::string get_filename() const
- {
- return filename;
- }
-
- void set_filename(std::string filename)
- {
- this->filename = filename;
- }
-
private:
Texture(const Texture&);
Texture& operator=(const Texture&);
private:
Texture(const Texture&);
Texture& operator=(const Texture&);
{
for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i)
{
{
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;
}
{
log_warning << "Texture '" << i->first << "' not freed" << std::endl;
}
if(!texture) {
texture = create_image_texture(filename);
if(!texture) {
texture = create_image_texture(filename);
- image_textures[texture->get_filename()] = texture;
+ texture->cache_filename = filename;
+ image_textures[filename] = texture;
-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);
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;
- TexturePtr texture = create_dummy_texture();
- texture->set_filename(filename);
- return texture;
+ return create_dummy_texture();
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);
}
- TexturePtr result = VideoSystem::new_texture(subimage.get());
- result->set_filename(filename);
- return result;
+ return VideoSystem::new_texture(subimage.get());
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;
- TexturePtr texture = create_dummy_texture();
- texture->set_filename(filename);
- return texture;
+ return create_dummy_texture();
- TexturePtr result = VideoSystem::new_texture(image.get());
- result->set_filename(filename);
- return result;
+ return 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;
log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
+ return VideoSystem::new_texture(image.get());
private:
friend class Texture;
private:
friend class Texture;
- void release(Texture* texture);
+ void reap_cache_entry(const std::string& filename);
typedef std::map<std::string, boost::weak_ptr<Texture> > ImageTextures;
ImageTextures image_textures;
typedef std::map<std::string, boost::weak_ptr<Texture> > ImageTextures;
ImageTextures image_textures;