Haio 0.2.X
Homebrew Asset IO/CDN
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
8namespace Haio {
9
11 HWND hwnd = nullptr;
12 HINSTANCE hInstance = nullptr;
13 bool running = false;
14};
15
16WindowsWindow::WindowsWindow(const char* title, int width, int height)
17 : title(title), width(width), height(height) {
18 p = std::make_unique<Impl>();
19}
20
24
25static 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
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
84 if (p->hwnd) {
85 DestroyWindow(p->hwnd);
86 p->hwnd = nullptr;
87 }
88 p->running = false;
89}
90
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
105 return !p->running;
106}
107
108}
~WindowsWindow() override
Definition window.cpp:21
WindowsWindow(const char *title, int width, int height)
Definition window.cpp:16
void init() override
Definition window.cpp:53
bool shouldClose() const override
Definition window.cpp:104
void shutdown() override
Definition window.cpp:83
void pollEvents() override
Definition window.cpp:91
Definition haio.hpp:11