MoonWorks/src/Graphics/GraphicsResource.cs

55 lines
1.2 KiB
C#
Raw Normal View History

using System;
namespace MoonWorks.Graphics
{
2022-02-23 05:14:32 +00:00
public abstract class GraphicsResource : IDisposable
{
public GraphicsDevice Device { get; }
public IntPtr Handle { get; protected set; }
2022-02-23 05:14:32 +00:00
public bool IsDisposed { get; private set; }
2022-03-04 01:16:39 +00:00
protected abstract Action<IntPtr, IntPtr> QueueDestroyFunction { get; }
2022-02-23 05:14:32 +00:00
private WeakReference<GraphicsResource> selfReference;
2022-03-02 07:21:42 +00:00
public GraphicsResource(GraphicsDevice device, bool trackResource = true)
2022-02-23 05:14:32 +00:00
{
Device = device;
2022-03-02 07:21:42 +00:00
if (trackResource)
{
selfReference = new WeakReference<GraphicsResource>(this);
Device.AddResourceReference(selfReference);
}
2022-02-23 05:14:32 +00:00
}
2022-02-23 05:14:32 +00:00
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (selfReference != null)
{
2022-03-04 01:16:39 +00:00
QueueDestroyFunction(Device.Handle, Handle);
2022-02-23 05:14:32 +00:00
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);
}
}
}