forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimsys_w16.c
543 lines (444 loc) · 12.2 KB
/
simsys_w16.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
/*
* simsys_s.c
*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project and may not be used
* in other projects without written permission of the author.
*/
#ifndef WIN32
#error "Only Windows has GDI!"
#endif
#ifndef _MSC_VER
#include <unistd.h>
#include <sys/time.h>
#endif
#include <stdio.h>
// windows Bibliotheken DirectDraw 5.x
#define UNICODE 1
#include <windows.h>
#include <winreg.h>
#include <wingdi.h>
#include <mmsystem.h>
// needed for wheel
#ifndef WM_MOUSEWHEEL
# define WM_MOUSEWHEEL 0x020A
#endif
#ifndef GET_WHEEL_DELTA_WPARAM
# define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
#endif
#include "simmem.h"
#include "simversion.h"
#include "simsys.h"
#include "simevent.h"
static HWND hwnd;
static MSG msg;
static RECT WindowSize = { 0, 0, 0, 0 };
static RECT MaxSize;
static HINSTANCE hInstance;
static BITMAPINFOHEADER *AllDib = NULL;
static unsigned short *AllDibData = NULL;
static HDC hdc = NULL;
/*
* Hier sind die Basisfunktionen zur Initialisierung der
* Schnittstelle untergebracht
* -> init,open,close
*/
int dr_os_init(const int* parameter)
{
return TRUE;
}
// query home directory
const char *dr_query_home()
{
static char buffer[1024];
DWORD len=1023;
HKEY hHomeDir;
if(RegOpenKeyA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", &hHomeDir)==ERROR_SUCCESS) {
RegQueryValueExA(hHomeDir,"Personal",NULL,NULL,(LPCSTR)buffer,&len);
strcat(buffer,"\\");
return buffer;
}
return NULL;
}
// open the window
int dr_os_open(int w, int h, int bpp, int fullscreen)
{
const wchar_t* const title =
#ifdef _MSC_VER
L"Simutrans " WIDE_VERSION_NUMBER;
#else
L"" SAVEGAME_PREFIX " " VERSION_NUMBER " - " VERSION_DATE;
#endif
// fake fullscreen
if (fullscreen && w == MaxSize.right && h == MaxSize.bottom) {
hwnd = CreateWindowEx(
WS_EX_TOPMOST,
L"Simu", title,
WS_POPUP,
0, 0,
w, h - 1,
NULL, NULL, hInstance, NULL
);
} else {
hwnd = CreateWindow(
L"Simu", title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
w + GetSystemMetrics(SM_CXFRAME),
h - 1 + 2 * GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION),
NULL, NULL, hInstance, NULL
);
}
ShowWindow(hwnd, SW_SHOW);
WindowSize.right = w;
WindowSize.bottom = h - 1;
AllDib = GlobalAlloc(GMEM_FIXED, sizeof(BITMAPINFOHEADER) + sizeof(DWORD) * 3);
AllDib->biSize = sizeof(BITMAPINFOHEADER);
AllDib->biCompression = BI_BITFIELDS;
*((DWORD*)(AllDib + 1) + 0) = 0x01;
*((DWORD*)(AllDib + 1) + 1) = 0x02;
*((DWORD*)(AllDib + 1) + 2) = 0x03;
AllDib->biSize = sizeof(BITMAPINFOHEADER);
AllDib->biWidth = w;
AllDib->biHeight = h;
AllDib->biPlanes = 1;
AllDib->biBitCount = 16;
AllDib->biCompression = BI_RGB;
AllDib->biClrUsed = 0;
AllDib->biClrImportant = 0;
#ifdef USE_16BIT_DIB
AllDib->biCompression = BI_BITFIELDS;
*((DWORD*)(AllDib + 1) + 0) = 0x0000001F;
*((DWORD*)(AllDib + 1) + 1) = 0x000007E0;
*((DWORD*)(AllDib + 1) + 2) = 0x0000F800;
#endif
return TRUE;
}
int dr_os_close(void)
{
if (hdc != NULL) {
ReleaseDC(hwnd, hdc);
hdc = NULL;
}
if (hwnd != NULL) {
DestroyWindow(hwnd);
}
if (AllDibData != NULL) {
GlobalFree(GlobalHandle(AllDibData));
AllDibData = NULL;
}
if (AllDib != NULL) {
GlobalFree(AllDib);
AllDib = NULL;
}
return TRUE;
}
// reiszes screen
int dr_textur_resize(unsigned short **textur, int w, int h, int bpp)
{
AllDib->biWidth = w;
WindowSize.right = w;
WindowSize.bottom = h - 1;
return TRUE;
}
unsigned short *dr_textur_init()
{
AllDibData = (unsigned short*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE, (MaxSize.right + 15) * 2 * (MaxSize.bottom + 2)));
return AllDibData;
}
/**
* Transform a 24 bit RGB color into the system format.
* @return converted color value
* @author Hj. Malthaner
*/
unsigned int get_system_color(unsigned int r, unsigned int g, unsigned int b)
{
#ifdef USE_16BIT_DIB
return ((r & 0x00F8) << 8) | ((g & 0x00FC) << 3) | (b >> 3);
#else
return ((r & 0x00F8) << 7) | ((g & 0x00F8) << 2) | (b >> 3); // 15 Bit
#endif
}
void dr_flush()
{
if (hdc != NULL) {
ReleaseDC(hwnd, hdc);
hdc = NULL;
}
}
void dr_textur(int xp, int yp, int w, int h)
{
if (hdc == NULL) hdc = GetDC(hwnd);
AllDib->biHeight = h+1;
StretchDIBits(
hdc,
xp, yp, w, h,
xp, h + 1, w, -h,
(LPSTR)(AllDibData + yp * WindowSize.right), (LPBITMAPINFO)AllDib,
DIB_RGB_COLORS, SRCCOPY
);
}
// move cursor to the specified location
void move_pointer(int x, int y)
{
POINT pt = { x, y };
ClientToScreen(hwnd, &pt);
SetCursorPos(pt.x, pt.y);
}
// set the mouse cursor (pointer/load)
void set_pointer(int loading)
{
SetCursor(LoadCursor(NULL, loading != 0 ? IDC_WAIT : IDC_ARROW));
}
/**
* Some wrappers can save screenshots.
* @return 1 on success, 0 if not implemented for a particular wrapper and -1
* in case of error.
* @author Hj. Malthaner
*/
int dr_screenshot(const char *filename)
{
FILE *fBmp = fopen(filename, "wb");
if (fBmp) {
BITMAPFILEHEADER bf;
unsigned i;
AllDib->biHeight = WindowSize.bottom + 1;
bf.bfType = 0x4d42; //"BM"
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(DWORD)*3;
bf.bfSize = (bf.bfOffBits + AllDib->biHeight * AllDib->biWidth * 2l + 3l) / 4l;
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, fBmp);
fwrite(AllDib, sizeof(BITMAPINFOHEADER) + sizeof(DWORD) * 3, 1, fBmp);
for (i = 0; i < AllDib->biHeight; i++) {
fwrite(AllDibData + (AllDib->biHeight - 1 - i) * AllDib->biWidth, AllDib->biWidth, 2, fBmp);
}
fclose(fBmp);
}
return 0;
}
/*
* Hier sind die Funktionen zur Messageverarbeitung
*/
struct sys_event sys_event;
/* Windows eventhandler: does most of the work */
LRESULT WINAPI WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_LBUTTONDOWN: /* originally ButtonPress */
sys_event.type = SIM_MOUSE_BUTTONS;
sys_event.code = SIM_MOUSE_LEFTBUTTON;
sys_event.key_mod = wParam >> 2;
sys_event.mx = LOWORD(lParam);
sys_event.my = HIWORD(lParam);
break;
case WM_LBUTTONUP: /* originally ButtonRelease */
sys_event.type = SIM_MOUSE_BUTTONS;
sys_event.code = SIM_MOUSE_LEFTUP;
sys_event.key_mod = wParam >> 2;
sys_event.mx = LOWORD(lParam);
sys_event.my = HIWORD(lParam);
break;
case WM_RBUTTONDOWN: /* originally ButtonPress */
sys_event.type = SIM_MOUSE_BUTTONS;
sys_event.code = SIM_MOUSE_RIGHTBUTTON;
sys_event.key_mod = wParam >> 2;
sys_event.mx = LOWORD(lParam);
sys_event.my = HIWORD(lParam);
break;
case WM_RBUTTONUP: /* originally ButtonRelease */
sys_event.type = SIM_MOUSE_BUTTONS;
sys_event.code = SIM_MOUSE_RIGHTUP;
sys_event.key_mod = wParam >> 2;
sys_event.mx = LOWORD(lParam);
sys_event.my = HIWORD(lParam);
break;
case WM_MOUSEMOVE:
sys_event.type = SIM_MOUSE_MOVE;
sys_event.code = SIM_MOUSE_MOVED;
sys_event.key_mod = wParam >> 2;
sys_event.mx = LOWORD(lParam);
sys_event.my = HIWORD(lParam);
break;
case WM_MOUSEWHEEL:
sys_event.type = SIM_MOUSE_BUTTONS;
sys_event.code = GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? SIM_MOUSE_WHEELUP : SIM_MOUSE_WHEELDOWN;
return 0;
case WM_SIZE: // resize client area
sys_event.type = SIM_SYSTEM;
sys_event.code = SIM_SYSTEM_RESIZE;
sys_event.mx = LOWORD(lParam) + 1;
if (sys_event.mx <= 0) sys_event.mx = 4;
sys_event.my = HIWORD(lParam);
if (sys_event.my <= 1) sys_event.my = 64;
break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdcp;
if (hdc != NULL) {
ReleaseDC(hwnd, hdc);
hdc = NULL;
}
hdcp = BeginPaint(hwnd, &ps);
AllDib->biHeight = WindowSize.bottom + 1;
StretchDIBits(
hdcp,
0, 0, WindowSize.right, WindowSize.bottom,
0, WindowSize.bottom + 1, WindowSize.right, -WindowSize.bottom,
(LPSTR)AllDibData, (LPBITMAPINFO)AllDib,
DIB_RGB_COLORS, SRCCOPY
);
EndPaint(hwnd, &ps);
break;
}
case WM_KEYDOWN: { /* originally KeyPress */
// check for not numlock!
int numlock = (GetKeyState(VK_NUMLOCK) & 1) == 0;
sys_event.type = SIM_KEYBOARD;
sys_event.code = 0;
if (numlock) {
// do low level special stuff here
switch (wParam) {
case VK_NUMPAD0: sys_event.code = '0'; break;
case VK_NUMPAD1: sys_event.code = '1'; break;
case VK_NUMPAD3: sys_event.code = '3'; break;
case VK_NUMPAD7: sys_event.code = '7'; break;
case VK_NUMPAD9: sys_event.code = '9'; break;
case VK_NUMPAD2: sys_event.code = SIM_KEY_DOWN; break;
case VK_NUMPAD4: sys_event.code = SIM_KEY_LEFT; break;
case VK_NUMPAD6: sys_event.code = SIM_KEY_RIGHT; break;
case VK_NUMPAD8: sys_event.code = SIM_KEY_UP; break;
case VK_SEPARATOR: sys_event.code = 127; //delete
break;
}
// check for numlock!
if (sys_event.code != 0) break;
}
// do low level special stuff here
switch (wParam) {
case VK_LEFT: sys_event.code = SIM_KEY_LEFT; break;
case VK_RIGHT: sys_event.code = SIM_KEY_RIGHT; break;
case VK_UP: sys_event.code = SIM_KEY_UP; break;
case VK_DOWN: sys_event.code = SIM_KEY_DOWN; break;
case VK_PRIOR: sys_event.code = '>'; break;
case VK_NEXT: sys_event.code = '<'; break;
case VK_DELETE: sys_event.code = 127; break;
case VK_HOME: sys_event.code = SIM_KEY_HOME; break;
case VK_END: sys_event.code = SIM_KEY_END; break;
}
// check for F-Keys!
if (sys_event.code == 0 && wParam >= VK_F1 && wParam <= VK_F15) {
sys_event.code = wParam - VK_F1 + SIM_KEY_F1;
sys_event.key_mod = (GetKeyState(VK_CONTROL) != 0) * 2; // control state
//printf("WindowsEvent: Key %i, (state %i)\n", sys_event.code, sys_event.key_mod);
}
// some result?
if (sys_event.code != 0) return 0;
sys_event.type = SIM_NOEVENT;
sys_event.code = 0;
break;
}
case WM_CHAR: /* originally KeyPress */
sys_event.type = SIM_KEYBOARD;
sys_event.code = wParam;
break;
case WM_CLOSE:
if (AllDibData != NULL) {
sys_event.type = SIM_SYSTEM;
sys_event.code = SIM_SYSTEM_QUIT;
return FALSE;
}
break;
case WM_DESTROY:
sys_event.type = SIM_SYSTEM;
sys_event.code = SIM_SYSTEM_QUIT;
if (AllDibData == NULL) {
PostQuitMessage(0);
hwnd = NULL;
return TRUE;
}
return FALSE;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return FALSE;
}
static void internal_GetEvents(int wait)
{
sys_event.type = SIM_NOEVENT;
sys_event.code = 0;
do {
// wait for keybord/mouse event
GetMessage(&msg, NULL, 0, 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
if (sys_event.type == SIM_NOEVENT) {
switch (msg.message) {
default:
//printf("Unbekanntes Ereignis # %d!\n",msg.message);
sys_event.type = SIM_IGNORE_EVENT;
sys_event.code = 0;
}
}
} while (wait && sys_event.type == SIM_IGNORE_EVENT);
}
void GetEvents()
{
internal_GetEvents(TRUE);
}
void GetEventsNoWait()
{
sys_event.type = SIM_NOEVENT;
sys_event.code = 0;
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
internal_GetEvents(FALSE);
}
}
void show_pointer(int yesno)
{
ShowCursor(yesno);
}
void ex_ord_update_mx_my()
{
// evt. called before update
}
unsigned long dr_time(void)
{
return timeGetTime();
}
void dr_sleep(unsigned long usec)
{
Sleep(usec >> 10);
}
/************************** Winmain ***********************************/
int simu_main(int argc, char **argv);
BOOL APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSW wc;
char *argv[32], *p;
int argc;
wc.lpszClassName = L"Simu";
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(100));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND + 1);
wc.lpszMenuName = NULL;
RegisterClass(&wc);
// prepare commandline
argc = 0;
argv[argc++] = SAVEGAME_PREFIX;
p = strtok(lpCmdLine, " ");
while (p != NULL) {
argv[argc++] = p;
p = strtok(NULL, " ");
}
argv[argc] = NULL;
GetWindowRect(GetDesktopWindow(), &MaxSize);
simu_main(argc, argv);
return 0;
}