forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 89
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
xezon
wants to merge
7
commits into
TheSuperHackers:main
Choose a base branch
from
xezon:xezon/decouple-logic-render
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d49449
bugfix(fps): Fix inaccurate frame rate cap (#1451)
xezon 461c301
tweak(fps): Decouple non-network logic update from render update (#1451)
xezon 37fa668
tweak(fps): Decouple WW3D::Sync from render update (#1451)
xezon b541cae
tweak(fps): Decouple scripted camera movement time step from render u…
xezon f422b4e
tweak(fps): Decouple drawable time step from render update (#1451)
xezon 579f189
bugfix(fps): Decouple camera shaker time step from render update (#1451)
xezon 3bf9396
bugfix(fps): Decouple camera zoom and rotation time step from render …
xezon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m_logicTickRate
might be a better name for these instead of usinglogicTimeScale
since time scale implies a period of time instead of a rate of action.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see that logic tick rate is a better name. Logic time scale is accurate, because time scale 2 means that the game runs twice as fast. If we can avoid changing the name, then that would be good, because it will take a while to rename everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is never set to a value that acts as a multiplier, it's set to a specific Tick rate / frames per second. That is then used to cap the update of the game logic.
Which is why the naming does not look correct to me on the surface compared to what you might have in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value
Int m_logicTimeScaleFPS
is a multiplier in disguise. It is synonymous toReal m_logicTimeScaleRatio
, just with a different unit. As we can see in the GameEngine class interface, the time scale ratio can be inferred from the fps value.