Timer help needed
BlitzMax Forums/BlitzMax Beginners Area/Timer help needed
| ||
| Hi, The following code works and displays the text at 250,0. I want to pause for maybe 1 second before displaying the next text.I'm not sure how to do this, do I need some sort of timer ? Any help appreciated. Graphics 800,600,0 'set gfx mode Repeat Cls DrawText "test text",250,0 Flip FlushMem Until KeyHit(KEY_ESCAPE) |
| ||
a quick and dirty example, but I think you get the point..Graphics 800,600,0 'set gfx mode Global a:String = "test text" Global time:Int = MilliSecs() Repeat check() Cls DrawText a,250,0 Flip FlushMem Until KeyHit(KEY_ESCAPE) Function check() If MilliSecs() - time > 1000 a = "HEY!" EndIf EndFunction |
| ||
Here's the timekeeping part of my current project, you might find it useful...Type _Timer Final Field Active:Byte Field LastTick:Int Field Frequency:Int Field Tick:Byte Method Update:Byte() If Active = True If LastTick + Frequency < MilliSecs() Tick = True LastTick = MilliSecs() EndIf EndIf End Method Method HasTicked:Byte() If Tick = True Then Tick = False; Return True Return False End Method Method Activate:Byte() Active = True LastTick = MilliSecs() End Method Method DeActivate:Byte() Active = False End Method Method SetFreq:Byte( NewFreq:Int ) Frequency = NewFreq End Method Method GetTimeSinceLastTick:Int() Return MilliSecs() - LastTick End Method End Type Then use it like this t:_Timer = New _Timer t.SetFreq(1000) t.Activate() blah blah... t.Update() If t.HasTicked() = True, 'Display next text exactly the same as LarsG's example, just more long winded and harder to follow ;) |
| ||
| Thanks alot guys, your examples will come in handy. |
| ||
You could use function pointers as a sort of CallBack feature that can simulate multi threading...
Type TTimer
Field BoundObject:Object
Field Clock
Field Interval
Field NextTick
Field Enabled = True
Field TickCallback(Sender:TTimer)
Function Create:TTimer(Interval, BoundObject:Object = null)
Local o:TTimer = New TTimer
o.BoundObject = BoundObject
o.Interval = Interval
o.Clock = MilliSecs()
o.NextTick = o.Clock + o.Interval
Return o
End Function
Method Update()
If Enabled Then
If MilliSecs() > NextTick Then
Clock = MilliSecs()
NextTick = Clock + Interval
If TickCallback <> Null Then TickCallback(Self)
End If
End If
End Method
End Type
Function TimerCallback(Sender:TTimer)
Print "Tick!"
End Function
Local T:TTimer = TTimer.Create(1000)
T.TickCallback = TimerCallback
While Not Keyhit(Key_Escape)
Cls
T.Update()
Flip
Flushmem
Wend
|