1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-24 03:47:18 +02:00

Fix freezing of hero and long enemy turns without sleeping in FramerateManager::framerateDelay()

This commit is contained in:
Alexander Wilms 2023-09-27 16:09:52 +02:00
parent 0d11c5197d
commit f52562eeb7
3 changed files with 14 additions and 18 deletions

View File

@ -129,11 +129,10 @@ void CGuiHandler::renderFrame()
CCS->curh->render();
SDL_RenderPresent(mainRenderer);
windows().onFrameRendered();
}
SDL_RenderPresent(mainRenderer);
framerate().framerateDelay(); // holds a constant FPS
}

View File

@ -15,22 +15,12 @@
#include <SDL_video.h>
FramerateManager::FramerateManager(int targetFrameRate)
: lastFrameIndex(0)
: targetFrameTime(Duration(boost::chrono::seconds(1)) / targetFrameRate)
, lastFrameIndex(0)
, lastFrameTimes({})
, lastTimePoint(Clock::now())
, vsyncEnabled(settings["video"]["vsync"].Bool())
{
if(settings["video"]["vsync"].Bool())
{
static int display_in_use = settings["video"]["displayIndex"].Integer();
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(display_in_use, &mode);
int displayRefreshRate = mode.refresh_rate;
logGlobal->info("Display refresh rate is %d", displayRefreshRate);
targetFrameTime = Duration(boost::chrono::seconds(1)) / displayRefreshRate;
} else
{
targetFrameTime = Duration(boost::chrono::seconds(1)) / targetFrameRate;
}
boost::range::fill(lastFrameTimes, targetFrameTime);
}
@ -38,9 +28,14 @@ void FramerateManager::framerateDelay()
{
Duration timeSpentBusy = Clock::now() - lastTimePoint;
// FPS is higher than it should be, then wait some time
if(timeSpentBusy < targetFrameTime)
boost::this_thread::sleep_for(targetFrameTime - timeSpentBusy);
if(!vsyncEnabled)
{
// if FPS is higher than it should be, then wait some time
if(timeSpentBusy < targetFrameTime)
{
boost::this_thread::sleep_for(targetFrameTime - timeSpentBusy);
}
}
// compute actual timeElapsed taking into account actual sleep interval
// limit it to 100 ms to avoid breaking animation in case of huge lag (e.g. triggered breakpoint)

View File

@ -25,6 +25,8 @@ class FramerateManager
/// index of last measured frome in lastFrameTimes array
ui32 lastFrameIndex;
bool vsyncEnabled;
public:
FramerateManager(int targetFramerate);