1
0
mirror of https://github.com/narzoul/DDrawCompat synced 2024-12-30 08:55:36 +01:00

Replaced IDirectDraw raw pointers with smart pointers

This commit is contained in:
narzoul 2016-05-16 22:01:52 +02:00
parent 068cdb8028
commit e8f4a970e0
15 changed files with 130 additions and 126 deletions

View File

@ -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<IUnknown> 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<IDirectDraw7> dd)
{
if (!(g_fullScreenCooperativeFlags & DDSCL_NOWINDOWCHANGES))
{
@ -30,13 +31,11 @@ namespace
}
}
CompatDirectDraw<IDirectDraw7>::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<IDirectDraw7>::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<IDirectDraw7> dd)
{
if (CompatPrimarySurface::isDisplayModeChanged)
{
CompatDirectDraw<IDirectDraw7>::s_origVtable.RestoreDisplayMode(&dd);
dd->RestoreDisplayMode(&dd);
}
CompatDirectDraw<IDirectDraw7>::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<void**>(&dd));
CompatPtr<IDirectDraw7> dd(Compat::queryInterface<IDirectDraw7>(g_fullScreenDirectDraw.get()));
if (isActivated)
{
activateApp(*dd);
@ -107,8 +102,6 @@ namespace
{
deactivateApp(*dd);
}
CompatDirectDraw<IDirectDraw7>::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<IUnknown> dd, HWND hwnd, DWORD flags)
{
g_fullScreenDirectDraw = dd;
g_fullScreenCooperativeWindow = hwnd;

View File

@ -1,15 +1,17 @@
#pragma once
#define CINTERFACE
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Unknwnbase.h>
struct IUnknown;
#include "CompatWeakPtr.h"
namespace CompatActivateAppHandler
{
void installHooks();
bool isActive();
void setFullScreenCooperativeLevel(IUnknown* dd, HWND hwnd, DWORD flags);
void setFullScreenCooperativeLevel(CompatWeakPtr<IUnknown> dd, HWND hwnd, DWORD flags);
void uninstallHooks();
}

View File

@ -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<IDirectDrawSurface> createFullScreenTagSurface(IDirectDraw& dd)
CompatPtr<IDirectDrawSurface> createFullScreenTagSurface(CompatRef<IDirectDraw> dd)
{
DDSURFACEDESC desc = {};
desc.dwSize = sizeof(desc);
@ -34,7 +35,7 @@ namespace
desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
CompatPtr<IDirectDrawSurface> tagSurface;
CompatDirectDraw<IDirectDraw>::s_origVtable.CreateSurface(&dd, &desc, &tagSurface.getRef(), nullptr);
dd->CreateSurface(&dd, &desc, &tagSurface.getRef(), nullptr);
if (tagSurface)
{
CompatPtr<IDirectDrawSurface7> tagSurface7(tagSurface);
@ -71,11 +72,10 @@ namespace
g_fullScreenTagSurface = nullptr;
}
template <typename TDirectDraw>
HRESULT setDisplayMode(TDirectDraw* This, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP,
HRESULT setDisplayMode(CompatRef<IDirectDraw7> dd, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP,
DWORD dwRefreshRate, DWORD dwFlags)
{
typename Types<TDirectDraw>::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<TDirectDraw>::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<TDirectDraw>::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<IDirectDraw7> dd, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP)
{
IDirectDraw7* dd = nullptr;
CompatDirectDraw<IDirectDraw>::s_origVtable.QueryInterface(
This, IID_IDirectDraw7, reinterpret_cast<void**>(&dd));
HRESULT result = setDisplayMode(dd, dwWidth, dwHeight, dwBPP, 0, 0);
CompatDirectDraw<IDirectDraw7>::s_origVtable.Release(dd);
return result;
return setDisplayMode(dd, dwWidth, dwHeight, dwBPP, 0, 0);
}
void setFullScreenDirectDraw(IDirectDraw& dd)
void setFullScreenDirectDraw(CompatRef<IDirectDraw> 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<DirectDrawInterface&>(dd);
DirectDrawInterface& ddIntf = reinterpret_cast<DirectDrawInterface&>(dd.get());
fullScreenDirectDraw.vtable = ddIntf.vtable;
fullScreenDirectDraw.ddObject = ddIntf.ddObject;
g_fullScreenDirectDraw = &fullScreenDirectDraw;
@ -187,7 +181,7 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw<TDirectDraw>::CreateSurface(
if (isPrimary)
{
result = CompatDirectDrawSurface<TSurface>::createCompatPrimarySurface(
result = CompatDirectDrawSurface<TSurface>::createCompatPrimarySurface<TDirectDraw>(
*This, *lpDDSurfaceDesc, *lplpDDSurface);
}
else
@ -224,7 +218,8 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw<TDirectDraw>::RestoreDisplayMode(TDir
HRESULT result = s_origVtable.RestoreDisplayMode(This);
if (SUCCEEDED(result))
{
CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(*This);
CompatPtr<IDirectDraw7> dd(Compat::queryInterface<IDirectDraw7>(This));
CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(*dd);
CompatPrimarySurface::isDisplayModeChanged = false;
}
return result;
@ -244,12 +239,10 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw<TDirectDraw>::SetCooperativeLevel(
{
if (dwFlags & DDSCL_FULLSCREEN)
{
IDirectDraw* dd = nullptr;
s_origVtable.QueryInterface(This, IID_IDirectDraw, reinterpret_cast<void**>(&dd));
CompatPtr<IDirectDraw> dd(Compat::queryInterface<IDirectDraw>(This));
setFullScreenDirectDraw(*dd);
CompatActivateAppHandler::setFullScreenCooperativeLevel(
reinterpret_cast<IUnknown*>(g_fullScreenDirectDraw), hWnd, dwFlags);
CompatDirectDraw<IDirectDraw>::s_origVtable.Release(dd);
}
else if (isFullScreenDirectDraw(This) && g_fullScreenTagSurface)
{
@ -268,9 +261,15 @@ HRESULT STDMETHODCALLTYPE CompatDirectDraw<TDirectDraw>::SetDisplayMode(
DWORD dwBPP,
Params... params)
{
return setDisplayMode(This, dwWidth, dwHeight, dwBPP, params...);
CompatPtr<IDirectDraw7> dd(Compat::queryInterface<IDirectDraw7>(This));
return setDisplayMode(*dd, dwWidth, dwHeight, dwBPP, params...);
}
template <> const IID& CompatDirectDraw<IDirectDraw>::s_iid = IID_IDirectDraw;
template <> const IID& CompatDirectDraw<IDirectDraw2>::s_iid = IID_IDirectDraw2;
template <> const IID& CompatDirectDraw<IDirectDraw4>::s_iid = IID_IDirectDraw4;
template <> const IID& CompatDirectDraw<IDirectDraw7>::s_iid = IID_IDirectDraw7;
template CompatDirectDraw<IDirectDraw>;
template CompatDirectDraw<IDirectDraw2>;
template CompatDirectDraw<IDirectDraw4>;

View File

@ -1,5 +1,7 @@
#pragma once
#include <type_traits>
#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 <typename Intf>
struct IsDirectDrawIntf : std::false_type {};
template<> struct IsDirectDrawIntf<IDirectDraw> : std::true_type {};
template<> struct IsDirectDrawIntf<IDirectDraw2> : std::true_type {};
template<> struct IsDirectDrawIntf<IDirectDraw4> : std::true_type {};
template<> struct IsDirectDrawIntf<IDirectDraw7> : std::true_type {};
template <typename NewIntf, typename OrigIntf>
std::enable_if_t<IsDirectDrawIntf<NewIntf>::value && IsDirectDrawIntf<OrigIntf>::value>
queryInterface(OrigIntf& origIntf, NewIntf*& newIntf)
{
CompatDirectDraw<OrigIntf>::s_origVtable.QueryInterface(
&origIntf, CompatDirectDraw<NewIntf>::s_iid, reinterpret_cast<void**>(&newIntf));
}
template <typename NewIntf>
std::enable_if_t<IsDirectDrawIntf<NewIntf>::value>
queryInterface(IUnknown& origIntf, NewIntf*& newIntf)
{
CompatDirectDraw<IDirectDraw>::s_origVtable.QueryInterface(
reinterpret_cast<IDirectDraw*>(&origIntf),
CompatDirectDraw<NewIntf>::s_iid, reinterpret_cast<void**>(&newIntf));
}
template <typename OrigIntf>
std::enable_if_t<IsDirectDrawIntf<OrigIntf>::value>
queryInterface(OrigIntf& origIntf, IUnknown*& newIntf)
{
CompatDirectDraw<OrigIntf>::s_origVtable.QueryInterface(
&origIntf, IID_IUnknown, reinterpret_cast<void**>(&newIntf));
}
}

View File

@ -174,13 +174,14 @@ void CompatDirectDrawSurface<TSurface>::setCompatVtable(Vtable<TSurface>& vtable
template <typename TSurface>
template <typename TDirectDraw>
HRESULT CompatDirectDrawSurface<TSurface>::createCompatPrimarySurface(
TDirectDraw& dd,
CompatRef<TDirectDraw> dd,
TSurfaceDesc compatDesc,
TSurface*& compatSurface)
{
if (0 == CompatPrimarySurface::displayMode.pixelFormat.dwSize)
{
CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(dd);
CompatPtr<IDirectDraw7> dd7(Compat::queryInterface<IDirectDraw7>(&dd));
CompatPrimarySurface::displayMode = CompatPrimarySurface::getDisplayMode(*dd7);
}
HRESULT result = RealPrimarySurface::create(dd);
@ -200,8 +201,7 @@ HRESULT CompatDirectDrawSurface<TSurface>::createCompatPrimarySurface(
compatDesc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
compatDesc.ddpfPixelFormat = CompatPrimarySurface::pixelFormat;
result = CompatDirectDraw<TDirectDraw>::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<IDirectDrawSurface4>;
template CompatDirectDrawSurface<IDirectDrawSurface7>;
template HRESULT CompatDirectDrawSurface<IDirectDrawSurface>::createCompatPrimarySurface(
IDirectDraw& dd,
CompatRef<IDirectDraw> dd,
TSurfaceDesc compatDesc,
IDirectDrawSurface*& compatSurface);
template HRESULT CompatDirectDrawSurface<IDirectDrawSurface>::createCompatPrimarySurface(
IDirectDraw2& dd,
CompatRef<IDirectDraw2> dd,
TSurfaceDesc compatDesc,
IDirectDrawSurface*& compatSurface);
template HRESULT CompatDirectDrawSurface<IDirectDrawSurface4>::createCompatPrimarySurface(
IDirectDraw4& dd,
CompatRef<IDirectDraw4> dd,
TSurfaceDesc compatDesc,
IDirectDrawSurface4*& compatSurface);
template HRESULT CompatDirectDrawSurface<IDirectDrawSurface7>::createCompatPrimarySurface(
IDirectDraw7& dd,
CompatRef<IDirectDraw7> dd,
TSurfaceDesc compatDesc,
IDirectDrawSurface7*& compatSurface);

View File

@ -19,7 +19,7 @@ public:
template <typename TDirectDraw>
static HRESULT createCompatPrimarySurface(
TDirectDraw& dd,
CompatRef<TDirectDraw> dd,
TSurfaceDesc compatDesc,
TSurface*& compatSurface);

View File

@ -1,7 +1,6 @@
#include <cstring>
#include <vector>
#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<IDirectDrawSurface7> surface;
HRESULT result = CompatDirectDraw<IDirectDraw7>::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<IDirectDraw7>::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)

View File

@ -1,7 +1,6 @@
#include <algorithm>
#include <cstring>
#include "CompatDirectDraw.h"
#include "CompatDirectDrawPalette.h"
#include "CompatPaletteConverter.h"
#include "CompatPrimarySurface.h"
@ -50,12 +49,6 @@ namespace
CompatPtr<IDirectDrawSurface7> 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<IDirectDrawSurface7> surface;
CompatDirectDraw<IDirectDraw7>::s_origVtable.CreateSurface(dd, &desc, &surface.getRef(), nullptr);
dd->CreateSurface(dd, &desc, &surface.getRef(), nullptr);
return surface;
}
}

View File

@ -1,7 +1,6 @@
#include <algorithm>
#include <vector>
#include "CompatDirectDraw.h"
#include "CompatDirectDrawSurface.h"
#include "CompatPrimarySurface.h"
#include "CompatPtr.h"
@ -33,13 +32,12 @@ namespace
namespace CompatPrimarySurface
{
template <typename TDirectDraw>
DisplayMode getDisplayMode(TDirectDraw& dd)
DisplayMode getDisplayMode(CompatRef<IDirectDraw7> dd)
{
DisplayMode dm = {};
typename CompatDirectDraw<TDirectDraw>::TSurfaceDesc desc = {};
DDSURFACEDESC2 desc = {};
desc.dwSize = sizeof(desc);
CompatDirectDraw<TDirectDraw>::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<IDirectDrawSurface7> getPrimary()
{
if (!g_primarySurface)

View File

@ -19,8 +19,7 @@ namespace CompatPrimarySurface
DWORD refreshRate;
};
template <typename TDirectDraw>
DisplayMode getDisplayMode(TDirectDraw& dd);
DisplayMode getDisplayMode(CompatRef<IDirectDraw7> dd);
CompatPtr<IDirectDrawSurface7> getPrimary();
bool isPrimary(void* surface);

View File

@ -1,7 +1,6 @@
#include <algorithm>
#include <vector>
#include "CompatDirectDraw.h"
#include "CompatPtr.h"
#include "DDrawLog.h"
#include "DDrawProcs.h"
@ -14,7 +13,7 @@ namespace
static std::vector<Surface> g_sysMemSurfaces;
static std::vector<Surface> g_vidMemSurfaces;
IDirectDraw7* createDirectDraw();
CompatPtr<IDirectDraw7> createDirectDraw();
Surface createSurface(DWORD width, DWORD height, const DDPIXELFORMAT& pf, DWORD caps);
std::vector<Surface>::iterator findSurface(DWORD width, DWORD height, const DDPIXELFORMAT& pf,
std::vector<Surface>& cachedSurfaces);
@ -24,22 +23,21 @@ namespace
void normalizePixelFormat(DDPIXELFORMAT& pf);
void returnSurface(const Surface& surface);
IDirectDraw7* createDirectDraw()
CompatPtr<IDirectDraw7> createDirectDraw()
{
IDirectDraw7* dd = nullptr;
CompatPtr<IDirectDraw7> dd;
HRESULT result = CALL_ORIG_DDRAW(DirectDrawCreateEx, nullptr,
reinterpret_cast<void**>(&dd), IID_IDirectDraw7, nullptr);
reinterpret_cast<void**>(&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<IDirectDraw7>::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<IDirectDraw7> getDirectDraw()
{
static IDirectDraw7* dd = createDirectDraw();
return dd;
static auto dd = new CompatPtr<IDirectDraw7>(createDirectDraw());
return *dd;
}
}

View File

@ -21,5 +21,5 @@ namespace DDrawRepository
~ScopedSurface();
};
IDirectDraw7* getDirectDraw();
CompatWeakPtr<IDirectDraw7> getDirectDraw();
}

View File

@ -42,16 +42,18 @@ namespace
CompatInterface::hookVtable(*intf);
}
void hookDirectDraw(IDirectDraw7& dd)
void hookDirectDraw(CompatRef<IDirectDraw7> dd)
{
IUnknown& ddUnk = reinterpret_cast<IUnknown&>(dd);
hookVtable<CompatDirectDraw<IDirectDraw>>(IID_IDirectDraw, ddUnk);
hookVtable<CompatDirectDraw<IDirectDraw2>>(IID_IDirectDraw2, ddUnk);
hookVtable<CompatDirectDraw<IDirectDraw4>>(IID_IDirectDraw4, ddUnk);
hookVtable<CompatDirectDraw<IDirectDraw7>>(IID_IDirectDraw7, ddUnk);
CompatDirectDraw<IDirectDraw7>::s_origVtable = *(&dd)->lpVtbl;
CompatPtr<IDirectDraw7> dd7(&dd);
hookVtable<CompatDirectDraw<IDirectDraw>>(dd7);
hookVtable<CompatDirectDraw<IDirectDraw2>>(dd7);
hookVtable<CompatDirectDraw<IDirectDraw4>>(dd7);
hookVtable<CompatDirectDraw<IDirectDraw7>>(dd7);
dd7.detach();
}
void hookDirectDrawSurface(IDirectDraw7& dd)
void hookDirectDrawSurface(CompatRef<IDirectDraw7> dd)
{
DDSURFACEDESC2 desc = {};
desc.dwSize = sizeof(desc);
@ -61,8 +63,7 @@ namespace
desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
CompatPtr<IDirectDrawSurface7> surface;
HRESULT result = CompatDirectDraw<IDirectDraw7>::s_origVtable.CreateSurface(
&dd, &desc, &surface.getRef(), nullptr);
HRESULT result = dd->CreateSurface(&dd, &desc, &surface.getRef(), nullptr);
if (SUCCEEDED(result))
{
CompatDirectDrawSurface<IDirectDrawSurface7>::s_origVtable = *surface.get()->lpVtbl;
@ -78,12 +79,11 @@ namespace
}
}
void hookDirectDrawPalette(IDirectDraw7& dd)
void hookDirectDrawPalette(CompatRef<IDirectDraw7> dd)
{
PALETTEENTRY paletteEntries[2] = {};
IDirectDrawPalette* palette = nullptr;
HRESULT result = CompatDirectDraw<IDirectDraw7>::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);

View File

@ -1,13 +1,11 @@
#include <atomic>
#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 <typename DirectDraw>
HRESULT RealPrimarySurface::create(DirectDraw& dd)
HRESULT RealPrimarySurface::create(CompatRef<DirectDraw> dd)
{
typename Types<DirectDraw>::TSurfaceDesc desc = {};
desc.dwSize = sizeof(desc);
@ -226,8 +224,7 @@ HRESULT RealPrimarySurface::create(DirectDraw& dd)
desc.dwBackBufferCount = 1;
CompatPtr<typename Types<DirectDraw>::TCreatedSurface> surface;
HRESULT result = CompatDirectDraw<DirectDraw>::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<DirectDraw>::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<IDirectDraw>);
template HRESULT RealPrimarySurface::create(CompatRef<IDirectDraw2>);
template HRESULT RealPrimarySurface::create(CompatRef<IDirectDraw4>);
template HRESULT RealPrimarySurface::create(CompatRef<IDirectDraw7>);
void RealPrimarySurface::disableUpdates()
{

View File

@ -5,12 +5,13 @@
#include <ddraw.h>
#include "CompatWeakPtr.h"
#include "CompatRef.h"
class RealPrimarySurface
{
public:
template <typename DirectDraw>
static HRESULT create(DirectDraw& dd);
static HRESULT create(CompatRef<DirectDraw> dd);
static void disableUpdates();
static void enableUpdates();