SDL2 - Vsync 不工作 [英] SDL2 - Vsync not working
问题描述
我在我的程序中使用了 vsync,在我最小化窗口之前它工作正常.我在创建渲染器时这样做了:
I use vsync in my program, and it works fine until I minimize the window. I did this when I created the renderer:
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
这是游戏循环:
while (running)
{
checkEvent();
handleKeyboard(timer.getDelta());
render();
}
它为我提供了稳定的每秒 60 帧,但是当我最小化窗口时,我每秒获得超过 100000 帧.为什么会这样?
It gives me a stable 60 frames per second, but I get more than 100000 frames per second when I minimize the window. Why is that happening?
推荐答案
可能只是 SDL 中的一个错误.如果您对问题进行了更多调查:阅读文档做一些测试,您可以报告错误.它很可能很快就会被修复.瑞安和同事们工作得很好.:)
Probably just a bug in SDL. If you investigated the problem more: read docs do some tests, you can report the bug. It is likely going to be fixed pretty soon. Ryan and colleagues work great. :)
话虽如此.我永远不会假设 Vsync 适用于每个系统.您可能想要添加自己的帧率限制系统.依靠硬件来限制帧率是个坏主意.
That being said. I would never assume that Vsync works on every system. You probably want to add in your own framerate limitation system. Relying on the hardware to limit the framerate is a bad idea.
我在游戏中使用它来限制帧率:
I use this in my game to limit the framerate:
while (!gameLoop->done)
{
int start = SDL_GetTicks();
gameLoop->update();
int time = SDL_GetTicks() - start;
if (time < 0) continue; // if time is negative, the time probably overflew, so continue asap
int sleepTime = gameLoop->millisecondsForFrame - time;
if (sleepTime > 0)
{
SDL_Delay(sleepTime);
}
}
这篇关于SDL2 - Vsync 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!