Hey all. I'm working on a simple shmup game. Technical details of the project:
-Using VC++ 2008 Express
-Using SFML
-My machine is a WinXP Pro one.
Let me start by saying:
I DO NOT want to use delta time. It's not necessary in this case. Most of these games I've seen regulate time based on frame rate and that's the approach I want to take.
Anyway, SFML's built-in timing functions are no good, so I've taken the approach of using the windows ones and seem to be having some success, though I'm not certain if my approach is the best.
This is my regulation code (doesn't have class includes):
void TimeController::FrameRegulation()
{ int fps=60;
GameClass& game = GameClass::GetTheGame();
signed long dif = (signed)(GetTix() - nLastTick );
nLastTick += dif;
nAccum += dif * fps;
if(nAccum < 1000) // not yet time for next frame
{ if(nAccum < nMinSleepThreshold)
Sleep(1);
return;
}
// we will be running at least 1 frame here.
// see if we need to run more (falling behind)
if(nAccum >= (nSkipFramesBehind*1000))
{ if(nFramesSkipped >= nMaxFrameSkip)
ResyncFrameTime();
else
{ ++nFramesSkipped;
nAccum -= 1000;
game.GameLogic();
return;
}
}
// run/draw a frame
nAccum -= 1000;
nFramesSkipped = 0;
game.GameLogic();
game.GameRender();
}
//This is called before the game loop starts.
void TimeController::ResyncFrameTime()
{ nFramesSkipped = 0;
nLastTick = GetTix();
nAccum = 0;
StartTiming();
}
//Get the ticks using the seemingly accurate QPC!!!
unsigned long TimeController::GetTix()
{ LARGE_INTEGER freq;
LARGE_INTEGER tick;
if(!QueryPerformanceFrequency(&freq) || !freq.QuadPart)
return GetTickCount(); // QPC not supported, fall back to GetTickCount
QueryPerformanceCounter(&tick);
return (unsigned long)(tick.QuadPart * 1000 / freq.QuadPart);
}
It DOES seem to be consistent on my computer along with some other people's (though some report lag... Might be the under-sleeping), but what I'm wondering is if anyone's taken the approach of just regulating frame rate and has maybe done it better.
If anyone cares to test the app as it is, I have it here:
http://sivak.nintendoage.com/Shmuppy.zipNote: The in-game FPS reader is wrong. If you have Fraps, try that.
Note #2: If you drag the window or press alt, the game goes insane fast. I'm wondering how this will be fixable...