Timers and Milliseconds

BlitzMax Forums/BlitzMax Beginners Area/Timers and Milliseconds

blackwater(Posted 2012) [#1]
Hi all, this will be my second newb post for today :)

It would be nice to measure real time in my game for event purposes, such as creating something that will get fired off in X amount of seconds from it's creation.

For my main game loop, I'm using a Repeat Forever loop, inside that is a WaitEvent statement.

I created a master timer with 1000 for hertz. I thought this would create a timer that would get fired off every millisecond but it's not. It actually takes about 15 seconds to go 1 second in real time.

Any suggestions on how to measure real time based on seconds?


Jesse(Posted 2012) [#2]
hope this helps:
[monkeycode]
Graphics 640,480
Local game:TGame = New TGame.Create()

Repeat
Cls
game.Update()
game.Render()
Flip()
Until KeyDown(KEY_ESCAPE)

Type TGame
Field startTime:Int
Field time:Int
Field seconds:Int
Field minutes:Int
Field hours:Int

Method Create:TGame()
startTime = MilliSecs()
Return Self
End Method

Method Update:Int()
time = MilliSecs() - startTime
Local secs:Int = time /1000
Local mins:Int = secs/60
Local hrs:Int = mins/60
seconds = secs Mod 60
minutes = mins Mod 60
hours = hrs
Return 1
End Method

Method Render:Int()
DrawText "time "+hours+":"+minutes+":"+seconds,50,50
Return 1
End Method


End Type
[/monkeycode]


GfK(Posted 2012) [#3]
CreateTimer (1000) will create a 1000hz timer, so by the time 15 seconds has passed, timer.Ticks() will return 15000.

It's worth noting that you should not use Millisecs for timing as it will eventually loop around to a negative value and this can cause problems- aside from the fact that it returns the system up-time in millisecs so doesn't even start at zero. By the same note, timer.ticks() will have the same loop-around problem and given that you don't ever really need a 1000hz timer, a 10hz one is normally plenty and as good as eradicates the loop-around issue (your game will have run continually for about ten months).


blackwater(Posted 2012) [#4]
Hi guys, sorry for the very late response on this.

GFK, what I don't understand is then how would I check things or make things happen in a timely manner?

For example, I have a method so that when the mouse gets near the edge of the screen it will start scrolling in that direction. With a 1000 mz timer I can make that scroll happen at a normal speed.

I just tried a 10 mz timer instead, and now the scroll is taking much longer because it doesn't get checked as often as a 1000 mz timer. Besides using a timer, I don't know else would I check for that?

My main loop is a Repeat with Wait For Event, I just realized that I can probably check things before the WFE statement? Or is there a better way to handle this?


TaskMaster(Posted 2012) [#5]
As long as you do your time subtraction correctly, the time looping around will not hurt anything, the math comes out correctly anyway.

Subtract the old value from the new value and you will always get a positive difference.