question for you guys, what is the best way to go about calculating an automatic frameskip value? this is what i've got in the FreeBASIC version of my emulator. wondering if there's a more accurate/better method, or if there are any inherent problems in this algorithm?
Do
#Ifdef __FB_WIN32__
QueryPerformanceCounter(CPtr(Any Ptr, @curtimer))
#Else 'then it's linux
gettimeofday(@timing, NULL)
curtimer = timing.tv_usec
#EndIf
'calculate auto-frameskip
timertemp = curtimer - lasttimer
If timertemp >= (timerfreq / ((60/frameskip)\2)) Then frameskip = frameskip + 1
If timertemp < (timerfreq / 60) Then frameskip = frameskip - 1
If frameskip > 30 Then frameskip = 30 'limit max frameskip count.. if your system is THAT slow, you shouldn't even be trying this.
If frameskip < 1 Then frameskip = 1
'end auto-frameskip calcs
If timertemp >= (timerfreq / 60) Then Exit Do
#Ifdef __FB_WIN32__
SwitchToThread() 'give up remainder of CPU timeslice if we shouldn't resume yet
#EndIf
Loop
Another way to think of automatic frameskip is "Skip Video Frames When Behind". So you look at time. If you're ahead, you're fine. If you're behind, repeatedly skip frames (up to a maximum) until you are on time again.
I'll go copy-paste the Actionscript 1.x code I made for autoframeskip in a flash game here... Code's a little bit messy, hopefully you can figure out some kind of logic from this, some variables aren't even used.
yeah, that looks like a pretty good way to do it. it might be slightly more accurate that way, i'm going to play around with both methods and see how they do.