SteelBugs said:Please help: How to Reset Device in Full screen application??i had spent alot of time with that
thanks :-\
Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Try
'If mActive Then
If Not game Is Nothing Then
game.PlayFrame()
End If
Me.Invalidate()
'End If
Catch ex As DirectXException
Console.WriteLine("DX frmMain_Paint(): " + ex.ErrorString)
Catch ex As Exception
Console.WriteLine("frmMain_Paint(): " + ex.Message)
End Try
End Sub
Public Sub PlayFrame()
gfx.Render()
If mGameOver Then
Close()
End If
Application.DoEvents()
End Sub
Public Sub Render()
Try
If Not mDevice Is Nothing Then
If Not ValidateDevice() Then
Exit Sub
End If
mDevice.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0F, 0)
mDevice.BeginScene()
mDevice.EndScene()
If Not mDevice Is Nothing Then
mDevice.Present()
End If
'End If
End If
Catch ex As Direct3DXException
Console.WriteLine("DX Graphics.Render(): " + ex.ToString())
Catch ex As Exception
Console.WriteLine("Graphics.Render(): " + ex.ToString())
End Try
End Sub
Public Function ValidateDevice() As Boolean
Dim deviceState As Int32
'Do
If Not mDevice.CheckCooperativeLevel(deviceState) Then
If deviceState = ResultCode.DeviceLost Then
Console.WriteLine("Graphics.ValidateDevice(): Device LOST ")
Return False
End If
If deviceState = ResultCode.DeviceNotReset Then
Try
mDevice.Reset(mPresentParams)
Catch ex As InvalidCallException
Console.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) InvalidCallException ")
Return False
Catch ex As DeviceLostException
Console.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) DeviceLostException ")
Return False
Catch ex As DriverInternalErrorException
Console.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) DriverInternalErrorException ")
Return False
Catch ex As OutOfVideoMemoryException
Console.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) OutOfVideoMemoryException")
Return False
Catch ex As OutOfMemoryException
Console.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) OutOfMemoryException ")
Return False
Finally
mfrm.Close()
End Try
End If
End If
Return True
End Function
Aragorn7 said:Set the device = nothing when the application regains focus. Once you destroy the device, then you need to reinitialize it (and all of the textures too).
' *********************
' (f) Oleh Novosad
' *********************
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.Direct3D.D3DX
Imports System.Diagnostics
Imports System.Threading
Public Class Graphics
Implements IDisposable
Public mDevice As Device
Public mPresentParams As PresentParameters
Public mDm As DisplayMode
Public mfrm As Form
Public Sub New(ByVal frm As System.Windows.Forms.Form)
' INITIALIZE GRAPHICS
Try
mfrm = frm
mDm = Manager.Adapters.Default.CurrentDisplayMode
mPresentParams = New PresentParameters
mPresentParams.Windowed = False
mPresentParams.SwapEffect = SwapEffect.Discard
mPresentParams.BackBufferFormat = Format.A8R8G8B8
mPresentParams.AutoDepthStencilFormat = DepthFormat.D16
mPresentParams.EnableAutoDepthStencil = True
mPresentParams.FullScreenRefreshRateInHz = mDm.RefreshRate
mPresentParams.BackBufferHeight = mDm.Height
mPresentParams.BackBufferWidth = mDm.Width
mPresentParams.SwapEffect = SwapEffect.Discard
mPresentParams.PresentationInterval = PresentInterval.Immediate
Dim adapterOrdinal As Integer = Manager.Adapters.Default.Adapter
Dim flags As CreateFlags = CreateFlags.SoftwareVertexProcessing
Dim caps As Caps = Manager.GetDeviceCaps(adapterOrdinal, DeviceType.Hardware)
If caps.DeviceCaps.SupportsHardwareTransformAndLight Then
' Replace the software vertex processing
flags = CreateFlags.HardwareVertexProcessing
End If
If caps.DeviceCaps.SupportsPureDevice Then
flags = flags Or CreateFlags.PureDevice
End If
mDevice = New Device(0, DeviceType.Hardware, frm, flags, mPresentParams)
AddHandler mDevice.DeviceReset, AddressOf DeviceReset
DeviceReset(mDevice, Nothing)
AddHandler mDevice.DeviceResizing, AddressOf DeviceResizing
Catch ex As DirectXException
Debug.WriteLine("Graphics.New(): " + ex.ErrorString)
Catch ex As Exception
Debug.WriteLine("Graphics.New(): " + ex.Message)
End Try
End Sub
Public Sub DeviceReset(ByVal obj As Object, ByVal e As EventArgs)
' For future
'Dim dev As Device = CType(obj, Device)
' PUT HERE YOUR TEXTURE RELOADING
'LoadTextures(dev)
End Sub
Public Sub Render()
Try
If Not mDevice Is Nothing Then
If Not ValidateDevice() Then
Debug.WriteLine("Graphics.Render(): Unable to VALIDATE")
Exit Sub
End If
mDevice.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0F, 0)
mDevice.BeginScene()
mDevice.EndScene()
If Not mDevice Is Nothing Then
mDevice.Present()
End If
End If
Catch ex As DeviceLostException
Debug.WriteLine("Graphics.Render(): catched DeviceLostException")
ValidateDevice()
Catch ex As Direct3DXException
Debug.WriteLine("DX Graphics.Render(): " + ex.ToString())
Catch ex As Exception
Debug.WriteLine("Graphics.Render(): " + ex.ToString())
End Try
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
'Free up mem
If Not mDevice Is Nothing Then
mDevice.Dispose()
mDevice = Nothing
End If
mDm = Nothing
mPresentParams = Nothing
End Sub
Public Function ValidateDevice() As Boolean
' VALIDATE DEVICE FUNCTION
Try
mDevice.TestCooperativeLevel()
Catch ex As DeviceLostException
Return False
Catch ex As DeviceNotResetException
Try
Debug.WriteLine("WE TRY TO RESET DEVICE")
mDevice.Reset(mPresentParams)
Debug.WriteLine("DEVICE RESETED")
Catch ex1 As InvalidCallException
Debug.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) InvalidCallException ")
Return False
Catch ex1 As DeviceLostException
Debug.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) DeviceLostException ")
Return False
Catch ex1 As DriverInternalErrorException
Debug.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) DriverInternalErrorException ")
Return False
Catch ex1 As OutOfVideoMemoryException
Debug.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) OutOfVideoMemoryException")
Return False
Catch ex1 As OutOfMemoryException
Debug.WriteLine("Graphics.ValidateDevice(): mDevice.Reset(mPresentParams) OutOfMemoryException ")
Return False
End Try
End Try
Return True
End Function
Public Sub DeviceResizing(ByVal obj As Object, ByVal e As System.ComponentModel.CancelEventArgs)
'Debug.WriteLine("Cancel default DeviceResizing")
e.Cancel = True [B]' CANCEL DEFFAULT DEVICE RESETING FOR FULL SCREEN APP[/B]
End Sub
End Class
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
mActive = True
End Sub
Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Try
'If mActive Then
If Not game Is Nothing Then
game.PlayFrame()
End If
Me.Invalidate()
'End If
Catch ex As DirectXException
Console.WriteLine("DX frmMain_Paint(): " + ex.ErrorString)
Catch ex As Exception
Console.WriteLine("frmMain_Paint(): " + ex.Message)
End Try
End Sub
Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
mActive = False
game.Dispose()
End Sub
Public Class ThisGame
Implements IDisposable
Public mGameOver As Boolean = False
Public mFrm As System.Windows.Forms.Form
Public Sub New(ByVal frm As System.Windows.Forms.Form)
'******************
'* Init LIB vars
gfx = New Graphics(frm) ' gfx is a public variable of Vars module
' (everywhere can access it)
mFrm = frm
End Sub
Public Sub PlayFrame()
Try
gfx.Render()
If mGameOver Then
Close()
End If
Application.DoEvents()
Catch ex As DeviceLostException
Debug.WriteLine("ThisGame.PlayFrame(): Try to validate")
gfx.ValidateDevice()
End Try
End Sub
Public Sub Close()
mFrm.Close()
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
gfx.Dispose()
Application.Exit()
System.Environment.Exit(System.Environment.ExitCode) 'FORCE an exit if it didnt exit
End Sub
End Class