|
| 1 | +/* |
| 2 | +** Command & Conquer Generals Zero Hour(tm) |
| 3 | +** Copyright 2025 Electronic Arts Inc. |
| 4 | +** |
| 5 | +** This program is free software: you can redistribute it and/or modify |
| 6 | +** it under the terms of the GNU General Public License as published by |
| 7 | +** the Free Software Foundation, either version 3 of the License, or |
| 8 | +** (at your option) any later version. |
| 9 | +** |
| 10 | +** This program is distributed in the hope that it will be useful, |
| 11 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +** GNU General Public License for more details. |
| 14 | +** |
| 15 | +** You should have received a copy of the GNU General Public License |
| 16 | +** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | +#include "PreRTS.h" |
| 19 | + |
| 20 | +#include "Common/FramePacer.h" |
| 21 | + |
| 22 | +#include "GameClient/View.h" |
| 23 | + |
| 24 | +#include "GameLogic/GameLogic.h" |
| 25 | +#include "GameLogic/ScriptEngine.h" |
| 26 | + |
| 27 | +#include "GameNetwork/NetworkDefs.h" |
| 28 | +#include "GameNetwork/NetworkInterface.h" |
| 29 | + |
| 30 | + |
| 31 | +FramePacer* TheFramePacer = NULL; |
| 32 | + |
| 33 | +FramePacer::FramePacer() |
| 34 | +{ |
| 35 | + // Set the time slice size to 1 ms. |
| 36 | + timeBeginPeriod(1); |
| 37 | + |
| 38 | + m_maxFPS = BaseFps; |
| 39 | + m_logicTimeScaleFPS = LOGICFRAMES_PER_SECOND; |
| 40 | + m_updateTime = 1.0f / BaseFps; // initialized to something to avoid division by zero on first use |
| 41 | + m_enableLogicTimeScale = FALSE; |
| 42 | + m_isTimeFrozen = FALSE; |
| 43 | + m_isGameHalted = FALSE; |
| 44 | +} |
| 45 | + |
| 46 | +FramePacer::~FramePacer() |
| 47 | +{ |
| 48 | + // Restore the previous time slice for Windows. |
| 49 | + timeEndPeriod(1); |
| 50 | +} |
| 51 | + |
| 52 | +void FramePacer::update() |
| 53 | +{ |
| 54 | + Bool allowFpsLimit = TheTacticalView->getTimeMultiplier()<=1 && !TheScriptEngine->isTimeFast(); |
| 55 | + |
| 56 | + // I'm disabling this in debug because many people need alt-tab capability. If you happen to be |
| 57 | + // doing performance tuning, please just change this on your local system. -MDC |
| 58 | +#if defined(RTS_DEBUG) |
| 59 | + if (allowFpsLimit) |
| 60 | + ::Sleep(1); // give everyone else a tiny time slice. |
| 61 | +#endif |
| 62 | + |
| 63 | +#if defined(_ALLOW_DEBUG_CHEATS_IN_RELEASE) |
| 64 | + allowFpsLimit &= !(!TheGameLogic->isGamePaused() && TheGlobalData->m_TiVOFastMode); |
| 65 | +#else //always allow this cheat key if we're in a replay game. |
| 66 | + allowFpsLimit &= !(!TheGameLogic->isGamePaused() && TheGlobalData->m_TiVOFastMode && TheGameLogic->isInReplayGame()); |
| 67 | +#endif |
| 68 | + |
| 69 | + // TheSuperHackers @bugfix xezon 05/08/2025 Re-implements the frame rate limiter |
| 70 | + // with higher resolution counters to cap the frame rate more accurately to the desired limit. |
| 71 | + allowFpsLimit &= TheGlobalData->m_useFpsLimit; |
| 72 | + const UnsignedInt maxFps = allowFpsLimit ? getFramesPerSecondLimit() : RenderFpsPreset::UncappedFpsValue; |
| 73 | + m_updateTime = m_frameRateLimit.wait(maxFps); |
| 74 | +} |
| 75 | + |
| 76 | +void FramePacer::setFramesPerSecondLimit( Int fps ) |
| 77 | +{ |
| 78 | + DEBUG_LOG(("FramePacer::setFramesPerSecondLimit() - setting max fps to %d (TheGlobalData->m_useFpsLimit == %d)", fps, TheGlobalData->m_useFpsLimit)); |
| 79 | + m_maxFPS = fps; |
| 80 | +} |
| 81 | + |
| 82 | +Int FramePacer::getFramesPerSecondLimit() const |
| 83 | +{ |
| 84 | + return m_maxFPS; |
| 85 | +} |
| 86 | + |
| 87 | +Real FramePacer::getUpdateTime() const |
| 88 | +{ |
| 89 | + return m_updateTime; |
| 90 | +} |
| 91 | + |
| 92 | +Real FramePacer::getUpdateFps() const |
| 93 | +{ |
| 94 | + return 1.0f / m_updateTime; |
| 95 | +} |
| 96 | + |
| 97 | +void FramePacer::setTimeFrozen(Bool frozen) |
| 98 | +{ |
| 99 | + m_isTimeFrozen = frozen; |
| 100 | +} |
| 101 | + |
| 102 | +void FramePacer::setGameHalted(Bool halted) |
| 103 | +{ |
| 104 | + m_isGameHalted = halted; |
| 105 | +} |
| 106 | + |
| 107 | +Bool FramePacer::isTimeFrozen() const |
| 108 | +{ |
| 109 | + return m_isTimeFrozen; |
| 110 | +} |
| 111 | + |
| 112 | +Bool FramePacer::isGameHalted() const |
| 113 | +{ |
| 114 | + return m_isGameHalted; |
| 115 | +} |
| 116 | + |
| 117 | +void FramePacer::setLogicTimeScaleFps( Int fps ) |
| 118 | +{ |
| 119 | + m_logicTimeScaleFPS = fps; |
| 120 | +} |
| 121 | + |
| 122 | +Int FramePacer::getLogicTimeScaleFps() const |
| 123 | +{ |
| 124 | + return m_logicTimeScaleFPS; |
| 125 | +} |
| 126 | + |
| 127 | +void FramePacer::enableLogicTimeScale( Bool enable ) |
| 128 | +{ |
| 129 | + m_enableLogicTimeScale = enable; |
| 130 | +} |
| 131 | + |
| 132 | +Bool FramePacer::isLogicTimeScaleEnabled() const |
| 133 | +{ |
| 134 | + return m_enableLogicTimeScale; |
| 135 | +} |
| 136 | + |
| 137 | +Int FramePacer::getActualLogicTimeScaleFps(LogicTimeQueryFlags flags) const |
| 138 | +{ |
| 139 | + if (m_isTimeFrozen && (flags & IgnoreFrozenTime) == 0) |
| 140 | + { |
| 141 | + return 0; |
| 142 | + } |
| 143 | + |
| 144 | + if (m_isGameHalted && (flags & IgnoreHaltedGame) == 0) |
| 145 | + { |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + if (TheNetwork != NULL) |
| 150 | + { |
| 151 | + return TheNetwork->getFrameRate(); |
| 152 | + } |
| 153 | + |
| 154 | + if (isLogicTimeScaleEnabled()) |
| 155 | + { |
| 156 | + return min(getLogicTimeScaleFps(), getFramesPerSecondLimit()); |
| 157 | + } |
| 158 | + |
| 159 | + return getFramesPerSecondLimit(); |
| 160 | +} |
| 161 | + |
| 162 | +Real FramePacer::getActualLogicTimeScaleRatio(LogicTimeQueryFlags flags) const |
| 163 | +{ |
| 164 | + return (Real)getActualLogicTimeScaleFps(flags) / LOGICFRAMES_PER_SECONDS_REAL; |
| 165 | +} |
| 166 | + |
| 167 | +Real FramePacer::getActualLogicTimeScaleOverFpsRatio(LogicTimeQueryFlags flags) const |
| 168 | +{ |
| 169 | + // TheSuperHackers @info Clamps ratio to min 1, because the logic |
| 170 | + // frame rate is currently capped by the render frame rate. |
| 171 | + return min(1.0f, (Real)getActualLogicTimeScaleFps(flags) / getUpdateFps()); |
| 172 | +} |
| 173 | + |
| 174 | +Real FramePacer::getLogicTimeStepSeconds(LogicTimeQueryFlags flags) const |
| 175 | +{ |
| 176 | + return SECONDS_PER_LOGICFRAME_REAL * getActualLogicTimeScaleOverFpsRatio(flags); |
| 177 | +} |
| 178 | + |
| 179 | +Real FramePacer::getLogicTimeStepMilliseconds(LogicTimeQueryFlags flags) const |
| 180 | +{ |
| 181 | + return MSEC_PER_LOGICFRAME_REAL * getActualLogicTimeScaleOverFpsRatio(flags); |
| 182 | +} |
0 commit comments