Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "ImEasyRender.h"
- #include "imgui/imgui.h"
- #include "imgui/imgui_impl_dx11.h"
- #include "imgui/imgui_impl_win32.h"
- #pragma region D3DX_RENDER_FUNCTIONS
- static ID3D11Device* g_pd3dDevice = NULL;
- static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
- static IDXGISwapChain* g_pSwapChain = NULL;
- static ID3D11RenderTargetView* g_mainRenderTargetView = NULL;
- void CreateRenderTarget()
- {
- ID3D11Texture2D* pBackBuffer;
- g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)& pBackBuffer);
- g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
- pBackBuffer->Release();
- }
- void CleanupRenderTarget()
- {
- if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
- }
- extern 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_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) // Disable ALT application menu
- return 0;
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hWnd, msg, wParam, lParam);
- }
- HRESULT 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 E_FAIL;
- CreateRenderTarget();
- return S_OK;
- }
- 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; }
- }
- #pragma endregion
- int windowstate = 1;
- void ChangeClickability(bool canclick, HWND ownd) {
- long style = GetWindowLong(ownd, GWL_EXSTYLE);
- if (canclick) {
- style &= ~WS_EX_LAYERED;
- SetWindowLong(ownd, GWL_EXSTYLE, style);
- windowstate = 1;
- }
- else {
- style |= WS_EX_LAYERED;
- SetWindowLong(ownd, GWL_EXSTYLE, style);
- windowstate = 0;
- }
- }
- void ImOverlay::initialize(int width, int height) {
- this->overlayHeight = height;
- this->overlayWidth = width;
- //Set up the window we're going to draw on:
- int sWidth = (int)GetSystemMetrics(SM_CXSCREEN);
- int sHeight = (int)GetSystemMetrics(SM_CYSCREEN);
- this->wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("Kiware Overlay"), NULL };
- RegisterClassEx(&this->wc);
- HWND hwnd = CreateWindowExW(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE, L"KiWare Overlay", L"KiWare Overlay", WS_POPUP, 1, 1, 1360, 768, 0, 0, 0, 0);
- SetLayeredWindowAttributes(hwnd, 0, 0, LWA_ALPHA);
- SetLayeredWindowAttributes(hwnd, 0, RGB(0, 0, 0), LWA_COLORKEY);
- MARGINS Margin = { -1, -1, -1, -1 };
- DwmExtendFrameIntoClientArea(hwnd, &Margin);
- if (CreateDeviceD3D(hwnd) < 0)
- {
- CleanupDeviceD3D();
- }
- ShowWindow(hwnd, SW_SHOWDEFAULT);
- UpdateWindow(hwnd);
- ImGui::CreateContext();
- ImGuiIO& io = ImGui::GetIO(); (void)io;
- ImGui::StyleColorsDark();
- float clearColor[4] = { 0.0f,0.0f,0.0f,0.0f };
- ImGui_ImplWin32_Init(hwnd);
- ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
- this->overlay = hwnd;
- }
- void ImOverlay::startOverlay(void(*loopfunction)(), HWND target) {
- this->target = target;
- float clearColor[4] = { 0.0f,0.0f,0.0f,0.0f };
- bool enabled = true;
- SetForegroundWindow(this->overlay);
- MSG msg;
- ZeroMemory(&msg, sizeof(msg));
- while (msg.message != WM_QUIT) {
- if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); continue; }
- ImGui_ImplDX11_NewFrame();
- ImGui_ImplWin32_NewFrame();
- if (GetAsyncKeyState(VK_INSERT) & 0x8000) {
- enabled = !enabled;
- SetForegroundWindow(this->overlay);
- Sleep(250);
- }
- ImGui::NewFrame();
- if (enabled) {
- if (windowstate != 1) {
- ChangeClickability(true, this->overlay);
- }
- loopfunction();
- }
- else {
- ChangeClickability(false, this->overlay);
- }
- ImGui::Render();
- g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
- g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (float*)& clearColor);
- ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
- g_pSwapChain->Present(1, 0);
- }
- this->closeOverlay();
- }
- void ImOverlay::closeOverlay() {
- ImGui_ImplDX11_Shutdown();
- ImGui_ImplWin32_Shutdown();
- ImGui::DestroyContext();
- CleanupDeviceD3D();
- DestroyWindow(overlay);
- UnregisterClass(_T("Kiware Overlay"), this->wc.hInstance);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement