Results 1 to 8 of 8

Thread: XNA - Game Loop

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,378

    XNA - Game Loop

    I've been looking at some tutorials on youtube and I noticed something that they all have in common. For the game loop they run an infinate loop calling the proper graphicsdevice methods, but also calling application.doevents eg:
    Code:
            'grafix is my graphicsdevice
            Do While True = True
                grafix.Clear(Color.CornflowerBlue)
                grafix.Present()
    
                Application.DoEvents()
            Loop
    I know for a fact that calling application.doevents in an infinate loop is dangerous. So my question is, what should I do instead of that?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: XNA - Game Loop

    My guess is, that this is not XNA-related. You're most likely looking for a way to include XNA elements into VB.Net (ie. XNA in a windows control), right?
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,378

    Re: XNA - Game Loop

    Yeah, I don't have the game studio template so I'm using XNA with a windows form application. So I'm adding my references and then importing the xna.framework namespaces in the code.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: XNA - Game Loop

    Quote Originally Posted by dday9 View Post
    Yeah, I don't have the game studio template so I'm using XNA with a windows form application. So I'm adding my references and then importing the xna.framework namespaces in the code.
    There are several excellent examples on exactly how to do that online, all of which are in C# though, I'm afraid.
    If converting one from C# to VB is terribly tricky for you, you could ask Shaggy Hiker who has done just that not too long ago.
    I tried to use some XNA-code in a windows-control as well, but failed due to the inability of my copy of VB.Net to create an empty content-project (which was needed for my XNA code to work). If you're using VB Express 2010 and XNA 4.0, I'd advice you to abandon the project and try out pure XNA for VB in the free 2010 Express for Windows Phone. Or you could switch to C# (which for some reason has every available template, whereas those templates cannot be neither used nor exported to VB).
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: XNA - Game Loop

    A few months back, I was playing about with using XNA to run a game in a PictureBox on a Form and was following the youtube tutorial here. You won't be able to follow it unless you have XNA Game Studio 4.0 Refresh installed along with VB 2010 from the full Visual Studio 2010, or the free Visual Studio 2010 Express for Windows Phone (bundled in with the Windows Phone SDK) as mentioned by TJ above (sadly, you won't get the required templates to install in the standard version of VB2010 Express).

    The code the chap presents seems to work quite well, but as you mentioned, he's using Application.Doevents in his game loop. However towards the end of his second tutorial, he mentions some drawbacks and hints at some possible solutions; one of which is multithreading. Which made me think BackGroundWorker.

    I've not played with this much since then, but I had something along the lines of the following: (code is an exampe only; not meant to be functional):

    vb.net Code:
    1. Private GDevice As GraphicsDevice
    2. Private Sbatch As SpriteBatch
    3.  
    4. Private blnQuit As Boolean = False
    5.  
    6. Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
    7.     Try
    8.  
    9.         If InitialiseGraphics(pbGame) = False Then
    10.             MessageBox.Show("problem initialising the game. About to quit")
    11.             Application.Exit()
    12.         End If
    13.  
    14.         BackgroundWorker1.RunWorkerAsync()
    15.  
    16.     Catch ex As Exception
    17.         MessageBox.Show(ex.Message)
    18.     End Try
    19. End Sub
    20.  
    21.  
    22. Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    23.  
    24.     If Not blnQuit Then
    25.         '  don't allow the Form to close until we've released our resources
    26.         blnQuit = True
    27.         e.Cancel = True
    28.     End If
    29.  
    30. End Sub
    31.  
    32. Private Function InitialiseGraphics(ByRef surface As PictureBox) As Boolean
    33.    ' graphics initialisation code here
    34.    ' including:
    35.    '
    36.     GDevice = New GraphicsDevice(gAdapter, GraphicsProfile.HiDef, pParam)
    37.  
    38. End Function
    39.  
    40. Private Sub LoadGraphics()
    41.     ' load Texture2D's using Content manager here
    42.   End Sub
    43.  
    44.  
    45. Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    46.    
    47.     Call GameLoop()
    48.  
    49. End Sub
    50.  
    51. Private Sub GameLoop()
    52.     '   blnQuit set to True outside of the background thread
    53.     '   when we want to quit
    54.     Do Until blnQuit
    55.  
    56.         GDevice.Clear(xnaColor.CornflowerBlue)
    57.  
    58.         Sbatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
    59.         'Sbatch.Draw whatever goes here
    60.         Sbatch.End()
    61.  
    62.         GDevice.Present()
    63.  
    64.     Loop
    65.  
    66. End Sub
    67.  
    68.  
    69. Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    70.     '   tidy up by releasing any used resources
    71.     GDevice.Dispose()
    72.     Sbatch.Dispose()
    73.     '   Dispose any Texture2D's
    74.     '   no doubt I've overlooked something
    75.  
    76.     '   and finally close the form
    77.     Me.Close()
    78.  
    79. End Sub


    Note the FormClosing event is used to delay the user from closing the Form until various resources have been disposed of. It sets blnQuit to True, which terminates the game loop, so allowing the BackGroundWorker code to terminate and its RunWorkerCompleted event code to run where the tidy up is performed, and the Form finally closed.

  6. #6

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,378

    Re: XNA - Game Loop

    I had followed that same tutorial and was thinking about using a bg worker. I was kinda bummed to see that the link in his signature was broken and there are only 2 tutorials.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: XNA - Game Loop

    Quote Originally Posted by dday9 View Post
    I was kinda bummed to see that the link in his signature was broken and there are only 2 tutorials.
    Yeah, he did kind of leave you dangling, just as it was getting interesting too. I'm hoping you will take over where he left off.


    I notice you say above that you are doing this without the templates. Are you using any sort of content manager, or the helper class that he uses in the tutorial?

    I have to say that I don't really understand the content pipeline, and the helper class even less. Although they do appear to make life a lot easier, they also seem to tie you into the paid for version of VS or the Express for Phones version when coding in VB. in fact, I've just tried a slightly modified version of the code from the tutorial using the standard VB2010 Express without any templates, content managers or helper classes and it seems to be an awful lot simpler than I was expecting. I'm loading my sprites with
    Code:
    MySprite = Texture2D.FromStream(GDevice, IO.File.Open(<path to picture file>, IO.FileMode.Open))
    Are you doing something similar, or am I overlooking something blindingly obvious like I usually do?

  8. #8

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,378

    Re: XNA - Game Loop

    I'm not using the content manager or helper class. But I haven't gotten into watching the sprites lessons yet, as I kinda freaked out when I saw .DoEvents in an infinate loop. I'm going to be looking into it tonight and let you know what I wind up doing.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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