Simple Timer class (TTimer replacement)
Monkey Forums/Monkey Code/Simple Timer class (TTimer replacement)
| ||
For Blitzmax converts, this class will replace system timers. test.monkey (test app code, create a 10hz timer) Import mojo Import timerobject Function Main:Int() New myApp Return 0 End Function Class myApp extends App Field timer:TimerObject Method OnCreate() Self.timer = New TimerObject(10) SetUpdateRate(30) End Method Method OnRender() Cls DrawText Self.timer.Ticks(),50,50 End Method End Class timerobject.monkey Import mojo Class TimerObject Field hz:Int Field startTime:Int Method New(hertz:Int) Self.hz = hertz Self.startTime = Millisecs() End Method Method Ticks:Int() Return (Millisecs() - Self.startTime) / (1000 / Self.hz) End Method Method Reset:Int() Self.startTime = Millisecs() End Method End Class This only calculates the ticks when the Ticks() method is called. If you use a lot of this stuff in your code then you'd be better off adding an Update() method to this class and doing the calculation once per cycle, and store the result in Field _ticks:Int. |