Now we can finally start on the actual code for the game loop. We are starting by setting up some variables.
Dim TempTime As Long 'to keep track of time
Dim Fps As Double 'current fps
Dim FrameCount As Long 'count frames between fps update
The first one will hold the time for the next loop to occur. We will use GetTickCount to update it. The second one will hold the last time we checked the frame rate. We will use that to update the frame rate every second. The third one will hold the actual frame rate that second, and will be updated every time the game loop is called.
Now we will start the actual game loop. We will use a Wend loop to do this. We are checking the Running variable that we made earlier to see if we should still call the game loop. We do that like this:
While Running 'is the game running?
This loop will run as ling as Running is true. And inside this loop the rest of the game loop will be coded. So now we have to test if it was really time to call the game loop. Or if the computer was to fast and we have to wait a bit. We can do that in an If test.
If TempTime <= GetTickCount Then 'is it time to calc next frame?
The TempTime variable holds the last time we executed the actual update of the game. The first time TimeTemp will be 0, so it this test will always be true the first time. The right side we are calling the GetTickCount API that gives us the time since the PC was turned on. since that must be more then 0, it will go through the first time. But after it has gone through this test we have to update the TempTime, so it will not be 0 the next time. We can do that by writing inside the if statement:
TempTime = GetTickCount + 10 'set next tick time
This will give the TempTime variable the time now plus 10ms. So the next time the If test will test if it has gone 10ms since last time it was called. If not the test will be done again later, to see if it has used enough time. We are doing this so it should work at the same speed on all PCs. If you are adding too little time it might be slower on old PCs then to your PC, so be sure to not add to little time.
When we first are in the game loop we can check if the game was paused. If it is, we will not draw more frames for now. We can do that too using a small loop like this:
While Pause 'is the game paused?
DoEvents 'infinite loop
Pause is set to be false by default. So the first time it will not enter the loop. We can change this later using keys. As you see this is a pretty tight loop. And will just go round and round if Pause is true. And do nothing more. I have did put a DoEvents in the loops, so the app has the time to check for other messages during the loop such as we are pressing the pause key again, so the pause loop can end.
After this you can write the game code as you want. Usually this is done by calling functions. So a big game won’t look messy. So it could look like something like this.
'And so on
But it the example project I have only written a small comment about it.
Now it is time too look at the frame count. We have to add one more to the FrameCount variable every time we get here. And we have to check how long we have been counting the frames. We want it to be FPS so that means that for every 1000ms we need to start at zero again and print the frame count. I am writing it in the caption of the form, so you can see it there in the example project.
FrameCount = FrameCount + 1 'fps counter
If Fps + 1000 <= GetTickCount Then
Me.Caption = "Fps: " & FrameCount 'Printing the actual frame count
Fps = GetTickCount
FrameCount = 0
The first line will add one more to the frame count. The second line will test to see if it has been more then 1000ms since the last frame count output. So the left side will hold the last time we did output it in the FPS variable, and then we are "adding" 1000ms to it, to check against the right side. The right side will return the actual time now. If it is more then a second since last FPS output the If test will be true, and if will go into the If statement. It will print "FPS:" and then the actual frame count. We could get a more accurate frame count by using this code in stead of just printing FrameCount, but it isn't that big a difference.
Round(1000 / ((GetTickCount - Fps + 0.00001) / FrameCount), 0)
Then after that is done, we are updating the FPS variable with the exact time now. So it will be 1sec to the next time we output the frame rate. And we have to remember to set the FrameCount to zero again, so we are not adding the FrameCount from the last output too.
Then we had finished our game if statement in the game loop. And are out in the outer loop again. We are adding one more DoEvents to it, so the app gets all the messages that it need to perform all the tasks like keypresses. And then the game loop is finished.