From e8f4a970e0a325d65b9417ab1e6f01e08fa7d2da Mon Sep 17 00:00:00 2001 From: narzoul Date: Mon, 16 May 2016 22:01:52 +0200 Subject: [PATCH] Replaced IDirectDraw raw pointers with smart pointers --- DDrawCompat/CompatActivateAppHandler.cpp | 27 +++++--------- DDrawCompat/CompatActivateAppHandler.h | 6 ++- DDrawCompat/CompatDirectDraw.cpp | 47 ++++++++++++------------ DDrawCompat/CompatDirectDraw.h | 40 ++++++++++++++++++++ DDrawCompat/CompatDirectDrawSurface.cpp | 16 ++++---- DDrawCompat/CompatDirectDrawSurface.h | 2 +- DDrawCompat/CompatGdiDcCache.cpp | 16 +++----- DDrawCompat/CompatPaletteConverter.cpp | 10 +---- DDrawCompat/CompatPrimarySurface.cpp | 13 ++----- DDrawCompat/CompatPrimarySurface.h | 3 +- DDrawCompat/DDrawRepository.cpp | 27 +++++--------- DDrawCompat/DDrawRepository.h | 2 +- DDrawCompat/DllMain.cpp | 26 ++++++------- DDrawCompat/RealPrimarySurface.cpp | 18 ++++----- DDrawCompat/RealPrimarySurface.h | 3 +- 15 files changed, 130 insertions(+), 126 deletions(-) diff --git a/DDrawCompat/CompatActivateAppHandler.cpp b/DDrawCompat/CompatActivateAppHandler.cpp index 0bf9377..9b77c6b 100644 --- a/DDrawCompat/CompatActivateAppHandler.cpp +++ b/DDrawCompat/CompatActivateAppHandler.cpp @@ -4,6 +4,7 @@ #include "CompatGdi.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" +#include "CompatRef.h" #include "DDrawLog.h" extern HWND g_mainWindow; @@ -11,14 +12,14 @@ extern HWND g_mainWindow; namespace { bool g_isActive = true; - IUnknown* g_fullScreenDirectDraw = nullptr; + CompatWeakPtr g_fullScreenDirectDraw = nullptr; HWND g_fullScreenCooperativeWindow = nullptr; DWORD g_fullScreenCooperativeFlags = 0; HHOOK g_callWndProcHook = nullptr; void handleActivateApp(bool isActivated); - void activateApp(IDirectDraw7& dd) + void activateApp(CompatRef dd) { if (!(g_fullScreenCooperativeFlags & DDSCL_NOWINDOWCHANGES)) { @@ -30,13 +31,11 @@ namespace } } - CompatDirectDraw::s_origVtable.SetCooperativeLevel( - &dd, g_fullScreenCooperativeWindow, g_fullScreenCooperativeFlags); + dd->SetCooperativeLevel(&dd, g_fullScreenCooperativeWindow, g_fullScreenCooperativeFlags); if (CompatPrimarySurface::isDisplayModeChanged) { const CompatPrimarySurface::DisplayMode& dm = CompatPrimarySurface::displayMode; - CompatDirectDraw::s_origVtable.SetDisplayMode( - &dd, dm.width, dm.height, 32, dm.refreshRate, 0); + dd->SetDisplayMode(&dd, dm.width, dm.height, 32, dm.refreshRate, 0); } auto primary(CompatPrimarySurface::getPrimary()); @@ -47,14 +46,13 @@ namespace } } - void deactivateApp(IDirectDraw7& dd) + void deactivateApp(CompatRef dd) { if (CompatPrimarySurface::isDisplayModeChanged) { - CompatDirectDraw::s_origVtable.RestoreDisplayMode(&dd); + dd->RestoreDisplayMode(&dd); } - CompatDirectDraw::s_origVtable.SetCooperativeLevel( - &dd, g_fullScreenCooperativeWindow, DDSCL_NORMAL); + dd->SetCooperativeLevel(&dd, g_fullScreenCooperativeWindow, DDSCL_NORMAL); if (!(g_fullScreenCooperativeFlags & DDSCL_NOWINDOWCHANGES)) { @@ -95,10 +93,7 @@ namespace if (g_fullScreenDirectDraw) { - IDirectDraw7* dd = nullptr; - g_fullScreenDirectDraw->lpVtbl->QueryInterface( - g_fullScreenDirectDraw, IID_IDirectDraw7, reinterpret_cast(&dd)); - + CompatPtr dd(Compat::queryInterface(g_fullScreenDirectDraw.get())); if (isActivated) { activateApp(*dd); @@ -107,8 +102,6 @@ namespace { deactivateApp(*dd); } - - CompatDirectDraw::s_origVtable.Release(dd); } if (isActivated) @@ -133,7 +126,7 @@ namespace CompatActivateAppHandler return g_isActive; } - void setFullScreenCooperativeLevel(IUnknown* dd, HWND hwnd, DWORD flags) + void setFullScreenCooperativeLevel(CompatWeakPtr dd, HWND hwnd, DWORD flags) { g_fullScreenDirectDraw = dd; g_fullScreenCooperativeWindow = hwnd; diff --git a/DDrawCompat/CompatActivateAppHandler.h b/DDrawCompat/CompatActivateAppHandler.h index 94c857a..370e366 100644 --- a/DDrawCompat/CompatActivateAppHandler.h +++ b/DDrawCompat/CompatActivateAppHandler.h @@ -1,15 +1,17 @@ #pragma once +#define CINTERFACE #define WIN32_LEAN_AND_MEAN #include +#include -struct IUnknown; +#include "CompatWeakPtr.h" namespace CompatActivateAppHandler { void installHooks(); bool isActive(); - void setFullScreenCooperativeLevel(IUnknown* dd, HWND hwnd, DWORD flags); + void setFullScreenCooperativeLevel(CompatWeakPtr dd, HWND hwnd, DWORD flags); void uninstallHooks(); } diff --git a/DDrawCompat/CompatDirectDraw.cpp b/DDrawCompat/CompatDirectDraw.cpp index 8d9aae6..01f49c3 100644 --- a/DDrawCompat/CompatDirectDraw.cpp +++ b/DDrawCompat/CompatDirectDraw.cpp @@ -3,6 +3,7 @@ #include "CompatDirectDrawSurface.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" +#include "CompatRef.h" #include "IReleaseNotifier.h" namespace @@ -24,7 +25,7 @@ namespace IReleaseNotifier g_fullScreenTagSurfaceReleaseNotifier(&onReleaseFullScreenTagSurface); - CompatPtr createFullScreenTagSurface(IDirectDraw& dd) + CompatPtr createFullScreenTagSurface(CompatRef dd) { DDSURFACEDESC desc = {}; desc.dwSize = sizeof(desc); @@ -34,7 +35,7 @@ namespace desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; CompatPtr tagSurface; - CompatDirectDraw::s_origVtable.CreateSurface(&dd, &desc, &tagSurface.getRef(), nullptr); + dd->CreateSurface(&dd, &desc, &tagSurface.getRef(), nullptr); if (tagSurface) { CompatPtr tagSurface7(tagSurface); @@ -71,11 +72,10 @@ namespace g_fullScreenTagSurface = nullptr; } - template - HRESULT setDisplayMode(TDirectDraw* This, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP, + HRESULT setDisplayMode(CompatRef dd, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP, DWORD dwRefreshRate, DWORD dwFlags) { - typename Types::TSurfaceDesc desc = {}; + DDSURFACEDESC2 desc = {}; desc.dwSize = sizeof(desc); desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT; desc.dwWidth = dwWidth; @@ -95,8 +95,8 @@ namespace DDPIXELFORMAT pf = {}; if (dwBPP > 8) { - if (FAILED(CompatDirectDraw::s_origVtable.EnumDisplayModes( - This, 0, &desc, &pf, &enumDisplayModesCallback)) || 0 == pf.dwSize) + 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; @@ -108,8 +108,7 @@ namespace pf = desc.ddpfPixelFormat; } - HRESULT result = CompatDirectDraw::s_origVtable.SetDisplayMode( - This, dwWidth, dwHeight, 32, dwRefreshRate, dwFlags); + HRESULT result = dd->SetDisplayMode(&dd, dwWidth, dwHeight, 32, dwRefreshRate, dwFlags); if (SUCCEEDED(result)) { CompatPrimarySurface::displayMode.width = dwWidth; @@ -126,17 +125,12 @@ namespace return result; } - HRESULT setDisplayMode(IDirectDraw* This, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP) + HRESULT setDisplayMode(CompatRef dd, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP) { - IDirectDraw7* dd = nullptr; - CompatDirectDraw::s_origVtable.QueryInterface( - This, IID_IDirectDraw7, reinterpret_cast(&dd)); - HRESULT result = setDisplayMode(dd, dwWidth, dwHeight, dwBPP, 0, 0); - CompatDirectDraw::s_origVtable.Release(dd); - return result; + return setDisplayMode(dd, dwWidth, dwHeight, dwBPP, 0, 0); } - void setFullScreenDirectDraw(IDirectDraw& dd) + void setFullScreenDirectDraw(CompatRef dd) { g_fullScreenTagSurface.release(); g_fullScreenTagSurface = createFullScreenTagSurface(dd).detach(); @@ -156,7 +150,7 @@ namespace static DirectDrawInterface fullScreenDirectDraw = {}; ZeroMemory(&fullScreenDirectDraw, sizeof(fullScreenDirectDraw)); - DirectDrawInterface& ddIntf = reinterpret_cast(dd); + DirectDrawInterface& ddIntf = reinterpret_cast(dd.get()); fullScreenDirectDraw.vtable = ddIntf.vtable; fullScreenDirectDraw.ddObject = ddIntf.ddObject; g_fullScreenDirectDraw = &fullScreenDirectDraw; @@ -187,7 +181,7 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::CreateSurface( if (isPrimary) { - result = CompatDirectDrawSurface::createCompatPrimarySurface( + result = CompatDirectDrawSurface::createCompatPrimarySurface( *This, *lpDDSurfaceDesc, *lplpDDSurface); } else @@ -224,7 +218,8 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::RestoreDisplayMode(TDir HRESULT result = s_origVtable.RestoreDisplayMode(This); if (SUCCEEDED(result)) { - CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(*This); + CompatPtr dd(Compat::queryInterface(This)); + CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(*dd); CompatPrimarySurface::isDisplayModeChanged = false; } return result; @@ -244,12 +239,10 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::SetCooperativeLevel( { if (dwFlags & DDSCL_FULLSCREEN) { - IDirectDraw* dd = nullptr; - s_origVtable.QueryInterface(This, IID_IDirectDraw, reinterpret_cast(&dd)); + CompatPtr dd(Compat::queryInterface(This)); setFullScreenDirectDraw(*dd); CompatActivateAppHandler::setFullScreenCooperativeLevel( reinterpret_cast(g_fullScreenDirectDraw), hWnd, dwFlags); - CompatDirectDraw::s_origVtable.Release(dd); } else if (isFullScreenDirectDraw(This) && g_fullScreenTagSurface) { @@ -268,9 +261,15 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw::SetDisplayMode( DWORD dwBPP, Params... params) { - return setDisplayMode(This, dwWidth, dwHeight, dwBPP, params...); + CompatPtr dd(Compat::queryInterface(This)); + return setDisplayMode(*dd, dwWidth, dwHeight, dwBPP, params...); } +template <> const IID& CompatDirectDraw::s_iid = IID_IDirectDraw; +template <> const IID& CompatDirectDraw::s_iid = IID_IDirectDraw2; +template <> const IID& CompatDirectDraw::s_iid = IID_IDirectDraw4; +template <> const IID& CompatDirectDraw::s_iid = IID_IDirectDraw7; + template CompatDirectDraw; template CompatDirectDraw; template CompatDirectDraw; diff --git a/DDrawCompat/CompatDirectDraw.h b/DDrawCompat/CompatDirectDraw.h index 2b44d27..124c3f9 100644 --- a/DDrawCompat/CompatDirectDraw.h +++ b/DDrawCompat/CompatDirectDraw.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "CompatVtable.h" #include "DDrawTypes.h" #include "DirectDrawVtblVisitor.h" @@ -29,4 +31,42 @@ public: DWORD dwHeight, DWORD dwBPP, Params... params); + + static const IID& s_iid; }; + +namespace Compat +{ + template + struct IsDirectDrawIntf : std::false_type {}; + + template<> struct IsDirectDrawIntf : std::true_type {}; + template<> struct IsDirectDrawIntf : std::true_type {}; + template<> struct IsDirectDrawIntf : std::true_type {}; + template<> struct IsDirectDrawIntf : std::true_type {}; + + template + std::enable_if_t::value && IsDirectDrawIntf::value> + queryInterface(OrigIntf& origIntf, NewIntf*& newIntf) + { + CompatDirectDraw::s_origVtable.QueryInterface( + &origIntf, CompatDirectDraw::s_iid, reinterpret_cast(&newIntf)); + } + + template + std::enable_if_t::value> + queryInterface(IUnknown& origIntf, NewIntf*& newIntf) + { + CompatDirectDraw::s_origVtable.QueryInterface( + reinterpret_cast(&origIntf), + CompatDirectDraw::s_iid, reinterpret_cast(&newIntf)); + } + + template + std::enable_if_t::value> + queryInterface(OrigIntf& origIntf, IUnknown*& newIntf) + { + CompatDirectDraw::s_origVtable.QueryInterface( + &origIntf, IID_IUnknown, reinterpret_cast(&newIntf)); + } +} diff --git a/DDrawCompat/CompatDirectDrawSurface.cpp b/DDrawCompat/CompatDirectDrawSurface.cpp index 3513546..f358612 100644 --- a/DDrawCompat/CompatDirectDrawSurface.cpp +++ b/DDrawCompat/CompatDirectDrawSurface.cpp @@ -174,13 +174,14 @@ void CompatDirectDrawSurface::setCompatVtable(Vtable& vtable template template HRESULT CompatDirectDrawSurface::createCompatPrimarySurface( - TDirectDraw& dd, + CompatRef dd, TSurfaceDesc compatDesc, TSurface*& compatSurface) { if (0 == CompatPrimarySurface::displayMode.pixelFormat.dwSize) { - CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(dd); + CompatPtr dd7(Compat::queryInterface(&dd)); + CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(*dd7); } HRESULT result = RealPrimarySurface::create(dd); @@ -200,8 +201,7 @@ HRESULT CompatDirectDrawSurface::createCompatPrimarySurface( compatDesc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN; compatDesc.ddpfPixelFormat = CompatPrimarySurface::pixelFormat; - result = CompatDirectDraw::s_origVtable.CreateSurface( - &dd, &compatDesc, &compatSurface, nullptr); + result = dd->CreateSurface(&dd, &compatDesc, &compatSurface, nullptr); if (FAILED(result)) { Compat::Log() << "Failed to create the compat primary surface!"; @@ -551,18 +551,18 @@ template CompatDirectDrawSurface; template CompatDirectDrawSurface; template HRESULT CompatDirectDrawSurface::createCompatPrimarySurface( - IDirectDraw& dd, + CompatRef dd, TSurfaceDesc compatDesc, IDirectDrawSurface*& compatSurface); template HRESULT CompatDirectDrawSurface::createCompatPrimarySurface( - IDirectDraw2& dd, + CompatRef dd, TSurfaceDesc compatDesc, IDirectDrawSurface*& compatSurface); template HRESULT CompatDirectDrawSurface::createCompatPrimarySurface( - IDirectDraw4& dd, + CompatRef dd, TSurfaceDesc compatDesc, IDirectDrawSurface4*& compatSurface); template HRESULT CompatDirectDrawSurface::createCompatPrimarySurface( - IDirectDraw7& dd, + CompatRef dd, TSurfaceDesc compatDesc, IDirectDrawSurface7*& compatSurface); diff --git a/DDrawCompat/CompatDirectDrawSurface.h b/DDrawCompat/CompatDirectDrawSurface.h index d59ed91..18f4948 100644 --- a/DDrawCompat/CompatDirectDrawSurface.h +++ b/DDrawCompat/CompatDirectDrawSurface.h @@ -19,7 +19,7 @@ public: template static HRESULT createCompatPrimarySurface( - TDirectDraw& dd, + CompatRef dd, TSurfaceDesc compatDesc, TSurface*& compatSurface); diff --git a/DDrawCompat/CompatGdiDcCache.cpp b/DDrawCompat/CompatGdiDcCache.cpp index 9b7a60b..970b416 100644 --- a/DDrawCompat/CompatGdiDcCache.cpp +++ b/DDrawCompat/CompatGdiDcCache.cpp @@ -1,7 +1,6 @@ #include #include -#include "CompatDirectDraw.h" #include "CompatDirectDrawPalette.h" #include "CompatGdiDcCache.h" #include "CompatPrimarySurface.h" @@ -21,7 +20,6 @@ namespace DWORD g_maxUsedCacheSize = 0; DWORD g_ddLockThreadId = 0; - IDirectDraw7* g_directDraw = nullptr; IDirectDrawPalette* g_palette = nullptr; PALETTEENTRY g_paletteEntries[256] = {}; void* g_surfaceMemory = nullptr; @@ -68,9 +66,9 @@ namespace desc.lPitch = g_pitch; desc.lpSurface = g_surfaceMemory; + auto dd(DDrawRepository::getDirectDraw()); CompatPtr surface; - HRESULT result = CompatDirectDraw::s_origVtable.CreateSurface( - g_directDraw, &desc, &surface.getRef(), nullptr); + HRESULT result = dd->CreateSurface(dd, &desc, &surface.getRef(), nullptr); if (FAILED(result)) { LOG_ONCE("Failed to create a GDI surface: " << result); @@ -168,13 +166,9 @@ namespace CompatGdiDcCache bool init() { - g_directDraw = DDrawRepository::getDirectDraw(); - if (g_directDraw) - { - CompatDirectDraw::s_origVtable.CreatePalette( - g_directDraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256, g_paletteEntries, &g_palette, nullptr); - } - return nullptr != g_directDraw; + auto dd(DDrawRepository::getDirectDraw()); + dd->CreatePalette(dd, DDPCAPS_8BIT | DDPCAPS_ALLOW256, g_paletteEntries, &g_palette, nullptr); + return nullptr != g_palette; } void releaseDc(const CachedDc& cachedDc) diff --git a/DDrawCompat/CompatPaletteConverter.cpp b/DDrawCompat/CompatPaletteConverter.cpp index 42b1eaa..0bf503a 100644 --- a/DDrawCompat/CompatPaletteConverter.cpp +++ b/DDrawCompat/CompatPaletteConverter.cpp @@ -1,7 +1,6 @@ #include #include -#include "CompatDirectDraw.h" #include "CompatDirectDrawPalette.h" #include "CompatPaletteConverter.h" #include "CompatPrimarySurface.h" @@ -50,12 +49,6 @@ namespace CompatPtr createSurface(const DDSURFACEDESC2& primaryDesc, void* bits) { - IDirectDraw7* dd = DDrawRepository::getDirectDraw(); - if (!dd) - { - return nullptr; - } - DDSURFACEDESC2 desc = {}; desc.dwSize = sizeof(desc); desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS | @@ -67,8 +60,9 @@ namespace desc.lPitch = (primaryDesc.dwWidth + 3) & ~3; desc.lpSurface = bits; + auto dd(DDrawRepository::getDirectDraw()); CompatPtr surface; - CompatDirectDraw::s_origVtable.CreateSurface(dd, &desc, &surface.getRef(), nullptr); + dd->CreateSurface(dd, &desc, &surface.getRef(), nullptr); return surface; } } diff --git a/DDrawCompat/CompatPrimarySurface.cpp b/DDrawCompat/CompatPrimarySurface.cpp index 582f600..819c999 100644 --- a/DDrawCompat/CompatPrimarySurface.cpp +++ b/DDrawCompat/CompatPrimarySurface.cpp @@ -1,7 +1,6 @@ #include #include -#include "CompatDirectDraw.h" #include "CompatDirectDrawSurface.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" @@ -33,13 +32,12 @@ namespace namespace CompatPrimarySurface { - template - DisplayMode getDisplayMode(TDirectDraw& dd) + DisplayMode getDisplayMode(CompatRef dd) { DisplayMode dm = {}; - typename CompatDirectDraw::TSurfaceDesc desc = {}; + DDSURFACEDESC2 desc = {}; desc.dwSize = sizeof(desc); - CompatDirectDraw::s_origVtable.GetDisplayMode(&dd, &desc); + dd->GetDisplayMode(&dd, &desc); dm.width = desc.dwWidth; dm.height = desc.dwHeight; dm.pixelFormat = desc.ddpfPixelFormat; @@ -47,11 +45,6 @@ namespace CompatPrimarySurface return dm; } - template DisplayMode getDisplayMode(IDirectDraw& dd); - template DisplayMode getDisplayMode(IDirectDraw2& dd); - template DisplayMode getDisplayMode(IDirectDraw4& dd); - template DisplayMode getDisplayMode(IDirectDraw7& dd); - CompatPtr getPrimary() { if (!g_primarySurface) diff --git a/DDrawCompat/CompatPrimarySurface.h b/DDrawCompat/CompatPrimarySurface.h index 4d14314..dc62eab 100644 --- a/DDrawCompat/CompatPrimarySurface.h +++ b/DDrawCompat/CompatPrimarySurface.h @@ -19,8 +19,7 @@ namespace CompatPrimarySurface DWORD refreshRate; }; - template - DisplayMode getDisplayMode(TDirectDraw& dd); + DisplayMode getDisplayMode(CompatRef dd); CompatPtr getPrimary(); bool isPrimary(void* surface); diff --git a/DDrawCompat/DDrawRepository.cpp b/DDrawCompat/DDrawRepository.cpp index 354cda1..a169df9 100644 --- a/DDrawCompat/DDrawRepository.cpp +++ b/DDrawCompat/DDrawRepository.cpp @@ -1,7 +1,6 @@ #include #include -#include "CompatDirectDraw.h" #include "CompatPtr.h" #include "DDrawLog.h" #include "DDrawProcs.h" @@ -14,7 +13,7 @@ namespace static std::vector g_sysMemSurfaces; static std::vector g_vidMemSurfaces; - IDirectDraw7* createDirectDraw(); + CompatPtr createDirectDraw(); Surface createSurface(DWORD width, DWORD height, const DDPIXELFORMAT& pf, DWORD caps); std::vector::iterator findSurface(DWORD width, DWORD height, const DDPIXELFORMAT& pf, std::vector& cachedSurfaces); @@ -24,22 +23,21 @@ namespace void normalizePixelFormat(DDPIXELFORMAT& pf); void returnSurface(const Surface& surface); - IDirectDraw7* createDirectDraw() + CompatPtr createDirectDraw() { - IDirectDraw7* dd = nullptr; + CompatPtr dd; HRESULT result = CALL_ORIG_DDRAW(DirectDrawCreateEx, nullptr, - reinterpret_cast(&dd), IID_IDirectDraw7, nullptr); + reinterpret_cast(&dd.getRef()), IID_IDirectDraw7, nullptr); if (FAILED(result)) { Compat::Log() << "Failed to create a DirectDraw object in the repository: " << result; return nullptr; } - result = dd->lpVtbl->SetCooperativeLevel(dd, nullptr, DDSCL_NORMAL); + result = dd->SetCooperativeLevel(dd, nullptr, DDSCL_NORMAL); if (FAILED(result)) { Compat::Log() << "Failed to set the cooperative level in the repository: " << result; - dd->lpVtbl->Release(dd); return nullptr; } @@ -49,11 +47,6 @@ namespace Surface createSurface(DWORD width, DWORD height, const DDPIXELFORMAT& pf, DWORD caps) { Surface surface = {}; - IDirectDraw7* dd = DDrawRepository::getDirectDraw(); - if (!dd) - { - return surface; - } surface.desc.dwSize = sizeof(surface.desc); surface.desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS; @@ -62,8 +55,8 @@ namespace surface.desc.ddpfPixelFormat = pf; surface.desc.ddsCaps.dwCaps = caps; - CompatDirectDraw::s_origVtable.CreateSurface( - dd, &surface.desc, &surface.surface.getRef(), nullptr); + auto dd(DDrawRepository::getDirectDraw()); + dd->CreateSurface(dd, &surface.desc, &surface.surface.getRef(), nullptr); return surface; } @@ -177,9 +170,9 @@ namespace DDrawRepository returnSurface(*this); } - IDirectDraw7* getDirectDraw() + CompatWeakPtr getDirectDraw() { - static IDirectDraw7* dd = createDirectDraw(); - return dd; + static auto dd = new CompatPtr(createDirectDraw()); + return *dd; } } diff --git a/DDrawCompat/DDrawRepository.h b/DDrawCompat/DDrawRepository.h index 9ffd451..d425c0b 100644 --- a/DDrawCompat/DDrawRepository.h +++ b/DDrawCompat/DDrawRepository.h @@ -21,5 +21,5 @@ namespace DDrawRepository ~ScopedSurface(); }; - IDirectDraw7* getDirectDraw(); + CompatWeakPtr getDirectDraw(); } diff --git a/DDrawCompat/DllMain.cpp b/DDrawCompat/DllMain.cpp index 0fc5cc3..39720af 100644 --- a/DDrawCompat/DllMain.cpp +++ b/DDrawCompat/DllMain.cpp @@ -42,16 +42,18 @@ namespace CompatInterface::hookVtable(*intf); } - void hookDirectDraw(IDirectDraw7& dd) + void hookDirectDraw(CompatRef dd) { - IUnknown& ddUnk = reinterpret_cast(dd); - hookVtable>(IID_IDirectDraw, ddUnk); - hookVtable>(IID_IDirectDraw2, ddUnk); - hookVtable>(IID_IDirectDraw4, ddUnk); - hookVtable>(IID_IDirectDraw7, ddUnk); + CompatDirectDraw::s_origVtable = *(&dd)->lpVtbl; + CompatPtr dd7(&dd); + hookVtable>(dd7); + hookVtable>(dd7); + hookVtable>(dd7); + hookVtable>(dd7); + dd7.detach(); } - void hookDirectDrawSurface(IDirectDraw7& dd) + void hookDirectDrawSurface(CompatRef dd) { DDSURFACEDESC2 desc = {}; desc.dwSize = sizeof(desc); @@ -61,8 +63,7 @@ namespace desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; CompatPtr surface; - HRESULT result = CompatDirectDraw::s_origVtable.CreateSurface( - &dd, &desc, &surface.getRef(), nullptr); + HRESULT result = dd->CreateSurface(&dd, &desc, &surface.getRef(), nullptr); if (SUCCEEDED(result)) { CompatDirectDrawSurface::s_origVtable = *surface.get()->lpVtbl; @@ -78,12 +79,11 @@ namespace } } - void hookDirectDrawPalette(IDirectDraw7& dd) + void hookDirectDrawPalette(CompatRef dd) { PALETTEENTRY paletteEntries[2] = {}; IDirectDrawPalette* palette = nullptr; - HRESULT result = CompatDirectDraw::s_origVtable.CreatePalette( - &dd, DDPCAPS_1BIT, paletteEntries, &palette, nullptr); + HRESULT result = dd->CreatePalette(&dd, DDPCAPS_1BIT, paletteEntries, &palette, nullptr); if (SUCCEEDED(result)) { CompatDirectDrawPalette::hookVtable(*palette); @@ -101,7 +101,7 @@ namespace if (!isAlreadyInstalled) { Compat::Log() << "Installing DirectDraw hooks"; - IDirectDraw7* dd = DDrawRepository::getDirectDraw(); + auto dd(DDrawRepository::getDirectDraw()); if (dd) { hookDirectDraw(*dd); diff --git a/DDrawCompat/RealPrimarySurface.cpp b/DDrawCompat/RealPrimarySurface.cpp index f7150a1..9a1aabf 100644 --- a/DDrawCompat/RealPrimarySurface.cpp +++ b/DDrawCompat/RealPrimarySurface.cpp @@ -1,13 +1,11 @@ #include -#include "CompatDirectDraw.h" #include "CompatDirectDrawPalette.h" #include "CompatDirectDrawSurface.h" #include "CompatGdi.h" #include "CompatPaletteConverter.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" -#include "CompatRef.h" #include "Config.h" #include "DDrawScopedThreadLock.h" #include "DDrawProcs.h" @@ -217,7 +215,7 @@ namespace } template -HRESULT RealPrimarySurface::create(DirectDraw& dd) +HRESULT RealPrimarySurface::create(CompatRef dd) { typename Types::TSurfaceDesc desc = {}; desc.dwSize = sizeof(desc); @@ -226,8 +224,7 @@ HRESULT RealPrimarySurface::create(DirectDraw& dd) desc.dwBackBufferCount = 1; CompatPtr::TCreatedSurface> surface; - HRESULT result = CompatDirectDraw::s_origVtable.CreateSurface( - &dd, &desc, &surface.getRef(), nullptr); + HRESULT result = dd->CreateSurface(&dd, &desc, &surface.getRef(), nullptr); bool isFlippable = true; if (DDERR_NOEXCLUSIVEMODE == result) @@ -236,8 +233,7 @@ HRESULT RealPrimarySurface::create(DirectDraw& dd) desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; desc.dwBackBufferCount = 0; isFlippable = false; - result = CompatDirectDraw::s_origVtable.CreateSurface( - &dd, &desc, &surface.getRef(), nullptr); + result = dd->CreateSurface(&dd, &desc, &surface.getRef(), nullptr); } if (FAILED(result)) @@ -249,10 +245,10 @@ HRESULT RealPrimarySurface::create(DirectDraw& dd) return init(surface); } -template HRESULT RealPrimarySurface::create(IDirectDraw&); -template HRESULT RealPrimarySurface::create(IDirectDraw2&); -template HRESULT RealPrimarySurface::create(IDirectDraw4&); -template HRESULT RealPrimarySurface::create(IDirectDraw7&); +template HRESULT RealPrimarySurface::create(CompatRef); +template HRESULT RealPrimarySurface::create(CompatRef); +template HRESULT RealPrimarySurface::create(CompatRef); +template HRESULT RealPrimarySurface::create(CompatRef); void RealPrimarySurface::disableUpdates() { diff --git a/DDrawCompat/RealPrimarySurface.h b/DDrawCompat/RealPrimarySurface.h index 1703c84..f123287 100644 --- a/DDrawCompat/RealPrimarySurface.h +++ b/DDrawCompat/RealPrimarySurface.h @@ -5,12 +5,13 @@ #include #include "CompatWeakPtr.h" +#include "CompatRef.h" class RealPrimarySurface { public: template - static HRESULT create(DirectDraw& dd); + static HRESULT create(CompatRef dd); static void disableUpdates(); static void enableUpdates();