Original Post
I just coded an abstract D3DObject which holding any d3d resource
The device is globle and the class implements interface for Dispose.
if you call the object''s Dispose, you should also remove the event handlers to the device. If not, some time device will call these event handlers such as when device''s Reset function is called.
Here is the code. Hope to improve the class.
public abstract class UserD3DObject : System.IDisposable
{
protected Microsoft.DirectX .Direct3D .Device d3dDevice = DXManager.D3DDevice ;
private bool disposed = false;
public UserD3DObject ()
{
d3dDevice.D3DDevice .DeviceReset += new EventHandler(this.RestoreDeviceObjects);
d3dDevice.D3DDevice .DeviceLost += new EventHandler(this.InvalidateDeviceObjects);
d3dDevice.D3DDevice .Disposing += new EventHandler(this.DeleteDeviceObjects);
}
public virtual void Render(Yoda3D.DirectX .D3DControl d3dCtrl)
{
}
protected virtual void Dispose( bool disposing )
{
if(disposing)
{
DXManager.D3DDevice .DeviceReset -= new EventHandler(this.RestoreDeviceObjects);
DXManager.D3DDevice .DeviceLost -= new EventHandler(this.InvalidateDeviceObjects);
DXManager.D3DDevice .Disposing -= new EventHandler(this.DeleteDeviceObjects);
// Dispose managed resources.
//Components.Dispose();
}
disposed = true;
}
public virtual void InitializeDeviceObjects()
{
}
protected virtual void RestoreDeviceObjects(System.Object sender, System.EventArgs e)
{
}
protected virtual void InvalidateDeviceObjects(System.Object sender, System.EventArgs e)
{
}
protected virtual void DeleteDeviceObjects(System.Object sender, System.EventArgs e)
{
}
public void Dispose()
{
if (!this.disposed )
{
Dispose(true);
GC.SuppressFinalize(this);
this.disposed = true;
}
}
~UserD3DObject()
{
Dispose(false);
}
}