这是我处理游戏循环的方式:
while (running) {
diff = duration_cast<milliseconds>(end - start).count();
start = clock::now();
dt = diff / (16.0);
handleInput(); // get input
update(dt); // game logic
render(); // render game
SDL_GL_SwapWindow(window); // swap frame buffer
end = clock::now();
}
它旨在成为锁定到 60FPS 的固定时间步游戏(它是 SNES 游戏的重制模拟),但它在我的 144hz 屏幕上以 144 时间步运行,速度太快了。 Vsync 无法解决这个问题,那有什么可以呢?
最佳答案
这是一个如何实现游戏循环的简单示例:
int32_t tickInteval = 1000/FPS; // frequency in Hz to period in ms
uint32_t lastUpdateTime = 0;
int32_t deltaTime = 0;
while (running) { // running condition
uint32_t currentTime = SDL_GetTicks();
deltaTime = currentTime - lastUpdateTime;
int32_t timeToSleep = tickInteval - deltaTime;
if(timeToSleep > 0)
{
SDL_Delay(timeToSleep); // energy saving
}
update(deltaTime); // game logic
lastUpdateTime = currentTime;
}
我建议仔细研究这个主题。
更新。
人们可能会担心 uint32_t
溢出。是的,它会溢出。经过将近两个月的不间断游戏运行(准确地说是 49.7 天)。那么会发生什么? currentTime
将是一个非常小的正整数,lastUpdateTime
将是一个非常大的正整数。但是二相减无论如何都不会溢出。此外,如果差异不适合 int32_t
,它将围绕 UINT_MAX + 1
的模进行包装,从而产生一个小的正整数,这将是这两个值的精确滴答数不同(关于一个的无符号溢出)。
关于c++ - SDL/OpenGL 游戏在 144Hz 屏幕上运行速度过快;无法使用垂直同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40854974/