Delta Time routine
Posted: Mon Aug 07, 2017 11:12 am
Up until now, I've generally been using a just a fixed-time movement system. However, with the re-write of Triority, I've changed to a fixed-time with delta spike suppression, having converted the code by Thomas Haaks.
It works well, and includes the ability to write (and display) the delta and suppressed values, along with the logic and display FPS. Display the information will take a massive FPS hit though
It works well, and includes the ability to write (and display) the delta and suppressed values, along with the logic and display FPS. Display the information will take a massive FPS hit though
Code: Select all
DeclareModule DeltaTime
Declare.b InitDeltaTimer(logicCyclesPerSec.f=75.0,numSamples.i=0)
Declare.f Process()
Declare.b LogicUpdateRequired()
Declare.f GetLogicalFPS()
Declare.f GetTween()
Declare.b ShowSpikeSuppression(x.i,y.i)
Declare.b ShowFPS(x.i,y.i,showUpdateFPS.b=#True,showRenderFPS.b=#True)
Declare.b FreeDeltaTimeSprite()
Declare.b FreeDeltaTime()
Declare.b CreateDisplaySprite(width.i,height.i)
Declare.b DisplayDeltaSprite()
EndDeclareModule
Module DeltaTime
#MAX_SAMPLES = 32
Global.f newTime,oldTime,delta
Global.b dssOn,dssIndex
Global.i dssLenArray
Global.f Dim dssArray.f(0)
Global.f logicFPS,accumulator,tween
Global.f fpsAccumulator
Global.i updateCount,renderCount,updatesPerSecond,rendersPerSecond
Global.i sprite=#NOT_FOUND
Procedure.b InitDeltaTimer(logicCyclesPerSec.f=75.0,numSamples.i=0)
oldTime=ElapsedMilliseconds()
newTime=ElapsedMilliseconds()
updateCount=0
renderCount=0
updatesPerSecond=0
rendersPerSecond=0
dssLenArray=0
dssIndex=0
logicFPS=0.0
accumulator=0.0
tween=0.0
fpsAccumulator=0.0
logicFPS=1.0/logicCyclesPerSec
Dim dssArray(0)
If numSamples>0
If numSamples>#MAX_SAMPLES : numSamples=#MAX_SAMPLES : EndIf
dssOn=#True
Dim dssArray(numSamples)
dssLenArray=numSamples
Else
dssOn=#False
EndIf
ProcedureReturn #True
EndProcedure
Procedure.b FreeDeltaTime()
Dim dssArray(0)
EndProcedure
Procedure.b FreeDeltaTimeSprite()
If IsSprite(sprite)
FreeSprite(sprite)
sprite=#NOT_FOUND
EndIf
ProcedureReturn #True
EndProcedure
Procedure.b CreateDisplaySprite(width.i,height.i)
FreeDeltaTimeSprite()
sprite=CreateSprite(#PB_Any,width,height,#PB_Sprite_AlphaBlending)
ProcedureReturn Bool(IsSprite(sprite))
EndProcedure
Procedure.b DisplayDeltaSprite()
If IsSprite(sprite) : DisplayTransparentSprite(sprite,0,0) : EndIf
EndProcedure
Procedure.f Process()
Define.f smoothDelta=0.0
Define.i i
newTime=ElapsedMilliseconds()
delta=(newTime-oldTime)*0.001
oldTime=newTime
If dssOn
dssArray(dssIndex)=delta
For i=0 To dssLenArray-1
smoothDelta+dssArray(i)
Next
delta=smoothDelta/dssLenArray
dssIndex+1
If dssIndex>dssLenArray-1 : dssIndex=0 : EndIf
EndIf
accumulator+delta
fpsAccumulator+delta
If fpsAccumulator>1.0
fpsAccumulator-1.0
updatesPerSecond=updateCount
updateCount=0
rendersPerSecond=renderCount
renderCount=0
EndIf
ProcedureReturn delta
EndProcedure
Procedure.b LogicUpdateRequired()
If accumulator>logicFPS
updateCount+1
accumulator=logicFPS
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure.f GetLogicalFPS()
ProcedureReturn logicFPS
EndProcedure
Procedure.f GetTween()
renderCount+1
ProcedureReturn accumulator/logicFPS
EndProcedure
Procedure.b ShowSpikeSuppression(x.i,y.i)
Define.i i
Define.s text
If IsSprite(sprite)
If StartDrawing(SpriteOutput(sprite))
text="Delta Spike Suppressor : "
DrawText(x,y,text)
For i=0 To dssLenArray-1
y+TextHeight(text)
text=StrF(dssArray(i))
DrawText(x,y,text)
Next
text="Final Delta : "+StrF(delta)
DrawText(x,y,text)
StopDrawing()
ProcedureReturn #True
EndIf
EndIf
ProcedureReturn #False
EndProcedure
Procedure.b ShowFPS(x.i,y.i,showUpdateFPS.b=#True,showRenderFPS.b=#True)
Define.i ty
Define.s text
If IsSprite(sprite)
ty=y
If StartDrawing(SpriteOutput(sprite))
If showUpdateFPS
text="Logic FPS : "+StrF(updatesPerSecond)
DrawText(x,ty,text)
ty+TextHeight(text)
EndIf
If showRenderFPS
DrawText(x,ty,"Render FPS : "+StrF(rendersPerSecond))
EndIf
StopDrawing()
ProcedureReturn #True
EndIf
EndIf
ProcedureReturn #False
EndProcedure
EndModule