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(gameenginedevice): run the beach surf on real elapsed time

Olcay Seygan68426bab2e58
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
index b574085f8..29008757c 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
@@ -1227,26 +1227,41 @@ void WaterRenderObjClass::update( void )
// clock is the water's - it is scenery, so it runs off the wall clock.
//
// The per-second figures below are EA's per-frame steps times the 30 fps they were authored
- // against, so WATER_ANIM_SPEED 1.0f reproduces the shipped tempo exactly. It reads as a boil
- // rather than a swell at that rate, most visibly on the shell map behind the main menu, so
- // the whole animation - scroll and bump cycle alike - is scaled by one number and keeps its
- // shape while only its tempo changes.
+ // against, so a speed of 1.0f reproduces the shipped tempo exactly. It reads as a boil rather
+ // than a swell at that rate, most visibly on the shell map behind the main menu.
//
- const Real WATER_ANIM_SPEED = 0.2f;
+ // Two rates, not one. The scroll and the noise wobble are the swell and are continuous, so
+ // scaling them just slows the motion. The bump texture is a 32-frame flipbook and is the fine
+ // ripple, and a flipbook has no in-between state: the only way to slow it is to hold each
+ // frame longer, and at 0.2 that is six frames a second - the eye reads that as a stutter, not
+ // as slow water. So the flipbook keeps its authored rate and only the scroll is scaled.
+ //
+ const Real WATER_SCROLL_SPEED = 0.2f;
const Real AUTHORED_FPS = 30.0f;
+ const Real BUMP_FRAMES_PER_SEC = AUTHORED_FPS;
{
- static UnsignedInt lastScrollMs = 0;
- const UnsignedInt nowMs = timeGetTime();
- if (lastScrollMs == 0)
- lastScrollMs = nowMs;
-
- Real elapsedSec = (Real)(nowMs - lastScrollMs) * 0.001f;
- lastScrollMs = nowMs;
+ //
+ // timeGetTime ticks at 1ms even under timeBeginPeriod(1), and an uncapped shell runs
+ // several hundred frames a second, so the per-frame delta alternated between 0 and 1ms
+ // and the surface advanced in visible lurches. The performance counter is sub-microsecond.
+ //
+ static LARGE_INTEGER perfFreq = { 0 };
+ static LARGE_INTEGER lastCount = { 0 };
+ LARGE_INTEGER nowCount;
+
+ if (perfFreq.QuadPart == 0)
+ QueryPerformanceFrequency(&perfFreq);
+ QueryPerformanceCounter(&nowCount);
+ if (lastCount.QuadPart == 0)
+ lastCount.QuadPart = nowCount.QuadPart;
+
+ Real elapsedSec = (Real)((double)(nowCount.QuadPart - lastCount.QuadPart) / (double)perfFreq.QuadPart);
+ lastCount.QuadPart = nowCount.QuadPart;
if (elapsedSec > 0.1f)
elapsedSec = 0.1f; // a level load must not jump the surface forward
- const Real step = elapsedSec * AUTHORED_FPS * WATER_ANIM_SPEED;
+ const Real step = elapsedSec * AUTHORED_FPS * WATER_SCROLL_SPEED;
m_riverVOrigin += 0.002f * step;
m_riverXOffset += (Real)(0.0125*33/5000) * step;
@@ -1256,8 +1271,8 @@ void WaterRenderObjClass::update( void )
if (m_riverXOffset < -1) m_riverXOffset += 1;
if (m_riverYOffset < -1) m_riverYOffset += 1;
- // the bump texture is a flipbook, so it can only be slowed by holding each frame longer
- m_bumpFrameAccum += step;
+ // the flipbook runs at its authored rate - see the two-rates note above
+ m_bumpFrameAccum += elapsedSec * BUMP_FRAMES_PER_SEC;
while (m_bumpFrameAccum >= 1.0f)
{
m_bumpFrameAccum -= 1.0f;
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWaterTracks.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWaterTracks.cpp
index d3d8a219c..79876c7d6 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWaterTracks.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWaterTracks.cpp
@@ -829,14 +829,40 @@ void WaterTracksRenderSystem::shutdown( void )
void WaterTracksRenderSystem::update()
{
- static Int iLastTime=timeGetTime();
WaterTracksObj *mod=m_usedModules,*nextMod;
- Int timeDiff = timeGetTime()-iLastTime;
- iLastTime += timeDiff;
-
- //Lock framerate to 30 fps
- timeDiff = 1.0f/30.0f*1000.0f;
+ //
+ // This is where the beach waves come from, and EA measured the real elapsed time here and then
+ // threw it away: the next line used to be an unconditional `timeDiff = 1.0f/30.0f*1000.0f;`
+ // under the comment "Lock framerate to 30 fps". update() is called once per RENDER frame, so
+ // that hands every wave a fixed 33 ms of simulation per frame - correct only while the render
+ // loop is held at 30fps, and this fork's is not. At a few hundred frames a second the surf ran
+ // roughly ten times too fast, which is what the shell map behind the main menu shows.
+ //
+ // The waves are integrated in milliseconds through an Int (WaterTracksObj::update), so the
+ // remainder is carried rather than truncated - at 300fps a 3.3 ms frame would otherwise lose a
+ // third of its motion to the cast every frame. The counter is the performance counter and not
+ // timeGetTime for the same reason: a 1 ms tick quantises a 3 ms frame by a third.
+ //
+ static LARGE_INTEGER perfFreq = { 0 };
+ static LARGE_INTEGER lastCount = { 0 };
+ static Real carryMs = 0.0f;
+ LARGE_INTEGER nowCount;
+
+ if (perfFreq.QuadPart == 0)
+ QueryPerformanceFrequency(&perfFreq);
+ QueryPerformanceCounter(&nowCount);
+ if (lastCount.QuadPart == 0)
+ lastCount.QuadPart = nowCount.QuadPart;
+
+ Real elapsedMs = (Real)((double)(nowCount.QuadPart - lastCount.QuadPart) * 1000.0 / (double)perfFreq.QuadPart);
+ lastCount.QuadPart = nowCount.QuadPart;
+ if (elapsedMs > 100.0f)
+ elapsedMs = 100.0f; // a level load must not run the surf forward
+
+ carryMs += elapsedMs;
+ Int timeDiff = (Int)carryMs;
+ carryMs -= (Real)timeDiff;
//first update all the tracks
while( mod )