I'm using SlimDX and Direct3D9 classes to create a C# game engine.
I started with it one year ago and I remember that I successfully and safely managed to catch lost devices and reset them (on Windows 7). Now (on Windows 8) I started working on it again, but it looks like I'm not catching lost devices in any case anymore, especially in Ctrl+Alt+Del cases.
This is the previously working code: The "IsDeviceLost" method erroneously returns false at Ctrl+Alt+Del, so the device is about to present, but it crashes to a Direct3D9Exception: "D3DERR_DEVICELOST":
public void Update(float time)
{
if (!IsDeviceLost(true))
{
_device.Present();
}
}
private bool IsDeviceLost(bool resetIfNeeded)
{
bool deviceLost = false;
// Check if DeviceLost is detected
Result result = _device.TestCooperativeLevel();
if (result == ResultCode.DeviceLost)
{
Log.Write(LogType.Warning, "Direct3D9Manager: Device lost and cannot be reset yet.");
// Device has been lost and cannot be reset yet
deviceLost = true;
}
else if (result == ResultCode.DeviceNotReset)
{
Log.Write(LogType.Information, "Direct3D9Manager: ResultCode.DeviceNotReset");
// Device is available again but has not yet been reset
if (resetIfNeeded)
{
// Reset device and check if it can work again
_device.Reset(_presentParams);
deviceLost = IsDeviceLost(false);
if (deviceLost)
{
// Reset failed, device still lost
}
else
{
// Reset successful, device restored
// TODO: Reload textures and render states which are lost after a reset
}
}
else
{
deviceLost = true;
}
}
return deviceLost;
}
So I researched the web about this problem and found several code, which puts the Update code into the try part of a try-catch block, but I'm not sure if that is the right way to fix this.
- Isn't try-catch slow for an Update method in a game engine which gets called every frame?
- Aren't there better ways to catch a lost device, which are not using try-catch?