Advertisement
4lender

Untitled

Feb 16th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. #include <dwmapi.h>
  2. #include "Overlay.h"
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. ID3D11Device* g_pd3dDevice = NULL;
  7. ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
  8. IDXGISwapChain* g_pSwapChain = NULL;
  9. ID3D11RenderTargetView* g_mainRenderTargetView = NULL;
  10.  
  11. bool isDragging = false; // Флаг для отслеживания перетаскивания
  12. POINT dragStartPos; // Начальная позиция курсора при перетаскивании
  13. POINT windowStartPos; // Начальная позиция окна при перетаскивании
  14.  
  15. void CleanupRenderTarget();
  16. void CleanupDeviceD3D();
  17. void CreateRenderTarget();
  18. bool CreateDeviceD3D(HWND hWnd);
  19. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  20.  
  21. bool IsKeyDown(int VK)
  22. {
  23. return (GetAsyncKeyState(VK) & 0x8000) != 0;
  24. }
  25.  
  26. bool Overlay::CreateOverlay()
  27. {
  28. this->running = true;
  29.  
  30. wc = { sizeof(WNDCLASSEXA), 0, WndProc, 0, 0, NULL, NULL, NULL, NULL, TitleName, ClassName, NULL };
  31. RegisterClassExA(&wc);
  32. Hwnd = CreateWindowExA(WS_EX_TOPMOST, wc.lpszClassName, wc.lpszMenuName, WS_POPUP | WS_VISIBLE, 100, 100, 100, 100, NULL, NULL, wc.hInstance, NULL);
  33.  
  34. MARGINS margin = { -1 };
  35. DwmExtendFrameIntoClientArea(Hwnd, &margin);
  36.  
  37. if (!CreateDeviceD3D(Hwnd))
  38. {
  39. CleanupDeviceD3D();
  40. UnregisterClassA(wc.lpszClassName, wc.hInstance);
  41. exit(0);
  42. }
  43.  
  44. ShowWindow(Hwnd, SW_SHOWDEFAULT);
  45. UpdateWindow(Hwnd);
  46.  
  47. IMGUI_CHECKVERSION();
  48. ImGui::CreateContext();
  49. ImGuiIO& io = ImGui::GetIO(); (void)io;
  50. io.LogFilename = nullptr;
  51. io.IniFilename = nullptr;
  52.  
  53. ImGui_ImplWin32_Init(Hwnd);
  54. ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
  55.  
  56. SetWindowPos(Hwnd, nullptr, 0, 0, 1920, 1080, SWP_NOREDRAW);
  57. }
  58.  
  59. void Overlay::DestroyOverlay()
  60. {
  61. std::cout << "Shutting down overlay...\n";
  62. this->running = false;
  63.  
  64. ImGui_ImplDX11_Shutdown();
  65. ImGui_ImplWin32_Shutdown();
  66. ImGui::DestroyContext();
  67.  
  68. CleanupDeviceD3D();
  69. DestroyWindow(Hwnd);
  70. UnregisterClassA(wc.lpszClassName, wc.hInstance);
  71. std::cout << "Overlay closed!\n";
  72. }
  73.  
  74. void Overlay::OverlayLoop()
  75. {
  76. while (this->running)
  77. {
  78. MSG msg;
  79. while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  80. {
  81. TranslateMessage(&msg);
  82. DispatchMessage(&msg);
  83.  
  84. if (msg.message == WM_QUIT)
  85. {
  86. this->running = false;
  87. break;
  88. }
  89. }
  90.  
  91. ImGui_ImplDX11_NewFrame();
  92. ImGui_ImplWin32_NewFrame();
  93. ImGui::NewFrame();
  94.  
  95. HandleCheatOverlay();
  96.  
  97. ImGui::Render();
  98. const float clear_color_with_alpha[4] = { 0.f, 0.f, 0.f, 1.f }; // Черный цвет с альфа-каналом 1 (непрозрачный)
  99. g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
  100. g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
  101. ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
  102.  
  103. g_pSwapChain->Present(1, 0);
  104. }
  105. }
  106.  
  107. void CleanupRenderTarget()
  108. {
  109. if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
  110. }
  111.  
  112. void CleanupDeviceD3D()
  113. {
  114. CleanupRenderTarget();
  115. if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
  116. if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; }
  117. if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
  118. }
  119.  
  120. void CreateRenderTarget()
  121. {
  122. ID3D11Texture2D* pBackBuffer;
  123. g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
  124. g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
  125. pBackBuffer->Release();
  126. }
  127.  
  128. bool CreateDeviceD3D(HWND hWnd)
  129. {
  130. DXGI_SWAP_CHAIN_DESC sd;
  131. ZeroMemory(&sd, sizeof(sd));
  132. sd.BufferCount = 2;
  133. sd.BufferDesc.Width = 0;
  134. sd.BufferDesc.Height = 0;
  135. sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  136. sd.BufferDesc.RefreshRate.Numerator = 60;
  137. sd.BufferDesc.RefreshRate.Denominator = 1;
  138. sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  139. sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  140. sd.OutputWindow = hWnd;
  141. sd.SampleDesc.Count = 1;
  142. sd.SampleDesc.Quality = 0;
  143. sd.Windowed = TRUE;
  144. sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  145.  
  146. UINT createDeviceFlags = 0;
  147. D3D_FEATURE_LEVEL featureLevel;
  148. const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
  149. if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
  150. return false;
  151.  
  152. CreateRenderTarget();
  153. return true;
  154. }
  155.  
  156. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  157.  
  158. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  159. {
  160. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  161. return true;
  162.  
  163. switch (msg)
  164. {
  165. case WM_LBUTTONDOWN:
  166. // Начало перетаскивания
  167. isDragging = true;
  168. dragStartPos = { LOWORD(lParam), HIWORD(lParam) };
  169. GetWindowRect(hWnd, &windowStartPos);
  170. break;
  171.  
  172. case WM_MOUSEMOVE:
  173. if (isDragging)
  174. {
  175. // Перемещение окна
  176. POINT cursorPos;
  177. GetCursorPos(&cursorPos);
  178. int newX = windowStartPos.left + (cursorPos.x - dragStartPos.x);
  179. int newY = windowStartPos.top + (cursorPos.y - dragStartPos.y);
  180. SetWindowPos(hWnd, nullptr, newX, newY, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  181. }
  182. break;
  183.  
  184. case WM_LBUTTONUP:
  185. // Конец перетаскивания
  186. isDragging = false;
  187. break;
  188.  
  189. case WM_SIZE:
  190. if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
  191. {
  192. CleanupRenderTarget();
  193. g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
  194. CreateRenderTarget();
  195. }
  196. return 0;
  197.  
  198. case WM_SYSCOMMAND:
  199. if ((wParam & 0xfff0) == SC_KEYMENU)
  200. return 0;
  201. break;
  202.  
  203. case WM_DESTROY:
  204. PostQuitMessage(0);
  205. return 0;
  206. }
  207. return DefWindowProcA(hWnd, msg, wParam, lParam);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement