-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin32.cpp
More file actions
125 lines (102 loc) · 3.35 KB
/
win32.cpp
File metadata and controls
125 lines (102 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <string>
#include "Utils/Logger.hpp"
#include "Win32/Win32.hpp"
constexpr wchar_t WINDOW_CLASS_NAME[] = L"CAE_WindowsWindowClass";
LRESULT CALLBACK cae::Win32::WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
Win32 *self = nullptr;
if (msg == WM_NCCREATE)
{
auto *cs = reinterpret_cast<CREATESTRUCTW *>(lParam);
self = static_cast<Win32 *>(cs->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(self));
return TRUE;
}
self = reinterpret_cast<Win32 *>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (msg)
{
case WM_SIZE:
if (self != nullptr)
{
self->m_frameBufferResized = true;
self->m_frameBufferSize = {.width=LOWORD(lParam), .height=HIWORD(lParam)};
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
}
bool cae::Win32::create(const std::string &name, WindowSize size)
{
m_hInstance = GetModuleHandleW(nullptr);
m_frameBufferSize = size;
m_shouldClose = false;
m_frameBufferResized = false;
const int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0);
m_title.resize(len);
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, m_title.data(), len);
if (!m_title.empty() && m_title.back() == L'\0')
{
m_title.pop_back();
}
static bool classRegistered = false;
if (!classRegistered)
{
WNDCLASSW wc{};
wc.lpfnWndProc = WindowProc;
wc.hInstance = m_hInstance;
wc.lpszClassName = WINDOW_CLASS_NAME;
wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wc.hIcon = LoadIconW(nullptr, IDI_APPLICATION);
wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
if (RegisterClassW(&wc) == 0U)
{
utl::Logger::log("Failed to register Win32 window class", utl::LogLevel::WARNING);
return false;
}
classRegistered = true;
}
m_hwnd = CreateWindowExW(0, WINDOW_CLASS_NAME, L"TEST TITLE VISIBLE", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, size.width, size.height, nullptr, nullptr, m_hInstance, this);
if (m_hwnd == nullptr) {
return false;
}
SetWindowTextW(m_hwnd, m_title.c_str());
ShowWindow(m_hwnd, SW_SHOW);
UpdateWindow(m_hwnd);
return true;
}
void cae::Win32::close()
{
if (m_hwnd != nullptr)
{
DestroyWindow(m_hwnd);
m_hwnd = nullptr;
}
UnregisterClassW(WINDOW_CLASS_NAME, m_hInstance);
}
cae::NativeWindowHandle cae::Win32::getNativeHandle() const { return {.window = m_hwnd, .display = m_hInstance}; }
cae::WindowSize cae::Win32::getWindowSize() const
{
RECT rect{};
GetClientRect(m_hwnd, &rect);
return {.width = static_cast<uint16_t>(rect.right - rect.left),
.height = static_cast<uint16_t>(rect.bottom - rect.top)};
}
bool cae::Win32::setIcon(const std::string &path) const { return false; }
void cae::Win32::pollEvents()
{
MSG msg{};
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
m_shouldClose = true;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}