Results 1 to 4 of 4

Thread: [RESOLVED] XNA ViewPort - Device reset fails on resize

  1. #1

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Resolved [RESOLVED] XNA ViewPort - Device reset fails on resize

    As an introduction to XNA, I'm trying out the ViewPort code Jenner kindly posted in this thread (post #11) on the VB.Net forum.

    When I resize the viewport (e.g. by resizing the parent form on which the viewport is anchored to 3 sides) the device reset fails with an "Unknown Error". It doesn't necessarily happen the first time the Resized event fires but generally within three times. I can't see anything in the stack trace that explains the problem; everything looks as expected up to the reset. The reset code, which is called from the control's Resize or SizeChanged event, is as follows:

    Code:
       Private Sub ResetGraphicsDevice()
            If gDevice Is Nothing OrElse Me.Width = 0 OrElse Me.Height = 0 Then Return
                gDevice.PresentationParameters.BackBufferWidth = Me.Width
                gDevice.PresentationParameters.BackBufferHeight = Me.Height
            Try
                gDevice.Reset()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub
    Any ideas?

    BB

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: XNA ViewPort - Device reset fails on resize

    Ahh... I see the issue. When the graphics Device is resetting due to the resize-event, you need to suspend drawing to it. This is super easy because I'm already using a Monitor to control draw execution. You can use the same Monitor to reset the device and halt drawing.

    Change the "ResetGraphicsDevice" sub to the following:
    Code:
        Private Sub ResetGraphicsDevice()
            If gDevice Is Nothing OrElse Me.Width = 0 OrElse Me.Height = 0 Then Return
            If Threading.Monitor.TryEnter(monitorObject) Then
                Try
                    gDevice.PresentationParameters.BackBufferWidth = Me.Width
                    gDevice.PresentationParameters.BackBufferHeight = Me.Height
                    gDevice.Reset()
                Finally
                    Threading.Monitor.Exit(monitorObject)
                End Try
              
            End If
    
        End Sub
    Now, when it's drawing, it won't call reset. When it's resetting, it won't call drawing. Whichever routine has the monitorObject locked down is the one with exclusive control over it. Basically, if it's in the middle of a draw, and you try to resize it, thus reset it, it will not happen until the program is out of the draw loop. When it does get a chance to reset itself, the draw routine is skipped until it's done.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: XNA ViewPort - Device reset fails on resize

    That does the trick. It's also a nice illustration of the use of a Monitor which may prove useful in other situations. I can't rate you because I already did so recently, but many thanks and plaudits.

    I think I am going to have a clutch of other questions to ask about this XNA example, but I'll save them for another thread. BB

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] XNA ViewPort - Device reset fails on resize

    Thanks much! You know where to find me.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width