Results 1 to 14 of 14

Thread: Timers for a game

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    127

    Timers for a game

    I am currently trying to make a game in DirectX, but I was just wondering if there was anything better then using timers, because i thought I read before that Timers weren't really good for games.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    alternatively you could call sleep to delay, or use settimer functions or multimedia timers, depends really what you're upto
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    127
    Well I used timers for the first version I made of the game, where the timers handled moving, so that hitting more then one key wouldn't jam the program. Is there a function to maybe do something like:

    [Highlight=VB]
    ..Main Code..
    if wait is done then
    dostuff
    startwait 15milliseconds
    end if

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    no, but assign a variable say wait=gettickcount()+15 and wait til wait is less than gettickcount()

    btw gettickcount may not be that accurate, so you might have to choose another timer (such as multimedia timer or performancecounter)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Just remember that the GetTickCount API has diffrent resolution on diffrent Win plattforms.....

  6. #6
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Excuse me NoteMe? That would really screw some of my programs! Is it lower in older platforms? Is it always a millisecond after such and such point?
    Don't pay attention to this signature, it's contradictory.

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Sorry to screw up your apps...but it is true.....look it up on MSDN. Older Win versions have a really bad resolution on the GetTickCount API.....

  8. #8
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    VB Code:
    1. public sub Wait(Milliseconds as long)
    2. dim EndTime as long
    3.  
    4.    EndTime = GetTickCount+Milliseconds
    5.    While GetTickCount < EndTime
    6.       DoEvents
    7.    Wend
    8. end sub

    now picture a program using that consistently when your resolution is actually more along the lines of deciseconds (a tenth). Although I really don't think I have to worry about it cause everyone I know has 98+ ... and I don't use it TOO much...
    Don't pay attention to this signature, it's contradictory.

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    If I can remember it right it was something like

    Win2k/Xp = 10 ms
    WinMe = 20 ms
    Win98 = 40ms
    Win95 = 50ms


    Or something like that...

  10. #10
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    But is it always calculated in milliseconds?

    or are you just saying it updates less frequently? because that's fine by me.
    Don't pay attention to this signature, it's contradictory.

  11. #11
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Try this:

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Dim Pause As Boolean                                        'check if game is paused
    5. Dim Running As Boolean                                      'check if game is started
    6. Dim Fps As Double                                           'current fps
    7. Dim FrameCount As Long                                      'count frames between fps update
    8.  
    9. Private Sub Form_Load()
    10.     Me.Show                                                 'show the window
    11.     Running = True                                          'no the game is on
    12.     Main                                                    'start main loop
    13. End Sub
    14.  
    15. Private Sub Main()                                          'the main loop
    16.     Dim TempTime As Long                                    'to keep track of time
    17.    
    18.     While Running                                           'is the game running?
    19.         If TempTime <= GetTickCount Then                    'is it time to calc next frame?
    20.             TempTime = GetTickCount + 10                    'set next tick time
    21.            
    22.             While Pause                                     'is the game paused?
    23.                 DoEvents                                    'infinit loop
    24.             Wend
    25.            
    26.             'Code goes here.
    27.            
    28.             FrameCount = FrameCount + 1                     'fps counter
    29.             If Fps + 1000 <= GetTickCount Then
    30.                 Me.Caption = "Fps: " & Round(1000 / ((GetTickCount - Fps + 0.00001) / FrameCount), 0)
    31.                 Fps = GetTickCount
    32.                 FrameCount = 0
    33.             End If
    34.         End If
    35.         DoEvents                                            'handle win msgs
    36.     Wend
    37.    
    38. End Sub
    39.  
    40. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    41.     Running = False                                         'game over
    42. End Sub
    43.  
    44. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    45.     If KeyCode = vbKeyP Then                                'pause code
    46.         If Pause = False Then
    47.             Pause = True
    48.             Me.Caption = "Paused"
    49.         ElseIf Pause = True Then
    50.             Pause = False
    51.         End If
    52.     End If
    53. End Sub
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  12. #12
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yes it always updates in milliseconds.....but not every millisecond....if you want it more accurat....try using querreryperformmace something....API

  13. #13
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Ok, whew! I thought you meant that it actually saved 10 seconds as 100, instead of 10000. Which would ahve made all waits a BIT longer
    Don't pay attention to this signature, it's contradictory.

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Hehe...no no no..but if you have a waith for 10ms and the app is running on an old OS the waith will probably be more like 50ms....

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