[VB6] Managed Game Loop-VBForums
Results 1 to 3 of 3

Thread: [VB6] Managed Game Loop

  1. #1

    Thread Starter
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,072

    [VB6] Managed Game Loop

    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:
    1. Do While Running = True
    2.  
    3.      DoEvents
    4.  
    5.      Lock_Framerate 60      '60 frames per second
    6.  
    7.      Clear_Window
    8.  
    9.      Update_Game 'Physics, Movement, etc.
    10.  
    11.      Render
    12.  
    13. Loop

    Enjoy the code.
    Attached Files Attached Files

  2. #2
    New Member
    Join Date
    Jun 2006
    Posts
    5

    Re: [VB6] Managed Game Loop

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

    VB Code:
    1. Private Sub Lock_Framerate(Target_FPS As Long)
    2.  
    3.     Static Last_Time As Currency
    4.  
    5.     Dim Current_Time As Currency
    6.    
    7.     Dim FPS As Single
    8.    
    9.     Do
    10.  
    11.         QueryPerformanceCounter Current_Time
    12.    
    13.         FPS = Ticks_Per_Second / (Current_Time - Last_Time)
    14.        
    15.         ' DoEvents here?
    16.    
    17.     Loop While (FPS > Target_FPS)
    18.    
    19.     QueryPerformanceCounter Last_Time
    20.  
    21. 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...

  3. #3

    Thread Starter
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,072

    Re: [VB6] Managed Game Loop

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.