diff --git a/Vocaluxe/Lib/Draw/CDrawBase.cs b/Vocaluxe/Lib/Draw/CDrawBase.cs index fb1e7088..45f9f388 100644 --- a/Vocaluxe/Lib/Draw/CDrawBase.cs +++ b/Vocaluxe/Lib/Draw/CDrawBase.cs @@ -39,6 +39,7 @@ namespace Vocaluxe.Lib.Draw private int _NextID; protected readonly Dictionary _Textures = new Dictionary(); + protected readonly Dictionary _TextureCache = new Dictionary(); private readonly Queue _TexturesToLoad = new Queue(); protected int _H; @@ -75,6 +76,7 @@ namespace Vocaluxe.Lib.Draw texture.Dispose(); } _Textures.Clear(); + _TextureCache.Clear(); } } @@ -178,6 +180,23 @@ namespace Vocaluxe.Lib.Draw /// A STexture object containing the added texture public CTextureRef AddTexture(string texturePath) { + CTextureRef textureRef; + lock (_Textures) + { + WeakReference reference; + if (_TextureCache.TryGetValue(texturePath, out reference)) + { + if (!reference.IsAlive) + _TextureCache.Remove(texturePath); + else + { + textureRef = (CTextureRef)reference.Target; + textureRef.RefCount++; + return textureRef; + } + } + } + if (!File.Exists(texturePath)) { CLog.LogError("Can't find File: " + texturePath); @@ -193,16 +212,19 @@ namespace Vocaluxe.Lib.Draw CLog.LogError("Error loading Texture: " + texturePath); return null; } - CTextureRef s; try { - s = AddTexture(bmp, texturePath); + textureRef = AddTexture(bmp, texturePath); } finally { bmp.Dispose(); } - return s; + lock (_Textures) + { + _TextureCache[texturePath] = new WeakReference(textureRef); + } + return textureRef; } public CTextureRef AddTexture(Bitmap bmp) @@ -375,15 +397,19 @@ namespace Vocaluxe.Lib.Draw return; lock (_Textures) { - TTextureType t; - if (_Textures.TryGetValue(texture.ID, out t)) + if (--texture.RefCount <= 0) { - if (t != null) - t.Dispose(); - _Textures.Remove(texture.ID); + TTextureType t; + if (_Textures.TryGetValue(texture.ID, out t)) + { + if (t != null) + t.Dispose(); + _Textures.Remove(texture.ID); + } + _TextureCache.Remove(texture.TexturePath); } + texture.ID = -1; } - texture.ID = -1; texture = null; } diff --git a/VocaluxeLib/Draw/CTextureRef.cs b/VocaluxeLib/Draw/CTextureRef.cs index df379ac1..5f2afb33 100644 --- a/VocaluxeLib/Draw/CTextureRef.cs +++ b/VocaluxeLib/Draw/CTextureRef.cs @@ -25,6 +25,7 @@ namespace VocaluxeLib.Draw public class CTextureRef { public int ID; + public int RefCount = 1; public string TexturePath = "";