Ooops
January 15, 2021, 05:06:42 PM

Author Topic: [bmx] Callback Timer by Kryzon [ 1+ years ago ]  (Read 541 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Callback Timer by Kryzon [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Callback Timer
Author : Kryzon
Posted : 1+ years ago

Description : This is a simple modification of the cross-platform timers present in the BRL.Timer module. Save the source code as "TCallbackTimer.bmx" or similar and then 'Import' this file into your programs.

For this to work, you need to modify your BRL.timer.mod module (located in BlitzMax/mod/brl.mod/timer.mod/timer.bmx). Change the "timer" parameters of the Extern functions from "TTimer" to "Object" type, like so:

Code: [Select]
Extern 'OLD.
Function bbTimerStart( hertz#,timer:TTimer )
Function bbTimerStop( handle,timer:TTimer )
End Extern
Code: [Select]
Extern 'NEW
Function bbTimerStart( hertz#,timer:Object )
Function bbTimerStop( handle,timer:Object ) 'Changed to Object type.
End Extern
Then rebuild your modules.

Instead of emitting an event, a "callback timer" calls a BlitzMax function that you specify.
This is useful if you have a function that you would like to be called with a certain frequency, but you don't want to keep polling time and calling it manually.
All of it is done by the OS and BlitzMax.

The callback function that you create cannot return a value, but it can accept a parameter 'data:Object.' You can use this to transfer arbitrary data to the function (a string or a custom type object).

You can stop (and consequently free) the timer whenever you want, and you can change the callback function or the data argument while the timer is active.

There's a practical example in the first comment at the bottom. [/i]

Code :
Code: BlitzMax
  1. Import BRL.Timer
  2.  
  3. Type TCallbackTimer
  4.  
  5.         Method Ticks:Int()
  6.                 Return _ticks
  7.         End Method
  8.        
  9.         Method Stop()
  10.                 If Not _handle Return
  11.                 bbTimerStop _handle,Self
  12.                 _handle=0
  13.                 _func=Null
  14.                 _data=Null
  15.         End Method
  16.        
  17.         Method Fire()          
  18.                 If Not _handle Return
  19.                 _ticks:+1
  20.                
  21.                 'Invoke the function with the data argument.
  22.                
  23.                 _func( _data )         
  24.         End Method
  25.  
  26.         Method Wait:Int()
  27.                 If Not _handle Return -1
  28.                 Local n:Int
  29.                 Repeat
  30.                         WaitSystem
  31.                         n=_ticks-_wticks
  32.                 Until n
  33.                 _wticks:+n
  34.                 Return n
  35.         End Method
  36.        
  37.         Method SetCallback( newFunc( data:Object ) )
  38.                 _func = newFunc
  39.         End Method
  40.  
  41.         Method SetData( newData:Object )
  42.                 _data = newData
  43.         End Method
  44.  
  45.         Method GetData:Object()
  46.                 Return _data           
  47.         End Method
  48.        
  49.         Function Create:TCallbackTimer( hertz#, func( data:Object ), newData:Object = Null )
  50.                 Local t:TCallbackTimer=New TCallbackTimer
  51.                 Local handle:Int=bbTimerStart( hertz,t )
  52.                 If Not handle Return Null
  53.                 t._func=func
  54.                 t._handle=handle
  55.                 t._data=newData
  56.                 Return t
  57.         End Function
  58.  
  59.         Field _ticks:Int
  60.         Field _wticks:Int
  61.         Field _func( data:Object )
  62.         Field _handle:Int
  63.         Field _data:Object
  64.  
  65. End Type
  66.  
  67. Function CreateCallbackTimer:TCallbackTimer( hertz#, func( data:Object ), newData:Object = Null )
  68.         Return TCallbackTimer.Create( hertz, func, newData )
  69. End Function
  70.  
  71. Function SetTimerCallback( cTimer:TCallbackTimer, func( data:Object ) )
  72.         cTimer.SetCallback( func )
  73. End Function
  74.  
  75. Function SetTimerData( cTimer:TCallbackTimer, newData:Object = Null )
  76.         cTimer.SetData( newData )
  77. End Function
  78.  
  79. Function GetTimerData:Object( cTimer:TCallbackTimer )
  80.         Return cTimer.GetData()
  81. End Function
  82.  
  83. Function WaitCallbackTimer:Int( cTimer:TCallbackTimer )
  84.         Return cTimer.Wait()
  85. End Function
  86.  
  87. Function StopCallbackTimer( cTimer:TCallbackTimer )
  88.         cTimer.Stop()
  89. End Function


Comments :


Kryzon(Posted 1+ years ago)

 EXAMPLE:
Code: [Select]
SuperStrict
Import "TCallbackTimer.bmx"

'Timer rate in hertz (ticks per second).

Const HERTZ:Int = 1

'The callback that I want the timer to invoke on each tick.
'The function ALWAYS needs to take in the 'data:Object' parameter, even if it doesn't use it.

Function MyCallback( data:Object )

'In this particular function, the data being received is an instance of a callback timer.

Local seconds:Int = TCallbackTimer( data ).Ticks()

If seconds < 2 Then
Print String( seconds ) + " second"
Else
Print String( seconds ) + " seconds"
EndIf

End Function


'Create the timer.
'Note how the function name is written without the round brackets, so we're referring to the function pointer.

Print ""
Print "Starting the timer. Press any key to quit."
Print "0 seconds"

Local myTimer:TCallbackTimer = CreateCallbackTimer( HERTZ, MyCallback )
SetTimerData( myTimer, myTimer ) 'Setting the timer itself as the data to be passed to the callback.

Graphics( 400, 300, 0 )
WaitKey()
End


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal