You can't use screen invalid() as a way to recover from an invalid render device in DBP. This simply isn't possible to do safely. This command is misleading and does not provide information you can rely upon for this kind of event.
For example:
do
if screen invalid()
gosub fixmatrix
endif
` <- Render device invalidates here
sync ` <- DBP crashes here
loop
The only way to do this is to use another solution like PureGDK which supports D3D callbacks so that your recovery code will be called BEFORE execution is resumed.
Here is an example using PureGDK and PureBasic:
XIncludeFile "..\..\include\h.InitPureGDK.pb"
XIncludeFile "..\..\include\puregdk\core\h.PureGDK.pb"
If Not InitPureGDK("gdkengine.dll", "..\..\..\..\")
End
EndIf
;/ Show the PureGDK render window
OpenWindow(0, 0, 0, 640, 480, "DarkBasic Professional - PureGDK", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
dbOpenScreen(WindowID(0), 0, 0, 640, 480)
Procedure D3DDeviceLost()
;/ Backup project data here
dbDeleteMatrix(1)
MessageRequester("Info", "D3DDeviceLost", #MB_TOPMOST)
EndProcedure
Procedure D3DDeviceReset()
;/ Reload project data here
dbColorBackdrop(RGB(Random(255), Random(255), Random(255)))
dbCreateMatrix(600, 600, 24, 24, 1)
dbUpdateMatrix(1)
MessageRequester("Info", "D3DDeviceReset", #MB_TOPMOST)
EndProcedure
dbSetD3DDeviceCallback(@D3DDeviceLost(), #GDK_Callback_DeviceLost)
dbSetD3DDeviceCallback(@D3DDeviceReset(), #GDK_Callback_DeviceReset)
dbColorBackdrop(RGB(0,0,0))
dbCreateMatrix(600, 600, 24, 24, 1)
dbUpdateMatrix(1)
Repeat: Delay(1)
dbSync()
Until WindowEvent()=#PB_Event_CloseWindow
End
Here is an compiled example:
http://puregdk.com/files/upload/d3d-callbacks-matrix.zip