3434static 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
886887const 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- */
891889void 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