我想知道SDL 2.0中是否有幀速率上限?如果是這樣,那麼如何修改默認上限?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));
}
}
當定時器:: GetSystemTicks( )功能簡單如下:
Uint32 Timer::GetSystemTicks()
{
return timeGetTime();
}
如果需要,我可以提供更多的代碼。
我不知道是肯定的,但我會想象SDL被設置爲不提供任何超過屏幕的刷新率(通常爲60Hz)的速度更快。以更快的速度顯示的點是什麼?如果你想爲其他東西(例如物理)加快速度,那麼在一個單獨的線程上循環運行它。 –
'timeGetTime()'不準確。它在Windows上的精確度通常在16毫秒左右,這將會導致62 FPS。 –
您是否使用SDL_RENDERER_PRESENTVSYNC標誌創建了渲染器? https://wiki.libsdl.org/SDL_RendererFlags?highlight=%28%5CbCategoryEnum%5Cb%29%7C%28SDLEnumTemplate%29 – olevegard