我目前有一些接近以下實現的基於物理的遊戲的FPS獨立遊戲循環。它在我測試過的每臺電腦上運行得非常好,在幀速下降時保持遊戲速度一致。然而,我將要移植到嵌入式設備上,這些嵌入式設備可能會在視頻方面更加艱難,我想知道它是否還會削減芥末。這是一個FPS獨立遊戲循環的良好實現嗎?
編輯:
對於這個問題,假設毫秒()返回該程序運行毫秒經過的時間。 msecs的實現在不同平臺上有所不同。這個循環也在不同的平臺上以不同的方式運行。
#define MSECS_PER_STEP 20
int stepCount, stepSize; // these are not globals in the real source
void loop() {
int i,j;
int iterations =0;
static int accumulator; // the accumulator holds extra msecs
static int lastMsec;
int deltatime = msec() - lastMsec;
lastMsec = msec();
// deltatime should be the time since the last call to loop
if (deltatime != 0) {
// iterations determines the number of steps which are needed
iterations = deltatime/MSECS_PER_STEP;
// save any left over millisecs in the accumulator
accumulator += deltatime%MSECS_PER_STEP;
}
// when the accumulator has gained enough msecs for a step...
while (accumulator >= MSECS_PER_STEP) {
iterations++;
accumulator -= MSECS_PER_STEP;
}
handleInput(); // gathers user input from an event queue
for (j=0; j<iterations; j++) {
// here step count is a way of taking a more granular step
// without effecting the overall speed of the simulation (step size)
for (i=0; i<stepCount; i++) {
doStep(stepSize/(float) stepCount); // forwards the sim
}
}
}
我知道我可能不應該在整數中存儲時間的東西,但這不是真的我在這裏問 – 2010-10-18 16:08:01