Advertisement
LxrdKxnny

WindowsWindow.cpp

May 9th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include "Windows/WindowsWindow.h"
  2. #include "Windows/WindowsInput.h"
  3. #include "Logging/LogMacros.h"
  4. #include "D3D11Device.h"
  5.  
  6. #include "imgui.h"
  7.  
  8. WNDCLASS FWindowsWindow::WindowClass = {};
  9.  
  10. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  11.  
  12. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  13. {
  14.     if (ImGui_ImplWin32_WndProcHandler(hwnd, uMsg, wParam, lParam))
  15.     {
  16.         return true;
  17.     }
  18.  
  19.     switch (uMsg)
  20.     {
  21.         case WM_SIZE:
  22.         {
  23.             if (FD3D11Device::GetDevice()->GetID3D11Device() != nullptr && wParam != SIZE_MINIMIZED)
  24.             {
  25.                 FD3D11RenderTarget::GetRenderTarget()->CleanupRenderTarget();
  26.                 FD3D11Device::GetDevice()->GetIDXGISwapChain()->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
  27.                 FD3D11RenderTarget::CreateRenderTarget(FD3D11Device::GetDevice()->GetID3D11Device());
  28.             }
  29.  
  30.             return 0;
  31.         }
  32.         case WM_SYSCOMMAND:
  33.         {
  34.             if ((wParam & 0xfff0) == SC_KEYMENU)
  35.             {
  36.                 return 0;
  37.             }
  38.  
  39.             break;
  40.         }
  41.         case WM_DESTROY:
  42.         {
  43.             PostQuitMessage(0);
  44.             return 0;
  45.         }
  46.         case WM_DPICHANGED:
  47.         {
  48.             if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports)
  49.             {
  50.                 const RECT* SuggestedRect = (RECT*)lParam;
  51.                 SetWindowPos(hwnd, NULL, SuggestedRect->left, SuggestedRect->top, SuggestedRect->right - SuggestedRect->left, SuggestedRect->bottom - SuggestedRect->top, SWP_NOZORDER | SWP_NOACTIVATE);
  52.             }
  53.  
  54.             break;
  55.         }
  56.     }
  57.  
  58.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  59. }
  60.  
  61. std::unique_ptr<FWindowsWindow> FWindowsWindow::Create()
  62. {
  63.     return std::make_unique<FWindowsWindow>();
  64. }
  65.  
  66. void FWindowsWindow::Initialize(HINSTANCE InHInstance, HINSTANCE InHPrevInstance, const bool bShowImmediately)
  67. {
  68.     WindowClass.lpfnWndProc = WindowProc;
  69.     WindowClass.hInstance = InHInstance;
  70.     WindowClass.lpszClassName = "FrostWindow";
  71.     WindowClass.hCursor = LoadCursor(InHInstance, IDC_ARROW);
  72.  
  73.     RegisterClass(&WindowClass);
  74.  
  75.     RECT AvailableArea;
  76.     SystemParametersInfo(SPI_GETWORKAREA, 0, &AvailableArea, 0);
  77.  
  78.     ScreenWidth = AvailableArea.right - AvailableArea.left;
  79.     ScreenHeight = AvailableArea.bottom - AvailableArea.top;
  80.  
  81.     WindowWidth = 1280;
  82.     WindowHeight = 720;
  83.  
  84.     Hwnd = CreateWindowEx(
  85.             0,
  86.             "FrostWindow",
  87.             "Frost Game - Scene - Windows, Mac & Linux - Frost Editor 2023.1.0 <DX11>",
  88.             WS_POPUP | WS_MAXIMIZEBOX,
  89.             bIsWindowMaximized ? AvailableArea.left : (ScreenWidth - WindowWidth) / 2,
  90.             bIsWindowMaximized ? AvailableArea.top : (ScreenHeight - WindowHeight) / 2,
  91.             bIsWindowMaximized ? ScreenWidth : WindowWidth,
  92.             bIsWindowMaximized ? ScreenHeight : WindowHeight,
  93.             NULL,
  94.             NULL,
  95.             InHInstance,
  96.             NULL);
  97.  
  98.     ShowWindow(Hwnd, bShowImmediately);
  99.     UpdateWindow(Hwnd);
  100.  
  101.     ShowWindow(Hwnd, bIsWindowMaximized ? SW_MAXIMIZE : SW_SHOWMAXIMIZED);
  102.     SetWindowPos(Hwnd, NULL, 0, 0, WindowWidth, WindowHeight, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  103.  
  104.     FD3D11Device::CreateDevice(Hwnd);
  105. }
  106.  
  107. void FWindowsWindow::Start()
  108. {
  109.     Input = std::make_unique<FWindowsInput>(Hwnd);
  110. }
  111.  
  112. void FWindowsWindow::Update()
  113. {
  114.     Input->Update();
  115.  
  116.     FD3D11Device::GetDevice()->GetIDXGISwapChain()->Present(1, 0);
  117. }
  118.  
  119. void FWindowsWindow::Shutdown()
  120. {
  121.     FD3D11Device::GetDevice()->CleanupDevice();
  122.  
  123.     DestroyWindow(Hwnd);
  124.     UnregisterClass(WindowClass.lpszClassName, WindowClass.hInstance);
  125. }
  126.  
  127. void FWindowsWindow::SetWindowMinimized()
  128. {
  129.     ShowWindow(Hwnd, SW_MINIMIZE);
  130. }
  131.  
  132. void FWindowsWindow::SetWindowMaximized(bool bInShouldMaximize)
  133. {
  134.     if (bInShouldMaximize)
  135.     {
  136.         SetWindowLong(Hwnd, GWL_STYLE, WS_POPUP | WS_MAXIMIZEBOX);
  137.         bIsWindowMaximized = true;
  138.     }
  139.     else
  140.     {
  141.         ShowWindow(Hwnd, SW_RESTORE);
  142.         bIsWindowMaximized = false;
  143.     }
  144.  
  145.     ShowWindow(Hwnd, true);
  146. }
  147.  
  148. void FWindowsWindow::SetWindowFullscreen(bool bInShouldFullscreen)
  149. {
  150.     if (bInShouldFullscreen)
  151.     {
  152.         SetWindowLong(Hwnd, GWL_STYLE, WS_POPUP | WS_MAXIMIZE);
  153.         bIsWindowFullscreen = true;
  154.     }
  155.     else
  156.     {
  157.         SetWindowLong(Hwnd, GWL_STYLE, WS_POPUP | WS_MAXIMIZEBOX);
  158.         bIsWindowFullscreen = false;
  159.     }
  160.  
  161.     ShowWindow(Hwnd, true);
  162. }
  163.  
  164. int FWindowsWindow::GetWindowX()
  165. {
  166.     return WindowRect.left;
  167. }
  168.  
  169. int FWindowsWindow::GetWindowY()
  170. {
  171.     return WindowRect.top;
  172. }
  173.  
  174. bool FWindowsWindow::IsWindowMaximized()
  175. {
  176.     return bIsWindowMaximized;
  177. }
  178.  
  179. bool FWindowsWindow::IsWindowFullscreen()
  180. {
  181.     return bIsWindowFullscreen;
  182. }
  183.  
  184. int FWindowsWindow::GetWindowWidth()
  185. {
  186.     return WindowWidth;
  187. }
  188.  
  189. int FWindowsWindow::GetWindowHeight()
  190. {
  191.     return WindowHeight;
  192. }
  193.  
  194. std::unique_ptr<FInput>& FWindowsWindow::GetInput()
  195. {
  196.     return Input;
  197. }
  198.  
  199. HWND FWindowsWindow::GetHwnd()
  200. {
  201.     if (Hwnd)
  202.     {
  203.         return Hwnd;
  204.     }
  205.     else
  206.     {
  207.         FROST_LOG_ERROR("Failed to retrieve 'Hwnd'!");
  208.         return nullptr;
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement