3434static 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
836837const 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- */
841839void 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}
0 commit comments