# VBForums CodeBank > Codebank - Game Programming >  [VB6] Managed Game Loop

## Jacob Roman

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) :


VB 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.  :Wink:

----------


## Daddyo

Would the following code extract do well with a DoEvents() in the loop, so that Windows can process other messages etc?


VB Code:
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

When it comes to games, almost all of them use 100% of the CPU. Don't worry about it.  :Wink:

----------

