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.

feat(gameclient): uncap the renderer and clock the UI animations

Olcay Seygan2de56fd9bc0b
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/GameWindowTransitions.h b/GeneralsMD/Code/GameEngine/Include/GameClient/GameWindowTransitions.h
index bf7aa462d..c76d15b3e 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/GameWindowTransitions.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/GameWindowTransitions.h
@@ -648,6 +648,8 @@ private:
TransitionWindowList m_transitionWindowList;
Int m_directionMultiplier;
Int m_currentFrame; ///< maintain how long we've spent on this transition;
+ UnsignedInt m_lastStepMs; ///< wall clock of the last step - the frame counter is not a clock
+ Real m_stepAccumMs; ///< milliseconds banked toward the next step
AsciiString m_name;
};
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/UiAnimClock.h b/GeneralsMD/Code/GameEngine/Include/GameClient/UiAnimClock.h
new file mode 100644
index 000000000..8cfcf366f
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/UiAnimClock.h
@@ -0,0 +1,66 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** This program is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation, either version 3 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// FILE: UiAnimClock.h ////////////////////////////////////////////////////////
+//
+// A wall clock for the fixed-step client animations.
+//
+// The renderer is not frame-rate capped any more. Everything that used to
+// advance one step per rendered frame - the menu window animations, the window
+// transitions, the control bar scheme, the screen filter fades - was authored
+// against 30fps and now runs at whatever the machine manages, so a menu
+// transition covers its whole travel in a few milliseconds and reads as a snap.
+// Each of them asks the function below whether enough real time has gone by to
+// take its next step.
+//
+// This lives in its own header, deliberately: its callers are spread across
+// GameEngine and GameEngineDevice, and the natural home
+// (AnimateWindowManager.h) names std::list without including it, so it does not
+// parse everywhere this is needed.
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#ifndef __UIANIMCLOCK_H_
+#define __UIANIMCLOCK_H_
+
+#include "Lib/BaseType.h"
+
+//-----------------------------------------------------------------------------
+/**
+ * Pace a fixed-step UI animation off the wall clock.
+ *
+ * 'lastMs' and 'accumMs' are the caller's state, zeroed when the animation is
+ * (re)started - a zero 'lastMs' seeds itself from 'nowMs' and returns FALSE,
+ * so a freshly started animation holds its first frame rather than taking two
+ * steps on the frame it starts.
+ *
+ * Elapsed time is clamped to a single step, so a stall - a level load, a
+ * dragged window - cannot bank up a burst of steps and jump the animation
+ * forward when it ends.
+ *
+ * Returns TRUE at most once per step period.
+ */
+Bool GameClient_isUiAnimStepDue( UnsignedInt &lastMs, Real &accumMs, UnsignedInt nowMs,
+ Real stepsPerSec );
+
+/// what the menus were authored against, and what everything using the helper above steps at
+const Real UI_ANIM_STEPS_PER_SEC = 30.0f;
+
+#endif // __UIANIMCLOCK_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
index e5896e64c..d829d9aea 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
@@ -2641,13 +2641,39 @@ void Drawable::setStealthLook(StealthLookType look)
//-------------------------------------------------------------------------------------------------
void Drawable::draw( View *view )
{
+ //
+ // The fade below is a fixed fraction per rendered frame, and the renderer is not capped any
+ // more: at a few hundred fps the stealth shimmer is gone the instant it appears. The fade is
+ // exponential, so the frame-rate-independent form is the scalar raised to the number of 30fps
+ // steps that actually elapsed. Worked out once per client frame - draw() runs once per
+ // drawable, and every drawable in a frame must fade by the same amount.
+ //
+ static UnsignedInt fadeFrame = ~0;
+ static UnsignedInt fadeLastMs = 0;
+ static Real fadeScalar = MATERIAL_PASS_OPACITY_FADE_SCALAR;
+
+ if ( fadeFrame != TheGameClient->getFrame() )
+ {
+ fadeFrame = TheGameClient->getFrame();
+
+ const UnsignedInt nowMs = timeGetTime();
+ if ( fadeLastMs == 0 )
+ fadeLastMs = nowMs;
+
+ Real steps = (Real)( nowMs - fadeLastMs ) * 0.001f * 30.0f;
+ fadeLastMs = nowMs;
+ if ( steps > 4.0f )
+ steps = 4.0f; // a level load must not fade everything out at once
+
+ fadeScalar = powf( MATERIAL_PASS_OPACITY_FADE_SCALAR, steps );
+ }
if ( testTintStatus( TINT_STATUS_FRENZY ) == FALSE )
{
if ( getObject() && getObject()->isEffectivelyDead() )
m_secondMaterialPassOpacity = 0.0f;//dead folks don't stealth anyway
else if ( m_secondMaterialPassOpacity > VERY_TRANSPARENT_MATERIAL_PASS_OPACITY )// keep fading any add'l material unless something has set it to zero
- m_secondMaterialPassOpacity *= MATERIAL_PASS_OPACITY_FADE_SCALAR;
+ m_secondMaterialPassOpacity *= fadeScalar;
else
m_secondMaterialPassOpacity = 0.0f;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/AnimateWindowManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/AnimateWindowManager.cpp
index 754c48f5c..e4431be12 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/AnimateWindowManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/AnimateWindowManager.cpp
@@ -54,6 +54,7 @@
#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
#include "GameClient/AnimateWindowManager.h"
+#include "GameClient/UiAnimClock.h"
#include "GameClient/GameWindow.h"
#include "GameClient/Display.h"
#include "GameClient/ProcessAnimateWindow.h"
@@ -183,6 +184,28 @@ void AnimateWindowManager::reset( void )
m_stepAccumMs = 0.0f;
}
+//-----------------------------------------------------------------------------
+Bool GameClient_isUiAnimStepDue( UnsignedInt &lastMs, Real &accumMs, UnsignedInt nowMs,
+ Real stepsPerSec )
+{
+ const Real msPerStep = 1000.0f / stepsPerSec;
+
+ if (lastMs == 0)
+ lastMs = nowMs;
+
+ Real elapsedMs = (Real)(nowMs - lastMs);
+ lastMs = nowMs;
+ if (elapsedMs > msPerStep)
+ elapsedMs = msPerStep; // never burst several steps after a stall
+
+ accumMs += elapsedMs;
+ if (accumMs < msPerStep)
+ return FALSE;
+
+ accumMs -= msPerStep;
+ return TRUE;
+}
+
void AnimateWindowManager::update( void )
{
//
@@ -191,22 +214,9 @@ void AnimateWindowManager::update( void )
// the wall clock at that same rate instead - otherwise a menu animation covers its whole
// travel in a handful of milliseconds and appears to snap into place.
//
- const Real WINDOW_ANIM_STEPS_PER_SEC = 30.0f;
- const Real msPerStep = 1000.0f / WINDOW_ANIM_STEPS_PER_SEC;
-
- UnsignedInt nowMs = timeGetTime();
- if (m_lastStepMs == 0)
- m_lastStepMs = nowMs;
-
- Real elapsedMs = (Real)(nowMs - m_lastStepMs);
- m_lastStepMs = nowMs;
- if (elapsedMs > msPerStep)
- elapsedMs = msPerStep; // never burst several steps after a stall
-
- m_stepAccumMs += elapsedMs;
- if (m_stepAccumMs < msPerStep)
+ if (!GameClient_isUiAnimStepDue(m_lastStepMs, m_stepAccumMs, timeGetTime(),
+ UI_ANIM_STEPS_PER_SEC))
return;
- m_stepAccumMs -= msPerStep;
ProcessAnimateWindow *processAnim = NULL;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarScheme.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarScheme.cpp
index 8b0bfac00..6f0014dbc 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarScheme.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarScheme.cpp
@@ -62,6 +62,7 @@
#include "GameClient/Image.h"
#include "GameClient/GameWindowManager.h"
#include "GameClient/GadgetPushButton.h"
+#include "GameClient/UiAnimClock.h"
#ifdef _INTERNAL
// for occasional debugging...
//#pragma optimize("", off)
@@ -1085,7 +1086,19 @@ void ControlBarSchemeManager::setControlBarScheme(AsciiString schemeName)
//-----------------------------------------------------------------------------
void ControlBarSchemeManager::update( void )
{
- if(m_currentScheme)
+ //
+ // The scheme animations count .ini-authored frames, one per call, and this is called once per
+ // rendered frame. With the frame rate uncapped that runs them several times too fast, so pace
+ // them off the wall clock at the rate they were authored against. State is a static rather than
+ // a member because there is exactly one scheme manager and nothing restarts this animation.
+ //
+ static UnsignedInt lastStepMs = 0;
+ static Real stepAccumMs = 0.0f;
+
+ if (!GameClient_isUiAnimStepDue(lastStepMs, stepAccumMs, timeGetTime(), UI_ANIM_STEPS_PER_SEC))
+ return;
+
+ if(m_currentScheme)
m_currentScheme->update();
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GameWindowTransitions.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GameWindowTransitions.cpp
index 9ae2cea36..3d1807dba 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GameWindowTransitions.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GameWindowTransitions.cpp
@@ -60,6 +60,7 @@
#include "GameClient/GameWindowTransitions.h"
#include "GameClient/GameWindow.h"
#include "GameClient/GameWindowManager.h"
+#include "GameClient/UiAnimClock.h"
//-----------------------------------------------------------------------------
// DEFINES ////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
@@ -233,6 +234,8 @@ TransitionGroup::TransitionGroup( void )
{
m_currentFrame = 0;
m_fireOnce = FALSE;
+ m_lastStepMs = 0;
+ m_stepAccumMs = 0.0f;
}
TransitionGroup::~TransitionGroup( void )
@@ -251,6 +254,8 @@ void TransitionGroup::init( void )
{
m_currentFrame = 0;
m_directionMultiplier = 1;
+ m_lastStepMs = 0;
+ m_stepAccumMs = 0.0f;
TransitionWindowList::iterator it = m_transitionWindowList.begin();
while (it != m_transitionWindowList.end())
{
@@ -263,6 +268,15 @@ void TransitionGroup::init( void )
void TransitionGroup::update( void )
{
+ //
+ // One frame of transition per call was right when the frame rate was capped; rendering is
+ // uncapped now, so a whole menu transition would otherwise play out in a few milliseconds.
+ // Step off the wall clock at the rate the .ini frame counts were authored against instead.
+ //
+ if (!GameClient_isUiAnimStepDue(m_lastStepMs, m_stepAccumMs, timeGetTime(),
+ UI_ANIM_STEPS_PER_SEC))
+ return;
+
m_currentFrame += m_directionMultiplier; // we go forward or backwards depending.
TransitionWindowList::iterator it = m_transitionWindowList.begin();
while (it != m_transitionWindowList.end())
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index 210167a9e..0bd705911 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -1827,11 +1827,26 @@ AGAIN:
}
}
- if (!freezeTime)
+ if (!freezeTime)
{
- /// @todo Decouple framerate from timestep
- // for now, use constant time steps to avoid animations running independent of framerate
- syncTime += TheW3DFrameLengthInMsec;
+ //
+ // This is W3D's clock: every skeletal animation, texture flipbook and W3D-driven effect
+ // reads its time from it. EA advanced it a constant TheW3DFrameLengthInMsec per rendered
+ // frame - the @todo below - which is only the right answer while the renderer is pinned to
+ // 30fps. It is not any more, so advance it by the real time that has actually passed and
+ // the animations keep their authored speed at whatever frame rate the machine manages.
+ //
+ static UnsignedInt prevSyncMs = 0;
+ const UnsignedInt nowSyncMs = timeGetTime();
+ if (prevSyncMs == 0)
+ prevSyncMs = nowSyncMs;
+
+ UnsignedInt deltaMs = nowSyncMs - prevSyncMs;
+ prevSyncMs = nowSyncMs;
+ if (deltaMs > TheW3DFrameLengthInMsec * 4)
+ deltaMs = TheW3DFrameLengthInMsec * 4; // a level load must not fast-forward everything
+
+ syncTime += deltaMs;
// allow W3D to update its internals
// WW3D::Sync( GetTickCount() );
}
@@ -1839,7 +1854,7 @@ AGAIN:
// Fast & Frozen time limits the time to 33 fps.
Int minTime = 30;
- static Int prevTime = timeGetTime(), now;
+ static Int prevTime = timeGetTime(), now;
now=timeGetTime();
if (TheTacticalView->getTimeMultiplier()>1)
@@ -1864,8 +1879,15 @@ AGAIN:
if(TheGlobalData->m_loadScreenRender != TRUE)
{
- // limit the framerate
- while(TheGlobalData->m_useFpsLimit && (now - prevTime) < minTime-1)
+ //
+ // The frame rate is no longer capped: this used to hold every frame to 33fps
+ // whenever m_useFpsLimit was set, which is any game started from the menus, and
+ // everything that rode on the render rate has been put on a clock of its own
+ // instead. The scripted-camera loop below is the exception - it advances the
+ // camera one step per iteration and has no clock, so a cinematic pan would run
+ // through in a few milliseconds. That one still gets paced.
+ //
+ while(loopForCameraMovement && (now - prevTime) < minTime-1)
{
::Sleep(1); // was a pure spin; this loop can run for whole camera pans
now = timeGetTime();
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp
index 42722d215..74d218dd6 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp
@@ -67,6 +67,7 @@
#include "GameClient/CommandXlat.h"
#include "GameClient/display.h"
#include "GameClient/Water.h"
+#include "GameClient/UiAnimClock.h"
#include "GameLogic/GameLogic.h"
#include "common/GlobalData.h"
#include "common/GameLOD.h"
@@ -84,6 +85,19 @@
// Turn this on to turn off pixel shaders. jba[4/3/2003]
#define do_not_DISABLE_PIXEL_SHADERS 1
+//
+// The screen filters below count their fade in render frames - m_fadeFrames comes from a script
+// that was written against a 30fps cap, and the renderer is not capped any more, so a fade that
+// should take a second is over in a few milliseconds. Exactly one filter fades at a time, so one
+// shared wall clock gate paces all of them.
+//
+static Bool fadeStepDue( void )
+{
+ static UnsignedInt lastMs = 0;
+ static Real accumMs = 0.0f;
+ return GameClient_isUiAnimStepDue( lastMs, accumMs, timeGetTime(), UI_ANIM_STEPS_PER_SEC );
+}
+
/** Interface definition for custom shaders we define in our app. These shaders can perform more complex
operations than those allowed in the WW3D2 shader system.
*/
@@ -387,7 +401,7 @@ Int ScreenBWFilter::set(enum FilterModes mode)
if (m_fadeDirection > 0)
{ //turning effect on
- m_curFadeFrame++;
+ if (fadeStepDue()) m_curFadeFrame++;
Int fade = m_curFadeFrame;
if (fade<m_fadeFrames)
@@ -404,7 +418,7 @@ Int ScreenBWFilter::set(enum FilterModes mode)
else
if (m_fadeDirection < 0)
{ //turning effect off
- m_curFadeFrame++;
+ if (fadeStepDue()) m_curFadeFrame++;
Int fade = m_curFadeFrame;
if (fade<m_fadeFrames)
{
@@ -609,7 +623,7 @@ Int ScreenBWFilterDOT3::set(enum FilterModes mode)
if (m_fadeDirection > 0)
{ //turning effect on
- m_curFadeFrame++;
+ if (fadeStepDue()) m_curFadeFrame++;
Int fade = m_curFadeFrame;
if (fade<m_fadeFrames)
@@ -626,7 +640,7 @@ Int ScreenBWFilterDOT3::set(enum FilterModes mode)
else
if (m_fadeDirection < 0)
{ //turning effect off
- m_curFadeFrame++;
+ if (fadeStepDue()) m_curFadeFrame++;
Int fade = m_curFadeFrame;
if (fade<m_fadeFrames)
{
@@ -716,7 +730,7 @@ Bool ScreenCrossFadeFilter::updateFadeLevel(void)
{
if (m_fadeDirection > 0)
{ //turning effect on
- m_curFadeFrame++;
+ if (fadeStepDue()) m_curFadeFrame++;
Int fade = m_curFadeFrame;
if (fade<m_fadeFrames)
@@ -738,7 +752,7 @@ Bool ScreenCrossFadeFilter::updateFadeLevel(void)
if (fade<m_fadeFrames)
{
m_curFadeValue = 1.0f - (Real)fade/(Real)m_fadeFrames;
- m_curFadeFrame++;
+ if (fadeStepDue()) m_curFadeFrame++;
}
else
{ m_curFadeValue = 0.0f;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
index a97fc0be8..7df395012 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
@@ -1019,7 +1019,14 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, int
_PresentParameters.EnableAutoDepthStencil = TRUE; // Driver will attempt to match Z-buffer depth
_PresentParameters.Flags=0; // We're not going to lock the backbuffer
- _PresentParameters.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
+ //
+ // D3DPRESENT_INTERVAL_DEFAULT is vsync, so fullscreen used to be pinned to the monitor's
+ // refresh however fast the machine could draw. The frame rate is deliberately uncapped now -
+ // everything that used to ride on the render rate keeps its own clock instead - so present
+ // as soon as the frame is ready. (Windowed presents are unaffected: this field only applies
+ // fullscreen, and the desktop compositor paces a windowed present regardless.)
+ //
+ _PresentParameters.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
_PresentParameters.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
/*
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 3379ec175..ac6c3edf5 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -30,6 +30,7 @@
#include "GameClient/Water.h"
#include "GameLogic/Module/PhysicsUpdate.h"
#include "GameClient/ParticleSys.h"
+#include "GameClient/ControlBar.h"
#include <stdio.h>
#include <string.h>
@@ -473,6 +474,70 @@ TEST(logic_tick_is_wall_clock_paced_not_render_paced)
CHECK( GameEngine_isLogicFrameDue( accum, 0.0f, 0 ) );
}
+/* AnimateWindowManager.cpp: the same argument one layer up. The menu animations
+ count fixed steps and were authored against a capped frame rate, so with the
+ renderer uncapped they have to be paced off the wall clock too or a whole
+ menu transition plays out in a handful of milliseconds. Unlike the logic
+ pacer this one owns the clock reading, so it takes 'now' rather than an
+ elapsed time - a caller can hand it timeGetTime() and nothing else.
+
+ Declared here rather than by including AnimateWindowManager.h: that header
+ does not parse outside the engine's own PreRTS.h include order. */
+extern Bool GameClient_isUiAnimStepDue( UnsignedInt &lastMs, Real &accumMs,
+ UnsignedInt nowMs, Real stepsPerSec );
+
+TEST(ui_anim_steps_at_a_fixed_rate_however_fast_the_renderer_is)
+{
+ /* must match UI_ANIM_STEPS_PER_SEC in GameClient/UiAnimClock.h */
+ const Real UI_ANIM_STEPS_PER_SEC = 30.0f;
+
+ /* 3 seconds of a 300fps renderer at 30 steps/sec is 90 steps, not 900. */
+ UnsignedInt last = 0;
+ Real accum = 0.0f;
+ Int due = 0;
+ UnsignedInt now = 100000; /* an arbitrary non-zero clock origin */
+ for( Int i = 0; i < 900; ++i )
+ {
+ now += 3; /* timeGetTime has 1ms resolution, so ~333fps */
+ if( GameClient_isUiAnimStepDue( last, accum, now, UI_ANIM_STEPS_PER_SEC ) )
+ ++due;
+ }
+ CHECK( due >= 79 && due <= 81 ); /* 2700ms at 33.3ms/step */
+
+ /* The very first call must not fire: with lastMs seeded to now there is no
+ elapsed time yet, so a freshly started animation holds its first frame
+ for a full step instead of jumping two frames on the frame it starts. */
+ last = 0;
+ accum = 0.0f;
+ CHECK_EQ( GameClient_isUiAnimStepDue( last, accum, 500000, UI_ANIM_STEPS_PER_SEC ), FALSE );
+
+ /* A renderer slower than the step rate steps once per call and never banks a
+ burst - a level load must not fast-forward the transition it returns to. */
+ last = 0;
+ accum = 0.0f;
+ now = 200000;
+ GameClient_isUiAnimStepDue( last, accum, now, UI_ANIM_STEPS_PER_SEC ); /* seed */
+ for( Int i = 0; i < 20; ++i )
+ {
+ now += 5000; /* five seconds of stall = 150 steps, if it banked them */
+ CHECK( GameClient_isUiAnimStepDue( last, accum, now, UI_ANIM_STEPS_PER_SEC ) );
+ }
+
+ /* And the rate really is the rate: half the step period never fires twice. */
+ last = 0;
+ accum = 0.0f;
+ now = 300000;
+ GameClient_isUiAnimStepDue( last, accum, now, UI_ANIM_STEPS_PER_SEC );
+ due = 0;
+ for( Int i = 0; i < 60; ++i )
+ {
+ now += 16; /* ~60fps against a 33.3ms step */
+ if( GameClient_isUiAnimStepDue( last, accum, now, UI_ANIM_STEPS_PER_SEC ) )
+ ++due;
+ }
+ CHECK( due >= 28 && due <= 30 ); /* 960ms of it */
+}
+
/* PhysicsUpdate.cpp: the forward speed a locomotor steers on is the projection
of the velocity onto the facing - a plain dot product. It used to be
sqrt((vx*dx)^2 + (vy*dy)^2), which is exact on the axes but reads only
@@ -655,3 +720,157 @@ TEST(income_is_zero_not_negative_when_nothing_comes_in)
const Int samples[4] = { 7000, 7000, 7000, 7000 };
CHECK_EQ(computeIncomePerMinute(samples, 4, 4, 2), 0);
}
+
+//////////////////////////////////////////////////////////////////////////////
+// Control bar three-panel layout
+//////////////////////////////////////////////////////////////////////////////
+/*
+ * ControlBarComputePanelLayout is the geometry half of ControlBar::layoutPanels:
+ * screen size in, uniform scale and the three panel rectangles out. It is split
+ * out precisely so it can be driven at resolutions this machine cannot display -
+ * the bar is laid out once at startup and there is no way to eyeball 1280x1024
+ * without owning that monitor.
+ *
+ * The panels' authored rectangles were carved out of one continuous strip and add
+ * up to 810 of the 800 design units. The first cut of this layout scaled by
+ * min(w/800, h/600), which on 16:9 is the height and leaves width to spare, but at
+ * 4:3 and squarer is the width - so 810 units of panel went into 800 units of
+ * screen, both seams overlapped, and the gaps the whole design is built around
+ * closed to nothing. Hence panels_keep_a_real_gap_on_square_screens below.
+ */
+
+static const Int theTestResolutions[][2] =
+{
+ { 800, 600 }, // 4:3, the resolution the .wnd is authored at
+ { 1024, 768 }, // 4:3
+ { 1280, 960 }, // 4:3
+ { 1280, 1024 }, // 5:4, squarer than the design
+ { 1600, 1200 }, // 4:3
+ { 1280, 720 }, // 16:9
+ { 1920, 1080 }, // 16:9
+ { 2560, 1080 }, // 21:9
+};
+static const Int theTestResolutionCount = sizeof(theTestResolutions) / sizeof(theTestResolutions[0]);
+
+TEST(controlbar_panels_keep_a_real_gap_on_square_screens)
+{
+ /* The regression. Every seam has to show world through it, at every aspect -
+ this is what failed at 4:3 and 5:4 when the scale was bounded by width alone. */
+ for (Int i = 0; i < theTestResolutionCount; ++i)
+ {
+ IRegion2D panel[ControlBar::CB_PANEL_COUNT];
+ Real scale = 0.0f;
+ ControlBarComputePanelLayout(theTestResolutions[i][0], theTestResolutions[i][1], panel, &scale);
+
+ CHECK(panel[ControlBar::CB_PANEL_CENTER].lo.x - panel[ControlBar::CB_PANEL_LEFT].hi.x >= 16);
+ CHECK(panel[ControlBar::CB_PANEL_RIGHT].lo.x - panel[ControlBar::CB_PANEL_CENTER].hi.x >= 16);
+ }
+}
+
+TEST(controlbar_panels_stay_on_screen_and_hug_their_edges)
+{
+ for (Int i = 0; i < theTestResolutionCount; ++i)
+ {
+ const Int w = theTestResolutions[i][0], h = theTestResolutions[i][1];
+ IRegion2D panel[ControlBar::CB_PANEL_COUNT];
+ Real scale = 0.0f;
+ ControlBarComputePanelLayout(w, h, panel, &scale);
+
+ // left panel pinned to the left edge, right panel to the right, all of it on screen
+ CHECK_EQ(panel[ControlBar::CB_PANEL_LEFT].lo.x, 0);
+ CHECK_EQ(panel[ControlBar::CB_PANEL_RIGHT].hi.x, w);
+
+ for (Int p = 0; p < ControlBar::CB_PANEL_COUNT; ++p)
+ {
+ CHECK(panel[p].lo.x >= 0);
+ CHECK(panel[p].hi.x <= w);
+ CHECK(panel[p].lo.y >= 0);
+ CHECK_EQ(panel[p].hi.y, h); // every panel sits on the bottom edge
+ CHECK(panel[p].width() > 0);
+ CHECK(panel[p].height() > 0);
+ }
+ }
+}
+
+TEST(controlbar_panels_are_never_stretched)
+{
+ /* One uniform scale is the point of the whole exercise: a panel's shape must not
+ depend on the aspect of the screen it is drawn on, or the cameos inside it come
+ out as rectangles. Compare each panel's width:height against the authored one. */
+ static const Real designRatio[ControlBar::CB_PANEL_COUNT] =
+ {
+ 186.0f / 179.0f, // left - design rect 0,421 .. 186,600
+ 438.0f / 168.0f, // center - design rect 186,432 .. 624,600
+ 186.0f / 172.0f, // right - design rect 614,428 .. 800,600
+ };
+
+ for (Int i = 0; i < theTestResolutionCount; ++i)
+ {
+ IRegion2D panel[ControlBar::CB_PANEL_COUNT];
+ Real scale = 0.0f;
+ ControlBarComputePanelLayout(theTestResolutions[i][0], theTestResolutions[i][1], panel, &scale);
+
+ for (Int p = 0; p < ControlBar::CB_PANEL_COUNT; ++p)
+ {
+ const Real ratio = (Real)panel[p].width() / (Real)panel[p].height();
+ CHECK_NEAR(ratio, designRatio[p], 0.03f); // slack for the floor/ceil rounding
+ }
+ }
+}
+
+TEST(controlbar_scale_is_bounded_by_height_on_a_wide_screen)
+{
+ /* 16:9 has width to spare, so the height is what runs out first and the bar is
+ drawn at exactly h/600 - the same size it would be on a 4:3 screen of that
+ height, which is what keeps it from growing as monitors get wider. */
+ IRegion2D panel[ControlBar::CB_PANEL_COUNT];
+ Real scale = 0.0f;
+ ControlBarComputePanelLayout(1920, 1080, panel, &scale);
+ CHECK_NEAR(scale, 1080.0f / 600.0f, 0.001f);
+}
+
+TEST(controlbar_scale_is_bounded_by_the_gap_on_a_square_screen)
+{
+ /* 5:4: neither the width nor the height bound leaves room between the panels, so
+ the gap bound is the one that has to win. w/800 = 1.6 and h/600 = 1.7067 both
+ lose to (w/2 - 24) / 405. */
+ IRegion2D panel[ControlBar::CB_PANEL_COUNT];
+ Real scale = 0.0f;
+ ControlBarComputePanelLayout(1280, 1024, panel, &scale);
+ CHECK(scale < 1280.0f / 800.0f);
+ CHECK_NEAR(scale, (1280.0f * 0.5f - 24.0f) / 405.0f, 0.001f);
+}
+
+/*
+ * BaseType.h's fast_float_ceil does not ceil an integer. It adds 0.99999994 and
+ * truncates, but 0.99999994 is only a sixteenth of a float ULP once the value is past
+ * 1.0, so the sum rounds up to the next whole float before the truncate ever runs:
+ * ceil(600) is 601, ceil(1080) is 1081, and so on for every positive whole number.
+ * Fractions - what it is normally handed - come out right, which is why this has
+ * survived. Every REAL_TO_INT_CEIL in the codebase inherits it.
+ *
+ * Pinned rather than fixed: the sum is in a header every translation unit includes and
+ * a correcting compare would cost the branch the whole routine exists to avoid, so the
+ * blast radius of changing it is the entire game. ControlBarComputePanelLayout works
+ * around it locally by flooring and pinning the edges that have to be exact - a screen
+ * height that ceils to one past the bottom of the screen is where this turned up.
+ */
+TEST(realtointceil_DEFECT_overshoots_every_whole_number)
+{
+ CHECK_EQ(REAL_TO_INT_CEIL(1.0f), 2);
+ CHECK_EQ(REAL_TO_INT_CEIL(600.0f), 601);
+ CHECK_EQ(REAL_TO_INT_CEIL(768.0f), 769);
+ CHECK_EQ(REAL_TO_INT_CEIL(1080.0f), 1081);
+ CHECK_EQ(REAL_TO_INT_CEIL(1920.0f), 1921);
+
+ // zero is the one whole number it gets right - 0.99999994 truncates back to it
+ CHECK_EQ(REAL_TO_INT_CEIL(0.0f), 0);
+
+ // genuine fractions round up correctly, which is what callers normally feed it
+ CHECK_EQ(REAL_TO_INT_CEIL(600.5f), 601);
+ CHECK_EQ(REAL_TO_INT_CEIL(1080.25f), 1081);
+
+ // negatives skip the addition entirely and truncate, which really is ceil
+ CHECK_EQ(REAL_TO_INT_CEIL(-600.0f), -600);
+ CHECK_EQ(REAL_TO_INT_CEIL(-600.5f), -600);
+}