GameClock and Deltatime
Monkey Forums/Monkey Code/GameClock and Deltatime
| ||
I tend to make use of delta-time, but since mojo is using Update on a fixed logic it almost feels like having a dynamic delta clock and not a fixed delta, since it's still basically fixed. If I make a class, like MainMenu I add OnRender, OnUpdate Right now I have passed my gameClock to that, like this: Method OnRender( clock:GameClock ) TimePassed += clock.Delta End But since I only use the delta, I could just as well use a float like this: Method OnRender( delta:float ) TimePassed += delta End How do you do, any comments? Here I'm also sharing my GameClock, feel free to use, so far it's been very handy for calculating FPS and DeltaTime, or to measure TimeSpans. Strict Import mojo.app Import mojo.graphics #rem GameTimer used to get good timing. Update should only be called once in the main loop, the update will calculate delta time and fps. #End Class GameClock Private Field secondsSinceStart:Float Field millisecsSinceStart:Int Field fps:Int = 60 Field delta:Float = 1.0 / 60.0 Field clockOffset:Int Field MsSinceLastFrame:Int Field TimestampLastFrame:Int Field FpsTimer:Float Field FpsCounter:Int Public Method New() Start() End Method Method Start:Void() clockOffset = Millisecs() End Method Method SecondsSinceStart:Float() Property Return secondsSinceStart End Method Method MillisecsSinceStart:Float() Property Return millisecsSinceStart End Method Method FPS:Int() Property Return fps End Method Delta:Float() Property Return delta End Method Method TimeSince:Int(timestamp:Int) millisecsSinceStart = TimeSinceStarted() Return millisecsSinceStart - timestamp End Method Method TimeStamp:Int() millisecsSinceStart = TimeSinceStarted() Return millisecsSinceStart End Method Method TimeSinceStarted:Int() Return Millisecs() - clockOffset End Method Method Update:Void() millisecsSinceStart = TimeSinceStarted() MsSinceLastFrame = millisecsSinceStart - TimestampLastFrame TimestampLastFrame = millisecsSinceStart delta = MsSinceLastFrame * 0.001 If delta < 0 Then Print "error: Delta < 0 " secondsSinceStart+= Self.Delta FpsTimer+= Self.Delta FpsCounter+=1 If (FpsTimer > 1) fps = FpsCounter FpsTimer = 0 FpsCounter = 0 Endif End Method Method DebugDraw:Void(x:Int = 20, y:Int = 20) Local row:Int = 0 DrawText( "Delta: " + Delta, x, y + row * 20 ) row+=1 DrawText "clockOffset: " + clockOffset, x, y + row * 20 row+=1 DrawText "MillisecsSinceStart: " + MillisecsSinceStart, x, y + row * 20 row+=1 DrawText "MsSinceLastFrame: " + MsSinceLastFrame, x, y + row * 20 row+=1 DrawText "FPS Count: " + FPS, x, y + row * 20 row+=1 DrawText "SecondsSinceStart: " + SecondsSinceStart, x, y + row * 20 row+=1 End Method End Class |