Repository navigation
Expand file tree
/
Copy pathdoomgeneric_win.c
More file actions
292 lines (245 loc) · 5.81 KB
/
Copy pathdoomgeneric_win.c
File metadata and controls
292 lines (245 loc) · 5.81 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "doomkeys.h"
#include "doomgeneric.h"
#include <stdio.h>
#include <Windows.h>
#define BLOCKS_ROW_SIZE 8
#define BLOCK_SIZE (DOOMGENERIC_RESX / BLOCKS_ROW_SIZE)
static uint32_t blockBuffer[BLOCK_SIZE * BLOCK_SIZE] = { 0 };
static BITMAPINFO bmi = { sizeof(BITMAPINFOHEADER), BLOCK_SIZE, -BLOCK_SIZE, 1, 32 };
static NOTIFYICONDATA nid = { sizeof(NOTIFYICONDATA), 0, 0, NIF_ICON };
static ICONINFO ii = { TRUE };
static HHOOK hook = 0;
static HWND s_Hwnd = 0;
static HDC s_Hdc = 0;
#define KEYQUEUE_SIZE 16
static unsigned short s_KeyQueue[KEYQUEUE_SIZE];
static unsigned int s_KeyQueueWriteIndex = 0;
static unsigned int s_KeyQueueReadIndex = 0;
static unsigned char convertToDoomKey(unsigned char key)
{
switch (key)
{
case VK_RETURN:
key = KEY_ENTER;
break;
case VK_DELETE: // escape closes the tray
key = KEY_ESCAPE;
break;
case VK_LEFT:
key = KEY_LEFTARROW;
break;
case VK_RIGHT:
key = KEY_RIGHTARROW;
break;
case VK_UP:
key = KEY_UPARROW;
break;
case VK_DOWN:
key = KEY_DOWNARROW;
break;
case VK_CONTROL:
key = KEY_FIRE;
break;
case VK_SPACE:
key = KEY_USE;
break;
case VK_SHIFT:
key = KEY_RSHIFT;
break;
default:
key = tolower(key);
break;
}
return key;
}
static void addKeyToQueue(int pressed, unsigned char keyCode)
{
unsigned char key = convertToDoomKey(keyCode);
unsigned short keyData = (pressed << 8) | key;
s_KeyQueue[s_KeyQueueWriteIndex] = keyData;
s_KeyQueueWriteIndex++;
s_KeyQueueWriteIndex %= KEYQUEUE_SIZE;
}
static void cleanup()
{
for (nid.uID = 0; nid.uID < BLOCKS_ROW_SIZE * BLOCKS_ROW_SIZE; nid.uID++)
{
Shell_NotifyIcon(NIM_DELETE, &nid);
}
DeleteObject(ii.hbmMask);
DeleteObject(ii.hbmColor);
}
BOOL WINAPI ctrlHandler(DWORD event)
{
switch (event)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
cleanup();
PostQuitMessage(0);
ExitProcess(0);
default:
break;
}
return FALSE;
}
LRESULT CALLBACK keyboardProc(const int code, const WPARAM wParam, const LPARAM lParam)
{
switch (wParam)
{
case WM_KEYDOWN:
addKeyToQueue(1, ((KBDLLHOOKSTRUCT *)lParam)->vkCode);
break;
case WM_KEYUP:
addKeyToQueue(0, ((KBDLLHOOKSTRUCT *)lParam)->vkCode);
break;
default:
break;
}
return CallNextHookEx(hook, code, wParam, lParam);
}
static LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
cleanup();
PostQuitMessage(0);
ExitProcess(0);
break;
case WM_KEYDOWN:
addKeyToQueue(1, wParam);
break;
case WM_KEYUP:
addKeyToQueue(0, wParam);
break;
default:
return DefWindowProcA(hwnd, msg, wParam, lParam);
}
return 0;
}
void DG_Init()
{
// window creation
const char windowClassName[] = "DoomWindowClass";
const char windowTitle[] = "Doom";
WNDCLASSEXA wc;
wc.cbSize = sizeof(WNDCLASSEXA);
wc.style = 0;
wc.lpfnWndProc = wndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = 0;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
wc.lpszClassName = windowClassName;
wc.hIconSm = 0;
if (!RegisterClassExA(&wc))
{
printf("Window Registration Failed!");
exit(-1);
}
s_Hwnd = CreateWindowExA(0, windowClassName, windowTitle, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if (!s_Hwnd)
{
printf("Window Creation Failed!");
exit(-1);
}
s_Hdc = GetDC(s_Hwnd);
memset(s_KeyQueue, 0, KEYQUEUE_SIZE * sizeof(unsigned short));
ii.hbmMask = CreateBitmap(BLOCK_SIZE, BLOCK_SIZE, 1, 1, 0);
ii.hbmColor = CreateCompatibleBitmap(s_Hdc, BLOCK_SIZE, BLOCK_SIZE);
SetDIBits(s_Hdc, ii.hbmColor, 0, BLOCK_SIZE, blockBuffer, &bmi, DIB_RGB_COLORS);
nid.hWnd = s_Hwnd;
Shell_NotifyIcon(NIM_ADD, &nid);
Shell_NotifyIcon(NIM_DELETE, &nid); // HACK: the first icon added often has its position set wrong, so we delete it immediately
for (nid.uID = 0; nid.uID < BLOCKS_ROW_SIZE * BLOCKS_ROW_SIZE; nid.uID++)
{
//nid.hIcon = CreateIconIndirect(&ii); // this usually reverses the order of icons
Shell_NotifyIcon(NIM_ADD, &nid);
//DestroyIcon(nid.hIcon);
}
}
void DG_DrawFrame()
{
MSG msg;
memset(&msg, 0, sizeof(msg));
while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE) > 0)
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
for (int i = 0; i < BLOCKS_ROW_SIZE; i++)
{
for (int j = 0; j < BLOCKS_ROW_SIZE; j++)
{
int blockStart = i * BLOCK_SIZE * DOOMGENERIC_RESX + j * BLOCK_SIZE;
if (blockStart >= DOOMGENERIC_RESX * DOOMGENERIC_RESY)
return; // no need to update empty icons
// copy block pixels
for (int k = 0; k < BLOCK_SIZE; k++)
{
int rowStart = blockStart + k * DOOMGENERIC_RESX;
if (rowStart < DOOMGENERIC_RESX * DOOMGENERIC_RESY)
memcpy(blockBuffer + k * BLOCK_SIZE, DG_ScreenBuffer + rowStart, BLOCK_SIZE * sizeof(uint32_t));
else
memset(blockBuffer + k * BLOCK_SIZE, 0, BLOCK_SIZE * sizeof(uint32_t));
}
// update icon
SetDIBits(s_Hdc, ii.hbmColor, 0, BLOCK_SIZE, blockBuffer, &bmi, DIB_RGB_COLORS);
nid.uID = i * BLOCKS_ROW_SIZE + j;
nid.hIcon = CreateIconIndirect(&ii);
Shell_NotifyIcon(NIM_MODIFY, &nid);
DestroyIcon(nid.hIcon);
}
}
}
void DG_SleepMs(uint32_t ms)
{
Sleep(ms);
}
uint32_t DG_GetTicksMs()
{
return GetTickCount();
}
int DG_GetKey(int *pressed, unsigned char *doomKey)
{
if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex)
{
//key queue is empty
return 0;
}
else
{
unsigned short keyData = s_KeyQueue[s_KeyQueueReadIndex];
s_KeyQueueReadIndex++;
s_KeyQueueReadIndex %= KEYQUEUE_SIZE;
*pressed = keyData >> 8;
*doomKey = keyData & 0xFF;
return 1;
}
}
void DG_SetWindowTitle(const char *title)
{
if (s_Hwnd)
{
SetWindowTextA(s_Hwnd, title);
}
}
int main(int argc, char **argv)
{
SetConsoleCtrlHandler(ctrlHandler, TRUE);
hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardProc, NULL, 0);
doomgeneric_Create(argc, argv);
for (int i = 0; ; i++)
{
doomgeneric_Tick();
}
return 0;
}