|
| 1 | +#include <string> |
| 2 | + |
| 3 | +#include <Utils/Logger.hpp> |
| 4 | + |
| 5 | +#include "Win32/Win32.hpp" |
| 6 | + |
| 7 | +LRESULT CALLBACK cae::Win32::WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 8 | +{ |
| 9 | + auto* self = reinterpret_cast<Win32*>(GetWindowLongPtr(hwnd, GWLP_USERDATA)); |
| 10 | + |
| 11 | + switch (msg) |
| 12 | + { |
| 13 | + case WM_CLOSE: |
| 14 | + if (self) self->m_shouldClose = true; |
| 15 | + return 0; |
| 16 | + case WM_SIZE: |
| 17 | + if (self) |
| 18 | + { |
| 19 | + self->m_frameBufferResized = true; |
| 20 | + self->m_frameBufferSize.width = LOWORD(lParam); |
| 21 | + self->m_frameBufferSize.height = HIWORD(lParam); |
| 22 | + } |
| 23 | + return 0; |
| 24 | + case WM_DESTROY: |
| 25 | + PostQuitMessage(0); |
| 26 | + return 0; |
| 27 | + default: |
| 28 | + return DefWindowProc(hwnd, msg, wParam, lParam); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +bool cae::Win32::create(const std::string &name, WindowSize size) |
| 33 | +{ |
| 34 | + m_hInstance = GetModuleHandle(nullptr); |
| 35 | + m_frameBufferSize = size; |
| 36 | + m_shouldClose = false; |
| 37 | + m_frameBufferResized = false; |
| 38 | + |
| 39 | + WNDCLASSW wc{}; |
| 40 | + wc.lpfnWndProc = WindowProc; |
| 41 | + wc.hInstance = m_hInstance; |
| 42 | + wc.lpszClassName = L"CAE_WindowsWindowClass"; |
| 43 | + wc.hCursor = LoadCursor(nullptr, IDC_ARROW); |
| 44 | + wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); |
| 45 | + wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1); |
| 46 | + |
| 47 | + if (!RegisterClassW(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) |
| 48 | + { |
| 49 | + utl::Logger::log("Failed to register window class", utl::LogLevel::WARNING); |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + m_hwnd = CreateWindowEx( |
| 54 | + 0, |
| 55 | + reinterpret_cast<LPCSTR>(L"CAE_WindowsWindowClass"), |
| 56 | + reinterpret_cast<LPCSTR>(std::wstring(name.begin(), name.end()).c_str()), |
| 57 | + WS_OVERLAPPEDWINDOW, |
| 58 | + CW_USEDEFAULT, CW_USEDEFAULT, |
| 59 | + size.width, size.height, |
| 60 | + nullptr, |
| 61 | + nullptr, |
| 62 | + m_hInstance, |
| 63 | + nullptr |
| 64 | + ); |
| 65 | + |
| 66 | + if (!m_hwnd) |
| 67 | + { |
| 68 | + utl::Logger::log("Failed to create Win32 window", utl::LogLevel::WARNING); |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + SetWindowLongPtr(m_hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); |
| 73 | + ShowWindow(m_hwnd, SW_SHOW); |
| 74 | + UpdateWindow(m_hwnd); |
| 75 | + |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +void cae::Win32::close() |
| 80 | +{ |
| 81 | + if (m_hwnd) |
| 82 | + { |
| 83 | + DestroyWindow(m_hwnd); |
| 84 | + m_hwnd = nullptr; |
| 85 | + } |
| 86 | + UnregisterClass(reinterpret_cast<LPCSTR>(L"CAE_WindowsWindowClass"), m_hInstance); |
| 87 | +} |
| 88 | + |
| 89 | + cae::NativeWindowHandle cae::Win32::getNativeHandle() const |
| 90 | +{ |
| 91 | + return {.window = m_hwnd, .display = m_hInstance}; |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | + cae::WindowSize cae::Win32::getWindowSize() const |
| 96 | +{ |
| 97 | + RECT rect{}; |
| 98 | + GetClientRect(m_hwnd, &rect); |
| 99 | + return {.width = static_cast<uint16_t>(rect.right - rect.left), |
| 100 | + .height = static_cast<uint16_t>(rect.bottom - rect.top)}; |
| 101 | +} |
| 102 | + |
| 103 | + bool cae::Win32::setIcon(const std::string &path) const |
| 104 | +{ |
| 105 | + return false; |
| 106 | +} |
| 107 | + |
| 108 | +void cae::Win32::pollEvents() |
| 109 | +{ |
| 110 | + MSG msg{}; |
| 111 | + while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) |
| 112 | + { |
| 113 | + if (msg.message == WM_QUIT) |
| 114 | + { |
| 115 | + m_shouldClose = true; |
| 116 | + } |
| 117 | + TranslateMessage(&msg); |
| 118 | + DispatchMessage(&msg); |
| 119 | + } |
| 120 | +} |
0 commit comments