Results 1 to 3 of 3

Thread: [VB6] Managed Game Loop

  1. #1
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 04
    Location
    Miami Beach, FL
    Posts
    4,864

    [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
    Last edited by Jacob Roman; Aug 9th, 2006 at 02:22 PM.
    Useful Forum Tips:

    - If you found any post that was useful, please consider rating their post

    - Give sensible thread titles, please!
    - Be sure to use [HIGHLIGHT=VB][/HIGHLIGHT] tags when posting code written in VB.
    - Write posts like a conversation, not like an email. That way you can avoid saying "Dear so and so" in every post.
    - When your problem is solved, go up to Thread Tools and click on Mark Thread Resolved

    My Contributions: Massive DirectX 2D Tutorials For VB5/VB6/VB.NET || 3D Engine In Pure VB || Friction Force ||DJ Turntable Simulation || Scratching Wavs || Time Based Movement || Newton Physics Simulation || Managed Game Loop || Rigid Body Collision Detection || World of Warcraft RGB Battle System || Bosskillers (World of Warcraft 2D Clone) || Street Fighter Controls (Flawless Simulation) || A* Pathfinding (VB6 & VB.NET) *UPDATED || *NEW* Nintendo Entertainment System Emulator (VB6) || *NEW* Video Game Making Tips (VB6 & VB.Net)

    Expert. Ask me anything NES!

    My Smilies:


  2. #2
    New Member
    Join Date
    Jun 06
    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
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 04
    Location
    Miami Beach, FL
    Posts
    4,864

    Re: [VB6] Managed Game Loop

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

    - If you found any post that was useful, please consider rating their post

    - Give sensible thread titles, please!
    - Be sure to use [HIGHLIGHT=VB][/HIGHLIGHT] tags when posting code written in VB.
    - Write posts like a conversation, not like an email. That way you can avoid saying "Dear so and so" in every post.
    - When your problem is solved, go up to Thread Tools and click on Mark Thread Resolved

    My Contributions: Massive DirectX 2D Tutorials For VB5/VB6/VB.NET || 3D Engine In Pure VB || Friction Force ||DJ Turntable Simulation || Scratching Wavs || Time Based Movement || Newton Physics Simulation || Managed Game Loop || Rigid Body Collision Detection || World of Warcraft RGB Battle System || Bosskillers (World of Warcraft 2D Clone) || Street Fighter Controls (Flawless Simulation) || A* Pathfinding (VB6 & VB.NET) *UPDATED || *NEW* Nintendo Entertainment System Emulator (VB6) || *NEW* Video Game Making Tips (VB6 & VB.Net)

    Expert. Ask me anything NES!

    My Smilies:


Posting Permissions

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