Results 1 to 10 of 10

Thread: How do I make a game loop? [SOLVED]

  1. #1

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    How do I make a game loop? [SOLVED]

    I've always used a timer despite there lag of at least 1 ms.

    How do I make a game loop? when and where do I initially call if from?

    and can I have one and still use all the nice functons on the form like mouse_up/down and key_up/down?

    It seems like whenever i use one it 'freezes' the game or at least ignors all built in fxns.
    Thanks!

    NOMAD
    Last edited by NOMADMAN; Oct 7th, 2003 at 11:14 AM.

  2. #2
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    hope this helps you:

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Dim Running As Boolean
    5. Dim Pause As Boolean
    6.  
    7. Private Sub Form_Load()
    8.    Me.Show
    9.    Main
    10. End Sub
    11.  
    12. Private Sub Main()
    13.     Dim TempTime As Long
    14.    
    15.     While Running
    16.         If TempTime <= GetTickCount Then
    17.             TempTime = GetTickCount + Interval
    18.             While Pause
    19.                 DoEvents
    20.             Wend
    21.             'game code goes here
    22.         End If
    23.         DoEvents
    24.     Wend
    25.    
    26. End Sub
    27.  
    28. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    29.     Running = False
    30. End Sub


    Just set the Pause bool to true to make the game pause.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Also set Running to true before you call the Main sub otherwise it won't run.


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    And

    If you want an FPS counter, use GetTickCount to save time elapsed in a var, then use an if sentece, something like:

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Dim Running As Boolean
    5. Dim Pause As Boolean
    6. Dim FPS, FPSCounter As Integer
    7. Dim FPSTimer As Integer
    8.  
    9. Private Sub Form_Load()
    10.    Me.Show
    11.    Main
    12.    FPSTimer=GetTickCount
    13.    FPS=0
    14.    FPSCounter=0
    15. End Sub
    16.  
    17. Private Sub Main()
    18.     Dim TempTime As Long
    19.    
    20.     While Running
    21.         If TempTime <= GetTickCount Then
    22.             TempTime = GetTickCount + Interval
    23.             While Pause
    24.                 DoEvents
    25.             Wend
    26. If GetTickCount=FPSTimer+1000 Then
    27. FPS=FPSCounter
    28. FPSCounter=0          
    29. FPSTimer=GetTickCount
    30. Else
    31. FPSCounter=FPSCounter+1
    32. End If
    33.  
    34. Me.Caption="MyGame - FPS: " & FPS
    35. 'game code goes here
    36.         End If
    37.         DoEvents
    38.     Wend
    39.    
    40. End Sub
    41.  
    42. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    43.     Running = False
    44. End Sub

    Something like that should work, haven't tried it though... was actually just an idea I got when reading the post, hehe.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  5. #5
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    ir you could do like this:

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Dim Running As Boolean
    5. Dim Pause As Boolean
    6. Dim Fps As Double
    7. Const Interval As Long = 50
    8.  
    9. Private Sub Form_Load()
    10.    Me.Show
    11.    Running = True
    12.    Main
    13. End Sub
    14. Private Sub Main()
    15.     Dim TempTime As Long
    16.     Dim FpsTimer As Long
    17.    
    18.     While Running
    19.         If TempTime <= GetTickCount Then
    20.             TempTime = GetTickCount + Interval
    21.             While Pause
    22.                 DoEvents
    23.             Wend
    24.             FpsTimer = GetTickCount - FpsTimer
    25.             Fps = 1000 / FpsTimer
    26.             Me.Caption = Fps
    27.             FpsTimer = GetTickCount
    28.            
    29.             'game code goes here
    30.         End If
    31.         DoEvents
    32.     Wend
    33.    
    34. End Sub
    35. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    36.     Running = False
    37. End Sub
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  6. #6
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    I have a submission on pscode.com

    If you search under Visual Basic for "fx fireworks" you'll be sure to find it.

    It's a loop which includes FPS adjustment. That is, despite what the system may run the code at, it'll be adjusted to a fixed FPS.

  7. #7

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    WOW

    Hey thanks cyborg, I can finally stop using those slow Timers

    For anyone interested when using a timer the code took approx. 55ms, now with the game loop it takes <1ms.

    Thanks all for the FPS code too...

    NOMAD

  8. #8
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    no problems, man! we're here to help!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  9. #9
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Talking

    Originally posted by cyborg
    no problems, man! we're here to help!
    And GET helped
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  10. #10
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    yes ofcourse!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

Posting Permissions

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



Click Here to Expand Forum to Full Width