Browse Source

More smooth fps counter

AlexVinS 10 years ago
parent
commit
5f7a90237c
2 changed files with 16 additions and 4 deletions
  1. 15 3
      client/gui/CGuiHandler.cpp
  2. 1 1
      client/gui/CGuiHandler.h

+ 15 - 3
client/gui/CGuiHandler.cpp

@@ -547,6 +547,8 @@ CFramerateManager::CFramerateManager(int rate)
 	this->rate = rate;
 	this->rateticks = (1000.0 / rate);
 	this->fps = 0;
+	this->accumulatedFrames = 0;
+	this->accumulatedTime = 0;
 }
 
 void CFramerateManager::init()
@@ -558,18 +560,28 @@ void CFramerateManager::framerateDelay()
 {
 	ui32 currentTicks = SDL_GetTicks();
 	timeElapsed = currentTicks - lastticks;
-
+	
 	// FPS is higher than it should be, then wait some time
 	if (timeElapsed < rateticks)
 	{
 		SDL_Delay(ceil(this->rateticks) - timeElapsed);
 	}
-	currentTicks = SDL_GetTicks();
+	
+	accumulatedTime += timeElapsed;
+	accumulatedFrames++;
 
-	fps = ceil(1000.0 / timeElapsed);
+	if(accumulatedFrames >= 100)
+	{
+		//about 2 second should be passed
+		fps = ceil(1000.0 / (accumulatedTime/accumulatedFrames));		
+		accumulatedTime = 0;
+		accumulatedFrames = 0;	
+	};	
 
+	currentTicks = SDL_GetTicks();
 	// recalculate timeElapsed for external calls via getElapsed()
 	// limit it to 1000 ms to avoid breaking animation in case of huge lag (e.g. triggered breakpoint)
 	timeElapsed = std::min<ui32>(currentTicks - lastticks, 1000);
+
 	lastticks = SDL_GetTicks();
 }

+ 1 - 1
client/gui/CGuiHandler.h

@@ -29,7 +29,7 @@ private:
 	double rateticks;
 	ui32 lastticks, timeElapsed;
 	int rate;
-
+	ui32 accumulatedTime,accumulatedFrames;
 public:
 	int fps; // the actual fps value