WaitTimer ( timer )
Parameters
| timer - a timer handle returned by CreateTimer |
Description
|
Waits for the next tick of a timer and returns how many ticks have passed. Put it at the top of your main loop and the loop settles to the timer's rate. If your frame finished early, WaitTimer sleeps until the next tick is due; if it arrived exactly on time, it returns immediately. The return value is the useful part that most code throws away. It is the number of ticks that have elapsed since the last call - 1 when you are keeping up, and more than 1 when the previous frame overran. That is your "how far behind am I" signal: multiply your movement by it to keep the game running at a consistent speed on a slow machine, or use it to decide to skip a render and catch up. The window keeps handling Windows messages while WaitTimer waits, so the program stays responsive and can still be closed. Remember that a timer paces your loop but does not present anything - you still call Flip to show each frame. Using both a timer and a vertical-blank Flip fights over the pacing, so pick one, or use Flip False alongside the timer. See also: CreateTimer, FreeTimer, MilliSecs, Delay, Flip. |
Example
; WaitTimer Example ; ----------------- Graphics 640,480,0,2 SetBuffer BackBuffer() ; WaitTimer halts the program until the next tick of a timer made with ; CreateTimer, locking the loop to the timer's rate. It returns how ; many ticks have passed since the last wait - normally 1, more if the ; loop fell behind - so you can catch up on missed ticks. timer=CreateTimer(60) ticks=1 While Not KeyDown(1) Cls ; Hold Space to fake a slow frame (60 ms of extra work) If KeyDown(57) Then Delay 60 ; Move one step per elapsed tick: when frames are dropped, ; ticks>1 makes up the lost ground and the speed stays constant x=(x+4*ticks) Mod 640 Rect x,220,40,40,True Text 0,0,"Hold Space: fake a slow frame Esc: exit" Text 0,20,"WaitTimer returned "+ticks+" tick(s) this frame" ; Flip False: don't also wait for the monitor - the timer sets the pace Flip False ; The documented command: wait for the 60 Hz timer's next tick ticks=WaitTimer(timer) Wend End
Index