私は現在、物理ベースのゲーム用の 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
}
}
}