Browse Source

Don't sleep in FramerateManager::framerateDelay() if VSync is enabled

Alexander Wilms 2 years ago
parent
commit
bfa5ef7990
2 changed files with 14 additions and 6 deletions
  1. 12 6
      client/gui/FramerateManager.cpp
  2. 2 0
      client/gui/FramerateManager.h

+ 12 - 6
client/gui/FramerateManager.cpp

@@ -11,22 +11,28 @@
 #include "StdInc.h"
 #include "FramerateManager.h"
 
+#include "../../lib/CConfigHandler.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);
 }
 
 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)
+	{
+		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);
+	}
 
 	// 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)

+ 2 - 0
client/gui/FramerateManager.h

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