-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
121 lines (102 loc) · 2.49 KB
/
Window.cpp
File metadata and controls
121 lines (102 loc) · 2.49 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
#include "Window.h"
#include <memory>
Window* window;
#define WINDOW_GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#define WINDOW_GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
exit(0);
return 0;
}
case WM_CLOSE:
{
PostQuitMessage(0);
exit(0);
return 0;
}
case WM_KEYDOWN:
{
window->keys[(unsigned int)wParam] = true;
return 0;
}
case WM_KEYUP:
{
window->keys[(unsigned int)wParam] = false;
return 0;
}
case WM_LBUTTONDOWN:
{
window->updateMouse(WINDOW_GET_X_LPARAM(lParam), WINDOW_GET_Y_LPARAM(lParam));
window->mouseButtons[0] = true;
return 0;
}
case WM_LBUTTONUP:
{
window->updateMouse(WINDOW_GET_X_LPARAM(lParam), WINDOW_GET_Y_LPARAM(lParam));
window->mouseButtons[0] = false;
return 0;
}
case WM_MOUSEMOVE:
{
window->updateMouse(WINDOW_GET_X_LPARAM(lParam), WINDOW_GET_Y_LPARAM(lParam));
return 0;
}
case WM_SETFOCUS:
{
ShowCursor(false);
break;
}
case WM_KILLFOCUS:
{
ShowCursor(true);
break;
}
default:
{
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
};
void Window::init(const std::string window_name , int window_width , int window_height ) {
width = window_width;
height = window_height;
style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
WNDCLASSEX wc;
hinstance = GetModuleHandle(NULL);
name = window_name;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
std::wstring wname = std::wstring(name.begin(), name.end());
wc.lpszClassName = wname.c_str();
wc.cbSize = sizeof(WNDCLASSEX);
RegisterClassEx(&wc);
hwnd = CreateWindowEx(WS_EX_APPWINDOW, wname.c_str(), wname.c_str(), style, 0, 0, width, height, NULL, NULL, hinstance, this);
//hwnd = CreateWindowEx(WS_EX_APPWINDOW, wname.c_str(), wname.c_str(), style, window_x, window_y, width, height, NULL, NULL, hinstance, this);
memset(keys, 0, 256 * sizeof(char));
window = this;
}
void Window::processMessages() {
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Checks if a specific key is currently pressed
bool Window::keyPressed(int key)
{
return keys[key];
}