So... whats wrong with my timing loop thats crippling it so much? I've tried to set and FPS of 200, but as soon as I do that, it tops out at 65fps. If I remove a couple of lines (the bit that makes it work), it jumps up to 350 again. So somethings rotten right in the state of Denmark... So, heres the timing loop, perhaps someone out there will spot it and save me staring at boring code for hours on end.....
long tLastGameFrame = System.currentTimeMillis();
long tNextGameFrame = tLastGameFrame + FPSRate;
Last = tLastGameFrame;
while (true)
{
long tNow = System.currentTimeMillis();
// Quick check to see it timer has gone backwards!!
if( tNow < tLastGameFrame ){
tLastGameFrame= tNow;
}
// Check to see if system is stuttering due background OS stuff.
// if jumped ahead over 5 seconds, then just reset...
if( (tNow-tNextGameFrame) > 5000 ){
tNextGameFrame = tNow;
}
// Timer has gone backwards
if( tNow < tLastGameFrame ){
tLastGameFrame= tNow;
}
dTime = (int)(tNow - Last);
if (dTime >= 1000){
dTime -= 1000; // dont get 0 (1000/1001)
actualfps = CurrentFPSCount;
CurrentFPSCount=0;
Last = tNow;
}
// Now the main timing loop
if( tNextGameFrame >= tNow )
{
// do nothing...or yield/sleep
} else
{
while( tNow>tNextGameFrame){
tLastGameFrame = tNextGameFrame;
tNextGameFrame += FPSRate;
}
CurrentFPSCount++;
}
And there you go... pretty simple. Oh, and the reason for the backwards check is because in the modern world of clock syncing (to time.com or something), your clock might briefly go backwards. If this happens timer loops break - had this happen twice! Once on mobile phones, and once on a PC. Took me AGES to figure out the Mobile phone one, but fortunately I remembered about it for the PC.
No comments:
Post a Comment