-
Notifications
You must be signed in to change notification settings - Fork 11
/
overlay.cpp
366 lines (302 loc) · 13.2 KB
/
overlay.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "../pch.h"
// microsoft safe release of pointers
template <typename T>
inline VOID c_overlay::safe_release(T*& p) {
if (NULL != p) {
p->Release();
p = NULL;
}
}
BOOL c_overlay::init_device() {
DXGI_SWAP_CHAIN_DESC vSwapChainDesc;
ZeroMemory(&vSwapChainDesc, sizeof(vSwapChainDesc));
vSwapChainDesc.BufferCount = 2;
vSwapChainDesc.BufferDesc.Width = 0;
vSwapChainDesc.BufferDesc.Height = 0;
vSwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
vSwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
vSwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
vSwapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
vSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
vSwapChainDesc.OutputWindow = window_handle;
vSwapChainDesc.SampleDesc.Count = 1;
vSwapChainDesc.SampleDesc.Quality = 0;
vSwapChainDesc.Windowed = TRUE;
vSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
D3D_FEATURE_LEVEL vFeatureLevel;
D3D_FEATURE_LEVEL vFeatureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, vFeatureLevelArray, 2, D3D11_SDK_VERSION, &vSwapChainDesc, &swap_chain, &d3d_device, &vFeatureLevel, &device_context) != S_OK)
return false;
init_render_target();
ShowWindow(window_handle, SW_SHOWNORMAL);
UpdateWindow(window_handle);
return true;
}
VOID c_overlay::dest_device() {
dest_render_target();
safe_release(swap_chain);
safe_release(device_context);
safe_release(d3d_device);
}
VOID c_overlay::init_imgui() {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsClassic();
ImGuiStyle& style = ImGui::GetStyle();
style.WindowTitleAlign.x = 0.50f;
style.WindowPadding = ImVec2(15, 15);
style.WindowRounding = 4.0f;
style.FramePadding = ImVec2(2, 2);
style.FrameRounding = 2.0f;
style.ItemSpacing = ImVec2(12, 8);
style.ItemInnerSpacing = ImVec2(8, 6);
style.IndentSpacing = 25.0f;
style.ScrollbarSize = 15.0f;
style.ScrollbarRounding = 9.0f;
style.GrabMinSize = 5.0f;
style.GrabRounding = 3.0f;
style.Colors[ImGuiCol_Text] = ImVec4(0.80f, 0.80f, 0.83f, 1.00f);
style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.043f, 0.043f, 0.043f, 1.00f);
style.Colors[ImGuiCol_ChildBg] = ImVec4(0.043f, 0.043f, 0.043f, 1.00f);
style.Colors[ImGuiCol_PopupBg] = ImVec4(0.043f, 0.043f, 0.043f, 1.00f);
style.Colors[ImGuiCol_Border] = ImVec4(0.012f, 0.91f, 0.957f, 1.f);
style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.043f, 0.043f, 0.043f, 0.00f);
style.Colors[ImGuiCol_FrameBg] = ImVec4(0.043f, 0.043f, 0.043f, 1.00f);
style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 0.98f, 0.95f, 0.75f);
style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.07f, 0.07f, 0.09f, 1.00f);
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style.Colors[ImGuiCol_CheckMark] = ImVec4(0.012f, 0.91f, 0.957f, 1.f);
style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style.Colors[ImGuiCol_Button] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style.Colors[ImGuiCol_Header] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style.Colors[ImGuiCol_PlotLines] = ImVec4(0.40f, 0.39f, 0.38f, 0.63f);
style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f);
style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.40f, 0.39f, 0.38f, 0.63f);
style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f);
style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.25f, 1.00f, 0.00f, 0.43f);
style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.043f, 0.043f, 0.043f, 0.73f);
ImGuiIO& io = ImGui::GetIO();
font->im_font = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Tahoma.ttf", 24.0f);
font->font_size = 24.f;
ImGui_ImplWin32_Init(window_handle);
ImGui_ImplDX11_Init(d3d_device, device_context);
}
VOID c_overlay::dest_imgui() {
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
}
VOID c_overlay::init_render_target() {
ID3D11Texture2D* back_buffer = nullptr;
swap_chain->GetBuffer(0, IID_PPV_ARGS(&back_buffer));
if (back_buffer)
d3d_device->CreateRenderTargetView(back_buffer, NULL, &render_target_view);
// todo: error here if it can't get the backbuffer of the render target
back_buffer->Release();
}
VOID c_overlay::dest_render_target() {
if (!render_target_view)
return;
render_target_view->Release();
render_target_view = NULL;
}
c_overlay::c_overlay() :
ready(false),
exit(false),
window_handle(nullptr),
window_class({}),
window_width((FLOAT)GetSystemMetrics(SM_CXSCREEN)),
window_height((FLOAT)GetSystemMetrics(SM_CYSCREEN)),
font(nullptr),
d3d_device(nullptr),
device_context(nullptr),
swap_chain(nullptr),
render_target_view(nullptr),
draw_list(nullptr),
input_handler_thread(NULL),
breath(NULL),
menu_ticks(NULL)
{
// get a handle to the nvidia window
window_handle = FindWindowA("CEF-OSC-WIDGET", "NVIDIA GeForce Overlay"); //Hijack nvidia share, bc for some reason this is fucking possible?
auto getInfo = GetWindowLongA(window_handle, -20);
auto changeAttributes = SetWindowLongA(window_handle, -20, (LONG_PTR)(getInfo | 0x20));
// avoiding window flags: https://www.unknowncheats.me/forum/general-programming-and-reversing/229070-overlay-window-topmost-top-game-windows-video.html
MARGINS margins = { -1 };
margins.cyBottomHeight = margins.cyTopHeight = margins.cxLeftWidth = margins.cxRightWidth = -1;
DwmExtendFrameIntoClientArea(window_handle, &margins);
SetLayeredWindowAttributes(window_handle, 0x000000, 0xFF, 0x02);
SetWindowPos(window_handle, HWND_TOPMOST, 0, 0, 0, 0, 0x0002 | 0x0001);
if (!window_handle)
return; // error grabbing nvidia overlay
font = new s_font();
if (!init_device())
return; // error creating device
init_imgui();
}
c_overlay::~c_overlay() {
dest_imgui();
dest_device();
delete font;
}
const void c_overlay::render(FLOAT width, FLOAT height) {
if (exit) {
ready = true;
return;
}
ImGuiStyle& style = ImGui::GetStyle();
static bool l_crosshair = false;
static bool l_show_fps = true;
init_draw_list(); // we only do this once a loop
// a double value between 0 && 1 based on the anim time
double menu_anim = sin((M_PI / 2.0) * (((menu_ticks == 1 ? 0 : 1) * pow(menu_ticks, 2)) / ((double)pow(menu_anim_time - 1, 2))));
circle({ window_width / 2, window_height / 2 }, menu_anim * (window_width * 0.573674 /*Magical constant to calc radius of a 16:9 window found using some basic trig*/), ImColor(11, 11, 11, 80));
style.Alpha = menu_anim;
style.Colors[ImGuiCol_Border] = ImVec4(0.012f, 0.91f, 0.957f, ((breath / (float)breath_anim_time) * .7f) + .3f);
if (l_show_fps) {
std::string fps = std::to_string(static_cast<int>(ImGui::GetIO().Framerate));
text({ window_width - ImGui::CalcTextSize(fps.c_str()).x, 0 }, fps);
}
//auto io = &ImGui::GetIO();
//overlay.text({io->MousePos.x, io->MousePos.y + 10}, "CURSOR IS RIGHT HERE");
if (l_crosshair)
crosshair(10, ImColor(3, 232, 244));
if (menu_ticks == 1)
return;
// imgui menu itself
ImGui::Begin("Misc", 0, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize);
ImGui::Checkbox("Enable Crosshair", &l_crosshair);
ImGui::Checkbox("Enable FPS Counter", &l_show_fps);
ImGui::End();
}
BOOL c_overlay::msg_loop() {
if (window_handle) {
MSG msg{ 0 };
ImVec4 clear_clr = { 0,0,0,0 };
if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
render(window_width, window_height);
ImGui::Render();
device_context->OMSetRenderTargets(1, &render_target_view, NULL);
device_context->ClearRenderTargetView(render_target_view, (float*)&clear_clr); // might break, if so then put cheat color of type ImVec4 { 0,0,0,0 }
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
swap_chain->Present(1, 0);
return msg.message != WM_QUIT;
}
return false;
}
DWORD c_overlay::input_handler() {
// timers for our animations
breath_timer.start();
menu_timer.start();
for (;!exit;) {
// for our imgui menu interaction
ImGuiIO& io = ImGui::GetIO();
POINT p{};
GetCursorPos(&p);
io.MousePos = ImVec2((float)p.x, (float)p.y);
io.MouseDown[0] = GetAsyncKeyState(VK_LBUTTON) & 0x8000;
io.MouseDown[1] = GetAsyncKeyState(VK_RBUTTON) & 0x8000;
RECT rect{ 0 };
POINT point{ 0 };
GetClientRect(window_handle, &rect);
SetWindowPos(window_handle, HWND_TOPMOST, rect.left, rect.top, rect.right, rect.bottom, SWP_NOREDRAW);
// panic key
exit = GetAsyncKeyState(VK_END) & 0x8000;
// timer for our breathing animation
int breath_tmr = breath_timer.end();
if (breath_tmr >= breath_anim_time * 2) {
breath = breath_anim_time;
breath_timer.start();
}
else if (breath_tmr > breath_anim_time)
breath = breath_anim_time - (breath_tmr - breath_anim_time);
else if (breath_tmr <= breath_anim_time)
breath = breath_tmr;
// timer for menu circle animation & fade in
int menu_tmr = menu_timer.end();
if (GetAsyncKeyState(VK_INSERT) & 0x8000 && menu_tmr > menu_anim_time) {
show_menu = !show_menu;
menu_ticks = menu_anim_time;
menu_timer.start();
}
else if (menu_tmr < menu_anim_time)
menu_ticks = (show_menu ? menu_tmr : menu_anim_time - menu_tmr);
}
return 1;
}
VOID c_overlay::init_draw_list() {
draw_list = ImGui::GetBackgroundDrawList();
}
VOID c_overlay::crosshair(const FLOAT aSize, ImU32 color) {
draw_list->AddLine({ window_width / 2,window_height / 2 - (aSize + 1) }, { window_width / 2 ,window_height / 2 + (aSize + 1) }, color, 2);
draw_list->AddLine({ window_width / 2 - (aSize + 1),window_height / 2 }, { window_width / 2 + (aSize + 1), window_height / 2 }, color, 2);
}
VOID c_overlay::box(const ImVec2& pos, const FLOAT width, const FLOAT height, ImU32 color, const FLOAT line_width) {
std::array<ImVec2, 4> box_lines{ pos,ImVec2{pos.x + width,pos.y},ImVec2{pos.x + width,pos.y + height},ImVec2{pos.x,pos.y + height} };
draw_list->AddPolyline(box_lines.data(), box_lines.size(), color, true, 2);
}
VOID c_overlay::line(const ImVec2& point1, const ImVec2 point2, ImU32 color, const FLOAT line_width) {
draw_list->AddLine(point1, point2, color, line_width);
}
VOID c_overlay::circle(const ImVec2& point, const FLOAT radius, ImU32 color) {
draw_list->AddCircleFilled(point, radius, color, 200); // num segments is how circular the circle actually is
}
VOID c_overlay::text(const ImVec2& pos, const std::string& text, ImU32 color, bool outline) {
if (outline) {
draw_list->AddText(font->im_font, 18.f, { pos.x + 1, pos.y }, IM_COL32_BLACK, text.c_str());
draw_list->AddText(font->im_font, 18.f, { pos.x - 1, pos.y }, IM_COL32_BLACK, text.c_str());
draw_list->AddText(font->im_font, 18.f, { pos.x, pos.y + 1 }, IM_COL32_BLACK, text.c_str());
draw_list->AddText(font->im_font, 18.f, { pos.x, pos.y - 1 }, IM_COL32_BLACK, text.c_str());
}
draw_list->AddText(font->im_font, 18.f, pos, color, text.c_str());
}
VOID c_overlay::radial_gradient(const ImVec2& center, float radius, ImU32 col_in, ImU32 col_out) {
if (((col_in | col_out) & IM_COL32_A_MASK) == 0 || radius < 0.5f)
return;
draw_list->_PathArcToFastEx(center, radius, 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, 0);
const int count = draw_list->_Path.Size - 1;
unsigned int vtx_base = draw_list->_VtxCurrentIdx;
draw_list->PrimReserve(count * 3, count + 1);
const ImVec2 uv = draw_list->_Data->TexUvWhitePixel;
draw_list->PrimWriteVtx(center, uv, col_in);
for (int n = 0; n < count; n++)
draw_list->PrimWriteVtx(draw_list->_Path[n], uv, col_out);
for (int n = 0; n < count; n++) {
draw_list->PrimWriteIdx((ImDrawIdx)(vtx_base));
draw_list->PrimWriteIdx((ImDrawIdx)(vtx_base + 1 + n));
draw_list->PrimWriteIdx((ImDrawIdx)(vtx_base + 1 + ((n + 1) % count)));
}
draw_list->_Path.Size = 0;
}
BOOL c_overlay::in_screen(const ImVec2& pos) {
return !(pos.x > window_width || pos.x<0 || pos.y>window_height || pos.y < 0);
}
DWORD __stdcall c_overlay::start_thread(void* Param) {
c_overlay* This = (c_overlay*)Param;
return This->input_handler();
}
BOOL c_overlay::start_input_handler() {
return CreateThread(NULL, 0, start_thread, (VOID*)this, 0, &input_handler_thread) != INVALID_HANDLE_VALUE;
}