Stefan Ram was thinking very hard :
I was just not aware that one can program this way in VB.
You say »udpategame would process input«. How do I do this in VB?
»udpategame would process input« sound as if the program would
query (read) the user input, while AFAIK in VB one instead defines
subs such as
Sub Command1_Click()
...
End Sub
which are called by the UI thread whenever appropriate.
I used to think that a loop such as:
need to run in the UI thread, since it is updating the UI,
but then, it will block the processing of events as long as
it is running, since there is only one UI thread and this is
currently busy executing this loop. So what do you do to
»process input« within this loop, when it actually needs to
terminate for input to be processed in the single UI thread?
It seems we are talking about the render loop for a game. I'm not a
games programmer, by any streatch of the imagination - but, it's
something I have played with a bit. Personally, if I were you - I
would look into using XNA for your game needs - but, I'm not sure of
the level of VB support currently. I'm basically a C# guy... XNA will
handle a lot of the plumbing of the basic render loop, i/o, resource
handeling, etc. So, it might be worth it to look into it.
That, said - I'll try to give you a couple of basic options if your
talking windows forms/gdi+ style games, at least the ones I've used
1) In some case simple windows forms timer can be sufficient depending
on how precise your timeing needs to be (if I remember correctly, a
windows forms timer can be +-100ms or so). You can just set your timer
up in the load event (or as I do, I usually encapsulate all of this
into a game class - but, I'm just trying to keep it simple):
GameTimer.Enabled = True
GameTimer.Interval = 1000/ FRAMES_PER_SECOND ' CALCULATE THE DESIRED
INTERVAL
Then you just handle your game stuff in the tick event:
Sub GameTimer_Tick (ByVal sender As Object, ByVal e As EventArgs)
Handles GameTimer.Tick
' handle your game state udpate and drawing here
End Sub
2) Use an api based timer if you need more percision. Other than
that, it's similar to above except you have to make use of p/invoke
I can help you with that if you go this route.
3) use a render loop with Application.DoEvents() - this my fav
Again, I tend to encapsulate a lot of the game into a class. I have a
in progress version of the old basic gorilas game that i've been
tinkering with from time-to-time. My main method looks like this (this
is C#, but, the VB code wouldn't be all that different:
static void Main ()
{
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault ( false );
// show the form and start the game loop
using ( GorillaForm gorillaForm = new GorillaForm () )
{
GorillaGame = new Game ( gorillaForm );
gorillaForm.Show ();
while ( !GorillaGame.GameOver )
{
GorillaGame.Render ();
Application.DoEvents ();
}
}
}
Basically, that lets the loop run as fast as it can - and still window
messages are processed (Application.DoEvents()). Basically, the
GorillaForm is nothing but a blank form with some default settings
(fixed border, etc). The game class hooks all of the input events,
etc, and handles them.
I'm not endorsing any particular method - but, the point is that you
probably don't need to worry about threading and syncronization, if
this is just a typical game app?