Skip to content

Commit 605b9b3

Browse files
authored
tweak(fps): Simplify function W3DDisplay::updateAverageFPS() and remove its frame spike filter (#1662)
1 parent 341bef2 commit 605b9b3

File tree

6 files changed

+36
-76
lines changed

6 files changed

+36
-76
lines changed

Generals/Code/GameEngine/Include/Common/GameEngine.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class GameEngine : public SubsystemInterface
7878

7979
virtual void setFramesPerSecondLimit( Int fps ); ///< Set the max render and engine update fps.
8080
virtual Int getFramesPerSecondLimit( void ); ///< Get the max render and engine update fps.
81-
Real getUpdateTime(); ///< Get the last engine update delta time.
81+
Real getUpdateTime(); ///< Get the last engine update delta time in seconds.
8282
Real getUpdateFps(); ///< Get the last engine update fps.
8383

8484
static Bool isTimeFrozen(); ///< Returns true if a script has frozen time.
@@ -128,7 +128,7 @@ class GameEngine : public SubsystemInterface
128128
Int m_maxFPS; ///< Maximum frames per second for rendering
129129
Int m_logicTimeScaleFPS; ///< Maximum frames per second for logic time scale
130130

131-
Real m_updateTime; ///< Last engine update delta time
131+
Real m_updateTime; ///< Last engine update delta time in seconds
132132
Real m_logicTimeAccumulator; ///< Frame time accumulated towards submitting a new logic frame
133133

134134
Bool m_quitting; ///< true when we need to quit the game

Generals/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DDisplay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class W3DDisplay : public Display
161161
void drawCurrentDebugDisplay( void ); ///< draws current debug display
162162
void calculateTerrainLOD(void); ///< Calculate terrain LOD.
163163
void renderLetterBox(UnsignedInt time); ///< draw letter box border
164-
void updateAverageFPS(void); ///< figure out the average fps over the last 30 frames.
164+
void updateAverageFPS(void); ///< calculate the average fps over the last 30 frames.
165165

166166
Byte m_initialized; ///< TRUE when system is initialized
167167
LightClass *m_myLight[LightEnvironmentClass::MAX_LIGHTS]; ///< light hack for now

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
static void drawFramerateBar(void);
3535

3636
// SYSTEM INCLUDES ////////////////////////////////////////////////////////////
37+
#include <numeric>
3738
#include <stdlib.h>
3839
#include <windows.h>
3940
#include <io.h>
@@ -835,21 +836,16 @@ void W3DDisplay::reset( void )
835836

836837
const UnsignedInt START_CUMU_FRAME = LOGICFRAMES_PER_SECOND / 2; // skip first half-sec
837838

838-
/** Update a moving average of the last 30 fps measurements. Also try to filter out temporary spikes.
839-
This code is designed to be used by the GameLOD sytems to determine the correct dynamic LOD setting.
840-
*/
841839
void W3DDisplay::updateAverageFPS(void)
842840
{
843-
const Real MaximumFrameTimeCutoff = 0.5f; //largest frame interval (seconds) we accept before ignoring it as a momentary "spike"
844-
const Int FPS_HISTORY_SIZE = 30; //keep track of the last 30 frames
841+
constexpr const Int FPS_HISTORY_SIZE = 30;
845842

846843
static Int64 lastUpdateTime64 = 0;
847844
static Int historyOffset = 0;
848-
static Int numSamples = 0;
849-
static double fpsHistory[FPS_HISTORY_SIZE];
845+
static Real fpsHistory[FPS_HISTORY_SIZE] = {0};
850846

851-
Int64 freq64 = getPerformanceCounterFrequency();
852-
Int64 time64 = getPerformanceCounter();
847+
const Int64 freq64 = getPerformanceCounterFrequency();
848+
const Int64 time64 = getPerformanceCounter();
853849

854850
#if defined(RTS_DEBUG)
855851
if (TheGameLogic->getFrame() == START_CUMU_FRAME)
@@ -858,37 +854,21 @@ void W3DDisplay::updateAverageFPS(void)
858854
}
859855
#endif
860856

861-
Int64 timeDiff = time64 - lastUpdateTime64;
857+
const Int64 timeDiff = time64 - lastUpdateTime64;
862858

863859
// convert elapsed time to seconds
864-
double elapsedSeconds = (double)timeDiff/(double)(freq64);
865-
866-
if (elapsedSeconds <= MaximumFrameTimeCutoff) //make sure it's not a spike
867-
{
868-
// append new sameple to fps history.
869-
if (historyOffset >= FPS_HISTORY_SIZE)
870-
historyOffset = 0;
860+
Real elapsedSeconds = (Real)timeDiff/(Real)freq64;
871861

872-
m_currentFPS = 1.0/elapsedSeconds;
873-
fpsHistory[historyOffset++] = m_currentFPS;
874-
numSamples++;
875-
if (numSamples > FPS_HISTORY_SIZE)
876-
numSamples = FPS_HISTORY_SIZE;
877-
}
862+
// append new sample to fps history.
863+
if (historyOffset >= FPS_HISTORY_SIZE)
864+
historyOffset = 0;
878865

879-
if (numSamples)
880-
{
881-
// determine average frame rate over our past history.
882-
Real average=0;
883-
for (Int i=0,j=historyOffset-1; i<numSamples; i++,j--)
884-
{
885-
if (j < 0)
886-
j=FPS_HISTORY_SIZE-1; // wrap around to front of buffer
887-
average += fpsHistory[j];
888-
}
866+
m_currentFPS = 1.0f/elapsedSeconds;
867+
fpsHistory[historyOffset++] = m_currentFPS;
889868

890-
m_averageFPS = average / (Real)numSamples;
891-
}
869+
// determine average frame rate over our past history.
870+
const Real sum = std::accumulate(fpsHistory, fpsHistory + FPS_HISTORY_SIZE, 0.0f);
871+
m_averageFPS = sum / FPS_HISTORY_SIZE;
892872

893873
lastUpdateTime64 = time64;
894874
}

GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class GameEngine : public SubsystemInterface
7878

7979
virtual void setFramesPerSecondLimit( Int fps ); ///< Set the max render and engine update fps.
8080
virtual Int getFramesPerSecondLimit( void ); ///< Get the max render and engine update fps.
81-
Real getUpdateTime(); ///< Get the last engine update delta time.
81+
Real getUpdateTime(); ///< Get the last engine update delta time in seconds.
8282
Real getUpdateFps(); ///< Get the last engine update fps.
8383

8484
static Bool isTimeFrozen(); ///< Returns true if a script has frozen time.
@@ -127,7 +127,7 @@ class GameEngine : public SubsystemInterface
127127
Int m_maxFPS; ///< Maximum frames per second for rendering
128128
Int m_logicTimeScaleFPS; ///< Maximum frames per second for logic time scale
129129

130-
Real m_updateTime; ///< Last engine update delta time
130+
Real m_updateTime; ///< Last engine update delta time in seconds
131131
Real m_logicTimeAccumulator; ///< Frame time accumulated towards submitting a new logic frame
132132

133133
Bool m_quitting; ///< true when we need to quit the game

GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DDisplay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class W3DDisplay : public Display
162162
void drawCurrentDebugDisplay( void ); ///< draws current debug display
163163
void calculateTerrainLOD(void); ///< Calculate terrain LOD.
164164
void renderLetterBox(UnsignedInt time); ///< draw letter box border
165-
void updateAverageFPS(void); ///< figure out the average fps over the last 30 frames.
165+
void updateAverageFPS(void); ///< calculate the average fps over the last 30 frames.
166166

167167
Byte m_initialized; ///< TRUE when system is initialized
168168
LightClass *m_myLight[LightEnvironmentClass::MAX_LIGHTS]; ///< light hack for now

GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
static void drawFramerateBar(void);
3535

3636
// SYSTEM INCLUDES ////////////////////////////////////////////////////////////
37+
#include <numeric>
3738
#include <stdlib.h>
3839
#include <windows.h>
3940
#include <io.h>
@@ -885,21 +886,16 @@ void W3DDisplay::reset( void )
885886

886887
const UnsignedInt START_CUMU_FRAME = LOGICFRAMES_PER_SECOND / 2; // skip first half-sec
887888

888-
/** Update a moving average of the last 30 fps measurements. Also try to filter out temporary spikes.
889-
This code is designed to be used by the GameLOD sytems to determine the correct dynamic LOD setting.
890-
*/
891889
void W3DDisplay::updateAverageFPS(void)
892890
{
893-
const Real MaximumFrameTimeCutoff = 0.5f; //largest frame interval (seconds) we accept before ignoring it as a momentary "spike"
894-
const Int FPS_HISTORY_SIZE = 30; //keep track of the last 30 frames
891+
constexpr const Int FPS_HISTORY_SIZE = 30;
895892

896893
static Int64 lastUpdateTime64 = 0;
897894
static Int historyOffset = 0;
898-
static Int numSamples = 0;
899-
static double fpsHistory[FPS_HISTORY_SIZE];
895+
static Real fpsHistory[FPS_HISTORY_SIZE] = {0};
900896

901-
Int64 freq64 = getPerformanceCounterFrequency();
902-
Int64 time64 = getPerformanceCounter();
897+
const Int64 freq64 = getPerformanceCounterFrequency();
898+
const Int64 time64 = getPerformanceCounter();
903899

904900
#if defined(RTS_DEBUG)
905901
if (TheGameLogic->getFrame() == START_CUMU_FRAME)
@@ -908,37 +904,21 @@ void W3DDisplay::updateAverageFPS(void)
908904
}
909905
#endif
910906

911-
Int64 timeDiff = time64 - lastUpdateTime64;
907+
const Int64 timeDiff = time64 - lastUpdateTime64;
912908

913909
// convert elapsed time to seconds
914-
double elapsedSeconds = (double)timeDiff/(double)(freq64);
915-
916-
if (elapsedSeconds <= MaximumFrameTimeCutoff) //make sure it's not a spike
917-
{
918-
// append new sameple to fps history.
919-
if (historyOffset >= FPS_HISTORY_SIZE)
920-
historyOffset = 0;
910+
Real elapsedSeconds = (Real)timeDiff/(Real)freq64;
921911

922-
m_currentFPS = 1.0/elapsedSeconds;
923-
fpsHistory[historyOffset++] = m_currentFPS;
924-
numSamples++;
925-
if (numSamples > FPS_HISTORY_SIZE)
926-
numSamples = FPS_HISTORY_SIZE;
927-
}
912+
// append new sample to fps history.
913+
if (historyOffset >= FPS_HISTORY_SIZE)
914+
historyOffset = 0;
928915

929-
if (numSamples)
930-
{
931-
// determine average frame rate over our past history.
932-
Real average=0;
933-
for (Int i=0,j=historyOffset-1; i<numSamples; i++,j--)
934-
{
935-
if (j < 0)
936-
j=FPS_HISTORY_SIZE-1; // wrap around to front of buffer
937-
average += fpsHistory[j];
938-
}
916+
m_currentFPS = 1.0f/elapsedSeconds;
917+
fpsHistory[historyOffset++] = m_currentFPS;
939918

940-
m_averageFPS = average / (Real)numSamples;
941-
}
919+
// determine average frame rate over our past history.
920+
const Real sum = std::accumulate(fpsHistory, fpsHistory + FPS_HISTORY_SIZE, 0.0f);
921+
m_averageFPS = sum / FPS_HISTORY_SIZE;
942922

943923
lastUpdateTime64 = time64;
944924
}

0 commit comments

Comments
 (0)