daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include "ImEasyRender.h"
  4.  
  5. #include "imgui/imgui.h"
  6. #include "imgui/imgui_impl_dx11.h"
  7. #include "imgui/imgui_impl_win32.h"
  8.  
  9. #pragma region D3DX_RENDER_FUNCTIONS
  10.  
  11. static ID3D11Device* g_pd3dDevice = NULL;
  12. static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
  13. static IDXGISwapChain* g_pSwapChain = NULL;
  14. static ID3D11RenderTargetView* g_mainRenderTargetView = NULL;
  15.  
  16. void CreateRenderTarget()
  17. {
  18.     ID3D11Texture2D* pBackBuffer;
  19.     g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)& pBackBuffer);
  20.     g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
  21.     pBackBuffer->Release();
  22. }
  23.  
  24. void CleanupRenderTarget()
  25. {
  26.     if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
  27. }
  28.  
  29.  
  30. extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  31. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  32. {
  33.     if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  34.         return true;
  35.  
  36.     switch (msg)
  37.     {
  38.     case WM_SIZE:
  39.         if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
  40.         {
  41.             CleanupRenderTarget();
  42.             g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
  43.             CreateRenderTarget();
  44.         }
  45.         return 0;
  46.     case WM_SYSCOMMAND:
  47.         if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  48.             return 0;
  49.         break;
  50.     case WM_DESTROY:
  51.         PostQuitMessage(0);
  52.         return 0;
  53.     }
  54.     return DefWindowProc(hWnd, msg, wParam, lParam);
  55. }
  56.  
  57. HRESULT CreateDeviceD3D(HWND hWnd)
  58. {
  59.     // Setup swap chain
  60.     DXGI_SWAP_CHAIN_DESC sd;
  61.     ZeroMemory(&sd, sizeof(sd));
  62.     sd.BufferCount = 2;
  63.     sd.BufferDesc.Width = 0;
  64.     sd.BufferDesc.Height = 0;
  65.     sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  66.     sd.BufferDesc.RefreshRate.Numerator = 60;
  67.     sd.BufferDesc.RefreshRate.Denominator = 1;
  68.     sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  69.     sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  70.     sd.OutputWindow = hWnd;
  71.     sd.SampleDesc.Count = 1;
  72.     sd.SampleDesc.Quality = 0;
  73.     sd.Windowed = TRUE;
  74.     sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  75.  
  76.     UINT createDeviceFlags = 0;
  77.     //createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
  78.     D3D_FEATURE_LEVEL featureLevel;
  79.     const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
  80.     if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
  81.         return E_FAIL;
  82.  
  83.     CreateRenderTarget();
  84.  
  85.     return S_OK;
  86. }
  87.  
  88. void CleanupDeviceD3D()
  89. {
  90.     CleanupRenderTarget();
  91.     if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
  92.     if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; }
  93.     if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
  94. }
  95.  
  96.  
  97. #pragma endregion
  98.  
  99. int windowstate = 1;
  100.  
  101.  
  102.  
  103. void ChangeClickability(bool canclick, HWND ownd) {
  104.     long style = GetWindowLong(ownd, GWL_EXSTYLE);
  105.     if (canclick) {
  106.         style &= ~WS_EX_LAYERED;
  107.         SetWindowLong(ownd, GWL_EXSTYLE, style);
  108.         windowstate = 1;
  109.     }
  110.     else {
  111.         style |= WS_EX_LAYERED;
  112.         SetWindowLong(ownd, GWL_EXSTYLE, style);
  113.         windowstate = 0;
  114.     }
  115. }
  116.  
  117.  
  118.  
  119. void ImOverlay::initialize(int width, int height) {
  120.     this->overlayHeight = height;
  121.     this->overlayWidth = width;
  122.  
  123.     //Set up the window we're going to draw on:
  124.     int sWidth = (int)GetSystemMetrics(SM_CXSCREEN);
  125.     int sHeight = (int)GetSystemMetrics(SM_CYSCREEN);
  126.     this->wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("Kiware Overlay"), NULL };
  127.     RegisterClassEx(&this->wc);
  128.     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);
  129.     SetLayeredWindowAttributes(hwnd, 0, 0, LWA_ALPHA);
  130.     SetLayeredWindowAttributes(hwnd, 0, RGB(0, 0, 0), LWA_COLORKEY);
  131.  
  132.     MARGINS Margin = { -1, -1, -1, -1 };
  133.     DwmExtendFrameIntoClientArea(hwnd, &Margin);
  134.     if (CreateDeviceD3D(hwnd) < 0)
  135.     {
  136.         CleanupDeviceD3D();
  137.     }
  138.  
  139.     ShowWindow(hwnd, SW_SHOWDEFAULT);
  140.     UpdateWindow(hwnd);
  141.     ImGui::CreateContext();
  142.     ImGuiIO& io = ImGui::GetIO(); (void)io;
  143.     ImGui::StyleColorsDark();
  144.  
  145.     float clearColor[4] = { 0.0f,0.0f,0.0f,0.0f };
  146.  
  147.     ImGui_ImplWin32_Init(hwnd);
  148.     ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
  149.     this->overlay = hwnd;
  150.    
  151. }
  152.  
  153. void ImOverlay::startOverlay(void(*loopfunction)(), HWND target) {
  154.  
  155.     this->target = target;
  156.  
  157.     float clearColor[4] = { 0.0f,0.0f,0.0f,0.0f };
  158.    
  159.     bool enabled = true;
  160.  
  161.     SetForegroundWindow(this->overlay);
  162.  
  163.     MSG msg;
  164.     ZeroMemory(&msg, sizeof(msg));
  165.     while (msg.message != WM_QUIT) {
  166.         if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) { TranslateMessage(&msg);            DispatchMessage(&msg);            continue; }
  167.         ImGui_ImplDX11_NewFrame();
  168.         ImGui_ImplWin32_NewFrame();
  169.  
  170.         if (GetAsyncKeyState(VK_INSERT) & 0x8000) {
  171.             enabled = !enabled;
  172.             SetForegroundWindow(this->overlay);
  173.             Sleep(250);
  174.         }
  175.         ImGui::NewFrame();
  176.         if (enabled) {
  177.             if (windowstate != 1) {
  178.                 ChangeClickability(true, this->overlay);
  179.             }
  180.             loopfunction();
  181.         }
  182.         else {
  183.             ChangeClickability(false, this->overlay);
  184.         }
  185.         ImGui::Render();
  186.         g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
  187.         g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (float*)& clearColor);
  188.         ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
  189.         g_pSwapChain->Present(1, 0);
  190.    
  191.     }
  192.     this->closeOverlay();
  193. }
  194.  
  195. void ImOverlay::closeOverlay() {
  196.     ImGui_ImplDX11_Shutdown();
  197.     ImGui_ImplWin32_Shutdown();
  198.     ImGui::DestroyContext();
  199.  
  200.     CleanupDeviceD3D();
  201.     DestroyWindow(overlay);
  202.     UnregisterClass(_T("Kiware Overlay"), this->wc.hInstance);
  203. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top