-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgkiounit.c
581 lines (545 loc) · 13.1 KB
/
gkiounit.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
/*
Graphics and Keyboard Input/Output Unit
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __MSDOS__
#include <alloc.h>
#include <dos.h>
#endif
#ifdef __WIN32__
#include <windows.h>
#include "win32api.h"
#endif
#include "gkiounit.h"
/* bitmap header: BITMAPFILEHEADER + BITMAPINFOHEADER + PALETTE[2] */
#define BMP_HDR_SIZE 62
/* bitmap data ((640/8)*480) */
#define BMP_BUF_SIZE 38400U
#ifdef __MSDOS__
static unsigned char far *scrmem = MK_FP(0xA000, 0x0000);
static unsigned char PrevVidMode = 0;
#endif
#ifdef __WIN32__
/*
note that the biHeight is negative, so the bitmap is a top-down DIB and its origin is the upper left corner
*/
static unsigned char bmphdr[BMP_HDR_SIZE] = {
0x42, 0x4D, 0x3E, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x20, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00,
};
/* offscreen buffer - BMP file size */
static unsigned char scrbuf[BMP_BUF_SIZE + BMP_HDR_SIZE];
static unsigned char *scrmem = NULL;
static unsigned int lastchar = 0;
HWND wnd = 0;
HANDLE th;
HANDLE eh;
BOOL IsWndMode = FALSE;
#endif
static unsigned char *GraphFont = NULL;
static unsigned char GraphColor = 0;
#ifdef __MSDOS__
unsigned int x_kbhit(void) {
union REGS regs;
regs.h.ah = 0x01;
int86(0x16, ®s, ®s);
/* 6 - zero flag is set when buffer empty */
return((regs.x.flags & 0x40) ? 0 : 1);
}
unsigned int x_getch(void) {
union REGS regs;
regs.h.ah = 0x00;
int86(0x16, ®s, ®s);
return(regs.x.ax);
}
#endif
#ifdef __WIN32__
unsigned int x_kbhit(void) {
/*
this was allow to go through all possible loops and checks
so the KB_ESC will be readed by the calling of x_getch()
*/
if (lastchar & 2) {
lastchar ^= 4;
}
if (lastchar & 4) {
return(0);
}
return(lastchar ? 1 : 0);
}
unsigned int x_getch(void) {
unsigned int x;
if (eh && (!lastchar)) {
WaitForSingleObject(eh, INFINITE);
}
x = lastchar;
if (LOBYTE(lastchar) == 1) {
lastchar = 0;
}
return(x);
}
#endif
/*
http://atrevida.comprenica.com/atrtut19.html
http://alexfru.narod.ru/emiscdocs.html
http://alexfru.chat.ru/
*/
/*
all this needed to drop graphics files
EGAVGA.BGI and GRAPHICS.H and reduce
final program size since there are no
need in color graphics anyway
*/
#ifdef __MSDOS__
void SetVideoMode(unsigned char mode) {
union REGS regs;
regs.h.ah = 0x00;
regs.h.al = mode;
int86(0x10, ®s, ®s);
}
int IsVGADisplay(void) {
union REGS regs;
regs.h.ah = 0x1A;
regs.h.al = 0x00;
int86(0x10, ®s, ®s);
/* test VGA support
AL = 0x1A - function supported
BL - active display, BH - inactive display
values:
7 - VGA mono
8 - VGA color
more - MCGA
0xFF - unsupported
*/
return(((regs.h.al == 0x1A) && (regs.h.bl >= 0x07) && (regs.h.bl != 0xFF)) ? 1 : 0);
}
unsigned char GetVideoMode(void) {
union REGS regs;
regs.h.ah = 0x0F;
int86(0x10, ®s, ®s);
return(regs.h.al);
}
void ClearScreen(void) {
unsigned int i;
/* Turbo C 2.01 doesn't have
_fmemset() for far pointers */
for (i = 0; i < BMP_BUF_SIZE; i++) {
scrmem[i] = GraphColor;
}
}
int InitGraphics(void) {
int result;
result = 2;
/* not in graphic mode */
if (!PrevVidMode) {
result = 0;
/* test for VGA support */
if (IsVGADisplay()) {
/* save current vid mode
add 1 for zero mode */
PrevVidMode = GetVideoMode() + 1;
/* set mode 640x480x2 (B/W) */
SetVideoMode(0x11);
ClearScreen();
result = 1;
}
}
return(result);
}
void FreeGraphics(void) {
/* in graphic mode */
if (PrevVidMode) {
/* restore vid mode */
SetVideoMode(PrevVidMode - 1);
GraphColor = 0;
PrevVidMode = 0;
GraphFont = NULL;
}
}
#endif
#ifdef __WIN32__
/* draw screen buffer to window frame */
void BufferToScreen(HWND wnd) {
HBITMAP hOld, hBack;
PAINTSTRUCT ps;
HDC hc, hMem;
RECT rc;
hc = BeginPaint(wnd, &ps);
hMem = CreateCompatibleDC(hc);
hBack = CreateDIBitmap(
hc, (BITMAPINFOHEADER *)(&scrbuf[sizeof(BITMAPFILEHEADER)]),
CBM_INIT, (void *)(&scrbuf[BMP_HDR_SIZE]),
(BITMAPINFO *)(&scrbuf[sizeof(BITMAPFILEHEADER)]), DIB_RGB_COLORS
);
if (hBack) {
hOld = SelectObject(hMem, hBack);
GetClientRect(wnd, &rc);
BitBlt(hc, rc.left, rc.top, rc.right, rc.bottom, hMem, 0, 0, SRCCOPY);
SelectObject(hMem, hOld);
DeleteObject(hBack);
}
DeleteDC(hMem);
EndPaint(wnd, &ps);
}
void DumpScreen(void) {
TCHAR s[16];
HANDLE fl;
DWORD i;
/* find non-existent file */
for (i = 1; i; i++) {
wsprintf(s, "%08u.bmp", i);
/* file not exists */
if (GetFileAttributes(s) == INVALID_FILE_ATTRIBUTES) {
fl = CreateFile(s, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (fl != INVALID_HANDLE_VALUE) {
WriteFile(fl, scrbuf, sizeof(scrbuf), &i, NULL);
CloseHandle(fl);
}
break;
}
}
}
LRESULT CALLBACK WndPrc(HWND wnd, UINT umsg, WPARAM wparm, LPARAM lparm) {
DEVMODE dm;
RECT rc;
switch (umsg) {
case WM_CREATE:
/* if Shift pressed - run in windowed mode */
if (!IsWndMode) {
/* go to the fullscreen */
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
dm.dmPelsWidth = VID_MAX_X + 1;
dm.dmPelsHeight = VID_MAX_Y + 1;
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
} else {
rc.top = 0;
rc.left = 0;
rc.right = VID_MAX_X + 1;
rc.bottom = VID_MAX_Y + 1;
AdjustWindowRect(&rc, GetWindowLong(wnd, GWL_STYLE), FALSE);
rc.bottom -= rc.top;
rc.right -= rc.left;
MoveWindow(wnd,
(GetSystemMetrics(SM_CXSCREEN) - rc.right) / 2,
(GetSystemMetrics(SM_CYSCREEN) - rc.bottom) / 2,
rc.right,
rc.bottom,
TRUE
);
}
return(0);
break;
case WM_SETCURSOR:
/* hide cursor for client area in fullscreen mode */
if (!IsWndMode) {
SetCursor((LOWORD(lparm) == HTCLIENT) ? NULL : LoadCursor(NULL, IDC_ARROW));
return(TRUE);
}
break;
case WM_KEYDOWN:
if (wparm == VK_F12) { DumpScreen(); }
if (!lastchar) {
lastchar = MAKEWORD(1, LOBYTE(HIWORD(lparm)));
SetEvent(eh);
}
break;
case WM_PAINT:
BufferToScreen(wnd);
return(0);
break;
case WM_DESTROY:
if (!IsWndMode) {
/* restore vid mode */
ChangeDisplaySettings(NULL, 0);
}
/* drop keyboard event */
lastchar = MAKEWORD(2, KB_ESC);
SetEvent(eh);
PostQuitMessage(0);
return(0);
break;
}
return(DefWindowProc(wnd, umsg, wparm, lparm));
}
DWORD WINAPI WndLoopThreadFunc(LPVOID parm) {
WNDCLASSEX wcex;
MSG wmsg;
IsWndMode = GetAsyncKeyState(VK_SHIFT) ? TRUE : FALSE;
if (!IsWndMode) {
IsWndMode = MessageBox(0, "Run in fullscreen mode?", "Confirm", MB_YESNO | MB_ICONQUESTION) == IDNO;
}
/* create window */
ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(wcex);
wcex.lpszClassName = "{6B8C783A-9676-47D5-B91A-DA0ABFFD6C3B}";
wcex.hInstance = GetModuleHandle(NULL);
wcex.lpfnWndProc = WndPrc;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
if (!RegisterClassEx(&wcex)) {
SetEvent(eh);
return(1);
}
wnd = CreateWindowEx(
0, wcex.lpszClassName, "TPROGRAM",
/* if Shift pressed - run in windowed mode */
(!IsWndMode) ? (WS_VISIBLE | WS_SYSMENU | WS_POPUP) :
(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE),
0,
0,
VID_MAX_X + 1, VID_MAX_Y + 1,
0, 0,
wcex.hInstance, NULL
);
if (!wnd) {
SetEvent(eh);
return(2);
}
/* signaling that everything alright */
SetEvent(eh);
while (GetMessage(&wmsg, 0, 0, 0)) {
TranslateMessage(&wmsg);
DispatchMessage(&wmsg);
}
UnregisterClass(wcex.lpszClassName, wcex.hInstance);
wnd = 0;
return(0);
}
void ClearScreen(void) {
if (scrmem) {
FillMemory(scrmem, BMP_BUF_SIZE, GraphColor);
InvalidateRect(wnd, NULL, TRUE);
}
}
int InitGraphics(void) {
int result;
DWORD tid;
result = 2;
/* not in graphic mode */
if (!scrmem) {
result = 0;
eh = CreateEvent(NULL, FALSE, FALSE, NULL);
/* create windows loop thread */
th = CreateThread(NULL, 0, &WndLoopThreadFunc, NULL, 0, &tid);
if (th) {
WaitForSingleObject(eh, INFINITE);
if (wnd) {
/* init bitmap data */
memmove(scrbuf, bmphdr, BMP_HDR_SIZE);
/* offset to the actual bitmap data */
scrmem = &scrbuf[BMP_HDR_SIZE];
result = 1;
ClearScreen();
}
}
if (!wnd) {
CloseHandle(eh);
eh = NULL;
}
}
return(result);
}
void FreeGraphics(void) {
DWORD ts;
/* in graphic mode */
if (scrmem) {
/* close and destroy window */
if (wnd) {
SendMessage(wnd, WM_CLOSE, 0, 0);
}
if (GetExitCodeThread(th, &ts) && (ts == STILL_ACTIVE)) {
/* wait until everything cleaned up */
WaitForSingleObject(th, INFINITE);
}
CloseHandle(th);
/* keyboard event */
CloseHandle(eh);
eh = 0;
/* cleanup */
scrmem = NULL;
}
}
#endif
void DrawPixel(int x, int y) {
if ((x >= 0) && (y >= 0) && (x <= VID_MAX_X) && (y <= VID_MAX_Y)) {
if (GraphColor) {
scrmem[(y * 80) + (x >> 3)] |= (128 >> (x & 7));
} else {
scrmem[(y * 80) + (x >> 3)] &= ~(128 >> (x & 7));
}
}
}
/* Bresenhem algo */
void DrawLineX(int x1, int y1, int x2, int y2) {
int i, dx, dy, sx, sy, d1, d2, dd;
/* flip line */
if (y1 > y2) {
y1 ^= y2;
y2 ^= y1;
y1 ^= y2;
}
/* initialize */
dx = x2 - x1;
dx = (dx < 0) ? (-dx) : (dx);
dy = y2 - y1;
sx = 1;
if (x1 > x2) { sx = -1; }
if (x1 == x2) { sx = 0; }
sy = 1;
if (dx >= dy) {
/* generate dx >= dy */
d1 = dy + dy;
dd = d1 - dx;
d2 = dd - dx;
for (i = 0; i <= dx; i++) {
DrawPixel(x1, y1);
x1 += sx;
if (dd < 0) {
dd += d1;
} else {
dd += d2;
y1 += sy;
}
}
} else {
/* generate dx < dy */
d1 = dx + dx;
dd = d1 - dy;
d2 = dd - dy;
for (i = 0; i <= dy; i++) {
DrawPixel(x1, y1);
y1 += sy;
if (dd < 0) {
dd += d1;
} else {
dd += d2;
x1 += sx;
}
}
}
}
void DrawTextX(int x, int y, char *s, int len) {
unsigned char *h, b;
int i, j, k;
if (GraphFont) {
for (k = 0; k < len; k++) {
h = GraphFont;
h += ((unsigned char) s[k]) << 3; /* *8 */
for (j = 0; j < 8; j++) {
b = 128;
for (i = 0; i < 8; i++) {
if (*h & b) {
DrawPixel(x + i, y);
}
b >>= 1;
}
y++;
h++;
}
y -= 8;
x += 8;
}
}
#ifdef __WIN32__
InvalidateRect(wnd, NULL, TRUE);
#endif
}
void DrawRectX(int x1, int y1, int x2, int y2) {
DrawLineX(x1, y1, x2, y1);
DrawLineX(x2, y1, x2, y2);
DrawLineX(x2, y2, x1, y2);
DrawLineX(x1, y2, x1, y1);
#ifdef __WIN32__
InvalidateRect(wnd, NULL, TRUE);
#endif
}
/* very slow, but just fine for small areas */
void FillRectX(int x1, int y1, int x2, int y2) {
int i;
/* flip */
if (y1 > y2) {
y1 ^= y2;
y2 ^= y1;
y1 ^= y2;
}
for (i = y1; i <= y2; i++) {
DrawLineX(x1, i, x2, i);
}
#ifdef __WIN32__
InvalidateRect(wnd, NULL, TRUE);
#endif
}
void SetGraphColor(unsigned char color) {
GraphColor = color ? 0xFF : 0;
}
void SetActiveFont(unsigned char *font) {
GraphFont = font;
}
/* image routines */
void PutMonoImage(unsigned int x, unsigned int y, unsigned char *p) {
unsigned int i, sw, iw, ih, lw;
if (p) {
iw = (p[19] << 8) | p[18];
iw = (iw / 8) + ((iw % 8) ? 1 : 0);
lw = iw + ((4 - (iw & 3)) & 3);
ih = (p[23] << 8) | p[22];
sw = (VID_MAX_X + 1) / 8;
y += ih - 1;
y *= sw;
y += (x / 8);
p += BMP_HDR_SIZE;
for (i = 0; i < ih; i++) {
#ifdef __MSDOS__
movedata(_DS, (unsigned) &p[i * lw], 0xA000, y - (sw * i), iw);
#endif
#ifdef __WIN32__
memmove(&scrmem[y - (sw * i)], &p[i * lw], iw);
#endif
}
}
#ifdef __WIN32__
InvalidateRect(wnd, NULL, TRUE);
#endif
}
unsigned char *GetMonoImage(unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
unsigned char *p;
unsigned int i, sw, iw, lw, sz;
p = NULL;
w &= 0xFFFF;
h &= 0xFFFF;
if (w && h) {
iw = (w / 8) + ((w % 8) ? 1 : 0);
lw = iw + ((4 - (iw & 3)) & 3);
sz = (lw * h) + BMP_HDR_SIZE;
p = (unsigned char *) malloc(sz);
if (p) {
memset(p, 0, sz);
sw = (VID_MAX_X + 1) / 8;
y += h - 1;
y *= sw;
y += (x / 8);
p += BMP_HDR_SIZE;
for (i = 0; i < h; i++) {
#ifdef __MSDOS__
movedata(0xA000, y - (sw * i), _DS, (unsigned) &p[i * lw], iw);
#endif
#ifdef __WIN32__
memmove(&p[i * lw], &scrmem[y - (sw * i)], iw);
#endif
}
p -= BMP_HDR_SIZE;
p[18] = w & 0xFF;
p[19] = w >> 8;
p[22] = h & 0xFF;
p[23] = h >> 8;
}
}
return(p);
}