Skip to content

Commit

Permalink
Translate messages before create or reset device
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Sep 13, 2024
1 parent 39e384c commit ad79269
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 34 deletions.
1 change: 1 addition & 0 deletions DDrawCompat/v0.3.1/Dll/DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ namespace Compat32
if (RunOnce && !DDrawCompat::IsEnabled())
{
RunOnce = false;
Dll::g_currentModule = hModule_dll;
Time::init();
Compat32::Log() << "Installing memory management hooks";
Win32::MemoryManagement::installHooks();
Expand Down
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7143
#define BUILD_NUMBER 7144
79 changes: 51 additions & 28 deletions d3d9/IDirect3D9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ HRESULT m_IDirect3D9Ex::CreateDeviceT(D3DPRESENT_PARAMETERS& d3dpp, bool& MultiS
hr = CreateDeviceT(Adapter, DeviceType, hFocusWindow, BehaviorFlags, &d3dpp, (d3dpp.Windowed) ? nullptr : pFullscreenDisplayMode, ppReturnedDeviceInterface);
}

if (SUCCEEDED(hr))
{
GetFinalPresentParameter(&d3dpp, DeviceDetails);
}

return hr;
}

Expand Down Expand Up @@ -519,6 +524,14 @@ void UpdatePresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, HWND
(IsWindow(pPresentationParameters->hDeviceWindow)) ? pPresentationParameters->hDeviceWindow :
DeviceDetails.DeviceWindow;

// Make sure all windows messages are handled before creating or resetting the device
MSG msg;
if (PeekMessageA(&msg, DeviceDetails.DeviceWindow, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}

// Check if window is minimized
if (IsIconic(DeviceDetails.DeviceWindow))
{
Expand Down Expand Up @@ -547,43 +560,53 @@ void UpdatePresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, HWND
DeviceDetails.BufferWidth = tempRect.right;
DeviceDetails.BufferHeight = tempRect.bottom;
}
}

// Set window size
if (SetWindow && pPresentationParameters->Windowed && IsWindow(DeviceDetails.DeviceWindow))
{
bool AnyChange = (LastBufferWidth != DeviceDetails.BufferWidth || LastBufferHeight != DeviceDetails.BufferHeight || LastDeviceWindow != DeviceDetails.DeviceWindow);

// Adjust window
RECT Rect;
GetClientRect(DeviceDetails.DeviceWindow, &Rect);
if (AnyChange || Rect.right - Rect.left != DeviceDetails.BufferWidth || Rect.bottom - Rect.top != DeviceDetails.BufferHeight)
// Set window size
if (SetWindow && pPresentationParameters->Windowed)
{
AdjustWindow(DeviceDetails.DeviceWindow, DeviceDetails.BufferWidth, DeviceDetails.BufferHeight, pPresentationParameters->Windowed);
}
bool AnyChange = (LastBufferWidth != DeviceDetails.BufferWidth || LastBufferHeight != DeviceDetails.BufferHeight || LastDeviceWindow != DeviceDetails.DeviceWindow);

// Set fullscreen resolution
if (AnyChange && Config.FullscreenWindowMode)
{
// Get monitor info
MONITORINFOEX infoex = {};
infoex.cbSize = sizeof(MONITORINFOEX);
BOOL bRet = GetMonitorInfo(Utils::GetMonitorHandle(DeviceDetails.DeviceWindow), &infoex);

// Get resolution list for specified monitor
DEVMODE newSettings = {};
newSettings.dmSize = sizeof(newSettings);
if (EnumDisplaySettings(bRet ? infoex.szDevice : nullptr, ENUM_CURRENT_SETTINGS, &newSettings) != 0)
// Adjust window
RECT Rect;
GetClientRect(DeviceDetails.DeviceWindow, &Rect);
if (AnyChange || Rect.right - Rect.left != DeviceDetails.BufferWidth || Rect.bottom - Rect.top != DeviceDetails.BufferHeight)
{
newSettings.dmPelsWidth = DeviceDetails.BufferWidth;
newSettings.dmPelsHeight = DeviceDetails.BufferHeight;
newSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
ChangeDisplaySettingsEx(bRet ? infoex.szDevice : nullptr, &newSettings, nullptr, CDS_FULLSCREEN, nullptr);
AdjustWindow(DeviceDetails.DeviceWindow, DeviceDetails.BufferWidth, DeviceDetails.BufferHeight, pPresentationParameters->Windowed);
}

// Set fullscreen resolution
if (AnyChange && Config.FullscreenWindowMode)
{
// Get monitor info
MONITORINFOEX infoex = {};
infoex.cbSize = sizeof(MONITORINFOEX);
BOOL bRet = GetMonitorInfo(Utils::GetMonitorHandle(DeviceDetails.DeviceWindow), &infoex);

// Get resolution list for specified monitor
DEVMODE newSettings = {};
newSettings.dmSize = sizeof(newSettings);
if (EnumDisplaySettings(bRet ? infoex.szDevice : nullptr, ENUM_CURRENT_SETTINGS, &newSettings) != 0)
{
newSettings.dmPelsWidth = DeviceDetails.BufferWidth;
newSettings.dmPelsHeight = DeviceDetails.BufferHeight;
newSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
ChangeDisplaySettingsEx(bRet ? infoex.szDevice : nullptr, &newSettings, nullptr, CDS_FULLSCREEN, nullptr);
}
}
}
}
}

void GetFinalPresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, DEVICEDETAILS& DeviceDetails)
{
if (IsWindow(pPresentationParameters->hDeviceWindow) || IsWindow(DeviceDetails.DeviceWindow))
{
DeviceDetails.BufferWidth = (pPresentationParameters->BackBufferWidth) ? pPresentationParameters->BackBufferWidth : DeviceDetails.BufferWidth;
DeviceDetails.BufferHeight = (pPresentationParameters->BackBufferHeight) ? pPresentationParameters->BackBufferHeight : DeviceDetails.BufferHeight;
DeviceDetails.DeviceWindow = (IsWindow(pPresentationParameters->hDeviceWindow)) ? pPresentationParameters->hDeviceWindow : DeviceDetails.DeviceWindow;
}
}

// Set Presentation Parameters for Multisample
void UpdatePresentParameterForMultisample(D3DPRESENT_PARAMETERS* pPresentationParameters, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD MultiSampleQuality)
{
Expand Down
1 change: 1 addition & 0 deletions d3d9/IDirect3D9Ex.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

void UpdatePresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, HWND hFocusWindow, DEVICEDETAILS& DeviceDetails, bool ForceExclusiveFullscreen, bool SetWindow);
void GetFinalPresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, DEVICEDETAILS& DeviceDetails);
void UpdatePresentParameterForMultisample(D3DPRESENT_PARAMETERS* pPresentationParameters, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD MultiSampleQuality);

class m_IDirect3D9Ex : public IDirect3D9Ex, public AddressLookupTableD3d9Object
Expand Down
17 changes: 12 additions & 5 deletions d3d9/IDirect3DDevice9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ HRESULT m_IDirect3DDevice9Ex::ResetT(T func, D3DPRESENT_PARAMETERS &d3dpp, D3DPR

// Setup presentation parameters
CopyMemory(&d3dpp, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
UpdatePresentParameter(&d3dpp, SHARED.DeviceWindow, SHARED, ForceFullscreen, true);
UpdatePresentParameter(&d3dpp, nullptr, SHARED, ForceFullscreen, true);

// Test for Multisample
if (SHARED.DeviceMultiSampleFlag)
Expand All @@ -193,7 +193,7 @@ HRESULT m_IDirect3DDevice9Ex::ResetT(T func, D3DPRESENT_PARAMETERS &d3dpp, D3DPR

// Reset presentation parameters
CopyMemory(&d3dpp, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
UpdatePresentParameter(&d3dpp, SHARED.DeviceWindow, SHARED, ForceFullscreen, false);
UpdatePresentParameter(&d3dpp, nullptr, SHARED, ForceFullscreen, false);

// Reset device
hr = ResetT(func, &d3dpp, pFullscreenDisplayMode);
Expand All @@ -211,7 +211,14 @@ HRESULT m_IDirect3DDevice9Ex::ResetT(T func, D3DPRESENT_PARAMETERS &d3dpp, D3DPR
}

// Reset device
return ResetT(func, &d3dpp, pFullscreenDisplayMode);
hr = ResetT(func, &d3dpp, pFullscreenDisplayMode);

if (SUCCEEDED(hr))
{
GetFinalPresentParameter(&d3dpp, SHARED);
}

return hr;
}

HRESULT m_IDirect3DDevice9Ex::Reset(D3DPRESENT_PARAMETERS *pPresentationParameters)
Expand Down Expand Up @@ -300,7 +307,7 @@ HRESULT m_IDirect3DDevice9Ex::CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS *p
// Setup presentation parameters
D3DPRESENT_PARAMETERS d3dpp;
CopyMemory(&d3dpp, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
UpdatePresentParameter(&d3dpp, SHARED.DeviceWindow, SHARED, ForceFullscreen, false);
UpdatePresentParameter(&d3dpp, nullptr, SHARED, ForceFullscreen, false);

// Test for Multisample
if (SHARED.DeviceMultiSampleFlag)
Expand All @@ -315,7 +322,7 @@ HRESULT m_IDirect3DDevice9Ex::CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS *p
if (FAILED(hr))
{
CopyMemory(&d3dpp, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
UpdatePresentParameter(&d3dpp, SHARED.DeviceWindow, SHARED, ForceFullscreen, false);
UpdatePresentParameter(&d3dpp, nullptr, SHARED, ForceFullscreen, false);

// Create CwapChain
hr = ProxyInterface->CreateAdditionalSwapChain(&d3dpp, ppSwapChain);
Expand Down

0 comments on commit ad79269

Please sign in to comment.