diff --git a/DDrawCompat/DDraw/DirectDraw.cpp b/DDrawCompat/DDraw/DirectDraw.cpp index 313cba2..abb3b1a 100644 --- a/DDrawCompat/DDraw/DirectDraw.cpp +++ b/DDrawCompat/DDraw/DirectDraw.cpp @@ -20,8 +20,6 @@ namespace { - const DWORD SURFACE_LOST_FLAG = 0x10000000; - template HRESULT STDMETHODCALLTYPE CreateSurface( TDirectDraw* This, TSurfaceDesc* lpDDSurfaceDesc, TSurface** lplpDDSurface, IUnknown* pUnkOuter) @@ -110,12 +108,12 @@ namespace HRESULT WINAPI restoreSurfaceLostFlag( LPDIRECTDRAWSURFACE7 lpDDSurface, LPDDSURFACEDESC2 /*lpDDSurfaceDesc*/, LPVOID lpContext) { - auto& surfacesToRestore = *static_cast*>(lpContext); - auto it = surfacesToRestore.find(DDraw::DirectDrawSurface::getSurfaceObject(*lpDDSurface)); + auto& surfacesToRestore = *static_cast*>(lpContext); + auto lcl = DDraw::DirectDrawSurface::getInt(*lpDDSurface).lpLcl; + auto it = surfacesToRestore.find(lcl); if (it != surfacesToRestore.end()) { - DWORD& flags = DDraw::DirectDrawSurface::getFlags(*lpDDSurface); - flags &= ~SURFACE_LOST_FLAG; + lcl->dwFlags &= ~DDRAWISURF_INVALID; surfacesToRestore.erase(it); } return DDENUMRET_OK; @@ -125,14 +123,14 @@ namespace LPDIRECTDRAWSURFACE7 lpDDSurface, LPDDSURFACEDESC2 /*lpDDSurfaceDesc*/, LPVOID lpContext) { auto& surfacesToRestore = *static_cast*>(lpContext); - DWORD& flags = DDraw::DirectDrawSurface::getFlags(*lpDDSurface); - if (!(flags & SURFACE_LOST_FLAG)) + auto lcl = DDraw::DirectDrawSurface::getInt(*lpDDSurface).lpLcl; + if (!(lcl->dwFlags & DDRAWISURF_INVALID)) { auto resource = D3dDdi::Device::findResource(DDraw::DirectDrawSurface::getDriverResourceHandle(*lpDDSurface)); if (resource && !resource->getOrigDesc().Flags.MatchGdiPrimary) { - flags |= SURFACE_LOST_FLAG; - surfacesToRestore.insert(DDraw::DirectDrawSurface::getSurfaceObject(*lpDDSurface)); + lcl->dwFlags |= DDRAWISURF_INVALID; + surfacesToRestore.insert(lcl); } } return DDENUMRET_OK; @@ -212,7 +210,7 @@ namespace DDraw } DDraw::ScopedThreadLock lock; - std::set surfacesToRestore; + std::set surfacesToRestore; TagSurface::forEachDirectDraw([&](CompatRef dd) { dd->EnumSurfaces(&dd, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL, nullptr, diff --git a/DDrawCompat/DDraw/DirectDraw.h b/DDrawCompat/DDraw/DirectDraw.h index ebe8caf..16e1a45 100644 --- a/DDrawCompat/DDraw/DirectDraw.h +++ b/DDrawCompat/DDraw/DirectDraw.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -18,15 +19,15 @@ namespace DDraw void suppressEmulatedDirectDraw(GUID*& guid); template - void* getDdObject(TDirectDraw& dd) + DDRAWI_DIRECTDRAW_INT& getInt(TDirectDraw& dd) { - return reinterpret_cast(&dd)[1]; + return reinterpret_cast(dd); } template HWND* getDeviceWindowPtr(TDirectDraw& dd) { - return &reinterpret_cast(&dd)[1][8]; + return &reinterpret_cast(getInt(dd).lpLcl->hWnd); } template diff --git a/DDrawCompat/DDraw/DirectDrawSurface.h b/DDrawCompat/DDraw/DirectDrawSurface.h index 7853934..4792994 100644 --- a/DDrawCompat/DDraw/DirectDrawSurface.h +++ b/DDrawCompat/DDraw/DirectDrawSurface.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -14,21 +15,15 @@ namespace DDraw std::vector> getAllAttachedSurfaces(CompatRef surface); template - void* getSurfaceObject(TSurface& surface) + DDRAWI_DDRAWSURFACE_INT& getInt(TSurface& surface) { - return reinterpret_cast(&surface)[1]; - } - - template - DWORD& getFlags(TSurface& surface) - { - return reinterpret_cast(&surface)[1][7]; + return reinterpret_cast(surface); } template HANDLE getRuntimeResourceHandle(TSurface& surface) { - return reinterpret_cast(&surface)[1][2]; + return reinterpret_cast(getInt(surface).lpLcl->hDDSurface); } template diff --git a/DDrawCompat/DDraw/Surfaces/TagSurface.cpp b/DDrawCompat/DDraw/Surfaces/TagSurface.cpp index 6f4c2e7..74ea9db 100644 --- a/DDrawCompat/DDraw/Surfaces/TagSurface.cpp +++ b/DDrawCompat/DDraw/Surfaces/TagSurface.cpp @@ -6,14 +6,14 @@ namespace { - std::map g_ddObjects; + std::map g_ddObjects; } namespace DDraw { - TagSurface::TagSurface(DWORD origCaps, void* ddObject) + TagSurface::TagSurface(DWORD origCaps, DDRAWI_DIRECTDRAW_LCL* ddLcl) : Surface(origCaps) - , m_ddObject(ddObject) + , m_ddLcl(ddLcl) , m_fullscreenWindow(nullptr) , m_fullscreenWindowStyle(0) , m_fullscreenWindowExStyle(0) @@ -23,7 +23,7 @@ namespace DDraw TagSurface::~TagSurface() { setFullscreenWindow(nullptr); - g_ddObjects.erase(m_ddObject); + g_ddObjects.erase(m_ddLcl); } HRESULT TagSurface::create(CompatRef dd) @@ -35,9 +35,9 @@ namespace DDraw desc.dwHeight = 1; desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; - auto ddObject = DDraw::DirectDraw::getDdObject(dd.get()); - auto privateData(std::make_unique(desc.ddsCaps.dwCaps, ddObject)); - g_ddObjects[ddObject] = privateData.get(); + auto ddLcl = DDraw::DirectDraw::getInt(dd.get()).lpLcl; + auto privateData(std::make_unique(desc.ddsCaps.dwCaps, ddLcl)); + g_ddObjects[ddLcl] = privateData.get(); IDirectDrawSurface* surface = nullptr; return Surface::create(dd, desc, surface, std::move(privateData)); @@ -57,25 +57,20 @@ namespace DDraw void TagSurface::forEachDirectDraw(std::function)> callback) { - struct DirectDrawInterface + for (auto& ddObj : g_ddObjects) { - const void* vtable; - void* ddObject; - DirectDrawInterface* next; - DWORD refCount; - }; - - for (auto ddObj : g_ddObjects) - { - DirectDrawInterface intf = { &CompatVtable::s_origVtable, ddObj.first, nullptr, 1 }; + DDRAWI_DIRECTDRAW_INT intf = {}; + intf.lpVtbl = &CompatVtable::s_origVtable; + intf.lpLcl = ddObj.first; + intf.dwIntRefCnt = 1; callback(CompatRef(reinterpret_cast(intf))); } } TagSurface* TagSurface::get(CompatRef dd) { - auto ddObject = DDraw::DirectDraw::getDdObject(dd.get()); - auto it = g_ddObjects.find(ddObject); + auto ddLcl = DDraw::DirectDraw::getInt(dd.get()).lpLcl; + auto it = g_ddObjects.find(ddLcl); if (it != g_ddObjects.end()) { return it->second; @@ -85,7 +80,7 @@ namespace DDraw { return nullptr; } - return g_ddObjects[ddObject]; + return g_ddObjects[ddLcl]; } void TagSurface::setFullscreenWindow(HWND hwnd) diff --git a/DDrawCompat/DDraw/Surfaces/TagSurface.h b/DDrawCompat/DDraw/Surfaces/TagSurface.h index bda76f2..d22e378 100644 --- a/DDrawCompat/DDraw/Surfaces/TagSurface.h +++ b/DDrawCompat/DDraw/Surfaces/TagSurface.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -11,7 +12,7 @@ namespace DDraw class TagSurface : public Surface { public: - TagSurface(DWORD origCaps, void* ddObject); + TagSurface(DWORD origCaps, DDRAWI_DIRECTDRAW_LCL* ddLcl); virtual ~TagSurface() override; static TagSurface* get(CompatRef dd); @@ -26,7 +27,7 @@ namespace DDraw private: static HRESULT create(CompatRef dd); - void* m_ddObject; + DDRAWI_DIRECTDRAW_LCL* m_ddLcl; HWND m_fullscreenWindow; LONG m_fullscreenWindowStyle; LONG m_fullscreenWindowExStyle;