Guest User

Untitled

a guest
Jun 6th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.48 KB | None | 0 0
  1. #include "draco.h"
  2. #include "gui.h"
  3.  
  4. int __stdcall wWinMain(HINSTANCE instance, HINSTANCE previousInstance, PWSTR arguments, int commandShow)
  5. {
  6.     gui::createWindow("menu", "menu class");
  7.     gui::createDevice();
  8.     gui::createImGui();
  9.  
  10.     while (gui::exit)
  11.     {
  12.         gui::beginRender();
  13.         gui::render();
  14.         gui::endRender();
  15.  
  16.         std::this_thread::sleep_for(std::chrono::milliseconds(10));
  17.     }
  18.  
  19.     gui::destroyImGui();
  20.     gui::destroyDevice();
  21.     gui::destroyWindow();
  22.  
  23.     return EXIT_SUCCESS;
  24. }
  25.  
  26. #pragma once
  27.  
  28. #include <d3d9.h>
  29.  
  30. namespace gui
  31. {
  32.     constexpr int WIDTH = 500;
  33.     constexpr int HEIGHT = 300;
  34.  
  35.     inline bool exit = true;
  36.  
  37.     inline HWND window = nullptr;
  38.     inline WNDCLASSEXA windowClass = {};
  39.  
  40.     // Position of window
  41.     inline POINTS position = {};
  42.  
  43.     inline PDIRECT3D9 d3d = nullptr;
  44.     inline LPDIRECT3DDEVICE9 device = nullptr;
  45.     inline D3DPRESENT_PARAMETERS presentParameters = {};
  46.  
  47.     auto createWindow(
  48.         const char* windowName,
  49.         const char* className) noexcept -> void;
  50.  
  51.     auto destroyWindow() noexcept -> void;
  52.  
  53.     auto createDevice() noexcept -> bool;
  54.     auto resetDevice() noexcept -> void;
  55.     auto destroyDevice() noexcept -> void;
  56.  
  57.     auto createImGui() noexcept -> void;
  58.     auto destroyImGui() noexcept -> void;
  59.  
  60.     auto beginRender() noexcept -> void;
  61.     auto endRender() noexcept -> void;
  62.     auto render() noexcept -> void;
  63. }
  64.  
  65. #include "gui.h"
  66. #include <windows.h>
  67.  
  68. #include "imgui/imgui.h"
  69. #include "imgui/imgui_impl_dx9.h"
  70. #include "imgui/imgui_impl_win32.h"
  71.  
  72. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND window, UINT message, WPARAM wparam, LPARAM lparam);
  73.  
  74. LRESULT WINAPI WindowProcess(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
  75. {
  76.     if (ImGui_ImplWin32_WndProcHandler(window, message, wparam, lparam))
  77.     {
  78.         return true;
  79.     }
  80.  
  81.     switch (message)
  82.     {
  83.    
  84.     // Called when window is resized
  85.     case WM_SIZE:
  86.         if (gui::device && wparam != SIZE_MINIMIZED)
  87.         {
  88.             gui::presentParameters.BackBufferWidth = LOWORD(lparam);
  89.             gui::presentParameters.BackBufferHeight = HIWORD(lparam);
  90.             gui::resetDevice();
  91.         }
  92.         return 0;
  93.  
  94.     case WM_SYSCOMMAND:
  95.         if ((wparam & 0xfff0) == SC_KEYMENU)
  96.         {
  97.             return 0;
  98.         }
  99.  
  100.     case WM_DESTROY:
  101.         PostQuitMessage(0);
  102.         return 0;
  103.  
  104.     case WM_LBUTTONDOWN:
  105.         gui::position = MAKEPOINTS(lparam);
  106.         return 0;
  107.  
  108.     case WM_MOUSEMOVE:
  109.         if (wparam == MK_LBUTTON)
  110.         {
  111.             const auto points = MAKEPOINTS(lparam);
  112.             auto rect = ::RECT{};
  113.  
  114.             GetWindowRect(gui::window, &rect);
  115.  
  116.             rect.left += points.x - gui::position.x;
  117.             rect.left += points.y - gui::position.y;
  118.  
  119.             if (gui::position.x >= 0 &&
  120.                 gui::position.x <= gui::WIDTH &&
  121.                 gui::position.y >= 0 && gui::position.y <= 19)
  122.             {
  123.                 SetWindowPos(
  124.                     gui::window,
  125.                     HWND_TOPMOST,
  126.                     rect.left,
  127.                     rect.top,
  128.                     0, 0,
  129.                     SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER
  130.                 );
  131.             }
  132.         }
  133.         return 0;
  134.     }
  135. }
  136.  
  137. auto gui::createWindow(const char* windowName, const char* className) noexcept -> void
  138. {
  139.     windowClass.cbSize = sizeof(WNDCLASSEXA);
  140.     windowClass.style = CS_CLASSDC;
  141.     windowClass.lpfnWndProc = WindowProcess;
  142.     windowClass.cbClsExtra = 0;
  143.     windowClass.cbWndExtra = 0;
  144.     windowClass.hInstance = GetModuleHandleA(0);
  145.     windowClass.hIcon = 0;
  146.     windowClass.hbrBackground = 0;
  147.     windowClass.lpszMenuName = 0;
  148.     windowClass.lpszClassName = className;
  149.     windowClass.hIconSm = 0;
  150.  
  151.     RegisterClassEx(&windowClass);
  152.  
  153.     window = CreateWindowEx(
  154.         0,
  155.         className,
  156.         windowName,
  157.         WS_POPUP,
  158.         100,
  159.         100,
  160.         WIDTH,
  161.         HEIGHT,
  162.         0,
  163.         0,
  164.         windowClass.hInstance,
  165.         0
  166.     );
  167.  
  168.     ShowWindow(window, SW_SHOWDEFAULT);
  169.     UpdateWindow(window);
  170. }
  171.  
  172. auto gui::destroyWindow() noexcept -> void
  173. {
  174.     DestroyWindow(window);
  175.     UnregisterClass(windowClass.lpszClassName, windowClass.hInstance);
  176. }
  177.  
  178. auto gui::createDevice() noexcept -> bool
  179. {
  180.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  181.  
  182.     if (!d3d)
  183.     {
  184.         return false;
  185.     }
  186.  
  187.     // Create space in memory
  188.     ZeroMemory(&presentParameters, sizeof(presentParameters));
  189.  
  190.     presentParameters.Windowed = TRUE;
  191.     presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
  192.     presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
  193.     presentParameters.EnableAutoDepthStencil = TRUE;
  194.     presentParameters.AutoDepthStencilFormat = D3DFMT_D16;
  195.     presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  196.  
  197.     if (d3d->CreateDevice(
  198.         D3DADAPTER_DEFAULT,
  199.         D3DDEVTYPE_HAL,
  200.         window,
  201.         D3DCREATE_HARDWARE_VERTEXPROCESSING,
  202.         &presentParameters,
  203.         &device) < 0)
  204.     {
  205.         return false;
  206.     }
  207.    
  208.     return true;
  209. }
  210.  
  211. auto gui::resetDevice() noexcept -> void
  212. {
  213.     ImGui_ImplDX9_InvalidateDeviceObjects();
  214.  
  215.     const auto result = device->Reset(&presentParameters);
  216.  
  217.     if (result == D3DERR_INVALIDCALL)
  218.     {
  219.         IM_ASSERT(0);
  220.     }
  221.  
  222.     ImGui_ImplDX9_CreateDeviceObjects();
  223. }
  224.  
  225. auto gui::destroyDevice() noexcept -> void
  226. {
  227.     if (device)
  228.     {
  229.         device->Release();
  230.         device = nullptr;
  231.     }
  232.  
  233.     if (d3d)
  234.     {
  235.         d3d->Release();
  236.         d3d = nullptr;
  237.     }
  238. }
  239.  
  240. auto gui::createImGui() noexcept -> void
  241. {
  242.     IMGUI_CHECKVERSION();
  243.     ImGui::CreateContext();
  244.     ImGuiIO& io = ::ImGui::GetIO();
  245.  
  246.     io.IniFilename = NULL;
  247.  
  248.     ImGui::StyleColorsDark();
  249.  
  250.     ImGui_ImplWin32_Init(window);
  251.     ImGui_ImplDX9_Init(device);
  252. }
  253.  
  254. auto gui::destroyImGui() noexcept -> void
  255. {
  256.     ImGui_ImplDX9_Shutdown();
  257.     ImGui_ImplWin32_Shutdown();
  258.     ImGui::DestroyContext();
  259. }
  260.  
  261. auto gui::beginRender() noexcept -> void
  262. {
  263.     MSG message;
  264.     while (PeekMessage(&message, 0, 0, 0, PM_REMOVE))
  265.     {
  266.         TranslateMessage(&message);
  267.         DispatchMessage(&message);
  268.     }
  269.  
  270.     ImGui_ImplDX9_NewFrame();
  271.     ImGui_ImplWin32_NewFrame();
  272.     ImGui::NewFrame();
  273. }
  274.  
  275. auto gui::endRender() noexcept -> void
  276. {
  277.     ImGui::EndFrame();
  278.     device->SetRenderState(D3DRS_ZENABLE, FALSE);
  279.     device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
  280.     device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
  281.  
  282.     device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0);
  283.  
  284.     if (device->BeginScene() >= 0)
  285.     {
  286.         ImGui::Render();
  287.         ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
  288.         device->EndScene();
  289.     }
  290.  
  291.     const auto result = device->Present(0, 0, 0, 0);
  292.  
  293.     if (result == D3DERR_DEVICELOST && device->TestCooperativeLevel() == D3DERR_DEVICENOTRESET)
  294.     {
  295.         resetDevice();
  296.     }
  297. }
  298.  
  299. auto gui::render() noexcept -> void
  300. {
  301.     ImGui::SetNextWindowPos({ 0, 0 });
  302.     ImGui::SetNextWindowSize({ WIDTH, HEIGHT });
  303.     ImGui::Begin(
  304.         "lol",
  305.         &exit,
  306.         ImGuiWindowFlags_NoResize |
  307.         ImGuiWindowFlags_NoSavedSettings |
  308.         ImGuiWindowFlags_NoCollapse |
  309.         ImGuiWindowFlags_NoMove
  310.     );
  311.  
  312.     ImGui::Button("enable");
  313.     ImGui::End();
  314. }
  315.  
Advertisement
Add Comment
Please, Sign In to add comment