From ad79269dcdc4554da35a41e1d86b042ba48b0265 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Thu, 12 Sep 2024 20:32:32 -0700 Subject: [PATCH] Translate messages before create or reset device --- DDrawCompat/v0.3.1/Dll/DllMain.cpp | 1 + Dllmain/BuildNo.rc | 2 +- d3d9/IDirect3D9Ex.cpp | 79 +++++++++++++++++++----------- d3d9/IDirect3D9Ex.h | 1 + d3d9/IDirect3DDevice9Ex.cpp | 17 +++++-- 5 files changed, 66 insertions(+), 34 deletions(-) diff --git a/DDrawCompat/v0.3.1/Dll/DllMain.cpp b/DDrawCompat/v0.3.1/Dll/DllMain.cpp index fc971338..3cc74a29 100644 --- a/DDrawCompat/v0.3.1/Dll/DllMain.cpp +++ b/DDrawCompat/v0.3.1/Dll/DllMain.cpp @@ -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(); diff --git a/Dllmain/BuildNo.rc b/Dllmain/BuildNo.rc index 6641a4b3..e6c1384c 100644 --- a/Dllmain/BuildNo.rc +++ b/Dllmain/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 7143 +#define BUILD_NUMBER 7144 diff --git a/d3d9/IDirect3D9Ex.cpp b/d3d9/IDirect3D9Ex.cpp index ef5c46d0..be7b2565 100644 --- a/d3d9/IDirect3D9Ex.cpp +++ b/d3d9/IDirect3D9Ex.cpp @@ -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; } @@ -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)) { @@ -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) { diff --git a/d3d9/IDirect3D9Ex.h b/d3d9/IDirect3D9Ex.h index d953a181..7ddafa6d 100644 --- a/d3d9/IDirect3D9Ex.h +++ b/d3d9/IDirect3D9Ex.h @@ -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 diff --git a/d3d9/IDirect3DDevice9Ex.cpp b/d3d9/IDirect3DDevice9Ex.cpp index 6261145f..7a6ebf0c 100644 --- a/d3d9/IDirect3DDevice9Ex.cpp +++ b/d3d9/IDirect3DDevice9Ex.cpp @@ -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) @@ -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); @@ -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) @@ -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) @@ -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);