Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Commit

Permalink
Fixed issue about Direct3D crash. Refs #23
Browse files Browse the repository at this point in the history
  • Loading branch information
DronCode committed Mar 1, 2020
1 parent d497b45 commit d75bdba
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion HM3CoreKill/HM3CoreKill/ck/HM3Direct3D.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#include <ck/HM3Direct3D.h>
#include <ck/HM3Function.h>
#include <imgui.h>
#include <imgui_impl_dx9.h>

namespace ck
{
static constexpr const int BeginSceneIndex = 41;
static constexpr const int EndSceneIndex = 42;
static constexpr const int GetDeviceStateIndex = 9;
static constexpr const int ResetIndex = 16;

typedef HRESULT(__stdcall* D3DBeginScene_t)(IDirect3DDevice9*);
typedef HRESULT(__stdcall* D3DEndScene_t)(IDirect3DDevice9*);
typedef HRESULT(__stdcall* D3DReset_t)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*);

D3DBeginScene_t originalBeginSceneFunc;
D3DEndScene_t originalEndSceneFunc;
D3DReset_t originalResetFunc;

HRESULT __stdcall Direct3DDevice_OnBeginScene(IDirect3DDevice9* device)
{
Expand All @@ -35,6 +39,14 @@ namespace ck
return result;
}

HRESULT __stdcall Direct3DDevice_OnReset(IDirect3DDevice9* device, D3DPRESENT_PARAMETERS* pPresentationParameters)
{
ImGui_ImplDX9_InvalidateDeviceObjects();
auto result = originalResetFunc(device, pPresentationParameters);
ImGui_ImplDX9_CreateDeviceObjects();
return result;
}

HM3Direct3D& HM3Direct3D::getInstance()
{
static HM3Direct3D instance;
Expand All @@ -45,12 +57,16 @@ namespace ck
{
auto beginScenePtr = reinterpret_cast<D3DBeginScene_t>(HM3Function::hookVFTable((DWORD)device, BeginSceneIndex, (DWORD)Direct3DDevice_OnBeginScene, false));
auto endScenePtr = reinterpret_cast<D3DEndScene_t>(HM3Function::hookVFTable((DWORD)device, EndSceneIndex, (DWORD)Direct3DDevice_OnEndScene, false));
auto resetPtr = reinterpret_cast<D3DReset_t>(HM3Function::hookVFTable((DWORD)device, ResetIndex, (DWORD)Direct3DDevice_OnReset, false));

if ((DWORD)beginScenePtr != (DWORD)&Direct3DDevice_OnBeginScene)
originalBeginSceneFunc = (D3DBeginScene_t)beginScenePtr;

if ((DWORD)endScenePtr != (DWORD)&Direct3DDevice_OnEndScene)
originalEndSceneFunc = (D3DBeginScene_t)endScenePtr;

if ((DWORD)resetPtr != (DWORD)&Direct3DDevice_OnReset)
originalResetFunc = (D3DReset_t)resetPtr;
}

void HM3Direct3D::initialize(IDirect3DDevice9* device)
Expand Down

0 comments on commit d75bdba

Please sign in to comment.