Results 1 to 14 of 14

Thread: Loop or Timer?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80

    Loop or Timer?

    In C++, I know you use a main game loop to go through all the code constantly. But I want to know if Visual basic games usually use something like that or not, or if it all gets put on a real fast timer.

    If it IS done with a loop, please tell me how.
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  2. #2
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    Timers are Evil(tm)

    VB Code:
    1. Do
    2.    'here goes your code
    3. Loop until ExitCondition
    Sanity is a full time job

    Puh das war harter Stoff!

  3. #3
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    Here's my rather messy main loop from my engine (slightly edited):

    VB Code:
    1. Dim cInc As Single
    2. Dim PerfFrequency As Currency, PerfTime As Currency, RealFPS As Double
    3. Dim StartTime As Currency, EndTime As Currency, m_strCaption As String
    4.     Call QueryPerformanceFrequency(PerfFrequency)
    5.     m_Engine.DesiredFramerate = 1000 \ (1000 \ ClipValue(m_Engine.DesiredFramerate, c_lngMinFramerate, c_lngMaxFramerate))
    6.     m_Engine.Running = True
    7.     Do While m_Engine.Running = True
    8.         If m_booCritical Then
    9.             DoEvents
    10.         Else
    11.             If QueryPerformanceCounter(PerfTime) = 0 Then
    12.                 CurrentTime = GetTickCount
    13.                 StartTime = CurrentTime
    14.             Else
    15.                 CurrentTime = GetTickCount
    16.                 StartTime = Int((PerfTime / PerfFrequency) * 1000)
    17.             End If
    18.             If LastTime = 0 Then LastTime = CurrentTime
    19.             FramesToRender = Abs(CurrentTime - LastTime) + FramerateBuildup
    20.             FramerateBuildup = 0
    21.             MSPerFrame = 1000 \ m_Engine.DesiredFramerate
    22.             FramerateBuildup = FramesToRender Mod MSPerFrame
    23.             If m_Engine.BalanceFramerate Then
    24.                 FramesToRender = FramesToRender \ MSPerFrame
    25.             Else
    26.                 FramesToRender = 1
    27.             End If
    28.             If FramesToRender > c_lngMaxFrameskip Then FramesToRender = c_lngMaxFrameskip
    29.             If FramesToRender > 0 Then
    30.                 If m_Engine.Paused Then
    31.                     FramesToRender = 0
    32.                 Else
    33.                     For RenderFrames = 1 To FramesToRender
    34.                         If m_Engine.Paused Then Else m_Engine.Update
    35.                         IncUPS = IncUPS + 1
    36.                         If m_Engine.Running = False Then Exit For: Exit Do
    37.                     Next RenderFrames
    38.                 End If
    39.                 If m_Engine.Running Then Redraw
    40.                 If m_GFX.Window.WindowState = 1 Then
    41.                 Else
    42.                     If m_Engine.Running Then m_GFX.FlipImage m_Backbuffer
    43.                 End If
    44.                 IncFPS = IncFPS + 1
    45.             Else
    46.                 If m_Engine.HarassCPU Then Else Call SleepEx(1, True)
    47.             End If
    48.             If m_Engine.Running = False Then Exit Do
    49.             If QueryPerformanceCounter(PerfTime) = 0 Then
    50.                 EndTime = GetTickCount
    51.             Else
    52.                 EndTime = Int((PerfTime / PerfFrequency) * 1000)
    53.             End If
    54.             If m_Engine.Running = False Then Exit Do
    55.             If Int(CurrentTime \ 1000) <> Int(LastTime \ 1000) Then
    56.                 CurrentFPS = IncFPS
    57.                 CurrentUPS = IncUPS
    58.                 IncFPS = 0
    59.                 IncUPS = 0
    60.             End If
    61.             LastTime = CurrentTime
    62.             DoEvents
    63.         End If
    64.     Loop
    65.     Shutdown

    It manages to maintain very very accurate framerates, with frameskipping, all without harassing the CPU (usually CPU usage stays between 0 and 10% when running at 60fps, unless the game is really detailed at the moment).

    Hope this helps, if you want some help understanding it, let me know - it's not commented :P
    "1 4m 4 1337 #4xz0r!'
    Janus

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    Wheres the loop go? In its own sub? If so, where does THAT sub get put?
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  5. #5
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well you can make a sub like this
    sub MainLoop()
    end sub

    just place it anywhere you want
    now if you want to call it you could have a button for example or just put it into the form load event
    Sanity is a full time job

    Puh das war harter Stoff!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    My loop is called when I press a command button. But after I do, it freezes up. This is the code for my loop:

    Sub Run()

    Do
    If GetKeyState(vbKeyA) = 1 Then
    MsgBox "Key A pressed"
    End If
    If GetKeyState(vbKeyQ) = 1 Then
    Running = False
    End If
    Loop Until Running = False

    End Sub
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  7. #7
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well this should work but maybe you should add a
    DoEvents into the loop

    and format your code...
    Sanity is a full time job

    Puh das war harter Stoff!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    I got my loop to stop crashing, but something might be wrong with my GetKeyState thing.

    Sub CheckMove()
    If GetKeyState(vbKeyUp) = 1 Then
    PC.xvel = PC.xpos - PC.xview
    PC.yvel = PC.ypos - PC.yview
    PC.xpos = PC.xpos + PC.xvel
    PC.ypos = PC.ypos + PC.yvel
    frmGame.Caption = PC.xpos
    End If

    End Sub

    I put CheckMove in my loop, and then in one press it overflows. I have in a different sub setting xpos and ypos to 0, and xview and yview to 120.
    Last edited by Drakon; Jun 2nd, 2002 at 03:09 PM.
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  9. #9
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    use gettickcount to check when the key was last pressed, and only allow another press after about 10ms or some value that you like... otherwise it can be really fast!
    Sanity is a full time job

    Puh das war harter Stoff!

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    How do I use it, and where do I use it?
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  11. #11
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    VB Code:
    1. Sub CheckMove()
    2. static LastPress as long
    3.   if lastpress<Gettickcount then
    4.     lastpress = gettickcount +50 'this will allow a keydown all 50 milli seconds
    5.     if GetKeyState(vbKeyUp) = 1 Then
    6.       PC.xvel = PC.xpos - PC.xview
    7.       PC.yvel = PC.ypos - PC.yview
    8.       PC.xpos = PC.xpos + PC.xvel
    9.       PC.ypos = PC.ypos + PC.yvel
    10.       frmGame.Caption = PC.xpos
    11.     end if
    12.   End If
    13. End Sub
    Sanity is a full time job

    Puh das war harter Stoff!

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    Now it doesn't do anything... do I have to do anything else anywhere?
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  13. #13
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    You have to call from inside a loop..

    VB Code:
    1. While DoEvents
    2.        CheckMove
    3.    Wend

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    I've already done that, otherwise it would never have worked, therefore preventing the overflow
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

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