Nerfed/Nerfed.Runtime/Gui/TextureStorage.cs

35 lines
872 B
C#
Raw Normal View History

using Nerfed.Runtime.Graphics;
namespace Nerfed.Runtime.Gui;
public class TextureStorage
{
private readonly Dictionary<IntPtr, WeakReference<Texture>> pointerToTexture = new Dictionary<IntPtr, WeakReference<Texture>>();
public IntPtr Add(Texture texture)
{
if (!pointerToTexture.ContainsKey(texture.Handle))
{
pointerToTexture.Add(texture.Handle, new WeakReference<Texture>(texture));
}
return texture.Handle;
}
public Texture GetTexture(IntPtr pointer)
{
if (!pointerToTexture.ContainsKey(pointer))
{
return null;
}
WeakReference<Texture> result = pointerToTexture[pointer];
if (!result.TryGetTarget(out Texture texture))
{
pointerToTexture.Remove(pointer);
return null;
}
return texture;
}
}