Khi*_*rus 2 c++ sdl frame-rate sdl-2
我想知道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;
Run Code Online (Sandbox Code Playgroud)
现在我采用计算出的帧速率并将其渲染到屏幕上:
if(mpTimerFPS != nullptr)
{
if(mpTimerFPSText != nullptr)
{
sprintf_s(mTimerFPSTextBuff, "FPS: %.1f", mFPS);
mpTimerFPSText->SetTexture(Window::UpdateText(mTimerFPSTextBuff, mpTimerFPSFont, mTimerFPSColor));
}
}
Run Code Online (Sandbox Code Playgroud)
Timer :: GetSystemTicks()函数的位置如下:
Uint32 Timer::GetSystemTicks()
{
return timeGetTime();
}
Run Code Online (Sandbox Code Playgroud)
如有必要,我可以提供更多代码.
Khi*_*rus 11
测试完回复后,我确定渲染器确实受到屏幕刷新率的限制,因为我SDL_RENDERER_PRESENTVSYNC在创建渲染器时使用了标记.以下代码行是限制帧速率的原因:
mpRenderer.reset(SDL_CreateRenderer(mpWindow.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
Run Code Online (Sandbox Code Playgroud)
即使我删除了该SDL_RENDERER_ACCELERATED标志,帧速率也会保留在监视器设置为刷新的任何位置.我通过将显示器的刷新率改为75Hz而不是60Hz来测试并证明这一点,我的测试增加到74-77 fps.我删除SDL_RENDERER_PRESENTVSYNC标志的那一刻,帧速率猛增.
作为旁注:我使用timeGetTime()以及其他滴答计数检索函数对此进行了测试,并且都返回了相同的结果.
参考SDL_CreateRenderer():https://wiki.libsdl.org/SDL_CreateRenderer