Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dwmapi.h>
- #include "Overlay.h"
- #include <iostream>
- #include <vector>
- ID3D11Device* g_pd3dDevice = NULL;
- ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
- IDXGISwapChain* g_pSwapChain = NULL;
- ID3D11RenderTargetView* g_mainRenderTargetView = NULL;
- bool isDragging = false; // Флаг для отслеживания перетаскивания
- POINT dragStartPos; // Начальная позиция курсора при перетаскивании
- POINT windowStartPos; // Начальная позиция окна при перетаскивании
- void CleanupRenderTarget();
- void CleanupDeviceD3D();
- void CreateRenderTarget();
- bool CreateDeviceD3D(HWND hWnd);
- LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
- bool IsKeyDown(int VK)
- {
- return (GetAsyncKeyState(VK) & 0x8000) != 0;
- }
- bool Overlay::CreateOverlay()
- {
- this->running = true;
- wc = { sizeof(WNDCLASSEXA), 0, WndProc, 0, 0, NULL, NULL, NULL, NULL, TitleName, ClassName, NULL };
- RegisterClassExA(&wc);
- Hwnd = CreateWindowExA(WS_EX_TOPMOST, wc.lpszClassName, wc.lpszMenuName, WS_POPUP | WS_VISIBLE, 100, 100, 100, 100, NULL, NULL, wc.hInstance, NULL);
- MARGINS margin = { -1 };
- DwmExtendFrameIntoClientArea(Hwnd, &margin);
- if (!CreateDeviceD3D(Hwnd))
- {
- CleanupDeviceD3D();
- UnregisterClassA(wc.lpszClassName, wc.hInstance);
- exit(0);
- }
- ShowWindow(Hwnd, SW_SHOWDEFAULT);
- UpdateWindow(Hwnd);
- IMGUI_CHECKVERSION();
- ImGui::CreateContext();
- ImGuiIO& io = ImGui::GetIO(); (void)io;
- io.LogFilename = nullptr;
- io.IniFilename = nullptr;
- ImGui_ImplWin32_Init(Hwnd);
- ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
- SetWindowPos(Hwnd, nullptr, 0, 0, 1920, 1080, SWP_NOREDRAW);
- }
- void Overlay::DestroyOverlay()
- {
- std::cout << "Shutting down overlay...\n";
- this->running = false;
- ImGui_ImplDX11_Shutdown();
- ImGui_ImplWin32_Shutdown();
- ImGui::DestroyContext();
- CleanupDeviceD3D();
- DestroyWindow(Hwnd);
- UnregisterClassA(wc.lpszClassName, wc.hInstance);
- std::cout << "Overlay closed!\n";
- }
- void Overlay::OverlayLoop()
- {
- while (this->running)
- {
- MSG msg;
- while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- if (msg.message == WM_QUIT)
- {
- this->running = false;
- break;
- }
- }
- ImGui_ImplDX11_NewFrame();
- ImGui_ImplWin32_NewFrame();
- ImGui::NewFrame();
- HandleCheatOverlay();
- ImGui::Render();
- const float clear_color_with_alpha[4] = { 0.f, 0.f, 0.f, 1.f }; // Черный цвет с альфа-каналом 1 (непрозрачный)
- 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);
- }
- }
- 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 CreateRenderTarget()
- {
- ID3D11Texture2D* pBackBuffer;
- g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
- g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
- pBackBuffer->Release();
- }
- bool CreateDeviceD3D(HWND hWnd)
- {
- 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;
- 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;
- }
- extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
- LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
- return true;
- switch (msg)
- {
- case WM_LBUTTONDOWN:
- // Начало перетаскивания
- isDragging = true;
- dragStartPos = { LOWORD(lParam), HIWORD(lParam) };
- GetWindowRect(hWnd, &windowStartPos);
- break;
- case WM_MOUSEMOVE:
- if (isDragging)
- {
- // Перемещение окна
- POINT cursorPos;
- GetCursorPos(&cursorPos);
- int newX = windowStartPos.left + (cursorPos.x - dragStartPos.x);
- int newY = windowStartPos.top + (cursorPos.y - dragStartPos.y);
- SetWindowPos(hWnd, nullptr, newX, newY, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
- }
- break;
- case WM_LBUTTONUP:
- // Конец перетаскивания
- isDragging = false;
- break;
- case WM_SIZE:
- if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
- {
- CleanupRenderTarget();
- g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
- CreateRenderTarget();
- }
- return 0;
- case WM_SYSCOMMAND:
- if ((wParam & 0xfff0) == SC_KEYMENU)
- return 0;
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProcA(hWnd, msg, wParam, lParam);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement