Skip to content

Commit

Permalink
Use device gamma when in windowed mode
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Sep 13, 2024
1 parent ad79269 commit f3625ad
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7144
#define BUILD_NUMBER 7145
32 changes: 32 additions & 0 deletions Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,38 @@ void Utils::ResetScreenSettings()
RedrawWindow(nullptr, nullptr, nullptr, RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_UPDATENOW);
}

void Utils::ResetGamma()
{
// Get the device context for the screen
HDC hdc = ::GetDC(NULL);

if (hdc)
{
// Create a default gamma ramp (linear ramp)
WORD defaultRamp[3][256] = {};
for (int i = 0; i < 256; i++)
{
WORD value = WORD(i * 257); // Linear mapping (0 maps to 0, 255 maps to 65535)
defaultRamp[0][i] = value; // Red
defaultRamp[1][i] = value; // Green
defaultRamp[2][i] = value; // Blue
}

// Set the default gamma ramp
if (!::SetDeviceGammaRamp(hdc, defaultRamp))
{
Logging::Log() << __FUNCTION__ << " Error: Failed to reset gamma ramp!";
}

// Release the device context
::ReleaseDC(NULL, hdc);
}
else
{
Logging::Log() << __FUNCTION__ << " Error: Failed to get device context!";
}
}

HWND Utils::GetTopLevelWindowOfCurrentProcess()
{
HWND foregroundWindow = GetForegroundWindow();
Expand Down
1 change: 1 addition & 0 deletions Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace Utils
bool IsWindows8OrNewer();
void GetScreenSettings();
void ResetScreenSettings();
void ResetGamma();
bool IsWindowRectEqualOrLarger(HWND srchWnd, HWND desthWnd);
HWND GetTopLevelWindowOfCurrentProcess();
HMONITOR GetMonitorHandle(HWND hWnd);
Expand Down
33 changes: 32 additions & 1 deletion d3d9/IDirect3DDevice9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,38 @@ void m_IDirect3DDevice9Ex::SetGammaRamp(THIS_ UINT iSwapChain, DWORD Flags, CONS
{
Logging::LogDebug() << __FUNCTION__ << " (" << this << ")";

return ProxyInterface->SetGammaRamp(iSwapChain, Flags, pRamp);
ProxyInterface->SetGammaRamp(iSwapChain, Flags, pRamp);

if (Config.EnableWindowMode && pRamp)
{
// Get the device context for the screen
HDC hdc = ::GetDC(SHARED.DeviceWindow);
if (!hdc)
{
LOG_LIMIT(100, __FUNCTION__ << " Error: Failed to get device context!");
return;
}

// Set up the gamma ramp array
WORD gammaRamp[3][256] = {};

for (int i = 0; i < 256; i++)
{
gammaRamp[0][i] = pRamp->red[i]; // Red channel
gammaRamp[1][i] = pRamp->green[i]; // Green channel
gammaRamp[2][i] = pRamp->blue[i]; // Blue channel
}

// Call the Windows API to set the gamma ramp
if (!::SetDeviceGammaRamp(hdc, gammaRamp))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: Failed to set gamma ramp!");
}
WasGammaSet = true;

// Release the device context
::ReleaseDC(SHARED.DeviceWindow, hdc);
}
}

HRESULT m_IDirect3DDevice9Ex::DeletePatch(UINT Handle)
Expand Down
11 changes: 11 additions & 0 deletions d3d9/IDirect3DDevice9Ex.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "Utils\Utils.h"

static constexpr size_t MAX_CLIP_PLANES = 6;

struct DEVICEDETAILS
Expand Down Expand Up @@ -57,6 +59,9 @@ class m_IDirect3DDevice9Ex : public IDirect3DDevice9Ex, public AddressLookupTabl
// Limit frame rate
void LimitFrameRate();

// For gamma
bool WasGammaSet = false;

// Anisotropic Filtering
void DisableAnisotropicSamplerState(bool AnisotropyMin, bool AnisotropyMag);
void ReeableAnisotropicSamplerState();
Expand Down Expand Up @@ -100,6 +105,12 @@ class m_IDirect3DDevice9Ex : public IDirect3DDevice9Ex, public AddressLookupTabl
{
LOG_LIMIT(3, __FUNCTION__ << " (" << this << ")" << " deleting interface!");

// Reset gamma
if (WasGammaSet)
{
Utils::ResetGamma();
}

// Remove WndProc after releasing d3d9 device
if (EnableWndProcHook)
{
Expand Down
9 changes: 2 additions & 7 deletions ddraw/IDirectDrawX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4098,13 +4098,8 @@ HRESULT m_IDirectDrawX::SetD9Gamma(DWORD dwFlags, LPDDGAMMARAMP lpRampData)
return DDERR_GENERIC;
}

if (!presParams.Windowed)
{
d3d9Device->SetGammaRamp(0, dwFlags, (D3DGAMMARAMP*)lpRampData);
return DD_OK;
}

return D3DERR_INVALIDCALL;
d3d9Device->SetGammaRamp(0, dwFlags, (D3DGAMMARAMP*)lpRampData);
return DD_OK;
}

HRESULT m_IDirectDrawX::CopyPrimarySurfaceToBackbuffer()
Expand Down

0 comments on commit f3625ad

Please sign in to comment.