c++ - SDL/OpenGL 游戏在 144Hz 屏幕上运行速度过快;无法使用垂直同步

标签 c++ sdl game-engine vsync

这是我处理游戏循环的方式:

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/

相关文章:

c# - 游戏中的对象如何交流

graphics - 在计算机图形学中,UVW 贴图和 UV 贴图有什么区别?

c# - 线程化依赖于 C# 中另一个类的函数 (Unity)

c++ - 在 gcc 4.8 中获取 operator<< 的地址失败

c++ - make_shared 与模板构造函数

c++ - Qt 5.2.1 无法在 mac OSX 10.8.5 上构建

c++ - 开关(event.type)不工作并且计时器不显示

c++ - 错误 `QObject` 子类和复制构造函数 : `QObject::QObject(const QObject&) is private`

events - 如何在 SDL 中检查或检测窗口是否最小化或未激活?

c - SDL 音频回拨不起作用?