1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00

Merge pull request #2952 from Alexander-Wilms/develop

Enable VSync to prevent screen tearing while scrolling across map
This commit is contained in:
Nordsoft91 2023-10-01 14:08:00 +02:00 committed by GitHub
commit 041ffceb07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 527 additions and 486 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

@ -11,11 +11,15 @@
#include "StdInc.h"
#include "FramerateManager.h"
#include "../../lib/CConfigHandler.h"
#include <SDL_video.h>
FramerateManager::FramerateManager(int targetFrameRate)
: targetFrameTime(Duration(boost::chrono::seconds(1)) / targetFrameRate)
, lastFrameIndex(0)
, lastFrameTimes({})
, lastTimePoint (Clock::now())
, lastTimePoint(Clock::now())
, vsyncEnabled(settings["video"]["vsync"].Bool())
{
boost::range::fill(lastFrameTimes, targetFrameTime);
}
@ -24,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);

View File

@ -274,8 +274,14 @@ void ScreenHandler::initializeWindow()
handleFatalError(message, true);
}
//create first available renderer if preferred not set. Use no flags, so HW accelerated will be preferred but SW renderer also will possible
mainRenderer = SDL_CreateRenderer(mainWindow, getPreferredRenderingDriver(), 0);
// create first available renderer if no preferred one is set
// use no SDL_RENDERER_SOFTWARE or SDL_RENDERER_ACCELERATED flag, so HW accelerated will be preferred but SW renderer will also be possible
uint32_t rendererFlags = 0;
if(settings["video"]["vsync"].Bool())
{
rendererFlags |= SDL_RENDERER_PRESENTVSYNC;
}
mainRenderer = SDL_CreateRenderer(mainWindow, getPreferredRenderingDriver(), rendererFlags);
if(mainRenderer == nullptr)
throw std::runtime_error("Unable to create renderer\n");
@ -570,4 +576,4 @@ bool ScreenHandler::hasFocus()
{
ui32 flags = SDL_GetWindowFlags(mainWindow);
return flags & SDL_WINDOW_INPUT_FOCUS;
}
}

View File

@ -149,7 +149,8 @@
"driver",
"displayIndex",
"showfps",
"targetfps"
"targetfps",
"vsync"
],
"properties" : {
"resolution" : {
@ -207,6 +208,10 @@
"targetfps" : {
"type" : "number",
"default" : 60
},
"vsync" : {
"type" : "boolean",
"default" : true
}
}
},

View File

@ -83,6 +83,8 @@ void CSettingsView::loadSettings()
ui->spinBoxInterfaceScaling->setValue(settings["video"]["resolution"]["scaling"].Float());
ui->spinBoxFramerateLimit->setValue(settings["video"]["targetfps"].Float());
ui->spinBoxFramerateLimit->setDisabled(settings["video"]["vsync"].Bool());
ui->checkBoxVSync->setChecked(settings["video"]["vsync"].Bool());
ui->spinBoxReservedArea->setValue(std::round(settings["video"]["reservedWidth"].Float() * 100));
ui->comboBoxFriendlyAI->setCurrentText(QString::fromStdString(settings["server"]["friendlyAI"].String()));
@ -494,6 +496,13 @@ void CSettingsView::on_spinBoxFramerateLimit_valueChanged(int arg1)
node->Float() = arg1;
}
void CSettingsView::on_checkBoxVSync_stateChanged(int arg1)
{
Settings node = settings.write["video"]["vsync"];
node->Bool() = arg1;
ui->spinBoxFramerateLimit->setDisabled(settings["video"]["vsync"].Bool());
}
void CSettingsView::on_comboBoxEnemyPlayerAI_currentTextChanged(const QString &arg1)
{
Settings node = settings.write["server"]["playerAI"];

View File

@ -62,6 +62,8 @@ private slots:
void on_spinBoxFramerateLimit_valueChanged(int arg1);
void on_checkBoxVSync_stateChanged(int arg1);
void on_comboBoxEnemyPlayerAI_currentTextChanged(const QString &arg1);
void on_comboBoxAlliedPlayerAI_currentTextChanged(const QString &arg1);

File diff suppressed because it is too large Load Diff