我正在尝试为 Game Boy 模拟器实现高分辨率时序。一个 16-17 毫秒的计时器足以让仿真以大致正确的速度进行,但它最终会失去与 BGB 等精确仿真的同步。
我最初在 while 循环中使用 QElapsedTimer。这给出了预期的结果并与 BGB 保持同步,但感觉真的很草率,并且由于不断运行的 while 循环,它尽可能多地消耗 CPU 时间。它还使程序在关闭后驻留在任务管理器中。我尝试使用一毫秒 QTimer 来实现它,该 QTimer 在执行下一帧之前测试 QElapsedTimer。尽管分辨率降低了,但由于检查了 QElapsedTimer,我认为时间会平均到正确的速度。这是我目前拥有的:
void Platform::start() {
nanoSecondsPerFrame = 1000000000 / system->getRefreshRate();
speedRegulationTimer->start();
emulationUpdateTimer->start(1);
}
void Platform::executionLoop() {
qint64 timeDelay;
if (frameLocked == true)
timeDelay = nanoSecondsPerFrame;
else
timeDelay = 0;
if (speedRegulationTimer->nsecsElapsed() >= timeDelay) {
speedRegulationTimer->restart();
// Execute the cycles of the emulated system for one frame.
system->setControllerInputs(buttonInputs);
system->executeCycles();
if (system->getIsRunning() == false) {
this->stop();
errorMessage = QString::fromStdString(system->getSystemError());
}
//timeDelay = speedRegulationTimer->nsecsElapsed();
FPS++;
}
}
对于 59.73 Hz 的刷新率,nanoSecondsPerFrame 计算为 16742005。speedRegulationTimer 是 QElapsedTimer。emulationUpdateTimer 是一个设置为 Qt:PreciseTimer 的 QTimer,并连接到 executionLoop。仿真确实运行,但运行速度约为 50-51 FPS,而不是预期的 59-60 FPS。这绝对是由于时间问题,因为在没有时间限制的情况下运行它会导致帧速率呈指数级增长。要么我的代码有明显的疏忽,要么计时器没有像我预期的那样工作。如果有人看到明显的问题或可以就此提供一些建议,我将不胜感激。