Skip to content

Commit

Permalink
Set main menu FPS limit to current display refresh rate (PR #4083)
Browse files Browse the repository at this point in the history
  • Loading branch information
samr46 authored Mar 8, 2025
1 parent 6bb19ac commit acbcc8e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Client/core/CCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ CCore::CCore()
m_bGettingIdleCallsFromMultiplayer = false;
m_bWindowsTimerEnabled = false;
m_timeDiscordAppLastUpdate = 0;
m_CurrentRefreshRate = 60;

// Create tray icon
m_pTrayIcon = new CTrayIcon();
Expand Down Expand Up @@ -1835,6 +1836,9 @@ void CCore::RecalculateFrameRateLimit(uint uiServerFrameRateLimit, bool bLogToCo
if ((m_uiFrameRateLimit == 0 || uiClientScriptRate < m_uiFrameRateLimit) && uiClientScriptRate > 0)
m_uiFrameRateLimit = uiClientScriptRate;

if (!IsConnected())
m_uiFrameRateLimit = m_CurrentRefreshRate;

// Removes Limiter from Frame Graph if limit is zero and skips frame limit
if (m_uiFrameRateLimit == 0)
{
Expand All @@ -1861,6 +1865,12 @@ void CCore::SetClientScriptFrameRateLimit(uint uiClientScriptFrameRateLimit)
RecalculateFrameRateLimit(-1, false);
}

void CCore::SetCurrentRefreshRate(uint value)
{
m_CurrentRefreshRate = value;
RecalculateFrameRateLimit(-1, false);
}

//
// Make sure the frame rate limit has been applied since the last call
//
Expand Down Expand Up @@ -1921,7 +1931,7 @@ void CCore::ApplyQueuedFrameRateLimit()
double dSpare = dTargetTimeToUse - m_FrameRateTimer.Get();
if (dSpare <= 0.0)
break;
if (dSpare >= 2.0)
if (dSpare >= 10.0)
Sleep(1);
}
m_FrameRateTimer.Reset();
Expand Down
2 changes: 2 additions & 0 deletions Client/core/CCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
void ApplyQueuedFrameRateLimit();
void EnsureFrameRateLimitApplied();
void SetClientScriptFrameRateLimit(uint uiClientScriptFrameRateLimit);
void SetCurrentRefreshRate(uint value);
void DoReliablePulse();

bool IsTimingCheckpoints();
Expand Down Expand Up @@ -371,6 +372,7 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
CElapsedTimeHD m_FrameRateTimer;
uint m_uiQueuedFrameRate;
bool m_bQueuedFrameRateValid;
uint m_CurrentRefreshRate;
bool m_requestNewNickname{false};
EDiagnosticDebugType m_DiagnosticDebug;

Expand Down
3 changes: 3 additions & 0 deletions Client/core/CMainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,9 @@ void CMainMenu::SetIsIngame(bool bIsIngame)
m_bIsIngame = bIsIngame;
m_Settings.SetIsModLoaded(bIsIngame);

// Reset frame rate limit
CCore::GetSingleton().RecalculateFrameRateLimit(-1, false);

m_ulMoveStartTick = GetTickCount32();
if (bIsIngame)
{
Expand Down
2 changes: 1 addition & 1 deletion Client/core/CModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void CModManager::DoPulsePostFrame()
if (m_client != nullptr)
CCore::GetSingleton().EnsureFrameRateLimitApplied(); // Catch missed frames
else
CCore::GetSingleton().ApplyFrameRateLimit(88); // Limit when not connected
CCore::GetSingleton().ApplyFrameRateLimit(); // Limit when not connected

if (m_state == State::PendingStart)
{
Expand Down
7 changes: 7 additions & 0 deletions Client/core/DXHook/CProxyDirect3D9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,13 @@ HRESULT HandleCreateDeviceResult(HRESULT hResult, IDirect3D9* pDirect3D, UINT Ad
strMessage += SString("Direct3D CreateDevice error: %08x", hResult);
BrowseToSolution("d3dcreatedevice-fail", EXIT_GAME_FIRST | ASK_GO_ONLINE, strMessage);
}
else
{
// Get current refresh rate
D3DDISPLAYMODE DisplayMode;
if (pDirect3D->GetAdapterDisplayMode(Adapter, &DisplayMode) == D3D_OK)
CCore::GetSingleton().SetCurrentRefreshRate(DisplayMode.RefreshRate);
}

return hResult;
}
Expand Down

0 comments on commit acbcc8e

Please sign in to comment.