brl_timer.md 2.1 KB


id: brl.timer title: BRL.Timer

sidebar_label: BRL.Timer

Functions

Function CreateTimer:TTimer( hertz#,event:TEvent=Null )

Create a timer

CreateTimer creates a timer object that 'ticks' hertz times per second.

Each time the timer ticks, event will be emitted using EmitEvent.

If event is Null, an event with an id equal to EVENT_TIMERTICK and source equal to the timer object will be emitted instead.

Returns

A new timer object

Example 1

SuperStrict

'Maximum allowable Timer is 16
Global timers:TTimer[500]

For Local n:Int = 0 Until 18
	timers[n] = CreateTimer(1)
	If timers[n] = Null
		Print "Cannot create timer "+n
	Else
		Print "Successfully created timer "+n
	EndIf
Next

Example 2

'Animation on MaxGUI canvas
SuperStrict

Import MaxGUI.Drivers

Local MyWindow:TGadget=CreateWindow("Canvas Example", 200,200,320,240)
Local MyCanvas:TGadget=CreateCanvas(10,10,290,140,MyWindow)
Local timer:TTimer=CreateTimer(60)
Local x:Int=0

Repeat
  WaitEvent()
  Select EventID()
  Case EVENT_WINDOWCLOSE
     End
  Case EVENT_TIMERTICK
     x=x+1
     If x>240 x=0
     RedrawGadget(MyCanvas)
  Case EVENT_GADGETPAINT
    SetGraphics CanvasGraphics (MyCanvas)
    Cls
    DrawRect  x,20,50,80
    Flip
   End Select
Forever


Function TimerTicks:Int( timer:TTimer )

Get timer tick counter

Returns

The number of times timer has ticked over


Function WaitTimer:Int( timer:TTimer )

Wait until a timer ticks

Returns

The number of ticks since the last call to WaitTimer

Example

SuperStrict

Framework BRL.StandardIO
Import BRL.TimerDefault

Local timer:TTimer = CreateTimer( 10 )

Repeat
	Print "Ticks="+WaitTimer( timer )
Forever


Function StopTimer( timer:TTimer )

Stop a timer

Once stopped, a timer can no longer be used.