MoonWorks/src/Graphics/GraphicsResource.cs

53 lines
1.4 KiB
C#
Raw Normal View History

using System;
namespace MoonWorks.Graphics
{
public abstract class GraphicsResource : IDisposable
{
public GraphicsDevice Device { get; }
public IntPtr Handle { get; protected set; }
public bool IsDisposed { get; private set; }
2022-02-19 05:02:16 +00:00
protected abstract Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction { get; }
2021-01-29 02:01:42 +00:00
private WeakReference<GraphicsResource> selfReference;
public GraphicsResource(GraphicsDevice device)
{
Device = device;
2021-01-29 02:01:42 +00:00
selfReference = new WeakReference<GraphicsResource>(this);
Device.AddResourceReference(selfReference);
}
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
2022-02-19 05:02:16 +00:00
Device.PrepareDestroyResource(this, QueueDestroyFunction);
if (selfReference != null)
{
Device.RemoveResourceReference(selfReference);
selfReference = null;
}
IsDisposed = true;
}
}
~GraphicsResource()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}