Results 1 to 16 of 16

Thread: Game Loops

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Game Loops

    I'm new at actually making a full game, so I'm hoping someone can help me.
    When you write a game loop, graphically speaking, you want it to run at or above around 30 fps, right? That means the game goes through all your items in the loop and draws all the images 30 times a second, right? Well, I've tried something like that where I have a ball and I animate it whenever the loop is called. The ball has an animation delay on it so it will only change frames so many times per second, say 10 or so. The problem is, if you let the loop run as fast as possible, without any time delay, it draws the the ball about 3000 times per second, and causes some serious lag with the mouse and such, even with DoEvents called in the loop. Should I put a time delay on the entire game loop?

  2. #2
    Junior Member
    Join Date
    Aug 2002
    Location
    Phoenix
    Posts
    30
    What you need to do, is add some sort of variable for the ball object called
    VB Code:
    1. dim m_timeDelay as Long
    (i make all "member" variables of "objects" or "classes" prefixed with m_)

    and use the GetTickCount() api function to find out how much time has passed sense the last animation. ie.
    VB Code:
    1. if GetTickCount >= m_timeDelay + 10000 Then '10000 millaseconds is 10 seconds
    2.  Call doAnimation 'do what ever animation stuff here
    3.  m_timeDelay = GetTickCount()
    4. End if

    m_timeDelay + someINT is how long you want to wait before you do an action. (1000 = 1 second)

    This wont effect game loop times so you can have a game loop 1/60 of a second (60 fps/60 loops per second) and only be animating or doing something a certain amount of time..


    I hope that wasn't too ambigous.

    EDIT: I use 10000 as the time delay becuase I misread your post and thought you wanted once per 10 seconds, instead of 10 times per second. use 10 for the latter.
    I'm in college now.

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Screw delays =).

    Here is what a professional game would use:
    Code:
    Dim tDelta As Single
    Dim s As Long
    Dim e As Long
    ...
    While True  'Your game loop
       s = GetTickCount()
       'Do Stuff
       DoEvents
       e = GetTickCount()
       tDelta = CSng(e - s) / 1000.0
    Wend
    tDelta holds the fraction of a second that has passed while the loop has run once. Simply hold a vector for the length you want your ball to move in one second, and multiply it by tDelta each time you add it to the currept position.

    Z.

  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    That wouldn't fix the problem with the cpu lag from the loop. Only a delay can fix the cpu lag. That code would allow for super smooth animation, but it would still lag up other processes (like the mouse).
    Involved in: Sentience

  5. #5
    Junior Member
    Join Date
    Aug 2002
    Location
    Phoenix
    Posts
    30
    Originally posted by Zaei
    Screw delays =).

    Here is what a professional game would use:
    Code:
    Dim tDelta As Single
    Dim s As Long
    Dim e As Long
    ...
    While True  'Your game loop
       s = GetTickCount()
       'Do Stuff
       DoEvents
       e = GetTickCount()
       tDelta = CSng(e - s) / 1000.0
    Wend
    tDelta holds the fraction of a second that has passed while the loop has run once. Simply hold a vector for the length you want your ball to move in one second, and multiply it by tDelta each time you add it to the currept position.

    Z.

    While your "PROFESSIONAL" code would be standard for movement, it doesn't work for what (his wording at least) implied. Which as I read it was, "how can I animate a ball 10 times every second?"

    So THERE!! My answer is better then yours (sure context is a technicality, but if its good enough in court...)

    But I will admit that as far as movement calculations, your method would be the way to go.
    I had read his question to be concerning more "Sprite Animation" then "Movement Animation" (again based on context). In which my answer would be the victor.
    I'm in college now.

  6. #6
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Actually, the first sentence of his post implies that he wants to make a full game, so why post code that would not be as useful to that end?

    Z.

  7. #7
    Junior Member
    Join Date
    Aug 2002
    Location
    Phoenix
    Posts
    30
    It useful.
    Especially for sprite animation, where graphics are based on a bitmap of several renderings of an animation. While you may think running through those animations at hyperspeed isnt it a problem, it is. Sprite animations are ment to happen at specific intervals.

    My Code specificly has purpose for sprite animation (which As I stated was what I thought he wanted).



    Infact I would say my code is twice as useful as yours. Becuase not only can it do its purpose, but it CAN (maybe not super smooth) do movement animation as well.

    !
    I'm in college now.

  8. #8
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    This is not worth arguing over. You do it your way, Ill do it mine. jmiller can choose which method he prefers.

    Z.

  9. #9
    Addicted Member
    Join Date
    Aug 2002
    Location
    Baltimore, MD
    Posts
    230
    Originally posted by Zaei
    Here is what a professional game would use:
    Hate to tell you, but a professional game wouldn't use that as professional games are written in C/C++.

  10. #10
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Concepts are language independent =).

    Z.

  11. #11
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Fight Fight Fight
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  12. #12
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by Sastraxi
    Fight Fight Fight
    Yeah, im really getting hammered on this one =).

    Z.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And to make you feel even more bad, a professional game would probably query the performance counter, if available. Just look at the D3D framework that comes with the C++ DX8 SDK.


    Don't take me to seriously...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Hey, im already doing ALL of this (including the QPC() and QPF() calls, and in C++), so I dont feel bad at all =).

    And besides, When you are using VB, there isnt THAT much point in using QueryPERFORMANCECounter() =).

    Z.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    But there is a little
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

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