Delta Timing Woes

BlitzMax Forums/BlitzMax Programming/Delta Timing Woes

VomitOnLino(Posted 2006) [#1]
Hello I have a short question,

after Implementing Indiepaths (Thanks man!) quite cool delta timing routine and with a bit of tweaking I managed to free a whole lot of ressources.

This was back on my laptop (not entirely powerful but not weak either) where the game went from 20-40fps to a stable 55. Now back on my main PC the game either proves to be entirely unplayable, or it is so slow that it is painful.
Now I already found the culprit - removing the line where the Delta is set to 1 when it goes above 1 fixes most of the problems..

I tried with vsync and without but nothing helped...

Some parts of the game with accurate timing still act weird - any help or insight on this problem would be greatly appreciated!


Graphics 800,600,32,NOSYNC

AutoMidHandle True

Type DeltaTime

Global DeltaTList :TList

Field OldTime :Int
Field ElapsedTime :Int
Field dFps :Float
Field dDelta :Float
Field CurrFps :Int
Field FPSCounter :Int
Field CurrTime :Int
Field CheckTime :Int

Method New ()
If DeltaTList = Null Then DeltaTList = New TList
DeltaTList.AddLast Self
End Method

Method Destroy ()
DeltaTList.Remove Self
End Method

Function Create:DeltaTime(Fps:Float)
Local dTime:DeltaTime = New DeltaTime
dTime.dFps = Fps
dTime.dDelta = 1000/Fps
Return dTime
End Function

Method Update()
Self.CurrTime = MilliSecs()
Self.ElapsedTime = Self.CurrTime - Self.OldTime
Self.OldTime = Self.CurrTime
If Self.CurrTime > Self.CheckTime
Self.CheckTime = Self.CurrTime + 1000
Self.CurrFps = Self.FPSCounter
Self.FPSCounter = 0
Else
Self.FPSCounter:+1
EndIf
End Method

Method FPS()
Return Self.CurrFps
End Method

Method CurrDelta:Float()
Return Double(Self.ElapsedTime)/Self.dDelta
End Method

Method SetFPS(NewFPS:Float)
Self.dFPS = NewFPS
Self.dDelta = 1000/Self.dFPS
End Method

End Type



SetMaskColor(255,0,255)
'Global blob:TImage = LoadImage("C:\blitzmax\samples\simonh\fireworks\spark.png")


Global Delta1:DeltaTime = DeltaTime.Create(35.0) ' Normal 35 FPS
Local Delta:Double

Global ScreenUpdate:Double
Global bx:Double=10

While Not KeyHit(KEY_ESCAPE)





Delta1.Update()

Delta = Delta1.CurrDelta()

If Delta > 1 Then Delta = 1 ' Delta should never be bigger than 1.

' -------------------------------GFX FUNCTIONS ---------------------------
ScreenUpdate = ScreenUpdate + Delta


If ScreenUpdate > 0.30 Then
Cls

RenderGFX(ScreenUpdate)
ScreenUpdate :- 0.3
Flip
EndIf

' ------------------------------------------------------------------------

'FlushMem

Wend


Function RenderGFX(Delta:Double=1)
DrawOval bx,100,10,10

bx=bx+(Delta * 2)

DrawText Delta1.FPS()+" FPS",10,10

If bx>799 Then bx = 0
End Function


Here is the code for the routine. Can anyone tell me what's wrong and how I can mend it ?


VomitOnLino(Posted 2006) [#2]
I have managed to fix most of the problems but one still presists - if the game is not highlighted for a longer time (several seconds) the timing seems to get f**ked up really bad.

Is there any way to remedy this - come on people don't let me sit there and help me! :(


Dreamora(Posted 2006) [#3]
It should go on if you use eventhooks
But polled stuff will pause when the window looses focus.


VomitOnLino(Posted 2006) [#4]
Thanks for your answer! :) What kind of polled stuff ?

I noticed that it also happens when the window is focussed the entire time - it takes some time but it happens... what's happening there ?

The game slows down to about 1/5th of the speed- running smoothly so I guess delta time somehow messes up - but how ?

EDIT: Trying fullscreen now - I will report back with my findings.

Yup. Happens in fullscreen aswell, so the window/focus eventhook thing is temporarily out of question - what is wrong with my delta algorithm I just can't seem to get it.

It works like this (pseudocode)

init deltatime
set fps to 45

game mainloop

if screenupdate > 0.75 then
do game stuff
draw stuff
call some types that use (the same) delta aswell
end if

repeat mainloop



Dreamora(Posted 2006) [#5]
hmm
this indead is strange ...


polled: everything that does not work through EventID and the event command set


VomitOnLino(Posted 2006) [#6]
Yeah... I'm trying to find some way around this but I can't seem to recognize a sheme behind this it seems to happen completely randomly and out of the blue. I will check it on my other PC asap.


TartanTangerine (was Indiepath)(Posted 2006) [#7]



VomitOnLino(Posted 2006) [#8]
Thanks indiepath, but I already am using your routine - however played around with your code. And I found out that if you add a small delay to the drawing cycle of your example, the ball does in fact _not_ travel at the same speed across the screen. Or did I misunderstand the concept of deltatime ?

I tought the ball would "lag" but still pass the screen in one second but even with a delay of 4 it takes much much longer!


Function RenderGFX(Delta:Double=1)
DrawOval bx,100,10,10
bx:+2*Delta
DrawText Delta1.FPS()+" FPS",10,10
If bx>799 Then bx = 0
Delay (4)
End Function


TartanTangerine (was Indiepath)(Posted 2006) [#9]
I was aware that you used my code and I reposted because you made some errors.

The reason that the Delay(4) slows everything down is because you've capped delta at 1. Remove the line that caps it and everything will be normal.


VomitOnLino(Posted 2006) [#10]
Yeah I have uncapped the delta. But still the blob moves more slowly with the delay.
I can't seem to get it to work - if you tell me how to post those neat code scrollbars I will repost the edited code.


TartanTangerine (was Indiepath)(Posted 2006) [#11]
[ codebox ] [ /codebox ]

remove the spaces


VomitOnLino(Posted 2006) [#12]

This only slightly modified code does not always run the same speed depending on the delay - it's really weird. Maybe you can tell me/correct me what I'm doing wrong.


TartanTangerine (was Indiepath)(Posted 2006) [#13]

Seems to be an issue with the Screenupdate variable - I've not got time right now to fix that. Try this code, it works!


VomitOnLino(Posted 2006) [#14]
Yup that one works... I'll try to look into the Screenupdate problem, because if I call all the gameupdating stuff every loop the game bogs down to a crawl on slower systems.