Code archives/Algorithms/Asynchronous timers
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This stuff lets you add asynchronous timers and execute code when they expire. If you set them again when they expire, the code can therefore be executed on a regular basis, while your game continues as normal (the normal timer functions pause execution of the whole program until they run out). | |||||
; Timer structure: Type Timer Field start Field timeOut End Type ; Set a timer: Function SetTimer.Timer (timeOut) t.Timer = New Timer t\start = MilliSecs () t\timeOut = t\start + timeOut Return t End Function ; Check for timeout: Function TimeOut (test.Timer) If test <> Null If test\timeOut < MilliSecs () Delete test Return 1 EndIf EndIf End Function Graphics 640, 480 SetBuffer BackBuffer () ; Set timer before main loop: t.Timer = SetTimer (1000) Repeat Cls ; If timer runs out, add another "o" and reset the timer: If TimeOut (t) a$ = a$ + "o" t = SetTimer (1000) EndIf ; This stuff carries on regardless: Rect MouseX (), MouseY (), 20, 20 Text 0, 0, a$ Flip Until KeyHit (1) End |
Comments
| ||
Be aware of the flaw: Millisecs() scrolls to 0 at midnight. I always use: If Abs(millisecs()-recordedtime)>desirednummillisecs then ... It's not perfect, but will keep you from a midnight stall. |
Code Archives Forum