Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
Loading...
Searching...
No Matches
window.cpp
Go to the documentation of this file.
1
#define WIN32_LEAN_AND_MEAN
2
#define NOMINMAX
3
#include <windows.h>
4
5
#include "
haio_common.hpp
"
6
#include "
window.hpp
"
7
8
namespace
Haio
{
9
10
struct
WindowsWindow::Impl
{
11
HWND
hwnd
=
nullptr
;
12
HINSTANCE
hInstance
=
nullptr
;
13
bool
running
=
false
;
14
};
15
16
WindowsWindow::WindowsWindow
(
const
char
* title,
int
width,
int
height)
17
: title(title), width(width), height(height) {
18
p = std::make_unique<Impl>();
19
}
20
21
WindowsWindow::~WindowsWindow
() {
22
shutdown
();
23
}
24
25
static
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
26
27
if
(msg == WM_NCCREATE) {
28
CREATESTRUCT* cs = (CREATESTRUCT*)lParam;
29
WindowsWindow
* window = (
WindowsWindow
*)cs->lpCreateParams;
30
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window);
31
}
32
33
auto
* window = (WindowsWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
34
35
switch
(msg) {
36
37
case
WM_DESTROY:
38
PostQuitMessage(0);
39
return
0;
40
41
case
WM_PAINT: {
42
PAINTSTRUCT ps;
43
HDC hdc = BeginPaint(hwnd, &ps);
44
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
45
EndPaint(hwnd, &ps);
46
return
0;
47
}
48
}
49
50
return
DefWindowProc(hwnd, msg, wParam, lParam);
51
}
52
53
void
WindowsWindow::init
() {
54
p->hInstance = GetModuleHandle(
nullptr
);
55
56
const
char
* CLASS_NAME =
"HaioWindowClass"
;
57
58
WNDCLASS wc = {};
59
wc.lpfnWndProc = Haio::WindowProc;
60
wc.hInstance = p->hInstance;
61
wc.lpszClassName = CLASS_NAME;
62
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
63
64
RegisterClass(&wc);
65
66
p->hwnd = CreateWindowEx(
67
0,
68
CLASS_NAME,
69
title,
70
WS_OVERLAPPEDWINDOW,
71
CW_USEDEFAULT, CW_USEDEFAULT,
72
width, height,
73
nullptr
,
74
nullptr
,
75
p->hInstance,
76
this
77
);
78
79
ShowWindow(p->hwnd, SW_SHOW);
80
p->running =
true
;
81
}
82
83
void
WindowsWindow::shutdown
() {
84
if
(p->hwnd) {
85
DestroyWindow(p->hwnd);
86
p->hwnd =
nullptr
;
87
}
88
p->running =
false
;
89
}
90
91
void
WindowsWindow::pollEvents
() {
92
MSG msg = {};
93
94
while
(PeekMessage(&msg,
nullptr
, 0, 0, PM_REMOVE)) {
95
TranslateMessage(&msg);
96
DispatchMessage(&msg);
97
98
if
(msg.message == WM_QUIT) {
99
p->running =
false
;
100
}
101
}
102
}
103
104
bool
WindowsWindow::shouldClose
()
const
{
105
return
!p->running;
106
}
107
108
}
Haio::WindowsWindow
Definition
window.hpp:7
Haio::WindowsWindow::~WindowsWindow
~WindowsWindow() override
Definition
window.cpp:21
Haio::WindowsWindow::WindowsWindow
WindowsWindow(const char *title, int width, int height)
Definition
window.cpp:16
Haio::WindowsWindow::init
void init() override
Definition
window.cpp:53
Haio::WindowsWindow::Impl::running
bool running
Definition
window.cpp:13
Haio::WindowsWindow::shouldClose
bool shouldClose() const override
Definition
window.cpp:104
Haio::WindowsWindow::shutdown
void shutdown() override
Definition
window.cpp:83
Haio::WindowsWindow::Impl::hInstance
HINSTANCE hInstance
Definition
window.cpp:12
Haio::WindowsWindow::pollEvents
void pollEvents() override
Definition
window.cpp:91
Haio::WindowsWindow::Impl::hwnd
HWND hwnd
Definition
window.cpp:11
Haio::WindowsWindow::Impl
Definition
window.cpp:10
haio_common.hpp
Haio
Definition
haio.hpp:11
window.hpp
source
platform
microslop
window.cpp
Generated by
1.18.0