-
Notifications
You must be signed in to change notification settings - Fork 1
/
platform.c
614 lines (555 loc) · 15.8 KB
/
platform.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include <windows.h>
#include <mmsystem.h>
#include "platform.h"
#include "modmusic/modmusic.h"
#include "keys.h"
#include "joy.h"
#pragma pack(push, 1)
typedef struct {
BITMAPFILEHEADER head;
BITMAPINFOHEADER info;
RGBQUAD cpal[256];
BYTE data[320 * 200];
} bmp_data;
typedef struct {
BITMAPFILEHEADER head;
BITMAPINFOHEADER info;
RGBQUAD cpal[256];
} bmp_head;
typedef struct {
bmp_head bh; // bitmap image header
DWORD _w; // fullscreen (desktop) width
DWORD _h; // fullscreen (desktop) height
DWORD w; // width of resized image for fullscreen mode
DWORD h; // height of resized image for fullscreen mode
DWORD x; // x position of image for keep-aspect fullscreen mode (black bars)
DWORD y; // y position of image for keep-aspect fullscreen mode (black bars)
DWORD wx; // windowed mode top window position
DWORD wy; // windowed mode left window position
BYTE *p; // bitmap data buffer for resized image
BOOL b; // block any operations in fullscreen <-> windowed transition (1/0)
BOOL f; // fullscreen flag (1/0)
} scr_head;
#pragma pack(pop)
static bmp_data scr_data;
static scr_head fscr;
static HWND hWndMain = 0;
static UINT hVSyncProc;
static LONG lSyncFlag = 0;
static KB_UPROC kbu_func = NULL;
static char kb_state = 0;
static DWORD dwShotNum = 0;
void DumpScreen(void) {
bmp_data scr_copy;
TCHAR s[1025];
HANDLE fl;
DWORD dw;
// make a copy of current frame since searching for non-existing file may take a while
CopyMemory(&scr_copy, &scr_data, sizeof(scr_copy));
do {
dwShotNum = (dwShotNum + 1) % 10000;
wsprintf(s, TEXT("POOH%04u.BMP"), dwShotNum);
} while (dwShotNum && (GetFileAttributes(s) != INVALID_FILE_ATTRIBUTES));
if (dwShotNum) {
fl = CreateFile(s, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (fl != INVALID_HANDLE_VALUE) {
WriteFile(fl, &scr_copy, sizeof(scr_copy), &dw, NULL);
CloseHandle(fl);
}
}
}
void WINAPIV sysDebugOut(const char *fmt, ...) {
char s[1025];
va_list args;
va_start(args, fmt);
wvsprintfA(s, fmt, args);
va_end(args);
OutputDebugStringA(s);
}
#define BMPLINESIZE(w, b) (((((w) * (b)) + 31) & ~31) >> 3)
int FillBitmapHeader(BYTE *buf, int iwidth, int iheight, int ibpp) {
BITMAPFILEHEADER *head;
BITMAPINFOHEADER *info;
int hsize, dsize;
hsize = sizeof(head[0]) + sizeof(info[0]) + (((ibpp <= 8) ? (1 << ibpp) : 0) * 4);
dsize = BMPLINESIZE(iwidth, ibpp) * iheight;
if (buf) {
head = (BITMAPFILEHEADER *) buf;
info = (BITMAPINFOHEADER *) &buf[sizeof(head[0])];
ZeroMemory(head, sizeof(head[0]));
ZeroMemory(info, sizeof(info[0]));
info->biSize = sizeof(info[0]);
info->biWidth = iwidth;
info->biHeight = -iheight; // negative height: top to bottom image
info->biPlanes = 1;
info->biBitCount = ibpp;
head->bfType = 0x4D42;
head->bfSize = hsize + dsize;
head->bfOffBits = hsize;
hsize = sizeof(head[0]) + sizeof(info[0]);
dsize = 0;
}
return(hsize + dsize);
}
// fast image resize
void ResizeToScreen(const BYTE *sb) {
DWORD i, j, coffx, coffy, cx, cy;
WORD *wx, *wy; // opt
BYTE *p, *o;
if (sb && fscr.f && fscr.p && (!fscr.b)) {
// TODO: replace hardcoded 320 and 200
coffx = (320UL << 16) / fscr.w;
coffy = (200UL << 16) / fscr.h;
cy = 0;
wx = (WORD *) &cx; wx++; // opt
wy = (WORD *) &cy; wy++; // opt
for (j = 0; j < fscr.h; j++) {
p = (BYTE *) &fscr.p[((j + fscr.y) * fscr._w) + fscr.x];
cx = 0;
o = &sb[*wy * 320]; // opt
for (i = 0; i < fscr.w; i++) {
//*p = sb[((cy >> 16) * 320) + (cx >> 16)];
*p = o[*wx]; // opt
cx += coffx;
p++;
}
cy += coffy;
}
}
}
#define GetMem(x) ((void *) LocalAlloc(LPTR, (x)))
#define FreeMem(x) LocalFree((x))
void SetWndStyle(HWND wnd) {
DWORD dwStyle, x, y;
RECT rc;
fscr.b = TRUE;
if (fscr.f) {
dwStyle = WS_VISIBLE | WS_SYSMENU | WS_POPUP;
x = 0;
y = 0;
rc.left = 0;
rc.top = 0;
rc.right = GetSystemMetrics(SM_CXSCREEN);
rc.bottom = GetSystemMetrics(SM_CYSCREEN);
// ---
fscr._w = rc.right;
fscr._h = rc.bottom;
FillBitmapHeader((BYTE *) &fscr.bh, fscr._w, fscr._h, 8);
fscr.w = fscr._w;
fscr.h = fscr._h;
if (1) { /* FIXME: preserve aspect */
if ((fscr.w * 3) != (fscr.h * 4)) {
if ((fscr.w / 4) > (fscr.h / 3)) {
fscr.w = (fscr.h * 4) / 3;
} else {
fscr.h = (fscr.w * 3) / 4;
}
}
}
fscr.x = (fscr._w - fscr.w) / 2;
fscr.y = (fscr._h - fscr.h) / 2;
fscr.p = GetMem(fscr._w * fscr._h);
if (fscr.p) { ZeroMemory(fscr.p, fscr._w * fscr._h); }
} else {
dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE;
x = fscr.wx;
y = fscr.wy;
rc.left = 0;
rc.top = 0;
rc.right = 320;
rc.bottom = 200;
// ---
if (fscr.p) {
FreeMem(fscr.p);
fscr.p = NULL;
}
}
AdjustWindowRect(&rc, dwStyle, FALSE);
SetWindowLong(wnd, GWL_STYLE, dwStyle);
SetWindowPos(wnd, fscr.f ? HWND_TOPMOST : HWND_NOTOPMOST,
x, y, rc.right - rc.left, rc.bottom - rc.top, SWP_FRAMECHANGED
);
fscr.b = FALSE;
}
void GameDraw(HWND wnd, bmp_data *scr_copy) {
HDC hc;
if (wnd) {
hc = GetDC(wnd);
if (hc) {
if ((!fscr.f) || (!fscr.p) || (fscr.b)) {
// direct screen output
SetDIBitsToDevice(hc,
0, 0, 320, 200,
0, 0, 0, 200,
(void *) scr_copy->data,
(BITMAPINFO *) &scr_copy->info,
DIB_RGB_COLORS
);
} else {
// resize image - a lot faster than call to StretchDIBits()
ResizeToScreen(scr_copy->data);
// update palette
CopyMemory(fscr.bh.cpal, scr_copy->cpal, sizeof(fscr.bh.cpal));
// direct screen output
SetDIBitsToDevice(hc,
0, 0, fscr._w, fscr._h,
0, 0, 0, fscr._h,
(void *) fscr.p,
(BITMAPINFO *) &fscr.bh.info,
DIB_RGB_COLORS
);
}
ReleaseDC(wnd, hc);
//ValidateRect(wnd, NULL);
//RedrawWindow(wnd, NULL, NULL, RDW_VALIDATE | RDW_NOERASE | RDW_NOFRAME | RDW_NOINTERNALPAINT | RDW_UPDATENOW | RDW_ALLCHILDREN);
GdiFlush();
}
}
}
void CALLBACK FrameUpdate(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) {
bmp_data scr_copy;
// draw only if not busy
if (!InterlockedExchange(&lSyncFlag, 1)) {
// make a local copy of whole frame
CopyMemory(&scr_copy, &scr_data, sizeof(scr_copy));
// draw frame
GameDraw((HWND) dwUser, &scr_copy);
// unlock state
InterlockedExchange(&lSyncFlag, 0);
}
}
int GameInit(HWND wnd) {
ZeroMemory(&scr_data, sizeof(scr_data));
FillBitmapHeader((BYTE *) &scr_data, 320, 200, 8);
// screen update
hVSyncProc = timeSetEvent(/*dwTicksPerFrame*/1000 / 60, 0, FrameUpdate, (DWORD) wnd, TIME_PERIODIC);
timeBeginPeriod(1);
return(hVSyncProc ? 1 : 0);
}
// class and title
static TCHAR WndClassName[] = TEXT("PoohWin32");
#define WM_MODE_XCHG (WM_USER + 101)
LRESULT CALLBACK WndProc(HWND wnd, UINT umsg, WPARAM wparm, LPARAM lparm) {
RECT rc;
switch (umsg) {
// main window just created - center it
case WM_CREATE:
ZeroMemory(&fscr, sizeof(fscr));
SetWndStyle(wnd);
GetWindowRect(wnd, &rc);
fscr.wx = (GetSystemMetrics(SM_CXSCREEN) - (rc.right - rc.left)) / 2;
fscr.wy = (GetSystemMetrics(SM_CYSCREEN) - (rc.bottom - rc.top)) / 2;
SetWindowPos(wnd, 0, fscr.wx, fscr.wy, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
if (!GameInit(wnd)) {
MessageBox(wnd, WndClassName, WndClassName, MB_ICONERROR);
return(-1);
}
hWndMain = wnd;
PostMessage(wnd, WM_MODE_XCHG, 0, 0);
break;
// fullscreen / windowed mode change
case WM_MODE_XCHG:
fscr.f ^= TRUE;
if (fscr.f) {
GetWindowRect(wnd, &rc);
fscr.wx = rc.left;
fscr.wy = rc.top;
}
SetWndStyle(wnd);
return(0);
// prevent system menu and hotkeys
// Alt, F10, Alt+Space and left-click on window icon
case WM_SYSCOMMAND:
if (!(
// Alt, F10, Alt+Space
(wparm == SC_KEYMENU) ||
// click on the window icon, 0xF093 == SC_MOUSEMENU | HTSYSMENU
(wparm == (SC_MOUSEMENU | HTSYSMENU)) ||
// double click on the window icon, 0xF063 == SC_CLOSE | HTSYSMENU
(wparm == (SC_CLOSE | HTSYSMENU))
)) {
break;
}
// fallthrough
// right click menu on the window caption
case WM_CONTEXTMENU:
// fallthrough
// do not redraw
case WM_PAINT:
return(0);
break;
// hide cursor only for client area and allow for the title bar and controls
case WM_SETCURSOR:
// hide in fullscreen only
if (fscr.f) {
SetCursor((LOWORD(lparm) == HTCLIENT) ? NULL : LoadCursor(NULL, IDC_ARROW));
return(TRUE);
}
break;
// key pressed
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
// switch between fullscreen / windowed mode
if (wparm == VK_F11) {
PostMessage(wnd, WM_MODE_XCHG, 0, 0);
break;
}
// take a screenshot
if (wparm == VK_F12) {
DumpScreen();
break;
}
if (kb_state && kbu_func) { kbu_func(MapVirtualKey(wparm, 0) & 0x7F); }
break;
// key released
case WM_SYSKEYUP:
case WM_KEYUP:
// from fullscreen / windowed mode and screenshots
if ((wparm == VK_F11) || (wparm == VK_F12)) { break; }
if (kb_state && kbu_func) { kbu_func((MapVirtualKey(wparm, 0) & 0x7F) | 0x80); }
break;
// cleanup game engine
case WM_DESTROY:
timeEndPeriod(1);
if (hVSyncProc) { timeKillEvent(hVSyncProc); hVSyncProc = 0; }
hWndMain = 0;
PostQuitMessage(0);
return(0);
break;
}
return(DefWindowProc(wnd, umsg, wparm, lparm));
}
DWORD WINAPI MainThread(LPVOID parm) {
WNDCLASSEX wce;
MSG wmsg;
HWND wnd;
RECT rc;
// InitCommonControls();
ZeroMemory(&wce, sizeof(wce));
wce.lpszClassName = WndClassName;
wce.cbSize = sizeof(wce);
wce.hInstance = GetModuleHandle(NULL);
wce.lpfnWndProc = &WndProc;
wce.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; // CS_OWNDC ???
wce.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wce.hCursor = LoadCursor(NULL, IDC_ARROW);
// only one instance allowed
wnd = FindWindow(wce.lpszClassName, NULL);
if (!wnd) {
// register window class
if (RegisterClassEx(&wce)) {
// create window
wnd = CreateWindowEx(
0, wce.lpszClassName, wce.lpszClassName,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,
0, 0, 320, 200,
0, 0,
wce.hInstance, NULL
);
// window created
if (wnd) {
// set new window size and position
rc.left = 0;
rc.top = 0;
rc.right = 320;
rc.bottom = 200;
AdjustWindowRect(&rc, GetWindowLong(wnd, GWL_STYLE), 0);
rc.right -= rc.left;
rc.bottom -= rc.top;
rc.left = (GetSystemMetrics(SM_CXSCREEN) - rc.right) / 2;
rc.top = (GetSystemMetrics(SM_CYSCREEN) - rc.bottom) / 2;
SetWindowPos(wnd, HWND_TOP, rc.left, rc.top, rc.right, rc.bottom, 0);
// default windows message loop
while (GetMessage(&wmsg, 0, 0, 0)) {
TranslateMessage(&wmsg);
DispatchMessage(&wmsg);
}
}
// unregister class
UnregisterClass(wce.lpszClassName, wce.hInstance);
}
if (!wnd) {
// can't register window class
MessageBox(0, WndClassName, NULL, MB_ICONERROR);
}
} else {
SetForegroundWindow(wnd);
}
ExitProcess(0); // FIXME:!!!
return(0);
}
int sysStart(void) {
HANDLE hThread;
DWORD ti;
hWndMain = 0;
hThread = CreateThread(NULL, 0, MainThread, NULL, 0, &ti);
if (hThread) {
CloseHandle(hThread);
ti = 0;
while (!hWndMain) {
Sleep(10);
ti++;
if (ti >= 200) { break; }
}
}
return((hThread && hWndMain) ? 1 : 0);
}
void sysSetPalNum(unsigned char n, unsigned char r, unsigned char g, unsigned char b) {
scr_data.cpal[n].rgbRed = (r << 2) | (r >> 4);
scr_data.cpal[n].rgbGreen = (g << 2) | (g >> 4);
scr_data.cpal[n].rgbBlue = (b << 2) | (b >> 4);
}
void *sysGetVidScreen(void) {
return(scr_data.data);
}
/*
https://i.sstatic.net/VfiZu.png
Vertical Timing (frame)
VGA Signal 640x480 @ 60 Hz Industry standard timing
Frame part | Lines | Time (ms)
-------------+-------+--------------------
Visible area | 480 | 15.253227408143
Front porch | 10 | 0.31777557100298
Sync pulse | 2 | 0.063555114200596
Back porch | 33 | 1.0486593843098
Whole frame | 525 | 16.683217477656
*/
// 60 FPS - DOS monitor refresh rate
DWORD dwPF = 0;
void sysWaitForVSync(void) {
DWORDLONG x;
do {
x = timeGetTime();
x = (x * 60) / 1000;
} while (((DWORD) x) == dwPF);
dwPF = x;
}
// 1193180 / 65536 ~ 18.206482 - DOS timer update rate
long sysGetTicks(void) {
DWORDLONG x;
x = timeGetTime();
x = (x * 1193180) / (1000 * 65536);
return(x);
}
/* Borland C random routines */
static long BC_RandSeed = 1;
void BC_srand(short value) {
BC_RandSeed = value;
}
short BC_rand(void) {
BC_RandSeed = (0x015A4E35L * BC_RandSeed) + 1;
return((BC_RandSeed >> 16) & 0x7FFF);
}
/* ======================================================================== */
short random(short __num) {
return( (short)(((long) BC_rand() * __num) / (0x7FFFU + 1)) );
}
void delay(unsigned short __milliseconds) {
Sleep(__milliseconds);
}
unsigned long coreleft(void) {
return(0x7FFFFFFF);
}
void textmode(short newmode) {
// FIXME: add more cleanup code here
ModFreeMusic();
}
/* ======================================================================== */
void modinit(void) {
ModInitMusic();
}
void moddevice(int *device) {
// FIXME: not used
}
void modvolume(int vol1, int vol2, int vol3, int vol4) {
ModLoudMusic((vol1 + vol2 + vol3 + vol4) / 4);
}
void modsetup(char *filenm, int looping, int prot, int mixspeed, int device, int *status) {
// looping always 4 (loop infinite)
ModPlayMusic(filenm);
}
void modstop(void) {
ModPlayMusic(NULL);
}
/* ======================================================================== */
void kb_stub(unsigned char butt) {
}
void kb_on(void) {
if (!kbu_func) {
kbu_func = kb_stub;
}
kb_state = 1;
}
void kb_off(void) {
kb_state = 0;
}
void kb_set(KB_UPROC funcaddr) {
kbu_func = funcaddr;
}
/* ======================================================================== */
static JS_UPROC jsu_func = NULL;
static char js_enabled = 0;
static unsigned short js_devices = 0;
void js_stub(unsigned short butt) {
}
void js_init(void) {
JOYINFOEX jie;
int i, n;
if (!jsu_func) {
jsu_func = js_stub;
}
js_devices = 0;
js_enabled = 0;
n = joyGetNumDevs();
for (i = 0; i < n; i++) {
jie.dwSize = sizeof(jie);
jie.dwFlags = JOY_RETURNALL;
// get first connected joystick
if (joyGetPosEx(i, &jie) == JOYERR_NOERROR) {
js_devices = i + 1; // FIXME: not a bitmask
break;
}
}
}
int js_state(void) {
return(js_devices);
}
void js_on(void) {
js_enabled = js_devices ? 1 : 0;
}
void js_off(void) {
js_enabled = 0;
}
void js_set(JS_UPROC funcaddr) {
jsu_func = funcaddr;
}
void js_read(void) {
JOYINFOEX jie;
unsigned short butt;
/* joystick enabled */
if (js_enabled) {
butt = 0;
/* joystick 1 */
jie.dwSize = sizeof(jie);
jie.dwFlags = JOY_RETURNALL;
// FIXME: js_devices not a bitmask
if (joyGetPosEx(js_devices - 1, &jie) == JOYERR_NOERROR) {
if (jie.dwXpos < 0x5555) { butt |= JOY1_L; }
if (jie.dwXpos > 0xAAAA) { butt |= JOY1_R; }
if (jie.dwYpos < 0x5555) { butt |= JOY1_U; }
if (jie.dwYpos > 0xAAAA) { butt |= JOY1_D; }
if (jie.dwButtons & JOY_BUTTON1) { butt |= JOY1_A; }
if (jie.dwButtons & JOY_BUTTON2) { butt |= JOY1_B; }
}
/* call user defined function */
jsu_func(butt);
}
}
void js_test(void) {
// FIXME: not used
}
int puts(const char *s) {
if ((s) && (*s == '!')) {
MessageBox(hWndMain ? hWndMain : 0, s, NULL, MB_OK);
}
}