ApplicationWindowWin.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Window/ApplicationWindowWin.h>
  6. #include <Utils/Log.h>
  7. #include <shellscalingapi.h>
  8. static ApplicationWindowWin *sWindow = nullptr;
  9. // Called every time the application receives a message
  10. static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  11. {
  12. PAINTSTRUCT ps;
  13. switch (message)
  14. {
  15. case WM_PAINT:
  16. BeginPaint(hWnd, &ps);
  17. EndPaint(hWnd, &ps);
  18. break;
  19. case WM_SIZE:
  20. if (sWindow != nullptr)
  21. {
  22. // Get new window size
  23. RECT rc;
  24. GetClientRect(hWnd, &rc);
  25. int width = max<int>(rc.right - rc.left, 8);
  26. int height = max<int>(rc.bottom - rc.top, 8);
  27. sWindow->OnWindowResized(width, height);
  28. }
  29. break;
  30. case WM_DESTROY:
  31. PostQuitMessage(0);
  32. break;
  33. default:
  34. return DefWindowProc(hWnd, message, wParam, lParam);
  35. }
  36. return 0;
  37. }
  38. void ApplicationWindowWin::Initialize()
  39. {
  40. // Prevent this window from auto scaling
  41. SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
  42. // Register class
  43. WNDCLASSEX wcex;
  44. wcex.cbSize = sizeof(WNDCLASSEX);
  45. wcex.style = CS_HREDRAW | CS_VREDRAW;
  46. wcex.lpfnWndProc = WndProc;
  47. wcex.cbClsExtra = 0;
  48. wcex.cbWndExtra = 0;
  49. wcex.hInstance = GetModuleHandle(nullptr);
  50. wcex.hIcon = nullptr;
  51. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  52. wcex.hbrBackground = nullptr;
  53. wcex.lpszMenuName = nullptr;
  54. wcex.lpszClassName = TEXT("TestFrameworkClass");
  55. wcex.hIconSm = nullptr;
  56. if (!RegisterClassEx(&wcex))
  57. FatalError("Failed to register window class");
  58. // Create window
  59. RECT rc = { 0, 0, mWindowWidth, mWindowHeight };
  60. AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
  61. mhWnd = CreateWindow(TEXT("TestFrameworkClass"), TEXT("TestFramework"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  62. rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, wcex.hInstance, nullptr);
  63. if (!mhWnd)
  64. FatalError("Failed to create window");
  65. // Show window
  66. ShowWindow(mhWnd, SW_SHOW);
  67. // Store the window pointer for the message loop
  68. sWindow = this;
  69. }
  70. void ApplicationWindowWin::MainLoop(RenderCallback inRenderCallback)
  71. {
  72. for (;;)
  73. {
  74. // Main message loop
  75. MSG msg = {};
  76. while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
  77. {
  78. TranslateMessage(&msg);
  79. DispatchMessage(&msg);
  80. if (msg.message == WM_QUIT)
  81. {
  82. // Handle quit events
  83. return;
  84. }
  85. }
  86. // Call the render callback
  87. if (!inRenderCallback())
  88. return;
  89. }
  90. }