-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.cpp
461 lines (403 loc) · 11.6 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "overlay.h"
extern int aim;
extern bool esp;
extern bool item_glow;
extern bool player_glow;
extern bool aim_no_recoil;
extern bool ready;
extern bool use_nvidia;
extern float max_dist;
extern float smooth;
extern float max_fov;
extern int bone;
extern bool thirdperson;
extern int spectators;
extern int allied_spectators;
extern bool chargerifle;
int width;
int height;
bool k_leftclick = false;
bool k_ins = false;
bool show_menu = false;
visuals v;
extern bool IsKeyDown(int vk);
LONG nv_default = WS_POPUP | WS_CLIPSIBLINGS;
LONG nv_default_in_game = nv_default | WS_DISABLED;
LONG nv_edit = nv_default_in_game | WS_VISIBLE;
LONG nv_ex_default = WS_EX_TOOLWINDOW;
LONG nv_ex_edit = nv_ex_default | WS_EX_LAYERED | WS_EX_TRANSPARENT;
LONG nv_ex_edit_menu = nv_ex_default | WS_EX_TRANSPARENT;
static DWORD WINAPI StaticMessageStart(void* Param)
{
Overlay* ov = (Overlay*)Param;
ov->CreateOverlay();
return 0;
}
BOOL CALLBACK EnumWindowsCallback(HWND hwnd, LPARAM lParam)
{
wchar_t className[255] = L"";
GetClassName(hwnd, className, 255);
if (use_nvidia)
{
if (wcscmp(XorStrW(L"CEF-OSC-WIDGET"), className) == 0) //Nvidia overlay
{
HWND* w = (HWND*)lParam;
if (GetWindowLong(hwnd, GWL_STYLE) != nv_default && GetWindowLong(hwnd, GWL_STYLE) != nv_default_in_game)
return TRUE;
*w = hwnd;
return TRUE;
}
}
else
{
if (wcscmp(XorStrW(L"overlay"), className) == 0) //Custom overlay
{
HWND* w = (HWND*)lParam;
*w = hwnd;
return TRUE;
}
}
return TRUE;
}
// Data
static ID3D11Device* g_pd3dDevice = NULL;
static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
static IDXGISwapChain* g_pSwapChain = NULL;
static ID3D11RenderTargetView* g_mainRenderTargetView = NULL;
// Forward declarations of helper functions
bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
void CreateRenderTarget();
void CleanupRenderTarget();
void Overlay::RenderMenu()
{
static bool aim_enable = false;
static bool vis_check = false;
static bool spec_disable = false;
static bool all_spec_disable = false;
if (aim > 0)
{
aim_enable = true;
if (aim > 1)
{
vis_check = true;
}
else
{
vis_check = false;
}
}
else
{
aim_enable = false;
vis_check = false;
}
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2(490, 215));
ImGui::Begin(XorStr("##title"), (bool*)true, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar);
if (ImGui::BeginTabBar(XorStr("Tab")))
{
if (ImGui::BeginTabItem(XorStr("Main")))
{
ImGui::Checkbox(XorStr("ESP"), &esp);
ImGui::Checkbox(XorStr("AIM"), &aim_enable);
if (aim_enable)
{
ImGui::SameLine();
ImGui::Checkbox(XorStr("Visibility check"), &vis_check);
ImGui::SameLine();
ImGui::Checkbox(XorStr("No recoil/sway"), &aim_no_recoil);
if (vis_check)
{
aim = 2;
}
else
{
aim = 1;
}
}
else
{
aim = 0;
}
ImGui::Checkbox(XorStr("Glow items"), &item_glow);
ImGui::Checkbox(XorStr("Glow players"), &player_glow);
ImGui::Checkbox(XorStr("Thirdperson"), &thirdperson);
ImGui::Checkbox(XorStr("Charge rifle hack"), &chargerifle);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem(XorStr("Config")))
{
ImGui::Text(XorStr("Max distance:"));
ImGui::SliderFloat(XorStr("##1"), &max_dist, 100.0f * 40, 800.0f * 40, "%.2f");
ImGui::SameLine();
ImGui::Text("(%d meters)", (int)(max_dist / 40));
ImGui::Text(XorStr("Smooth aim value:"));
ImGui::SliderFloat(XorStr("##2"), &smooth, 12.0f, 150.0f, "%.2f");
ImGui::Text(XorStr("Max FOV:"));
ImGui::SliderFloat(XorStr("##3"), &max_fov, 5.0f, 250.0f, "%.2f");
ImGui::Text(XorStr("Aim at (bone id):"));
ImGui::SliderInt(XorStr("##4"), &bone, 0, 175);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem(XorStr("Visuals")))
{
ImGui::Text(XorStr("ESP options:"));
ImGui::Checkbox(XorStr("Box"), &v.box);
ImGui::SameLine(0, 70.0f);
ImGui::Checkbox(XorStr("Name"), &v.name);
ImGui::Checkbox(XorStr("Line"), &v.line);
ImGui::Checkbox(XorStr("Distance"), &v.distance);
ImGui::Checkbox(XorStr("Health bar"), &v.healthbar);
ImGui::Checkbox(XorStr("Shield bar"), &v.shieldbar);
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
ImGui::Text(XorStr("Overlay FPS: %.3f ms/frame (%.1f FPS)"), 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
void Overlay::RenderInfo()
{
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2(50, 25));
ImGui::Begin(XorStr("##info"), (bool*)true, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar);
DrawLine(ImVec2(9, 5), ImVec2(45, 5), RED, 2);
ImGui::TextColored(RED, "%d", spectators);
ImGui::SameLine();
ImGui::Text("-");
ImGui::SameLine();
ImGui::TextColored(GREEN, "%d", allied_spectators);
ImGui::End();
}
void Overlay::ClickThrough(bool v)
{
if (v)
{
nv_edit = nv_default_in_game | WS_VISIBLE;
if (GetWindowLong(overlayHWND, GWL_EXSTYLE) != nv_ex_edit)
SetWindowLong(overlayHWND, GWL_EXSTYLE, nv_ex_edit);
}
else
{
nv_edit = nv_default | WS_VISIBLE;
if (GetWindowLong(overlayHWND, GWL_EXSTYLE) != nv_ex_edit_menu)
SetWindowLong(overlayHWND, GWL_EXSTYLE, nv_ex_edit_menu);
}
}
DWORD Overlay::CreateOverlay()
{
EnumWindows(EnumWindowsCallback, (LPARAM)&overlayHWND);
Sleep(300);
if (overlayHWND == 0)
{
printf(XorStr("Can't find the overlay\n"));
Sleep(1000);
exit(0);
}
HDC hDC = ::GetWindowDC(NULL);
width = ::GetDeviceCaps(hDC, HORZRES);
height = ::GetDeviceCaps(hDC, VERTRES);
running = true;
// Initialize Direct3D
if (!CreateDeviceD3D(overlayHWND))
{
CleanupDeviceD3D();
return 1;
}
// Show the window
::ShowWindow(overlayHWND, SW_SHOWDEFAULT);
::UpdateWindow(overlayHWND);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
// Setup Dear ImGui style
ImGui::StyleColorsDark();
ImGui::GetStyle().WindowMinSize = ImVec2(1, 1);
// Setup Platform/Renderer bindings
ImGui_ImplWin32_Init(overlayHWND);
ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 0.00f);
// Main loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));
ClickThrough(true);
while (running)
{
HWND wnd = GetWindow(GetForegroundWindow(), GW_HWNDPREV);
if (use_nvidia)
{
if (GetWindowLong(overlayHWND, GWL_STYLE) != nv_edit)
SetWindowLong(overlayHWND, GWL_STYLE, nv_edit);
if (show_menu)
{
ClickThrough(false);
}
else
{
if (GetWindowLong(overlayHWND, GWL_EXSTYLE) != nv_ex_edit)
SetWindowLong(overlayHWND, GWL_EXSTYLE, nv_ex_edit);
ClickThrough(true);
}
}
if (wnd != overlayHWND)
{
SetWindowPos(overlayHWND, wnd, 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOMOVE | SWP_NOSIZE);
::UpdateWindow(overlayHWND);
}
if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
continue;
}
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
if (IsKeyDown(VK_LBUTTON) && !k_leftclick)
{
io.MouseDown[0] = true;
k_leftclick = true;
}
else if (!IsKeyDown(VK_LBUTTON) && k_leftclick)
{
io.MouseDown[0] = false;
k_leftclick = false;
}
if (IsKeyDown(VK_INSERT) && !k_ins && ready)
{
show_menu = !show_menu;
ClickThrough(!show_menu);
k_ins = true;
}
else if (!IsKeyDown(VK_INSERT) && k_ins)
{
k_ins = false;
}
if (show_menu)
RenderMenu();
else
RenderInfo();
RenderEsp();
// Rendering
ImGui::EndFrame();
ImGui::Render();
const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(1, 0); // Present with vsync
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
ClickThrough(true);
CleanupDeviceD3D();
::DestroyWindow(overlayHWND);
return 0;
}
void Overlay::Start()
{
DWORD ThreadID;
CreateThread(NULL, 0, StaticMessageStart, (void*)this, 0, &ThreadID);
}
void Overlay::Clear()
{
running = 0;
Sleep(50);
if (use_nvidia)
{
SetWindowLong(overlayHWND, GWL_STYLE, nv_default);
SetWindowLong(overlayHWND, GWL_EXSTYLE, nv_ex_default);
}
}
int Overlay::getWidth()
{
return width;
}
int Overlay::getHeight()
{
return height;
}
// Helper functions
void CreateRenderTarget()
{
ID3D11Texture2D* pBackBuffer;
g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
if (pBackBuffer)
{
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
pBackBuffer->Release();
}
}
bool CreateDeviceD3D(HWND hWnd)
{
// Setup swap chain
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 2;
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
UINT createDeviceFlags = 0;
//createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
return false;
CreateRenderTarget();
return true;
}
void CleanupRenderTarget()
{
if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
}
void CleanupDeviceD3D()
{
CleanupRenderTarget();
if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; }
if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
}
void Overlay::DrawLine(ImVec2 a, ImVec2 b, ImColor color, float width)
{
ImGui::GetWindowDrawList()->AddLine(a, b, color, width);
}
void Overlay::DrawBox(ImColor color, float x, float y, float w, float h)
{
DrawLine(ImVec2(x, y), ImVec2(x + w, y), color, 1.0f);
DrawLine(ImVec2(x, y), ImVec2(x, y + h), color, 1.0f);
DrawLine(ImVec2(x + w, y), ImVec2(x + w, y + h), color, 1.0f);
DrawLine(ImVec2(x, y + h), ImVec2(x + w, y + h), color, 1.0f);
}
void Overlay::Text(ImVec2 pos, ImColor color, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect)
{
ImGui::GetWindowDrawList()->AddText(ImGui::GetFont(), ImGui::GetFontSize(), pos, color, text_begin, text_end, wrap_width, cpu_fine_clip_rect);
}
void Overlay::String(ImVec2 pos, ImColor color, const char* text)
{
Text(pos, color, text, text + strlen(text), 200, 0);
}
void Overlay::RectFilled(float x0, float y0, float x1, float y1, ImColor color, float rounding, int rounding_corners_flags)
{
ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(x0, y0), ImVec2(x1, y1), color, rounding, rounding_corners_flags);
}
void Overlay::ProgressBar(float x, float y, float w, float h, int value, int v_max)
{
ImColor barColor = ImColor(
min(510 * (v_max - value) / 100, 255),
min(510 * value / 100, 255),
25,
255
);
RectFilled(x, y, x + w, y + ((h / float(v_max)) * (float)value), barColor, 0.0f, 0);
}