How to Reset Device in Full screen application??

SteelBugs

Newcomer
Joined
Sep 27, 2004
Messages
3
Please help: How to Reset Device in Full screen application??i had spent alot of time with that
thanks :-\
 
SteelBugs said:
Please help: How to Reset Device in Full screen application??i had spent alot of time with that
thanks :-\

My PAINT sub looks like that:
Code:
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

My PlayFrame() looks like that:
Code:
Public Sub PlayFrame()

        gfx.Render()

        If mGameOver Then
            Close()
        End If

        Application.DoEvents()

    End Sub

my Render() looks like that :)
Code:
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

my ValidateDavice() looks like that:
Code:
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

What is wrong? When app is running and I press Alt+Tab, then return to app, i see error
Unhandled Exception: Error in the application.
-2146232832 (Unknown)
at Microsoft.DirectX.Direct3D.Device.Reset(PresentParameters presentationParameters)
at Microsoft.DirectX.Direct3D.Device.OnParentResized(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.Form.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WmMove(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)
at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SteelBugs.MainClass.Main() in D:\Commander\SteelBugs\MainClass.vb:line 12The program '[2112] SteelBugs.exe' has exited with code 0 (0x0).

HELP Please :confused:
 
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).
 
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).

Thanks a lot Aragorn7, but it doesnt seems to me the fully right way.
Finally I did it
My Graphics class now looks like that:
Code:
' *********************
' (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

And form's code looks like that:
Code:
    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


And my Game Class code:

Code:
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

Now it works as I wanted. I can tab between applications, return to game
and continue to play. :D
The only problem this game do nothing for now :p (just set and reset device :) )
 
Back
Top