-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathm_gfx_wingdi.c
345 lines (282 loc) · 11.9 KB
/
m_gfx_wingdi.c
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
GDI32 video backend, uses bitmap and StretchDIBits for rendering
*/
#include <windows.h>
#include <string.h>
#include "m_gfx.h"
#include "m_core.h"
// forward definition of palette in BGRA format - RGBQUAD
extern unsigned char GamePalette[1024];
typedef struct tagBITMAPINFO2 {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
} BITMAPINFO2;
BITMAPINFO2 bmp;
HINSTANCE hInst;
HWND hWnd;
MSG msg;
char AppTitle[] = "The Last Mission Win32 GDI Remake";
unsigned char Keys[128];
unsigned char ScreenBuffer[320*200];
unsigned char ScreenBigBuffer[640*400];
int scale2x = 2; // 1 - no scale 320x200; 2 - upscale to 640x400
int WndCreate();
void LM_GFX_Init();
//================================================================
void LM_ResetKeys()
{
memset(&Keys[0], 0, 128);
}
int LM_AnyKey()
{
for(int i = 0; i < 127; i++) {
if(Keys[i] == 1) return 1;
}
return 0;
}
int LM_Timer()
{
return GetTickCount();
}
void LM_Sleep(int sleep_time)
{
Sleep(sleep_time);
}
// pScreenBuffer - a pointer to a pointer to a buffer
int LM_Init(unsigned char **pScreenBuffer)
{
if(WndCreate() == 0) return 0;
LM_GFX_Init();
*pScreenBuffer = &ScreenBuffer[0];
return 1;
}
void LM_Deinit()
{
DestroyWindow(hWnd);
}
char LM_PollEvents()
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT) return 1;
}
return 0;
}
int _toggle = 1;
// hint: CALLBACK is absolutely necessary for watcom, mingw compiles fine without it
LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CLOSE:
LM_Deinit();
ExitProcess(0);
break;
case WM_KEYUP:
Keys[(lParam >> 16) & 0x7f] = 0;
break;
case WM_KEYDOWN:
Keys[(lParam >> 16) & 0x7f] = 1;
// toggle scaler system/manual
if(Keys[SC_S] == 1) {
Keys[SC_S] = 0;
_toggle ^= 1;
LM_GFX_SetScale(_toggle + 1);
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WndCreate()
{
WNDCLASS wc;
RECT rect;
// hint: mingw compiles & exe works without the following string, but watcom not
hInst = GetModuleHandle(0);
if(hInst == 0) return 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszMenuName = NULL;
wc.lpszClassName = AppTitle;
RegisterClass(&wc);
rect.top = 0;
rect.bottom = 400;
rect.left = 0;
rect.right = 640;
AdjustWindowRect(&rect, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE);
hWnd = CreateWindow( AppTitle,
AppTitle,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
hInst,
NULL);
if(hWnd != 0) {
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
return 1;
}
return 0;
}
void LM_GFX_Init()
{
if(GetDC(hWnd) == 0) return;
ReleaseDC(hWnd, 0);
memset(&bmp, 0, sizeof(bmp));
bmp.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmp.bmiHeader.biWidth = 320 * scale2x;
bmp.bmiHeader.biHeight = -200 * scale2x;
bmp.bmiHeader.biPlanes = 1;
bmp.bmiHeader.biBitCount = 8;
bmp.bmiHeader.biCompression = BI_RGB;
memcpy(&bmp.bmiColors[0], &GamePalette[0], 1024);
}
void LM_GFX_Deinit()
{
}
void LM_GFX_Flip(unsigned char *p)
{
RECT client;
HDC hDC;
hDC = GetDC(hWnd);
if(hDC != 0)
{
// double 320x200 to 640x200
if(scale2x == 2) {
for(int y = 0; y <= 199; y++) {
for(int x = 0; x <= 319; x++) {
unsigned char tmp = *(unsigned char *)(p + y*320+x);
ScreenBigBuffer[(y*2+1)*640+x*2] = tmp;
ScreenBigBuffer[(y*2+1)*640+x*2+1] = tmp;
ScreenBigBuffer[y*2*640+x*2] = tmp;
ScreenBigBuffer[y*2*640+x*2+1] = tmp;
}
}
}
GetClientRect(hWnd, &client);
client.right = 320 * scale2x;
client.bottom = 200 * scale2x;
StretchDIBits( hDC,
0, // Destination top left hand corner X Position
0, // Destination top left hand corner Y Position
client.right, // Destinations Width
client.bottom, // Desitnations height
0, // Source top left hand corner's X Position
0, // Source top left hand corner's Y Position
320 * scale2x, // Sources width
200 * scale2x, // Sources height
(scale2x == 1 ? p : &ScreenBigBuffer[0]), // Source's data
(BITMAPINFO *)&bmp, // Bitmap Info
DIB_RGB_COLORS, // operations
SRCCOPY);
ReleaseDC(hWnd, hDC);
}
}
void LM_GFX_WaitVSync()
{
// not needed in WinGDI
}
void LM_GFX_SetScale(int param)
{
RECT rect;
HDC hDC;
param = ((param - 1) & 1) + 1;
scale2x = param;
bmp.bmiHeader.biWidth = 320 * param;
bmp.bmiHeader.biHeight = -200 * param;
hDC = GetDC(hWnd);
GetClientRect(hWnd, &rect);
rect.top = 0;
rect.bottom = 200 * param;
rect.left = 0;
rect.right = 320 * param;
AdjustWindowRect(&rect, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE);
// Shitty function, had to bruteforce GetSystemMetrics
SetWindowPos( hWnd, 0, 0, 0,
rect.right + GetSystemMetrics(SM_CXFRAME)/2, // i don't understand why /2 but it works thus i don't care
rect.bottom + GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)/2,
SWP_NOMOVE);
ReleaseDC(hWnd, hDC);
}
// palette in bgra format
unsigned char GamePalette[1024] =
{
0x00, 0x00, 0x00, 0x00, 0xAA, 0x00, 0x00, 0x00, 0x00, 0xAA, 0x00, 0x00, 0xAA, 0xAA, 0x00, 0x00,
0x00, 0x00, 0xAA, 0x00, 0xAA, 0x00, 0xAA, 0x00, 0x00, 0x55, 0xAA, 0x00, 0xAA, 0xAA, 0xAA, 0x00,
0x55, 0x55, 0x55, 0x00, 0xFF, 0x55, 0x55, 0x00, 0x55, 0xFF, 0x55, 0x00, 0xFF, 0xFF, 0x55, 0x00,
0x55, 0x55, 0xFF, 0x00, 0xFF, 0x55, 0xFF, 0x00, 0x55, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00,
0x00, 0x00, 0x00, 0x00, 0x14, 0x14, 0x14, 0x00, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x2C, 0x2C, 0x00,
0x38, 0x38, 0x38, 0x00, 0x44, 0x44, 0x44, 0x00, 0x50, 0x50, 0x50, 0x00, 0x61, 0x61, 0x61, 0x00,
0x71, 0x71, 0x71, 0x00, 0x81, 0x81, 0x81, 0x00, 0x91, 0x91, 0x91, 0x00, 0xA1, 0xA1, 0xA1, 0x00,
0xB6, 0xB6, 0xB6, 0x00, 0xCA, 0xCA, 0xCA, 0x00, 0xE2, 0xE2, 0xE2, 0x00, 0xFF, 0xFF, 0xFF, 0x00,
0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x40, 0x00, 0xFF, 0x00, 0x7D, 0x00, 0xFF, 0x00, 0xBE, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xBE, 0x00, 0xFF, 0x00, 0x7D, 0x00, 0xFF, 0x00, 0x40, 0x00, 0xFF, 0x00,
0x00, 0x00, 0xFF, 0x00, 0x00, 0x40, 0xFF, 0x00, 0x00, 0x7D, 0xFF, 0x00, 0x00, 0xBE, 0xFF, 0x00,
0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xBE, 0x00, 0x00, 0xFF, 0x7D, 0x00, 0x00, 0xFF, 0x40, 0x00,
0x00, 0xFF, 0x00, 0x00, 0x40, 0xFF, 0x00, 0x00, 0x7D, 0xFF, 0x00, 0x00, 0xBE, 0xFF, 0x00, 0x00,
0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xBE, 0x00, 0x00, 0xFF, 0x7D, 0x00, 0x00, 0xFF, 0x40, 0x00, 0x00,
0xFF, 0x7D, 0x7D, 0x00, 0xFF, 0x7D, 0x9D, 0x00, 0xFF, 0x7D, 0xBE, 0x00, 0xFF, 0x7D, 0xDE, 0x00,
0xFF, 0x7D, 0x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x00, 0x1C, 0x00, 0x71, 0x00, 0x38, 0x00, 0x71, 0x00, 0x55, 0x00,
0x71, 0x00, 0x71, 0x00, 0x55, 0x00, 0x71, 0x00, 0x38, 0x00, 0x71, 0x00, 0x1C, 0x00, 0x71, 0x00,
0x00, 0x00, 0x71, 0x00, 0x00, 0x1C, 0x71, 0x00, 0x00, 0x38, 0x71, 0x00, 0x00, 0x55, 0x71, 0x00,
0x00, 0x71, 0x71, 0x00, 0x00, 0x71, 0x55, 0x00, 0x00, 0x71, 0x38, 0x00, 0x00, 0x71, 0x1C, 0x00,
0x00, 0x71, 0x00, 0x00, 0x1C, 0x71, 0x00, 0x00, 0x38, 0x71, 0x00, 0x00, 0x55, 0x71, 0x00, 0x00,
0x71, 0x71, 0x00, 0x00, 0x71, 0x55, 0x00, 0x00, 0x71, 0x38, 0x00, 0x00, 0x71, 0x1C, 0x00, 0x00,
0x71, 0x38, 0x38, 0x00, 0x71, 0x38, 0x44, 0x00, 0x71, 0x38, 0x55, 0x00, 0x71, 0x38, 0x61, 0x00,
0x71, 0x38, 0x71, 0x00, 0x61, 0x38, 0x71, 0x00, 0x55, 0x38, 0x71, 0x00, 0x44, 0x38, 0x71, 0x00,
0x38, 0x38, 0x71, 0x00, 0x38, 0x44, 0x71, 0x00, 0x38, 0x55, 0x71, 0x00, 0x38, 0x61, 0x71, 0x00,
0x38, 0x71, 0x71, 0x00, 0x38, 0x71, 0x61, 0x00, 0x38, 0x71, 0x55, 0x00, 0x38, 0x71, 0x44, 0x00,
0x38, 0x71, 0x38, 0x00, 0x44, 0x71, 0x38, 0x00, 0x55, 0x71, 0x38, 0x00, 0x61, 0x71, 0x38, 0x00,
0x71, 0x71, 0x38, 0x00, 0x71, 0x61, 0x38, 0x00, 0x71, 0x55, 0x38, 0x00, 0x71, 0x44, 0x38, 0x00,
0x71, 0x50, 0x50, 0x00, 0x71, 0x50, 0x59, 0x00, 0x71, 0x50, 0x61, 0x00, 0x71, 0x50, 0x69, 0x00,
0x71, 0x50, 0x71, 0x00, 0x69, 0x50, 0x71, 0x00, 0x61, 0x50, 0x71, 0x00, 0x59, 0x50, 0x71, 0x00,
0x50, 0x50, 0x71, 0x00, 0x50, 0x59, 0x71, 0x00, 0x50, 0x61, 0x71, 0x00, 0x50, 0x69, 0x71, 0x00,
0x50, 0x71, 0x71, 0x00, 0x50, 0x71, 0x69, 0x00, 0x50, 0x71, 0x61, 0x00, 0x50, 0x71, 0x59, 0x00,
0x50, 0x71, 0x50, 0x00, 0x59, 0x71, 0x50, 0x00, 0x61, 0x71, 0x50, 0x00, 0x69, 0x71, 0x50, 0x00,
0x71, 0x71, 0x50, 0x00, 0x71, 0x69, 0x50, 0x00, 0x71, 0x61, 0x50, 0x00, 0x71, 0x59, 0x50, 0x00,
0x40, 0x00, 0x00, 0x00, 0x40, 0x00, 0x10, 0x00, 0x40, 0x00, 0x20, 0x00, 0x40, 0x00, 0x30, 0x00,
0x40, 0x00, 0x40, 0x00, 0x30, 0x00, 0x40, 0x00, 0x20, 0x00, 0x40, 0x00, 0x10, 0x00, 0x40, 0x00,
0x00, 0x00, 0x40, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x20, 0x40, 0x00, 0x00, 0x30, 0x40, 0x00,
0x00, 0x40, 0x40, 0x00, 0x00, 0x40, 0x30, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00, 0x40, 0x10, 0x00,
0x00, 0x40, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x20, 0x40, 0x00, 0x00, 0x30, 0x40, 0x00, 0x00,
0x40, 0x40, 0x00, 0x00, 0x40, 0x30, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00, 0x40, 0x10, 0x00, 0x00,
0x40, 0x20, 0x20, 0x00, 0x40, 0x20, 0x28, 0x00, 0x40, 0x20, 0x30, 0x00, 0x40, 0x20, 0x38, 0x00,
0x40, 0x20, 0x40, 0x00, 0x38, 0x20, 0x40, 0x00, 0x30, 0x20, 0x40, 0x00, 0x28, 0x20, 0x40, 0x00,
0x20, 0x20, 0x40, 0x00, 0x20, 0x28, 0x40, 0x00, 0x20, 0x30, 0x40, 0x00, 0x20, 0x38, 0x40, 0x00,
0x20, 0x40, 0x40, 0x00, 0x20, 0x40, 0x38, 0x00, 0x20, 0x40, 0x30, 0x00, 0x20, 0x40, 0x28, 0x00,
0x20, 0x40, 0x20, 0x00, 0x28, 0x40, 0x20, 0x00, 0x30, 0x40, 0x20, 0x00, 0x38, 0x40, 0x20, 0x00,
0x40, 0x40, 0x20, 0x00, 0x40, 0x38, 0x20, 0x00, 0x40, 0x30, 0x20, 0x00, 0x40, 0x28, 0x20, 0x00,
0x40, 0x2C, 0x2C, 0x00, 0x40, 0x2C, 0x30, 0x00, 0x40, 0x2C, 0x34, 0x00, 0x40, 0x2C, 0x3C, 0x00,
0x40, 0x2C, 0x40, 0x00, 0x3C, 0x2C, 0x40, 0x00, 0x34, 0x2C, 0x40, 0x00, 0x30, 0x2C, 0x40, 0x00,
0x2C, 0x2C, 0x40, 0x00, 0x2C, 0x30, 0x40, 0x00, 0x2C, 0x34, 0x40, 0x00, 0x2C, 0x3C, 0x40, 0x00,
0x2C, 0x40, 0x40, 0x00, 0x2C, 0x40, 0x3C, 0x00, 0x2C, 0x40, 0x34, 0x00, 0x2C, 0x40, 0x30, 0x00,
0x2C, 0x40, 0x2C, 0x00, 0x30, 0x40, 0x2C, 0x00, 0x34, 0x40, 0x2C, 0x00, 0x3C, 0x40, 0x2C, 0x00,
0x40, 0x40, 0x2C, 0x00, 0x40, 0x3C, 0x2C, 0x00, 0x40, 0x34, 0x2C, 0x00, 0x40, 0x30, 0x2C, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};