[bb] Tickers by Jams [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:43

Previous topic - Next topic

BlitzBot

Title : Tickers
Author : Jams
Posted : 1+ years ago

Description : Simple ticking/timing code, not meant for heavy duty stuff.

Example:

Local Tick% = Ticker_New( 1000 )
Repeat
If Ticker_HasTicked( Tick% )
Print "Tick!"
Endif
Until Keyhit( 1 )
Ticker_Dispose( Tick% )


Code :
Code (blitzbasic) Select
;EXAMPLE USAGE:
;
Local Tick% = Ticker_New( 1000 )
Repeat
If Ticker_HasTicked( Tick% )
Print "Tick!"
EndIf
Until KeyHit( 1 )
Ticker_Dispose( Tick% )


;!====================================================================================================
; Ticker type.
;!====================================================================================================
Type Ticker
Field LastTick%
Field Frequency%
End Type

Function Ticker_New%( NewFrequency%=1000 )
Local this.Ticker = New Ticker

thisLastTick% = MilliSecs()
thisFrequency% = NewFrequency%

Return Handle( this )
End Function

Function Ticker_Dispose%( HND% )
Local this.Ticker = Object.Ticker( HND% )

Delete this
End Function

Function Ticker_Frequency%( HND%, NewFrequency%=-1 )
Local this.Ticker = Object.Ticker( HND% )

If ( NewFrequeny% = -1 )
Return thisFrequency%
Else
If NewFrequency% > 0
thisFrequency% = NewFrequency%
EndIf
EndIf
End Function

Function Ticker_HasTicked%( HND% )
Local this.Ticker = Object.Ticker( HND% )

If ( MilliSecs() > ( thisLastTick% +thisFrequency% ) )
thisLastTick% = MilliSecs()
Return True
EndIf

Return False
End Function


Comments : none...