very simple but working Timer wich can be paused

Monkey Forums/Monkey Code/very simple but working Timer wich can be paused

Salmakis(Posted 2013) [#1]
a Very simple timer, wich dont need any updates.

just start it with start(hz) or StartWithMS(ms:int)
and get the ticks since last ask for ticks() or since start with .ticks()
if you want to pause the timer just do ShiftTimer() at the end of the pause or if you want while the pause is running.

Import mojo

#rem
	Header:Simpler timer class, like the Blitzmax TTimer.
	it dont need any updates, just start it and get the ticks since last ticks (or last start) with .Ticks()		
	if you want a pause, just do .ShiftTimer() at the end of the pause (or at all the time while the pause is running)
#END

'summary: simple timer class
Class SimpleTimer
	
	Field StartMS:Int
	Field LastTickMS:int
	Field TickLen:int
	Field TickCount:int
	
	'summary:creates a new timer
	Method New(hz:Int = 1)
		Self.Start(hz)
	End Method
	
	'summary:(re) starts the timer with hertz
	Method Start(hz:int)
		StartMS = Millisecs()
		LastTickMS = StartMS
		TickLen = 1000 / hz
		TickCount = 0
	End
	
	'summary: (re) starts the timer with ms for ticklen
	Method StartWithMS(ms:Int)
		StartMS = Millisecs()
		LastTickMS = StartMS
		TickLen = ms
		TickCount = 0		
	End
	
	'summary: retuns how many ticks happned since last call of this method
	Method Ticks:int()
		Local ret:Int = 0
		While (LastTickMS + TickLen < Millisecs())
			LastTickMS += TickLen
			TickCount += 1
			ret += 1
		Wend
		Return (ret)
	End
	
	'summary: use this at pause time, or a the end of a pause.
	Method ShiftTimer()
		'just ignore the following ticks, dirty but cool
		Local TC:Int = TickCount
		Self.Ticks()
		TickCount = TC
	End
	
	'summary: returns how many ticks at all was returnet by ticks() since start (or creation)
	Method TicksSinceStart:Int()
		Return TickCount
	End
	
	'summary: returns how long this timer is running in millisecs
	Method MillisecsSinceStart:int()
		Return (Millisecs() -StartMS)
	End
End