Skip to content

tweak(fps): Decouple logic time step from render update #1451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Core/GameEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(GAMEENGINE_SRC
# Include/Common/Errors.h
Include/Common/file.h
Include/Common/FileSystem.h
Include/Common/FrameRateLimit.h
# Include/Common/FunctionLexicon.h
Include/Common/GameAudio.h
# Include/Common/GameCommon.h
Expand Down Expand Up @@ -569,6 +570,7 @@ set(GAMEENGINE_SRC
# Source/Common/DamageFX.cpp
# Source/Common/Dict.cpp
# Source/Common/DiscreteCircle.cpp
Source/Common/FrameRateLimit.cpp
# Source/Common/GameEngine.cpp
# Source/Common/GameLOD.cpp
# Source/Common/GameMain.cpp
Expand Down
70 changes: 70 additions & 0 deletions Core/GameEngine/Include/Common/FrameRateLimit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 TheSuperHackers
**
** 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/>.
*/

#pragma once

class FrameRateLimit
{
public:
FrameRateLimit();

Real wait(UnsignedInt maxFps);

private:
LARGE_INTEGER m_freq;
LARGE_INTEGER m_start;
};


enum FpsValueChange
{
FpsValueChange_Increase,
FpsValueChange_Decrease,
};


class RenderFpsPreset
{
public:
enum CPP_11(: UnsignedInt)
{
UncappedFpsValue = 1000,
};

static UnsignedInt getNextFpsValue(UnsignedInt value);
static UnsignedInt getPrevFpsValue(UnsignedInt value);
static UnsignedInt changeFpsValue(UnsignedInt value, FpsValueChange change);

private:
static const UnsignedInt s_fpsValues[];
};


class LogicTimeScaleFpsPreset
{
public:
enum CPP_11(: UnsignedInt)
{
StepFpsValue = 5,
};

static UnsignedInt getNextFpsValue(UnsignedInt value);
static UnsignedInt getPrevFpsValue(UnsignedInt value);
static UnsignedInt changeFpsValue(UnsignedInt value, FpsValueChange change);
};

126 changes: 126 additions & 0 deletions Core/GameEngine/Source/Common/FrameRateLimit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 TheSuperHackers
**
** 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/>.
*/

#include "PreRTS.h"
#include "Common/FrameRateLimit.h"


FrameRateLimit::FrameRateLimit()
{
QueryPerformanceFrequency(&m_freq);
QueryPerformanceCounter(&m_start);
}

Real FrameRateLimit::wait(UnsignedInt maxFps)
{
LARGE_INTEGER tick;
QueryPerformanceCounter(&tick);
double elapsedSeconds = static_cast<double>(tick.QuadPart - m_start.QuadPart) / m_freq.QuadPart;
const double targetSeconds = 1.0 / maxFps;
const double sleepSeconds = targetSeconds - elapsedSeconds - 0.002; // leave ~2ms for spin wait

if (sleepSeconds > 0.0)
{
// Non busy wait with Munkee sleep
DWORD dwMilliseconds = static_cast<DWORD>(sleepSeconds * 1000);
Sleep(dwMilliseconds);
}

// Busy wait for remaining time
do
{
QueryPerformanceCounter(&tick);
elapsedSeconds = static_cast<double>(tick.QuadPart - m_start.QuadPart) / m_freq.QuadPart;
}
while (elapsedSeconds < targetSeconds);

m_start = tick;
return (Real)elapsedSeconds;
}


const UnsignedInt RenderFpsPreset::s_fpsValues[] = {
30, 50, 56, 60, 65, 70, 72, 75, 80, 85, 90, 100, 110, 120, 144, 240, 480, UncappedFpsValue };

static_assert(LOGICFRAMES_PER_SECOND <= 30, "Min FPS values need to be revisited!");

UnsignedInt RenderFpsPreset::getNextFpsValue(UnsignedInt value)
{
const Int first = 0;
const Int last = ARRAY_SIZE(s_fpsValues) - 1;
for (Int i = first; i < last; ++i)
{
if (value >= s_fpsValues[i] && value < s_fpsValues[i + 1])
{
return s_fpsValues[i + 1];
}
}
return s_fpsValues[last];
}

UnsignedInt RenderFpsPreset::getPrevFpsValue(UnsignedInt value)
{
const Int first = 0;
const Int last = ARRAY_SIZE(s_fpsValues) - 1;
for (Int i = last; i > first; --i)
{
if (value <= s_fpsValues[i] && value > s_fpsValues[i - 1])
{
return s_fpsValues[i - 1];
}
}
return s_fpsValues[first];
}

UnsignedInt RenderFpsPreset::changeFpsValue(UnsignedInt value, FpsValueChange change)
{
switch (change)
{
default:
case FpsValueChange_Increase: return getNextFpsValue(value);
case FpsValueChange_Decrease: return getPrevFpsValue(value);
}
}


UnsignedInt LogicTimeScaleFpsPreset::getNextFpsValue(UnsignedInt value)
{
return value + StepFpsValue;
}

UnsignedInt LogicTimeScaleFpsPreset::getPrevFpsValue(UnsignedInt value)
{
if (value - StepFpsValue < LOGICFRAMES_PER_SECOND)
{
return LOGICFRAMES_PER_SECOND;
}
else
{
return value - StepFpsValue;
}
}

UnsignedInt LogicTimeScaleFpsPreset::changeFpsValue(UnsignedInt value, FpsValueChange change)
{
switch (change)
{
default:
case FpsValueChange_Increase: return getNextFpsValue(value);
case FpsValueChange_Decrease: return getPrevFpsValue(value);
}
}
24 changes: 19 additions & 5 deletions GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include "Common/SubsystemInterface.h"
#include "Common/GameType.h"

#define DEFAULT_MAX_FPS 45

// forward declarations
class AudioManager;
class GameLogic;
Expand Down Expand Up @@ -72,8 +70,18 @@ class GameEngine : public SubsystemInterface

virtual void execute( void ); /**< The "main loop" of the game engine.
It will not return until the game exits. */
virtual void setFramesPerSecondLimit( Int fps ); ///< Set the maximum rate engine updates are allowed to occur
virtual Int getFramesPerSecondLimit( void ); ///< Get maxFPS. Not inline since it is called from another lib.

virtual void setFramesPerSecondLimit( Int fps ); ///< Set the max render and engine update fps.
virtual Int getFramesPerSecondLimit( void ); ///< Get the max render fps.

virtual void setLogicTimeScaleFps( Int fps ); ///< Set the logic time scale fps and therefore scale the simulation time. Is capped by the max render fps and does not apply to network matches.
virtual Int getLogicTimeScaleFps(); ///< Get the raw logic time scale fps value.
virtual void enableLogicTimeScale( Bool enable ); ///< Enable the logic time scale setup. If disabled, the simulation time scale is bound to the render frame time or network update time.
virtual Bool isLogicTimeScaleEnabled(); ///< Check whether the logic time scale setup is enabled.
Int getActualLogicTimeScaleFps(); ///< Get the real logic time scale fps, depending on the max render fps, network state and enabled state.
Real getActualLogicTimeScaleRatio(); ///< Get the real logic time scale ratio, depending on the max render fps, network state and enabled state.
Real getActualLogicTimeScaleOverFpsRatio(); ///< Get the real logic time scale over render fps ratio, used to scale down steps in render updates to match logic updates.

virtual void setQuitting( Bool quitting ); ///< set quitting status
virtual Bool getQuitting(void); ///< is app getting ready to quit.

Expand All @@ -100,9 +108,15 @@ class GameEngine : public SubsystemInterface
virtual ParticleSystemManager* createParticleSystemManager( void ) = 0;
virtual AudioManager *createAudioManager( void ) = 0; ///< Factory for Audio Manager

Int m_maxFPS; ///< Maximum frames per second allowed
Int m_maxFPS; ///< Maximum frames per second for rendering
Int m_logicTimeScaleFPS; ///< Maximum frames per second for logic time scale
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to rename m_maxFPS to make it more relevant to the rendering FPS.

m_maxRenderFPS for example.

For the logic one, it would be better to have the naming match the rendering.

m_maxLogicFPS etc.

Copy link
Author

@xezon xezon Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not intent to rename m_maxFPS for this change to have a bit less diff.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_maxLogicFPS is not the correct terminology for this. Logic FPS is what we currently refer to as enum LOGICFRAMES_PER_SECOND=30.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_maxLogicFPS is not the correct terminology for this. Logic FPS is what we currently refer to as enum LOGICFRAMES_PER_SECOND=30.

It's the currently set max logic frame rate, LOGICFRAMES_PER_SECOND is the default maximum value. But since we can / need to be able to vary the current max logic frame rate, it makes sense to call it that for the variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_logicTimeScaleFPS is conceptually not equivalent to LOGICFRAMES_PER_SECOND or m_maxLogicFPS. It effectively is a ratio that scales the logic fps.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You ask me to rename it to m_maxLogicFPS but I am telling you this is not the right name for it. Originally I used this exact name for it, until I realized it is misleading because it would be pretty much identical to LOGICFRAMES_PER_SECOND but are not the same thing. This is why I called it Logic Time Scale.

Currently Logic Time Scale is capped by the Render Update. We could perhaps also uncap it and make it a substitute for fast forwarding globally.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not misleading though, i think you are putting too much emphasis on what LOGICFRAMES_PER_SECOND means compared to what it actually is meant to be.

LOGICFRAMES_PER_SECOND is the default maximum logic frame rate during normal gameplay etc. It's basically a value used for default configuration and normal configuration.
While your m_logicTimeScaleFps is the max logic FPS that is being used within the game at runtime. Which can vary to allow fast gameplay mode in skirmish or within mod maps etc.

Both values are related but mean different things.

Logic tick rate being capped by the rendering is fine, but that's a different problem overall. The logic tick rate does not need to exceed the rendering rate and probably never should. But varying the max logic rate implements the fast forward functionality, so of course the render rate has to increase if the logic rate was to exceed it.

Copy link
Author

@xezon xezon Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thing is, when we make LOGICFRAMES_PER_SECOND configurable, then what will be its name? Given your name proposal, it would end up being something like:

Int m_logicFPS;
Int m_maxLogicFPS;

I disagree with giving these 2 things the same name.

This will be better:

Int m_logicFPS;
Int m_logicTimeScaleFPS;

The logic tick rate does not need to exceed the rendering rate and probably never should.

It does so in the original game, during fast forwarding. I think it is fine to do that. I can explore this in a follow up change.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thing is, when we make LOGICFRAMES_PER_SECOND configurable, then what will be its name?

At that point it would be like with any other configurable variable. we have the config handling set Int m_maxLogicFPS; at startup etc. Or it uses the constant LOGICFRAMES_PER_SECOND as the default.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect it will be possible to change LOGICFRAMES_PER_SECOND at runtime to any value above 0.


Real m_frameTime; ///< Last render frame time
Real m_logicFrameTimeAccumulator; ///< Frame time accumulated towards submitting a new logic frame
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to distinguish this as rendering frame time in the name of the variable m_renderFrameTime etc.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


Bool m_quitting; ///< true when we need to quit the game
Bool m_isActive; ///< app has OS focus.
Bool m_enableLogicTimeScale;

};
inline void GameEngine::setQuitting( Bool quitting ) { m_quitting = quitting; }
Expand Down
4 changes: 4 additions & 0 deletions GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ class GameMessage : public MemoryPoolObject
MSG_META_HELP, ///< bring up help screen
#endif

MSG_META_INCREASE_MAX_RENDER_FPS, ///< TheSuperHackers @feature Increase the max render fps
MSG_META_DECREASE_MAX_RENDER_FPS, ///< TheSuperHackers @feature Decrease the max render fps
MSG_META_INCREASE_MAX_LOGIC_FPS, ///< TheSuperHackers @feature Increase the max logic fps
MSG_META_DECREASE_MAX_LOGIC_FPS, ///< TheSuperHackers @feature Decrease the max logic fps
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs renaming

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

MSG_META_TOGGLE_LOWER_DETAILS, ///< toggles graphics options to crappy mode instantly
MSG_META_TOGGLE_CONTROL_BAR, ///< show/hide controlbar

Expand Down
2 changes: 2 additions & 0 deletions GeneralsMD/Code/GameEngine/Include/GameClient/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class Display : public SubsystemInterface

virtual void drawViews( void ); ///< Render all views of the world
virtual void updateViews ( void ); ///< Updates state of world views
virtual void stepViews(); ///< Update views for every fixed time step

virtual VideoBuffer* createVideoBuffer( void ) = 0; ///< Create a video buffer that can be used for this display

Expand All @@ -118,6 +119,7 @@ class Display : public SubsystemInterface
virtual Bool isClippingEnabled( void ) = 0;
virtual void enableClipping( Bool onoff ) = 0;

virtual void step() {}; ///< Do one fixed time step
virtual void draw( void ); ///< Redraw the entire display
virtual void setTimeOfDay( TimeOfDay tod ) = 0; ///< Set the time of day for this display
virtual void createLightPulse( const Coord3D *pos, const RGBColor *color, Real innerRadius,Real attenuationWidth,
Expand Down
62 changes: 39 additions & 23 deletions GeneralsMD/Code/GameEngine/Include/GameClient/MetaEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ static const LookupListRec CategoryListName[] =
// KeyDefType; this is extremely important to maintain!
enum MappableKeyType CPP_11(: Int)
{
// keypad keys ----------------------------------------------------------------
MK_KP0 = KEY_KP0,
MK_KP1 = KEY_KP1,
MK_KP2 = KEY_KP2,
MK_KP3 = KEY_KP3,
MK_KP4 = KEY_KP4,
MK_KP5 = KEY_KP5,
MK_KP6 = KEY_KP6,
MK_KP7 = KEY_KP7,
MK_KP8 = KEY_KP8,
MK_KP9 = KEY_KP9,
MK_KPDEL = KEY_KPDEL,
MK_KPSTAR = KEY_KPSTAR,
MK_KPMINUS = KEY_KPMINUS,
MK_KPPLUS = KEY_KPPLUS,
MK_KPENTER = KEY_KPENTER,
MK_KPSLASH = KEY_KPSLASH,

MK_ESC = KEY_ESC,
MK_BACKSPACE = KEY_BACKSPACE,
MK_ENTER = KEY_ENTER,
Expand Down Expand Up @@ -122,16 +140,6 @@ enum MappableKeyType CPP_11(: Int)
MK_8 = KEY_8,
MK_9 = KEY_9,
MK_0 = KEY_0,
MK_KP1 = KEY_KP1,
MK_KP2 = KEY_KP2,
MK_KP3 = KEY_KP3,
MK_KP4 = KEY_KP4,
MK_KP5 = KEY_KP5,
MK_KP6 = KEY_KP6,
MK_KP7 = KEY_KP7,
MK_KP8 = KEY_KP8,
MK_KP9 = KEY_KP9,
MK_KP0 = KEY_KP0,
MK_MINUS = KEY_MINUS,
MK_EQUAL = KEY_EQUAL,
MK_LBRACKET = KEY_LBRACKET,
Expand All @@ -153,13 +161,30 @@ enum MappableKeyType CPP_11(: Int)
MK_PGDN = KEY_PGDN,
MK_INS = KEY_INS,
MK_DEL = KEY_DEL,
MK_KPSLASH = KEY_KPSLASH,
MK_NONE = KEY_NONE

};

static const LookupListRec KeyNames[] =
{
// keypad keys ----------------------------------------------------------------
{ "KEY_KP0", MK_KP0 },
{ "KEY_KP1", MK_KP1 },
{ "KEY_KP2", MK_KP2 },
{ "KEY_KP3", MK_KP3 },
{ "KEY_KP4", MK_KP4 },
{ "KEY_KP5", MK_KP5 },
{ "KEY_KP6", MK_KP6 },
{ "KEY_KP7", MK_KP7 },
{ "KEY_KP8", MK_KP8 },
{ "KEY_KP9", MK_KP9 },
{ "KEY_KPDEL", MK_KPDEL },
{ "KEY_KPSTAR", MK_KPSTAR },
{ "KEY_KPMINUS", MK_KPMINUS },
{ "KEY_KPPLUS", MK_KPPLUS },
{ "KEY_KPENTER", MK_KPENTER },
{ "KEY_KPSLASH", MK_KPSLASH },

{ "KEY_ESC", MK_ESC },
{ "KEY_BACKSPACE", MK_BACKSPACE },
{ "KEY_ENTER", MK_ENTER },
Expand Down Expand Up @@ -213,16 +238,6 @@ static const LookupListRec KeyNames[] =
{ "KEY_8", MK_8 },
{ "KEY_9", MK_9 },
{ "KEY_0", MK_0 },
{ "KEY_KP1", MK_KP1 },
{ "KEY_KP2", MK_KP2 },
{ "KEY_KP3", MK_KP3 },
{ "KEY_KP4", MK_KP4 },
{ "KEY_KP5", MK_KP5 },
{ "KEY_KP6", MK_KP6 },
{ "KEY_KP7", MK_KP7 },
{ "KEY_KP8", MK_KP8 },
{ "KEY_KP9", MK_KP9 },
{ "KEY_KP0", MK_KP0 },
{ "KEY_MINUS", MK_MINUS },
{ "KEY_EQUAL", MK_EQUAL },
{ "KEY_LBRACKET", MK_LBRACKET },
Expand All @@ -244,7 +259,6 @@ static const LookupListRec KeyNames[] =
{ "KEY_PGDN", MK_PGDN },
{ "KEY_INS", MK_INS },
{ "KEY_DEL", MK_DEL },
{ "KEY_KPSLASH", MK_KPSLASH },
{ "KEY_NONE", MK_NONE },
{ NULL, 0 } // keep this last!
};
Expand Down Expand Up @@ -301,7 +315,9 @@ enum CommandUsableInType CPP_11(: Int)
COMMANDUSABLE_NONE = 0,

COMMANDUSABLE_SHELL = (1 << 0),
COMMANDUSABLE_GAME = (1 << 1)
COMMANDUSABLE_GAME = (1 << 1),

COMMANDUSABLE_EVERYWHERE = COMMANDUSABLE_SHELL | COMMANDUSABLE_GAME,
};

static const char* TheCommandUsableInNames[] =
Expand Down
Loading
Loading