PDA

Click to See Complete Forum and Search --> : [VB6] Managed Game Loop


Jacob Roman
Aug 9th, 2006, 12:43 PM
What I have written is the proper way to setup a game loop that's locked at a certain framerate in realtime. For those who are addicted to Timers, you will not find any here. Timers are slow, sluggish, are not realtime, and get worse with the more you run simutaniously, mainly cause every other timer has to fire off all the code within it before the next one does. A proper game loop should look like this (Note that it's in pseudo code) :



Do While Running = True

DoEvents

Lock_Framerate 60 '60 frames per second

Clear_Window

Update_Game 'Physics, Movement, etc.

Render

Loop



Enjoy the code. ;)

Daddyo
Dec 11th, 2006, 07:39 PM
Would the following code extract do well with a DoEvents() in the loop, so that Windows can process other messages etc?

Private Sub Lock_Framerate(Target_FPS As Long)

Static Last_Time As Currency

Dim Current_Time As Currency

Dim FPS As Single

Do

QueryPerformanceCounter Current_Time

FPS = Ticks_Per_Second / (Current_Time - Last_Time)

' DoEvents here?

Loop While (FPS > Target_FPS)

QueryPerformanceCounter Last_Time

End Sub

Seems like there must be a way in Windows to setup a callback from a precision timer, so that the CPU doesn't go 100% loaded. I suppose this would require some C code...

Jacob Roman
Dec 12th, 2006, 07:49 PM
When it comes to games, almost all of them use 100% of the CPU. Don't worry about it. ;)