|
-
Jan 8th, 2013, 11:17 PM
#1
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?
-
Jan 9th, 2013, 04:40 AM
#2
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)
-
Jan 9th, 2013, 09:44 AM
#3
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.
-
Jan 9th, 2013, 10:28 AM
#4
Re: XNA - Game Loop
 Originally Posted by dday9
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)
-
Jan 9th, 2013, 03:55 PM
#5
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:
Private GDevice As GraphicsDevice Private Sbatch As SpriteBatch Private blnQuit As Boolean = False Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown Try If InitialiseGraphics(pbGame) = False Then MessageBox.Show("problem initialising the game. About to quit") Application.Exit() End If BackgroundWorker1.RunWorkerAsync() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing If Not blnQuit Then ' don't allow the Form to close until we've released our resources blnQuit = True e.Cancel = True End If End Sub Private Function InitialiseGraphics(ByRef surface As PictureBox) As Boolean ' graphics initialisation code here ' including: ' GDevice = New GraphicsDevice(gAdapter, GraphicsProfile.HiDef, pParam) End Function Private Sub LoadGraphics() ' load Texture2D's using Content manager here End Sub Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Call GameLoop() End Sub Private Sub GameLoop() ' blnQuit set to True outside of the background thread ' when we want to quit Do Until blnQuit GDevice.Clear(xnaColor.CornflowerBlue) Sbatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend) 'Sbatch.Draw whatever goes here Sbatch.End() GDevice.Present() Loop End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted ' tidy up by releasing any used resources GDevice.Dispose() Sbatch.Dispose() ' Dispose any Texture2D's ' no doubt I've overlooked something ' and finally close the form Me.Close() 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.
-
Jan 9th, 2013, 04:12 PM
#6
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.
-
Jan 9th, 2013, 05:52 PM
#7
Re: XNA - Game Loop
 Originally Posted by dday9
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?
-
Jan 9th, 2013, 06:01 PM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|