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(gameengine): rate readings take the plain mean until the window fills

Olcay Seygand84a63a92f54
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
index 6762502ef..847ac0ae1 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
@@ -72,6 +72,18 @@ Int GameEngine_logicCatchupMaxFrames( Int logicFps );
/** Whether the catch-up loop may start another logic tick this pass. */
Bool GameEngine_mayStartAnotherCatchupTick( Int ticksSoFar, Int maxTicks, Real elapsedMsInLoop );
+/** A rate sampled every half second, averaged, and the whole number written on screen for it. */
+struct RateReading
+{
+ Real average; ///< the samples so far, or the last several once there are that many
+ Int samples; ///< how many the average holds, up to the window
+ Int shown; ///< the average as written, moved only a full step at a time
+
+ RateReading() : average( 0.0f ), samples( 0 ), shown( 0 ) {}
+ void add( Real sample );
+ void restart() { samples = 0; } ///< the next sample starts the average again; shown stays
+};
+
// forward declarations
class AudioManager;
class GameLogic;
@@ -141,15 +153,11 @@ protected:
Int m_maxFPS; ///< Maximum frames per second allowed
UnsignedInt m_logicRateSampleMs; ///< wall clock the current logic rate sample started at, 0 for none
UnsignedInt m_logicRateSampleFrame; ///< logic frame it started on
- Real m_logicFpsAverage; ///< the samples averaged, 0 before there is one
- Int m_measuredLogicFps; ///< that average as countdowns divide by it, moved only a full step at a time
+ RateReading m_measuredLogicFps; ///< the rate countdowns divide by, 0 shown before there is one
Bool m_quitting; ///< true when we need to quit the game
Bool m_isActive; ///< app has OS focus.
};
-/// fold one half-second rate sample into a running average and the whole number shown for it
-extern void smoothRateReading( Real sample, Real &average, Int &shown );
-
inline void GameEngine::setQuitting( Bool quitting ) { m_quitting = quitting; }
inline Bool GameEngine::getQuitting(void) { return m_quitting; }
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index 5d7376dff..dd190226e 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -34,6 +34,7 @@
#define _IN_GAME_UI_H_
#include "Common/GameCommon.h"
+#include "Common/GameEngine.h" // for RateReading
#include "Common/GameType.h"
#include "Common/MessageStream.h" // for GameMessageTranslator
#include "Common/KindOf.h"
@@ -1284,11 +1285,9 @@ protected:
UnsignedInt m_hudDrawCount; ///< rendered frames counted by drawHudOverlay itself
UnsignedInt m_hudLastSampleFrame; ///< m_hudDrawCount the fps sample was last refreshed on
UnsignedInt m_hudLastSampleMs; ///< wall clock of that sample
- Real m_hudFps; ///< smoothed render rate
+ RateReading m_hudFps; ///< render rate
UnsignedInt m_hudLastSampleLogicFrame; ///< logic frame at that same sample
- 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
+ RateReading m_hudLogicHz; ///< logic frames actually simulated per real second
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/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index 58e4ed74f..e69766596 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -203,16 +203,22 @@ static const UnsignedInt LOGIC_RATE_SAMPLE_MS = 500;
the next, and every countdown dividing by it read 20s, 21s, 19s. The average over the last few
seconds takes that out, and the shown number 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. */
-void smoothRateReading( Real sample, Real &average, Int &shown )
+ through in a second or two.
+
+ Until the window has filled, the average is the plain mean of what it has: the first samples
+ after a load run far over the real rate, and letting the first one stand for the whole window
+ kept that number on screen for the opening seconds of every match. */
+void RateReading::add( Real sample )
{
- const Real SMOOTHING = 0.125f; // weight of one sample: about four seconds of them count
+ const Int WINDOW = 8; // samples that count once there are enough: about four seconds
const Real MIN_STEP = 0.75f; // over half, or an average settling on 20 from above sticks at 21
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;
+ if( samples < WINDOW )
+ ++samples;
+ average += ( sample - average ) / samples;
const Real step = max( MIN_STEP, shown * STEP_FRACTION );
- if( fabs( average - shown ) >= step )
+ if( samples == 1 || fabs( average - shown ) >= step )
shown = REAL_TO_INT( average + 0.5f );
}
@@ -241,7 +247,7 @@ void GameEngine::sampleLogicRate( void )
if( elapsedMs < LOGIC_RATE_SAMPLE_MS )
return;
- smoothRateReading( (frame - m_logicRateSampleFrame) * 1000.0f / elapsedMs, m_logicFpsAverage, m_measuredLogicFps );
+ m_measuredLogicFps.add( (frame - m_logicRateSampleFrame) * 1000.0f / elapsedMs );
m_logicRateSampleMs = nowMs;
m_logicRateSampleFrame = frame;
}
@@ -249,8 +255,8 @@ void GameEngine::sampleLogicRate( void )
Int GameEngine::getLogicFramesPerSecond( void )
{
// a stalled network game has no rate at all, and no countdown can say how long that lasts
- if( m_measuredLogicFps > 0 )
- return m_measuredLogicFps;
+ if( m_measuredLogicFps.shown > 0 )
+ return m_measuredLogicFps.shown;
return m_maxFPS > 0 ? m_maxFPS : LOGICFRAMES_PER_SECOND;
}
@@ -264,8 +270,6 @@ GameEngine::GameEngine( void )
m_maxFPS = 0;
m_logicRateSampleMs = 0;
m_logicRateSampleFrame = 0;
- m_logicFpsAverage = 0.0f;
- m_measuredLogicFps = 0;
m_quitting = FALSE;
m_isActive = FALSE;
@@ -351,7 +355,7 @@ void GameEngine::setFramesPerSecondLimit( Int fps )
// the speed keys change the rate on purpose; the countdowns start again from the next sample
// rather than taking the average's seconds to walk over
- m_logicFpsAverage = 0.0f;
+ m_measuredLogicFps.restart();
}
/* -replay <file>: the name the command line asked for, opened after init()'s resetAll(). See
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 20ed2100e..ac93a26d3 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1173,11 +1173,7 @@ InGameUI::InGameUI()
m_cameraSnapRepeatMs = 0;
m_subtitleFreezeStartMs = 0;
m_subtitleFreezeSteps = 0;
- 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;
@@ -9067,8 +9063,8 @@ void InGameUI::drawHudOverlay( void )
else if( nowMs - m_hudLastSampleMs >= 500 )
{
const Real elapsed = (Real)(nowMs - m_hudLastSampleMs);
- smoothRateReading( (clientFrame - m_hudLastSampleFrame) * 1000.0f / elapsed, m_hudFps, m_hudFpsShown );
- smoothRateReading( (logicFrame - m_hudLastSampleLogicFrame) * 1000.0f / elapsed, m_hudLogicHz, m_hudLogicHzShown );
+ m_hudFps.add( (clientFrame - m_hudLastSampleFrame) * 1000.0f / elapsed );
+ m_hudLogicHz.add( (logicFrame - m_hudLastSampleLogicFrame) * 1000.0f / elapsed );
m_hudLastSampleMs = nowMs;
m_hudLastSampleFrame = clientFrame;
m_hudLastSampleLogicFrame = logicFrame;
@@ -9101,7 +9097,7 @@ void InGameUI::drawHudOverlay( void )
wallClock.wHour, wallClock.wMinute,
gameSecs / 3600, (gameSecs / 60) % 60, gameSecs % 60,
realSecs / 3600, (realSecs / 60) % 60, realSecs % 60,
- m_hudLogicHzShown, m_hudFpsShown,
+ m_hudLogicHz.shown, m_hudFps.shown,
TheDisplay->getRendererName() );
UnicodeString frameText;
@@ -9139,8 +9135,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( m_hudLogicHzShown );
- m_hudValues[ "net.fps" ] = std::to_string( m_hudFpsShown );
+ m_hudValues[ "net.hz" ] = std::to_string( m_hudLogicHz.shown );
+ m_hudValues[ "net.fps" ] = std::to_string( m_hudFps.shown );
m_hudValues[ "net.renderer" ] = WideCharStringToMultiByte( TheDisplay->getRendererName() );
m_hudValues[ "net.frame" ] = std::to_string( logicFrame );
if( TheNetwork != NULL )
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 49ab46db2..29e67707c 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -3015,28 +3015,42 @@ TEST(controlbar_seconds_are_real_seconds_at_the_current_game_speed)
CHECK_EQ( ControlBar_secondsFromFramesAt( 0.0f, 200 ), 0 );
}
-extern void smoothRateReading( Real sample, Real &average, Int &shown );
-
TEST(rate_reading_holds_through_jitter_and_follows_a_real_drop)
{
- /* A steady 30Hz sampled every half second counts 14, 15 or 16 frames, 28 to 32 a second. The
- first sample is taken as it is; after that the number shown must not move. */
- Real average = 0.0f;
- Int shown = 0;
- smoothRateReading( 30.0f, average, shown );
- CHECK_EQ( shown, 30 );
+ /* The first sample after a load runs far over the real rate. Until the window is full it is one
+ sample among however many there are, not the whole average: 200 then seven 30s is their mean. */
+ RateReading rate;
+ rate.add( 200.0f );
+ CHECK_EQ( rate.shown, 200 );
+ rate.add( 30.0f );
+ CHECK_EQ( rate.shown, 115 );
+ for( Int sample = 0; sample < 6; sample++ )
+ rate.add( 30.0f );
+ CHECK_EQ( rate.shown, 51 );
+ for( Int sample = 0; sample < 40; sample++ )
+ rate.add( 30.0f );
+ CHECK_EQ( rate.shown, 30 );
+
+ /* A steady 30Hz sampled every half second counts 14, 15 or 16 frames, 28 to 32 a second, and
+ the number shown must not move for it */
const Real jitter[] = { 28.0f, 32.0f, 30.0f, 32.0f, 28.0f, 30.0f, 28.0f, 32.0f };
- for( Int sample = 0; sample < 4; sample++ )
+ for( Int round = 0; round < 4; round++ )
for( const Real &reading : jitter )
{
- smoothRateReading( reading, average, shown );
- CHECK_EQ( shown, 30 );
+ rate.add( reading );
+ CHECK_EQ( rate.shown, 30 );
}
/* a match that sinks to 20 and stays there reads 20 within a few seconds */
for( Int sample = 0; sample < 40; sample++ )
- smoothRateReading( 20.0f, average, shown );
- CHECK_EQ( shown, 20 );
+ rate.add( 20.0f );
+ CHECK_EQ( rate.shown, 20 );
+
+ /* a restart takes the next sample as it is, and keeps showing the old number until then */
+ rate.restart();
+ CHECK_EQ( rate.shown, 20 );
+ rate.add( 60.0f );
+ CHECK_EQ( rate.shown, 60 );
}
TEST(controlbar_experience_percent_fills_the_rank_and_clamps)