-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cpp
More file actions
200 lines (163 loc) · 3.95 KB
/
Copy pathEngine.cpp
File metadata and controls
200 lines (163 loc) · 3.95 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// DO NOT MODIFY THIS FILE
//
#define WIN32_LEAN_AND_MEAN
#include "Engine.h"
#include <windows.h>
#include <stdlib.h>
uint32_t buffer[SCREEN_HEIGHT][SCREEN_WIDTH] = { 0 };
static HINSTANCE hinst = 0;
static DWORD ticks = 0;
static bool is_active = true;
static POINT cursor_pos;
static bool quited = false;
static LARGE_INTEGER qpc_frequency = { 0 };
static LARGE_INTEGER qpc_ref_time = { 0 };
bool is_window_active()
{
return is_active;
}
void clear_buffer()
{
memset(buffer, 0, sizeof(buffer));
}
bool is_key_pressed(int button_vk_code)
{
return is_active && (GetAsyncKeyState(button_vk_code) & 0x8000);
}
bool is_mouse_button_pressed(int mouse_button_index)
{
if (!is_active)
return false;
if (mouse_button_index == 0)
return GetAsyncKeyState(VK_LBUTTON) != 0;
if (mouse_button_index == 1)
return GetAsyncKeyState(VK_RBUTTON) != 0;
return false;
}
int get_cursor_x()
{
return cursor_pos.x;
}
int get_cursor_y()
{
return cursor_pos.y;
}
void schedule_quit_game()
{
quited = true;
}
static void CALLBACK update_proc(HWND hwnd)
{
if (quited)
return;
is_active = GetActiveWindow() == hwnd;
GetCursorPos(&cursor_pos);
ScreenToClient(hwnd, &cursor_pos);
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
float dt = float((unsigned long)(t.QuadPart - qpc_ref_time.QuadPart)) / qpc_frequency.QuadPart;
if (dt > 0.1f)
dt = 0.1f;
act(dt);
if (!quited)
{
draw();
RedrawWindow(hwnd, NULL, 0, RDW_INVALIDATE | RDW_UPDATENOW);
}
qpc_ref_time = t;
}
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
BITMAPINFOHEADER bih;
bih.biSize = sizeof(bih);
bih.biWidth = SCREEN_WIDTH;
bih.biHeight = -SCREEN_HEIGHT;
bih.biPlanes = 1;
bih.biBitCount = 32;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 96;
bih.biYPelsPerMeter = 96;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
SetDIBitsToDevice(
hdc,
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
0, 0,
0, SCREEN_HEIGHT,
buffer,
(BITMAPINFO*)&bih,
DIB_RGB_COLORS);
EndPaint(hwnd, &ps);
}
break;
case WM_QUIT:
case WM_DESTROY:
quited = true;
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
SetProcessDPIAware();
hinst = hInstance;
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
WNDCLASSEXA wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wcex.lpszMenuName = 0;
wcex.lpszClassName = "GameTemplateWndClass";
wcex.hIconSm = 0;
RegisterClassExA(&wcex);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = SCREEN_WIDTH;
rect.bottom = SCREEN_HEIGHT;
AdjustWindowRectEx(&rect, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, FALSE, 0);
HWND hwnd = CreateWindowA(wcex.lpszClassName, "Game", WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT, 0, rect.right - rect.left, rect.bottom - rect.top, nullptr, nullptr, hInstance, nullptr);
if (!hwnd)
return 0;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
QueryPerformanceFrequency(&qpc_frequency);
QueryPerformanceCounter(&qpc_ref_time);
ticks = GetTickCount();
initialize();
MSG msg;
while (!quited)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
update_proc(hwnd);
Sleep(0);
}
finalize();
return (int)msg.wParam;
}