我想知道SDL 2.0是否有帧速率限制。如果有,如何修改默认限制?
我正在使用C++和SDL 2.0创建游戏引擎。当我将渲染后的帧速率显示到屏幕上时,结果不断在58到62帧之间波动。
我觉得这很奇怪,因为无论我在屏幕上渲染多少图像,或者我在一个周期中放入多少逻辑代码,显示的帧速率始终保持在58到62帧之间。
这是我的帧速率计算方法:
我正在使用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();
}
如果需要,我可以提供更多的代码。
timeGetTime()
不是很精确。在 Windows 上它的精度通常大约为16毫秒,这将给出最多 62 帧每秒的结果。 - Collin DauphineeSDL_RENDERER_PRESENTVSYNC
标志创建了渲染器?https://wiki.libsdl.org/SDL_RendererFlags?highlight=%28%5CbCategoryEnum%5Cb%29%7C%28SDLEnumTemplate%29 - olevegard