SDL 2.0 中是否内置了帧率限制?

3
我想知道SDL 2.0是否有帧速率限制。如果有,如何修改默认限制?
我正在使用C++和SDL 2.0创建游戏引擎。当我将渲染后的帧速率显示到屏幕上时,结果不断在58到62帧之间波动。
我觉得这很奇怪,因为无论我在屏幕上渲染多少图像,或者我在一个周期中放入多少逻辑代码,显示的帧速率始终保持在58到62帧之间。
这是我的帧速率计算方法:
// FPS
//Record the current system ticks
mSystemTicksCurrent = Timer::GetSystemTicks();

//Calculate the time it takes for a single cycle to run
mSingleCycle = mSystemTicksCurrent - mSystemTicksPrevious;

//to prevent division by zero...
if(mSingleCycle != 0)
{
    mFPS = 1000.0f / mSingleCycle;
}
//...indicate that a miniscule amount of time has elapsed, thus leading to an extremely fast frame rate
else
{
    mFPS = 9999.9f;
}


//Since we are done calculating the time it has taken for a single cycle to elapse,
//record the time that was used to calculate the elapsed cycle time for future use.
mSystemTicksPrevious = mSystemTicksCurrent;

现在,我将计算出的帧率渲染到屏幕上:

if(mpTimerFPS != nullptr)
{
    if(mpTimerFPSText != nullptr)
    {
        sprintf_s(mTimerFPSTextBuff, "FPS: %.1f", mFPS);
        mpTimerFPSText->SetTexture(Window::UpdateText(mTimerFPSTextBuff, mpTimerFPSFont, mTimerFPSColor));
    }
}

Timer::GetSystemTicks()函数的简单实现如下:

Uint32 Timer::GetSystemTicks()
{
    return timeGetTime();
}

如果需要,我可以提供更多的代码。


我不确定,但我想象SDL被设置为不会比屏幕的刷新率(通常为60Hz)更快地渲染。超过这个速率显示有什么意义呢?如果你想要其他方面更快的速率(例如物理),那么在单独的线程上运行它。 - Mikael Persson
timeGetTime() 不是很精确。在 Windows 上它的精度通常大约为16毫秒,这将给出最多 62 帧每秒的结果。 - Collin Dauphinee
2
你是否使用 SDL_RENDERER_PRESENTVSYNC 标志创建了渲染器?https://wiki.libsdl.org/SDL_RendererFlags?highlight=%28%5CbCategoryEnum%5Cb%29%7C%28SDLEnumTemplate%29 - olevegard
我使用SDL2(硬件加速渲染器)测试了SDL平均FPS测量中概述的方法,最后一种方法(最后一秒的FPS)显示大约3500个ticks。你也可以尝试一下。无论如何,我不认为SDL2限制帧率。 - jpw
1个回答

13

测试你的响应后,我确定渲染器受到屏幕刷新率的限制,因为我在创建渲染器时使用了SDL_RENDERER_PRESENTVSYNC标记。以下代码行是限制帧速率的代码:

mpRenderer.reset(SDL_CreateRenderer(mpWindow.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
即使我去掉了SDL_RENDERER_ACCELERATED标志,帧率仍然保持在监视器设置的刷新速度上。我通过将显示器的刷新率从60Hz更改为75Hz进行测试并证明了这一点,我的测试结果为74-77 fps。一旦我去掉了SDL_RENDERER_PRESENTVSYNC标志,帧率就会飙升。

另外提示:我使用了timeGetTime()以及其他提取计时器函数进行了测试,所有结果都相同。

SDL_CreateRenderer()的参考文献:https://wiki.libsdl.org/SDL_CreateRenderer


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接