Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameclient): average the hud's hz and fps readings

Olcay Seygane326284e5701
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index d69f33301..a2606260c 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -1279,7 +1279,9 @@ protected:
UnsignedInt m_hudLastSampleMs; ///< wall clock of that sample
Real m_hudFps; ///< smoothed render rate
UnsignedInt m_hudLastSampleLogicFrame; ///< logic frame at that same sample
- Real m_hudLogicHz; ///< logic frames actually simulated per real second
+ Real m_hudLogicHz; ///< logic frames actually simulated per real second, smoothed
+ Int m_hudFpsShown; ///< m_hudFps as written on screen, moved only a full step at a time
+ Int m_hudLogicHzShown; ///< m_hudLogicHz the same way
UnsignedInt m_hudRealClockBaseMs; ///< wall clock the two elapsed-time readouts were aligned at
UnsignedInt m_hudLastDrawMs; ///< wall clock of the previous overlay draw, so a pause can be taken back out of it
Int m_hudOverlayBottom; ///< bottom of everything drawn in the top right corner, so the superweapon timers start under it
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 71b73ffc5..993427c49 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1176,6 +1176,8 @@ InGameUI::InGameUI()
m_hudFps = 0.0f;
m_hudLastSampleLogicFrame = 0;
m_hudLogicHz = 0.0f;
+ m_hudFpsShown = 0;
+ m_hudLogicHzShown = 0;
m_hudRealClockBaseMs = 0;
m_hudLastDrawMs = 0;
m_hudOverlayBottom = 0;
@@ -9009,6 +9011,22 @@ void InGameUI::drawPeaceCountdown( UnsignedInt framesLeft )
peaceTimeColor( alpha ), GameMakeColor( 0, 0, 0, alpha ) );
}
+//-------------------------------------------------------------------------------------------------
+// A half-second sample is a count of whole frames, so a steady 30Hz reads 28 one sample and 32
+// the next. The average over the last few seconds takes that out, and the number on screen only
+// moves once the average has left it by a full step, so a rate sitting on x.5 does not flip
+// between two values while a real drop still gets through in a second or two.
+static void smoothHudRate( Real sample, Real &average, Int &shown )
+{
+ const Real SMOOTHING = 0.125f; // weight of one sample: about four seconds of them count
+ const Real STEP_FRACTION = 0.02f; // the step at high rates, where one frame is less than 1%
+
+ average = ( average == 0.0f ) ? sample : average + ( sample - average ) * SMOOTHING;
+ const Real step = max( 1.0f, shown * STEP_FRACTION );
+ if( fabs( average - shown ) >= step )
+ shown = REAL_TO_INT( average + 0.5f );
+}
+
//-------------------------------------------------------------------------------------------------
void InGameUI::drawHudOverlay( void )
{
@@ -9049,8 +9067,8 @@ void InGameUI::drawHudOverlay( void )
else if( nowMs - m_hudLastSampleMs >= 500 )
{
const Real elapsed = (Real)(nowMs - m_hudLastSampleMs);
- m_hudFps = (clientFrame - m_hudLastSampleFrame) * 1000.0f / elapsed;
- m_hudLogicHz = (logicFrame - m_hudLastSampleLogicFrame) * 1000.0f / elapsed;
+ smoothHudRate( (clientFrame - m_hudLastSampleFrame) * 1000.0f / elapsed, m_hudFps, m_hudFpsShown );
+ smoothHudRate( (logicFrame - m_hudLastSampleLogicFrame) * 1000.0f / elapsed, m_hudLogicHz, m_hudLogicHzShown );
m_hudLastSampleMs = nowMs;
m_hudLastSampleFrame = clientFrame;
m_hudLastSampleLogicFrame = logicFrame;
@@ -9083,7 +9101,7 @@ void InGameUI::drawHudOverlay( void )
wallClock.wHour, wallClock.wMinute,
gameSecs / 3600, (gameSecs / 60) % 60, gameSecs % 60,
realSecs / 3600, (realSecs / 60) % 60, realSecs % 60,
- REAL_TO_INT( m_hudLogicHz + 0.5f ), REAL_TO_INT( m_hudFps + 0.5f ),
+ m_hudLogicHzShown, m_hudFpsShown,
TheDisplay->getRendererName() );
UnicodeString frameText;
@@ -9121,8 +9139,8 @@ void InGameUI::drawHudOverlay( void )
m_hudValues[ "net.game" ] = reading;
sprintf( reading, "%02u:%02u:%02u", realSecs / 3600, ( realSecs / 60 ) % 60, realSecs % 60 );
m_hudValues[ "net.real" ] = reading;
- m_hudValues[ "net.hz" ] = std::to_string( REAL_TO_INT( m_hudLogicHz + 0.5f ) );
- m_hudValues[ "net.fps" ] = std::to_string( REAL_TO_INT( m_hudFps + 0.5f ) );
+ m_hudValues[ "net.hz" ] = std::to_string( m_hudLogicHzShown );
+ m_hudValues[ "net.fps" ] = std::to_string( m_hudFpsShown );
m_hudValues[ "net.renderer" ] = WideCharStringToMultiByte( TheDisplay->getRendererName() );
m_hudValues[ "net.frame" ] = std::to_string( logicFrame );
if( TheNetwork != NULL )