Add cache for loaded bitmaps (reduces amount of duplicate textures)
This commit is contained in:
1 parent
37aa8e5219
commit
3075006799
2 files changed
+36
-9
No files matched your search
@@ -39,6 +39,7 @@ namespace Vocaluxe.Lib.Draw
|
|||||||
private int _NextID;
|
private int _NextID;
|
||||||
|
|
||||||
protected readonly Dictionary<int, TTextureType> _Textures = new Dictionary<int, TTextureType>();
|
protected readonly Dictionary<int, TTextureType> _Textures = new Dictionary<int, TTextureType>();
|
||||||
|
protected readonly Dictionary<string, WeakReference> _TextureCache = new Dictionary<string, WeakReference>();
|
||||||
private readonly Queue<STextureQueue> _TexturesToLoad = new Queue<STextureQueue>();
|
private readonly Queue<STextureQueue> _TexturesToLoad = new Queue<STextureQueue>();
|
||||||
|
|
||||||
protected int _H;
|
protected int _H;
|
||||||
@@ -75,6 +76,7 @@ namespace Vocaluxe.Lib.Draw
|
|||||||
texture.Dispose();
|
texture.Dispose();
|
||||||
}
|
}
|
||||||
_Textures.Clear();
|
_Textures.Clear();
|
||||||
|
_TextureCache.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,6 +180,23 @@ namespace Vocaluxe.Lib.Draw
|
|||||||
/// <returns>A STexture object containing the added texture</returns>
|
/// <returns>A STexture object containing the added texture</returns>
|
||||||
public CTextureRef AddTexture(string texturePath)
|
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))
|
if (!File.Exists(texturePath))
|
||||||
{
|
{
|
||||||
CLog.LogError("Can't find File: " + texturePath);
|
CLog.LogError("Can't find File: " + texturePath);
|
||||||
@@ -193,16 +212,19 @@ namespace Vocaluxe.Lib.Draw
|
|||||||
CLog.LogError("Error loading Texture: " + texturePath);
|
CLog.LogError("Error loading Texture: " + texturePath);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
CTextureRef s;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
s = AddTexture(bmp, texturePath);
|
textureRef = AddTexture(bmp, texturePath);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
bmp.Dispose();
|
bmp.Dispose();
|
||||||
}
|
}
|
||||||
return s;
|
lock (_Textures)
|
||||||
|
{
|
||||||
|
_TextureCache[texturePath] = new WeakReference(textureRef);
|
||||||
|
}
|
||||||
|
return textureRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CTextureRef AddTexture(Bitmap bmp)
|
public CTextureRef AddTexture(Bitmap bmp)
|
||||||
@@ -375,15 +397,19 @@ namespace Vocaluxe.Lib.Draw
|
|||||||
return;
|
return;
|
||||||
lock (_Textures)
|
lock (_Textures)
|
||||||
{
|
{
|
||||||
TTextureType t;
|
if (--texture.RefCount <= 0)
|
||||||
if (_Textures.TryGetValue(texture.ID, out t))
|
|
||||||
{
|
{
|
||||||
if (t != null)
|
TTextureType t;
|
||||||
t.Dispose();
|
if (_Textures.TryGetValue(texture.ID, out t))
|
||||||
_Textures.Remove(texture.ID);
|
{
|
||||||
|
if (t != null)
|
||||||
|
t.Dispose();
|
||||||
|
_Textures.Remove(texture.ID);
|
||||||
|
}
|
||||||
|
_TextureCache.Remove(texture.TexturePath);
|
||||||
}
|
}
|
||||||
|
texture.ID = -1;
|
||||||
}
|
}
|
||||||
texture.ID = -1;
|
|
||||||
texture = null;
|
texture = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ namespace VocaluxeLib.Draw
|
|||||||
public class CTextureRef
|
public class CTextureRef
|
||||||
{
|
{
|
||||||
public int ID;
|
public int ID;
|
||||||
|
public int RefCount = 1;
|
||||||
|
|
||||||
public string TexturePath = "";
|
public string TexturePath = "";
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user