-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathRenderer.cpp
76 lines (59 loc) · 2.28 KB
/
Renderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "Renderer.h"
#include "Hooks.h"
Renderer renderer;
// Allow us to directly call the ImGui WndProc function.
extern LRESULT ImGui_ImplDX9_WndProcHandler(HWND, UINT, WPARAM, LPARAM);
// The original WndProc function.
WNDPROC game_wndproc = nullptr;
// Hooked WndProc function to process all incoming input messages.
LRESULT __stdcall hkWndProc(HWND window, UINT message_type, WPARAM w_param, LPARAM l_param) {
// Let the renderer decide whether we should pass this input message to the game.
if (renderer.HandleInputMessage(message_type, w_param, l_param))
return true;
// The GUI is inactive so pass the input to the game.
return CallWindowProc(game_wndproc, window, message_type, w_param, l_param);
};
Renderer::~Renderer() {
// Restore the original WndProc function.
SetWindowLongPtr(this->window, GWLP_WNDPROC, LONG_PTR(game_wndproc));
}
bool Renderer::IsReady() const {
// Whether 'Initialize' has been called successfully yet.
return this->ready;
}
bool Renderer::IsActive() const {
// Whether the GUI is accepting input and should be drawn.
return this->ready && this->active;
}
bool Renderer::Initialize(HWND window, IDirect3DDevice9* device) {
// Store the window handle for cleanup later.
this->window = window;
// Store a pointer to the original WndProc so we can call it in our hook.
game_wndproc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(window, GWLP_WNDPROC, LONG_PTR(hkWndProc)));
// Initialize ImGui implementation.
if (ImGui_ImplDX9_Init(window, device))
this->ready = true;
return this->ready;
}
static const char* PressingMenuKey = ("-- press a key --");
bool Renderer::HandleInputMessage(UINT message_type, WPARAM w_param, LPARAM l_param) {
//Because our renderer hook sucks. We have to improvise...
if (G::text == PressingMenuKey)
{
this->active = false;
if (!SetKeyCodeState::shouldListen)
{
this->active = true;
}
}
// Close menu when game window loses focus.
if (message_type == WM_NCACTIVATE && w_param == 0)
this->active = false;
// Toggle the menu when INSERT is pressed.
if (message_type == WM_KEYUP && w_param == VK_INSERT)
this->active = !this->active;
// When the GUI is active ImGui can handle input by itself.
if (this->active)
ImGui_ImplDX9_WndProcHandler(this->window, message_type, w_param, l_param);
return this->active;
}