From c4ea2541a905617c5ab5bef92ad10bd260ba804a Mon Sep 17 00:00:00 2001 From: narzoul Date: Sun, 22 May 2016 11:09:44 +0200 Subject: [PATCH] Moved display mode operations to CompatDisplayMode --- DDrawCompat/CompatActivateAppHandler.cpp | 13 +-- DDrawCompat/CompatDirectDraw.cpp | 91 ++---------------- DDrawCompat/CompatDirectDrawSurface.cpp | 21 ++-- DDrawCompat/CompatDisplayMode.cpp | 117 +++++++++++++++++++++++ DDrawCompat/CompatDisplayMode.h | 25 +++++ DDrawCompat/CompatGdiDcCache.cpp | 8 +- DDrawCompat/CompatGdiTitleBar.cpp | 2 +- DDrawCompat/CompatPaletteConverter.cpp | 32 ++++--- DDrawCompat/CompatPaletteConverter.h | 2 +- DDrawCompat/CompatPrimarySurface.cpp | 32 +++---- DDrawCompat/CompatPrimarySurface.h | 17 +--- DDrawCompat/DDrawCompat.vcxproj | 2 + DDrawCompat/DDrawCompat.vcxproj.filters | 6 ++ DDrawCompat/RealPrimarySurface.cpp | 18 ++-- 14 files changed, 213 insertions(+), 173 deletions(-) create mode 100644 DDrawCompat/CompatDisplayMode.cpp create mode 100644 DDrawCompat/CompatDisplayMode.h diff --git a/DDrawCompat/CompatActivateAppHandler.cpp b/DDrawCompat/CompatActivateAppHandler.cpp index 9b77c6b..371a4a7 100644 --- a/DDrawCompat/CompatActivateAppHandler.cpp +++ b/DDrawCompat/CompatActivateAppHandler.cpp @@ -1,6 +1,7 @@ #include "CompatActivateAppHandler.h" #include "CompatDirectDraw.h" #include "CompatDirectDrawSurface.h" +#include "CompatDisplayMode.h" #include "CompatGdi.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" @@ -32,11 +33,8 @@ namespace } dd->SetCooperativeLevel(&dd, g_fullScreenCooperativeWindow, g_fullScreenCooperativeFlags); - if (CompatPrimarySurface::isDisplayModeChanged) - { - const CompatPrimarySurface::DisplayMode& dm = CompatPrimarySurface::displayMode; - dd->SetDisplayMode(&dd, dm.width, dm.height, 32, dm.refreshRate, 0); - } + auto dm = CompatDisplayMode::getDisplayMode(dd); + dd->SetDisplayMode(&dd, dm.width, dm.height, 32, dm.refreshRate, dm.flags); auto primary(CompatPrimarySurface::getPrimary()); if (primary && SUCCEEDED(primary->Restore(primary))) @@ -48,10 +46,7 @@ namespace void deactivateApp(CompatRef dd) { - if (CompatPrimarySurface::isDisplayModeChanged) - { - dd->RestoreDisplayMode(&dd); - } + dd->RestoreDisplayMode(&dd); dd->SetCooperativeLevel(&dd, g_fullScreenCooperativeWindow, DDSCL_NORMAL); if (!(g_fullScreenCooperativeFlags & DDSCL_NOWINDOWCHANGES)) diff --git a/DDrawCompat/CompatDirectDraw.cpp b/DDrawCompat/CompatDirectDraw.cpp index 01f49c3..bf8c82a 100644 --- a/DDrawCompat/CompatDirectDraw.cpp +++ b/DDrawCompat/CompatDirectDraw.cpp @@ -1,7 +1,7 @@ #include "CompatActivateAppHandler.h" #include "CompatDirectDraw.h" #include "CompatDirectDrawSurface.h" -#include "CompatPrimarySurface.h" +#include "CompatDisplayMode.h" #include "CompatPtr.h" #include "CompatRef.h" #include "IReleaseNotifier.h" @@ -47,18 +47,6 @@ namespace return tagSurface; } - template - HRESULT PASCAL enumDisplayModesCallback( - TSurfaceDesc* lpDDSurfaceDesc, - LPVOID lpContext) - { - if (lpDDSurfaceDesc) - { - *static_cast(lpContext) = lpDDSurfaceDesc->ddpfPixelFormat; - } - return DDENUMRET_CANCEL; - } - bool isFullScreenDirectDraw(void* dd) { return dd && g_fullScreenDirectDraw && @@ -72,64 +60,6 @@ namespace g_fullScreenTagSurface = nullptr; } - HRESULT setDisplayMode(CompatRef dd, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP, - DWORD dwRefreshRate, DWORD dwFlags) - { - DDSURFACEDESC2 desc = {}; - desc.dwSize = sizeof(desc); - desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT; - desc.dwWidth = dwWidth; - desc.dwHeight = dwHeight; - desc.ddpfPixelFormat.dwSize = sizeof(desc.ddpfPixelFormat); - desc.ddpfPixelFormat.dwFlags = DDPF_RGB; - desc.ddpfPixelFormat.dwRGBBitCount = dwBPP; - - switch (dwBPP) - { - case 1: desc.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED1; break; - case 2: desc.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED2; break; - case 4: desc.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED4; break; - case 8: desc.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED8; break; - } - - DDPIXELFORMAT pf = {}; - if (dwBPP > 8) - { - if (FAILED(dd->EnumDisplayModes(&dd, 0, &desc, &pf, &enumDisplayModesCallback)) || - 0 == pf.dwSize) - { - Compat::Log() << "Failed to find the requested display mode: " << - dwWidth << "x" << dwHeight << "x" << dwBPP; - return DDERR_INVALIDMODE; - } - } - else - { - pf = desc.ddpfPixelFormat; - } - - HRESULT result = dd->SetDisplayMode(&dd, dwWidth, dwHeight, 32, dwRefreshRate, dwFlags); - if (SUCCEEDED(result)) - { - CompatPrimarySurface::displayMode.width = dwWidth; - CompatPrimarySurface::displayMode.height = dwHeight; - CompatPrimarySurface::displayMode.pixelFormat = pf; - CompatPrimarySurface::displayMode.refreshRate = dwRefreshRate; - CompatPrimarySurface::isDisplayModeChanged = true; - } - else - { - Compat::Log() << "Failed to set the display mode to " << dwWidth << "x" << dwHeight << "x32"; - } - - return result; - } - - HRESULT setDisplayMode(CompatRef dd, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP) - { - return setDisplayMode(dd, dwWidth, dwHeight, dwBPP, 0, 0); - } - void setFullScreenDirectDraw(CompatRef dd) { g_fullScreenTagSurface.release(); @@ -186,16 +116,19 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::CreateSurface( } else { - if (CompatPrimarySurface::displayMode.pixelFormat.dwSize != 0 && + if (lpDDSurfaceDesc && !(lpDDSurfaceDesc->dwFlags & DDSD_PIXELFORMAT) && (lpDDSurfaceDesc->dwFlags & DDSD_WIDTH) && (lpDDSurfaceDesc->dwFlags & DDSD_HEIGHT) && !((lpDDSurfaceDesc->dwFlags & DDSD_CAPS) && (lpDDSurfaceDesc->ddsCaps.dwCaps & (DDSCAPS_ALPHA | DDSCAPS_ZBUFFER)))) { + CompatPtr dd(Compat::queryInterface(This)); + auto dm = CompatDisplayMode::getDisplayMode(*dd); + TSurfaceDesc desc = *lpDDSurfaceDesc; desc.dwFlags |= DDSD_PIXELFORMAT; - desc.ddpfPixelFormat = CompatPrimarySurface::displayMode.pixelFormat; + desc.ddpfPixelFormat = dm.pixelFormat; result = s_origVtable.CreateSurface(This, &desc, lplpDDSurface, pUnkOuter); } else @@ -215,14 +148,8 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::CreateSurface( template HRESULT STDMETHODCALLTYPE CompatDirectDraw::RestoreDisplayMode(TDirectDraw* This) { - HRESULT result = s_origVtable.RestoreDisplayMode(This); - if (SUCCEEDED(result)) - { - CompatPtr dd(Compat::queryInterface(This)); - CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(*dd); - CompatPrimarySurface::isDisplayModeChanged = false; - } - return result; + CompatPtr dd(Compat::queryInterface(This)); + return CompatDisplayMode::restoreDisplayMode(*dd); } template @@ -262,7 +189,7 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::SetDisplayMode( Params... params) { CompatPtr dd(Compat::queryInterface(This)); - return setDisplayMode(*dd, dwWidth, dwHeight, dwBPP, params...); + return CompatDisplayMode::setDisplayMode(*dd, dwWidth, dwHeight, dwBPP, params...); } template <> const IID& CompatDirectDraw::s_iid = IID_IDirectDraw; diff --git a/DDrawCompat/CompatDirectDrawSurface.cpp b/DDrawCompat/CompatDirectDrawSurface.cpp index f358612..1da687d 100644 --- a/DDrawCompat/CompatDirectDrawSurface.cpp +++ b/DDrawCompat/CompatDirectDrawSurface.cpp @@ -3,6 +3,7 @@ #include "CompatDirectDraw.h" #include "CompatDirectDrawPalette.h" #include "CompatDirectDrawSurface.h" +#include "CompatDisplayMode.h" #include "CompatGdi.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" @@ -178,28 +179,20 @@ HRESULT CompatDirectDrawSurface::createCompatPrimarySurface( TSurfaceDesc compatDesc, TSurface*& compatSurface) { - if (0 == CompatPrimarySurface::displayMode.pixelFormat.dwSize) - { - CompatPtr dd7(Compat::queryInterface(&dd)); - CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(*dd7); - } - HRESULT result = RealPrimarySurface::create(dd); if (FAILED(result)) { return result; } - CompatPrimarySurface::width = CompatPrimarySurface::displayMode.width; - CompatPrimarySurface::height = CompatPrimarySurface::displayMode.height; - CompatPrimarySurface::pixelFormat = CompatPrimarySurface::displayMode.pixelFormat; - + CompatPtr dd7(Compat::queryInterface(&dd)); + const auto& dm = CompatDisplayMode::getDisplayMode(*dd7); compatDesc.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT; - compatDesc.dwWidth = CompatPrimarySurface::width; - compatDesc.dwHeight = CompatPrimarySurface::height; - compatDesc.ddsCaps.dwCaps ^= DDSCAPS_PRIMARYSURFACE; + compatDesc.dwWidth = dm.width; + compatDesc.dwHeight = dm.height; + compatDesc.ddsCaps.dwCaps &= ~DDSCAPS_PRIMARYSURFACE; compatDesc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN; - compatDesc.ddpfPixelFormat = CompatPrimarySurface::pixelFormat; + compatDesc.ddpfPixelFormat = dm.pixelFormat; result = dd->CreateSurface(&dd, &compatDesc, &compatSurface, nullptr); if (FAILED(result)) diff --git a/DDrawCompat/CompatDisplayMode.cpp b/DDrawCompat/CompatDisplayMode.cpp new file mode 100644 index 0000000..4535369 --- /dev/null +++ b/DDrawCompat/CompatDisplayMode.cpp @@ -0,0 +1,117 @@ +#include "CompatDisplayMode.h" + +namespace +{ + CompatDisplayMode::DisplayMode g_emulatedDisplayMode = {}; + + template + HRESULT PASCAL enumDisplayModesCallback( + TSurfaceDesc* lpDDSurfaceDesc, + LPVOID lpContext) + { + if (lpDDSurfaceDesc) + { + *static_cast(lpContext) = lpDDSurfaceDesc->ddpfPixelFormat; + } + return DDENUMRET_CANCEL; + } + + DDPIXELFORMAT getDisplayModePixelFormat( + CompatRef dd, DWORD width, DWORD height, DWORD bpp) + { + DDSURFACEDESC2 desc = {}; + desc.ddpfPixelFormat.dwSize = sizeof(desc.ddpfPixelFormat); + desc.ddpfPixelFormat.dwFlags = DDPF_RGB; + desc.ddpfPixelFormat.dwRGBBitCount = bpp; + + if (bpp <= 8) + { + switch (bpp) + { + case 1: desc.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED1; break; + case 2: desc.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED2; break; + case 4: desc.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED4; break; + case 8: desc.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED8; break; + default: return DDPIXELFORMAT(); + } + return desc.ddpfPixelFormat; + } + + desc.dwSize = sizeof(desc); + desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT; + desc.dwWidth = width; + desc.dwHeight = height; + + DDPIXELFORMAT pf = {}; + if (FAILED(dd->EnumDisplayModes(&dd, 0, &desc, &pf, &enumDisplayModesCallback)) || + 0 == pf.dwSize) + { + Compat::Log() << "Failed to find the requested display mode: " << + width << "x" << height << "x" << bpp; + } + + return pf; + } +} + +namespace CompatDisplayMode +{ + DisplayMode getDisplayMode(CompatRef dd) + { + if (0 == g_emulatedDisplayMode.width) + { + 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; + } + + HRESULT restoreDisplayMode(CompatRef dd) + { + const HRESULT result = dd->RestoreDisplayMode(&dd); + if (SUCCEEDED(result)) + { + ZeroMemory(&g_emulatedDisplayMode, sizeof(g_emulatedDisplayMode)); + } + return result; + } + + HRESULT setDisplayMode(CompatRef dd, + DWORD width, DWORD height, DWORD bpp, DWORD refreshRate, DWORD flags) + { + DDPIXELFORMAT pf = getDisplayModePixelFormat(dd, width, height, bpp); + if (0 == pf.dwSize) + { + return DDERR_INVALIDMODE; + } + + const HRESULT result = dd->SetDisplayMode(&dd, width, height, 32, refreshRate, flags); + if (FAILED(result)) + { + Compat::Log() << "Failed to set the display mode to " << width << "x" << height << + "x" << bpp << " (" << std::hex << result << std::dec << ')'; + return result; + } + + g_emulatedDisplayMode.width = width; + g_emulatedDisplayMode.height = height; + g_emulatedDisplayMode.pixelFormat = pf; + g_emulatedDisplayMode.refreshRate = refreshRate; + g_emulatedDisplayMode.flags = flags; + + return DD_OK; + } +} diff --git a/DDrawCompat/CompatDisplayMode.h b/DDrawCompat/CompatDisplayMode.h new file mode 100644 index 0000000..65e0d68 --- /dev/null +++ b/DDrawCompat/CompatDisplayMode.h @@ -0,0 +1,25 @@ +#pragma once + +#define CINTERFACE + +#include + +#include "CompatRef.h" + +namespace CompatDisplayMode +{ + struct DisplayMode + { + DWORD width; + DWORD height; + DDPIXELFORMAT pixelFormat; + DWORD refreshRate; + DWORD flags; + }; + + DisplayMode getDisplayMode(CompatRef dd); + DisplayMode getRealDisplayMode(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/CompatGdiDcCache.cpp b/DDrawCompat/CompatGdiDcCache.cpp index 7b3d302..e94164d 100644 --- a/DDrawCompat/CompatGdiDcCache.cpp +++ b/DDrawCompat/CompatGdiDcCache.cpp @@ -55,12 +55,8 @@ namespace CompatPtr createGdiSurface() { - DDSURFACEDESC2 desc = {}; - desc.dwSize = sizeof(desc); + DDSURFACEDESC2 desc = CompatPrimarySurface::getDesc(); desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS | DDSD_PITCH | DDSD_LPSURFACE; - desc.dwWidth = CompatPrimarySurface::width; - desc.dwHeight = CompatPrimarySurface::height; - desc.ddpfPixelFormat = CompatPrimarySurface::pixelFormat; desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; desc.lPitch = g_pitch; desc.lpSurface = g_surfaceMemory; @@ -74,7 +70,7 @@ namespace return nullptr; } - if (CompatPrimarySurface::pixelFormat.dwRGBBitCount <= 8) + if (desc.ddpfPixelFormat.dwRGBBitCount <= 8) { surface->SetPalette(surface, g_palette); } diff --git a/DDrawCompat/CompatGdiTitleBar.cpp b/DDrawCompat/CompatGdiTitleBar.cpp index ab854c5..df25873 100644 --- a/DDrawCompat/CompatGdiTitleBar.cpp +++ b/DDrawCompat/CompatGdiTitleBar.cpp @@ -96,7 +96,7 @@ namespace CompatGdi { flags |= DC_ACTIVE; } - if (CompatPrimarySurface::pixelFormat.dwRGBBitCount > 8) + if (CompatPrimarySurface::getDesc().ddpfPixelFormat.dwRGBBitCount > 8) { flags |= DC_GRADIENT; } diff --git a/DDrawCompat/CompatPaletteConverter.cpp b/DDrawCompat/CompatPaletteConverter.cpp index 0f73331..22fd6d6 100644 --- a/DDrawCompat/CompatPaletteConverter.cpp +++ b/DDrawCompat/CompatPaletteConverter.cpp @@ -1,6 +1,7 @@ #include #include +#include "CompatDisplayMode.h" #include "CompatPaletteConverter.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" @@ -25,7 +26,7 @@ namespace } } - HBITMAP createDibSection(const DDSURFACEDESC2& primaryDesc, void*& bits) + HBITMAP createDibSection(const CompatDisplayMode::DisplayMode& dm, void*& bits) { struct PalettizedBitmapInfo { @@ -35,28 +36,27 @@ namespace PalettizedBitmapInfo bmi = {}; bmi.header.biSize = sizeof(bmi.header); - bmi.header.biWidth = primaryDesc.dwWidth; - bmi.header.biHeight = -static_cast(primaryDesc.dwHeight); + bmi.header.biWidth = dm.width; + bmi.header.biHeight = -static_cast(dm.height); bmi.header.biPlanes = 1; - bmi.header.biBitCount = 8; + bmi.header.biBitCount = static_cast(dm.pixelFormat.dwRGBBitCount); bmi.header.biCompression = BI_RGB; - bmi.header.biClrUsed = 256; return CreateDIBSection(nullptr, reinterpret_cast(&bmi), DIB_RGB_COLORS, &bits, nullptr, 0); } - CompatPtr createSurface(const DDSURFACEDESC2& primaryDesc, void* bits) + CompatPtr createSurface(const CompatDisplayMode::DisplayMode& 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 = primaryDesc.dwWidth; - desc.dwHeight = primaryDesc.dwHeight; - desc.ddpfPixelFormat = CompatPrimarySurface::displayMode.pixelFormat; + desc.dwWidth = dm.width; + desc.dwHeight = dm.height; + desc.ddpfPixelFormat = dm.pixelFormat; desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; - desc.lPitch = (primaryDesc.dwWidth + 3) & ~3; + desc.lPitch = (dm.width * dm.pixelFormat.dwRGBBitCount / 8 + 3) & ~3; desc.lpSurface = bits; auto dd(DDrawRepository::getDirectDraw()); @@ -68,23 +68,25 @@ namespace namespace CompatPaletteConverter { - bool create(const DDSURFACEDESC2& primaryDesc) + bool create() { - if (CompatPrimarySurface::displayMode.pixelFormat.dwRGBBitCount > 8 && - primaryDesc.ddpfPixelFormat.dwRGBBitCount > 8) + auto dd(DDrawRepository::getDirectDraw()); + auto dm(CompatDisplayMode::getDisplayMode(*dd)); + if (dm.pixelFormat.dwRGBBitCount > 8 && + CompatDisplayMode::getRealDisplayMode(*dd).pixelFormat.dwRGBBitCount > 8) { return true; } void* bits = nullptr; - HBITMAP dib = createDibSection(primaryDesc, bits); + HBITMAP dib = createDibSection(dm, bits); if (!dib) { Compat::Log() << "Failed to create the palette converter DIB section"; return false; } - CompatPtr surface(createSurface(primaryDesc, bits)); + CompatPtr surface(createSurface(dm, bits)); if (!surface) { Compat::Log() << "Failed to create the palette converter surface"; diff --git a/DDrawCompat/CompatPaletteConverter.h b/DDrawCompat/CompatPaletteConverter.h index c5f2290..0d867a0 100644 --- a/DDrawCompat/CompatPaletteConverter.h +++ b/DDrawCompat/CompatPaletteConverter.h @@ -8,7 +8,7 @@ namespace CompatPaletteConverter { - bool create(const DDSURFACEDESC2& primaryDesc); + bool create(); HDC getDc(); CompatWeakPtr getSurface(); void release(); diff --git a/DDrawCompat/CompatPrimarySurface.cpp b/DDrawCompat/CompatPrimarySurface.cpp index d293b2e..1d3b535 100644 --- a/DDrawCompat/CompatPrimarySurface.cpp +++ b/DDrawCompat/CompatPrimarySurface.cpp @@ -9,8 +9,12 @@ namespace { + void onRelease(); + + DDSURFACEDESC2 g_primarySurfaceDesc = {}; CompatWeakPtr g_primarySurface = nullptr; std::vector g_primarySurfacePtrs; + IReleaseNotifier g_releaseNotifier(onRelease); void onRelease() { @@ -19,10 +23,8 @@ namespace g_primarySurfacePtrs.clear(); g_primarySurface = nullptr; CompatPrimarySurface::palette = nullptr; - CompatPrimarySurface::width = 0; - CompatPrimarySurface::height = 0; ZeroMemory(&CompatPrimarySurface::paletteEntries, sizeof(CompatPrimarySurface::paletteEntries)); - ZeroMemory(&CompatPrimarySurface::pixelFormat, sizeof(CompatPrimarySurface::pixelFormat)); + ZeroMemory(&g_primarySurfaceDesc, sizeof(g_primarySurfaceDesc)); RealPrimarySurface::release(); @@ -32,17 +34,9 @@ namespace namespace CompatPrimarySurface { - DisplayMode getDisplayMode(CompatRef dd) + const DDSURFACEDESC2& getDesc() { - DisplayMode dm = {}; - DDSURFACEDESC2 desc = {}; - desc.dwSize = sizeof(desc); - dd->GetDisplayMode(&dd, &desc); - dm.width = desc.dwWidth; - dm.height = desc.dwHeight; - dm.pixelFormat = desc.ddpfPixelFormat; - dm.refreshRate = desc.dwRefreshRate; - return dm; + return g_primarySurfaceDesc; } CompatPtr getPrimary() @@ -66,6 +60,10 @@ namespace CompatPrimarySurface CompatPtr surfacePtr(Compat::queryInterface(&surface)); g_primarySurface = surfacePtr; + ZeroMemory(&g_primarySurfaceDesc, sizeof(g_primarySurfaceDesc)); + g_primarySurfaceDesc.dwSize = sizeof(g_primarySurfaceDesc); + surface->GetSurfaceDesc(&surface, &g_primarySurfaceDesc); + g_primarySurfacePtrs.clear(); g_primarySurfacePtrs.push_back(&surface); g_primarySurfacePtrs.push_back(CompatPtr(surfacePtr)); @@ -73,17 +71,11 @@ namespace CompatPrimarySurface g_primarySurfacePtrs.push_back(CompatPtr(surfacePtr)); g_primarySurfacePtrs.push_back(surfacePtr); - IReleaseNotifier* releaseNotifierPtr = &releaseNotifier; + IReleaseNotifier* releaseNotifierPtr = &g_releaseNotifier; surface->SetPrivateData(&surface, IID_IReleaseNotifier, releaseNotifierPtr, sizeof(releaseNotifierPtr), DDSPD_IUNKNOWNPOINTER); } - DisplayMode displayMode = {}; - bool isDisplayModeChanged = false; CompatWeakPtr palette; PALETTEENTRY paletteEntries[256] = {}; - LONG width = 0; - LONG height = 0; - DDPIXELFORMAT pixelFormat = {}; - IReleaseNotifier releaseNotifier(onRelease); } diff --git a/DDrawCompat/CompatPrimarySurface.h b/DDrawCompat/CompatPrimarySurface.h index b388a07..fc47b2f 100644 --- a/DDrawCompat/CompatPrimarySurface.h +++ b/DDrawCompat/CompatPrimarySurface.h @@ -11,26 +11,11 @@ class IReleaseNotifier; namespace CompatPrimarySurface { - struct DisplayMode - { - LONG width; - LONG height; - DDPIXELFORMAT pixelFormat; - DWORD refreshRate; - }; - - DisplayMode getDisplayMode(CompatRef dd); - + const DDSURFACEDESC2& getDesc(); CompatPtr getPrimary(); bool isPrimary(void* surface); void setPrimary(CompatRef surface); - extern DisplayMode displayMode; - extern bool isDisplayModeChanged; extern CompatWeakPtr palette; extern PALETTEENTRY paletteEntries[256]; - extern LONG width; - extern LONG height; - extern DDPIXELFORMAT pixelFormat; - extern IReleaseNotifier releaseNotifier; } diff --git a/DDrawCompat/DDrawCompat.vcxproj b/DDrawCompat/DDrawCompat.vcxproj index 16b4498..e46fe32 100644 --- a/DDrawCompat/DDrawCompat.vcxproj +++ b/DDrawCompat/DDrawCompat.vcxproj @@ -146,6 +146,7 @@ + @@ -187,6 +188,7 @@ + diff --git a/DDrawCompat/DDrawCompat.vcxproj.filters b/DDrawCompat/DDrawCompat.vcxproj.filters index 9b2adf7..523595d 100644 --- a/DDrawCompat/DDrawCompat.vcxproj.filters +++ b/DDrawCompat/DDrawCompat.vcxproj.filters @@ -126,6 +126,9 @@ Header Files + + Header Files + @@ -206,6 +209,9 @@ Source Files + + Source Files + diff --git a/DDrawCompat/RealPrimarySurface.cpp b/DDrawCompat/RealPrimarySurface.cpp index 2ea209f..7f123fa 100644 --- a/DDrawCompat/RealPrimarySurface.cpp +++ b/DDrawCompat/RealPrimarySurface.cpp @@ -48,7 +48,7 @@ namespace bool result = false; auto primary(CompatPrimarySurface::getPrimary()); - if (CompatPrimarySurface::pixelFormat.dwRGBBitCount <= 8) + if (CompatPrimarySurface::getDesc().ddpfPixelFormat.dwRGBBitCount <= 8) { auto paletteConverter(CompatPaletteConverter::getSurface()); paletteConverter->Blt(paletteConverter, &g_updateRect, @@ -92,17 +92,17 @@ namespace HRESULT init(CompatPtr surface) { - DDSURFACEDESC2 desc = {}; - desc.dwSize = sizeof(desc); - surface->GetSurfaceDesc(surface, &desc); - - if (!CompatPaletteConverter::create(desc)) + if (!CompatPaletteConverter::create()) { return DDERR_GENERIC; } - CompatPtr backBuffer; + DDSURFACEDESC2 desc = {}; + desc.dwSize = sizeof(desc); + surface->GetSurfaceDesc(surface, &desc); + const bool isFlippable = 0 != (desc.ddsCaps.dwCaps & DDSCAPS_FLIP); + CompatPtr backBuffer; if (isFlippable) { DDSCAPS2 backBufferCaps = {}; @@ -294,8 +294,8 @@ void RealPrimarySurface::invalidate(const RECT* rect) } else { - SetRect(&g_updateRect, 0, 0, - CompatPrimarySurface::displayMode.width, CompatPrimarySurface::displayMode.height); + auto primaryDesc = CompatPrimarySurface::getDesc(); + SetRect(&g_updateRect, 0, 0, primaryDesc.dwWidth, primaryDesc.dwHeight); } }