From d9e095f75d8f74e17932aa90d76f2fcaf6aff5ed Mon Sep 17 00:00:00 2001 From: narzoul Date: Sun, 5 Jun 2016 19:23:03 +0200 Subject: [PATCH] Return the emulated display mode from GetDisplayMode --- DDrawCompat/CompatActivateAppHandler.cpp | 2 +- DDrawCompat/CompatDirectDraw.cpp | 21 +++++++++++++- DDrawCompat/CompatDirectDraw.h | 1 + DDrawCompat/CompatDirectDrawSurface.cpp | 6 ++-- DDrawCompat/CompatDisplayMode.cpp | 37 +++++++++--------------- DDrawCompat/CompatDisplayMode.h | 12 +------- DDrawCompat/CompatPaletteConverter.cpp | 25 +++++++++------- 7 files changed, 54 insertions(+), 50 deletions(-) diff --git a/DDrawCompat/CompatActivateAppHandler.cpp b/DDrawCompat/CompatActivateAppHandler.cpp index 8e44d8b..5665d58 100644 --- a/DDrawCompat/CompatActivateAppHandler.cpp +++ b/DDrawCompat/CompatActivateAppHandler.cpp @@ -36,7 +36,7 @@ namespace dd->SetCooperativeLevel(&dd, g_fullScreenCooperativeWindow, g_fullScreenCooperativeFlags); auto dm = CompatDisplayMode::getDisplayMode(dd); - dd->SetDisplayMode(&dd, dm.width, dm.height, 32, dm.refreshRate, dm.flags); + dd->SetDisplayMode(&dd, dm.dwWidth, dm.dwHeight, 32, dm.dwRefreshRate, 0); auto primary(CompatPrimarySurface::getPrimary()); if (primary && SUCCEEDED(primary->Restore(primary))) diff --git a/DDrawCompat/CompatDirectDraw.cpp b/DDrawCompat/CompatDirectDraw.cpp index d8388d1..e719b39 100644 --- a/DDrawCompat/CompatDirectDraw.cpp +++ b/DDrawCompat/CompatDirectDraw.cpp @@ -91,6 +91,7 @@ template void CompatDirectDraw::setCompatVtable(Vtable& vtable) { vtable.CreateSurface = &CreateSurface; + vtable.GetDisplayMode = &GetDisplayMode; vtable.RestoreDisplayMode = &RestoreDisplayMode; vtable.SetCooperativeLevel = &SetCooperativeLevel; vtable.SetDisplayMode = &SetDisplayMode; @@ -129,7 +130,7 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::CreateSurface( if (!(desc.dwFlags & DDSD_PIXELFORMAT)) { desc.dwFlags |= DDSD_PIXELFORMAT; - desc.ddpfPixelFormat = dm.pixelFormat; + desc.ddpfPixelFormat = dm.ddpfPixelFormat; } if (!((desc.dwFlags & DDSD_CAPS) && (desc.ddsCaps.dwCaps & (DDSCAPS_OFFSCREENPLAIN | DDSCAPS_OVERLAY | DDSCAPS_TEXTURE | @@ -154,6 +155,24 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::CreateSurface( return result; } +template +HRESULT STDMETHODCALLTYPE CompatDirectDraw::GetDisplayMode( + TDirectDraw* This, TSurfaceDesc* lpDDSurfaceDesc) +{ + const DWORD size = lpDDSurfaceDesc ? lpDDSurfaceDesc->dwSize : 0; + if (sizeof(DDSURFACEDESC) != size && sizeof(DDSURFACEDESC2) != size) + { + return DDERR_INVALIDPARAMS; + } + + CompatPtr dd(Compat::queryInterface(This)); + const DDSURFACEDESC2 dm = CompatDisplayMode::getDisplayMode(*dd); + CopyMemory(lpDDSurfaceDesc, &dm, size); + lpDDSurfaceDesc->dwSize = size; + + return DD_OK; +} + template HRESULT STDMETHODCALLTYPE CompatDirectDraw::RestoreDisplayMode(TDirectDraw* This) { diff --git a/DDrawCompat/CompatDirectDraw.h b/DDrawCompat/CompatDirectDraw.h index 124c3f9..0e37fc8 100644 --- a/DDrawCompat/CompatDirectDraw.h +++ b/DDrawCompat/CompatDirectDraw.h @@ -21,6 +21,7 @@ public: TSurface** lplpDDSurface, IUnknown* pUnkOuter); + static HRESULT STDMETHODCALLTYPE GetDisplayMode(TDirectDraw* This, TSurfaceDesc* lpDDSurfaceDesc); static HRESULT STDMETHODCALLTYPE RestoreDisplayMode(TDirectDraw* This); static HRESULT STDMETHODCALLTYPE SetCooperativeLevel(TDirectDraw* This, HWND hWnd, DWORD dwFlags); diff --git a/DDrawCompat/CompatDirectDrawSurface.cpp b/DDrawCompat/CompatDirectDrawSurface.cpp index 1da687d..527d4ef 100644 --- a/DDrawCompat/CompatDirectDrawSurface.cpp +++ b/DDrawCompat/CompatDirectDrawSurface.cpp @@ -188,11 +188,11 @@ HRESULT CompatDirectDrawSurface::createCompatPrimarySurface( CompatPtr dd7(Compat::queryInterface(&dd)); const auto& dm = CompatDisplayMode::getDisplayMode(*dd7); compatDesc.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT; - compatDesc.dwWidth = dm.width; - compatDesc.dwHeight = dm.height; + compatDesc.dwWidth = dm.dwWidth; + compatDesc.dwHeight = dm.dwHeight; compatDesc.ddsCaps.dwCaps &= ~DDSCAPS_PRIMARYSURFACE; compatDesc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN; - compatDesc.ddpfPixelFormat = dm.pixelFormat; + compatDesc.ddpfPixelFormat = dm.ddpfPixelFormat; result = dd->CreateSurface(&dd, &compatDesc, &compatSurface, nullptr); if (FAILED(result)) diff --git a/DDrawCompat/CompatDisplayMode.cpp b/DDrawCompat/CompatDisplayMode.cpp index 6e5f19a..beb62a8 100644 --- a/DDrawCompat/CompatDisplayMode.cpp +++ b/DDrawCompat/CompatDisplayMode.cpp @@ -8,7 +8,7 @@ namespace { CompatWeakPtr g_compatibleSurface = {}; HDC g_compatibleDc = nullptr; - CompatDisplayMode::DisplayMode g_emulatedDisplayMode = {}; + DDSURFACEDESC2 g_emulatedDisplayMode = {}; template @@ -72,7 +72,7 @@ namespace desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS; desc.dwWidth = 1; desc.dwHeight = 1; - desc.ddpfPixelFormat = g_emulatedDisplayMode.pixelFormat; + desc.ddpfPixelFormat = g_emulatedDisplayMode.ddpfPixelFormat; desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; auto dd = DDrawRepository::getDirectDraw(); @@ -130,6 +130,14 @@ namespace return pf; } + DDSURFACEDESC2 getRealDisplayMode(CompatRef dd) + { + DDSURFACEDESC2 desc = {}; + desc.dwSize = sizeof(desc); + dd->GetDisplayMode(&dd, &desc); + return desc; + } + void releaseCompatibleDc() { if (g_compatibleDc) @@ -183,29 +191,15 @@ namespace CompatDisplayMode return CALL_ORIG_FUNC(createDiscardableBitmap)(hdc, nWidth, nHeight); } - DisplayMode getDisplayMode(CompatRef dd) + DDSURFACEDESC2 getDisplayMode(CompatRef dd) { - if (0 == g_emulatedDisplayMode.width) + if (0 == g_emulatedDisplayMode.dwSize) { g_emulatedDisplayMode = getRealDisplayMode(dd); } return g_emulatedDisplayMode; } - DisplayMode getRealDisplayMode(CompatRef dd) - { - DDSURFACEDESC2 desc = {}; - desc.dwSize = sizeof(desc); - dd->GetDisplayMode(&dd, &desc); - - DisplayMode dm = {}; - dm.width = desc.dwWidth; - dm.height = desc.dwHeight; - dm.pixelFormat = desc.ddpfPixelFormat; - dm.refreshRate = desc.dwRefreshRate; - return dm; - } - void installHooks() { HOOK_FUNCTION(user32, ChangeDisplaySettingsExA, changeDisplaySettingsExA); @@ -240,11 +234,8 @@ namespace CompatDisplayMode return result; } - g_emulatedDisplayMode.width = width; - g_emulatedDisplayMode.height = height; - g_emulatedDisplayMode.pixelFormat = pf; - g_emulatedDisplayMode.refreshRate = refreshRate; - g_emulatedDisplayMode.flags = flags; + g_emulatedDisplayMode = getRealDisplayMode(dd); + g_emulatedDisplayMode.ddpfPixelFormat = pf; updateCompatibleDc(); return DD_OK; diff --git a/DDrawCompat/CompatDisplayMode.h b/DDrawCompat/CompatDisplayMode.h index 8742054..99dd053 100644 --- a/DDrawCompat/CompatDisplayMode.h +++ b/DDrawCompat/CompatDisplayMode.h @@ -8,15 +8,6 @@ namespace CompatDisplayMode { - struct DisplayMode - { - DWORD width; - DWORD height; - DDPIXELFORMAT pixelFormat; - DWORD refreshRate; - DWORD flags; - }; - void installHooks(); HBITMAP WINAPI createCompatibleBitmap(HDC hdc, int cx, int cy); @@ -24,8 +15,7 @@ namespace CompatDisplayMode const void* lpbInit, const BITMAPINFO* lpbmi, UINT fuUsage); HBITMAP WINAPI createDiscardableBitmap(HDC hdc, int nWidth, int nHeight); - DisplayMode getDisplayMode(CompatRef dd); - DisplayMode getRealDisplayMode(CompatRef dd); + DDSURFACEDESC2 getDisplayMode(CompatRef dd); HRESULT restoreDisplayMode(CompatRef dd); HRESULT setDisplayMode(CompatRef dd, DWORD width, DWORD height, DWORD bpp, DWORD refreshRate = 0, DWORD flags = 0); diff --git a/DDrawCompat/CompatPaletteConverter.cpp b/DDrawCompat/CompatPaletteConverter.cpp index 22fd6d6..8f77524 100644 --- a/DDrawCompat/CompatPaletteConverter.cpp +++ b/DDrawCompat/CompatPaletteConverter.cpp @@ -26,7 +26,7 @@ namespace } } - HBITMAP createDibSection(const CompatDisplayMode::DisplayMode& dm, void*& bits) + HBITMAP createDibSection(const DDSURFACEDESC2& dm, void*& bits) { struct PalettizedBitmapInfo { @@ -36,27 +36,27 @@ namespace PalettizedBitmapInfo bmi = {}; bmi.header.biSize = sizeof(bmi.header); - bmi.header.biWidth = dm.width; - bmi.header.biHeight = -static_cast(dm.height); + bmi.header.biWidth = dm.dwWidth; + bmi.header.biHeight = -static_cast(dm.dwHeight); bmi.header.biPlanes = 1; - bmi.header.biBitCount = static_cast(dm.pixelFormat.dwRGBBitCount); + bmi.header.biBitCount = static_cast(dm.ddpfPixelFormat.dwRGBBitCount); bmi.header.biCompression = BI_RGB; return CreateDIBSection(nullptr, reinterpret_cast(&bmi), DIB_RGB_COLORS, &bits, nullptr, 0); } - CompatPtr createSurface(const CompatDisplayMode::DisplayMode& dm, void* bits) + CompatPtr createSurface(const DDSURFACEDESC2& dm, void* bits) { DDSURFACEDESC2 desc = {}; desc.dwSize = sizeof(desc); desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS | DDSD_PITCH | DDSD_LPSURFACE; - desc.dwWidth = dm.width; - desc.dwHeight = dm.height; - desc.ddpfPixelFormat = dm.pixelFormat; + desc.dwWidth = dm.dwWidth; + desc.dwHeight = dm.dwHeight; + desc.ddpfPixelFormat = dm.ddpfPixelFormat; desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; - desc.lPitch = (dm.width * dm.pixelFormat.dwRGBBitCount / 8 + 3) & ~3; + desc.lPitch = (dm.dwWidth * dm.ddpfPixelFormat.dwRGBBitCount / 8 + 3) & ~3; desc.lpSurface = bits; auto dd(DDrawRepository::getDirectDraw()); @@ -72,8 +72,11 @@ namespace CompatPaletteConverter { auto dd(DDrawRepository::getDirectDraw()); auto dm(CompatDisplayMode::getDisplayMode(*dd)); - if (dm.pixelFormat.dwRGBBitCount > 8 && - CompatDisplayMode::getRealDisplayMode(*dd).pixelFormat.dwRGBBitCount > 8) + DDSURFACEDESC2 realDm = {}; + realDm.dwSize = sizeof(realDm); + dd->GetDisplayMode(dd, &realDm); + if (dm.ddpfPixelFormat.dwRGBBitCount > 8 && + realDm.ddpfPixelFormat.dwRGBBitCount > 8) { return true; }