forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simsys_x8.cc
501 lines (380 loc) · 9.73 KB
/
simsys_x8.cc
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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <X11/extensions/XShm.h>
#include "simmain.h"
#include "simsys.h"
struct sys_event sys_event;
static int using_shm = FALSE;
static int doing_sync = TRUE;
static int display_depth = 8;
static int is_truecolor = FALSE;
static Cursor standard_cursor;
static Cursor invisible_cursor;
static unsigned long planetab[32];
static unsigned long coltab[256];
static unsigned long colortrans[256];
static Display* md; /* global fuer die Zeichenroutinen */
static Window mw;
static GC mgc;
static int ms;
static unsigned long mbg; /* Vorder/Hintergrund */
static unsigned long mfg;
static XSizeHints mh;
static Colormap cmap;
static int width = 640;
static int height = 480;
// Time at program start
static struct timeval start;
static void init_cursors(void)
{
static const char bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Pixmap pix;
XColor cfg;
// create invisible cursor
cfg.red = cfg.green = cfg.blue = 0;
pix = XCreateBitmapFromData(md, mw, bits, 8, 8);
invisible_cursor = XCreatePixmapCursor(md, pix, pix, &cfg, &cfg, 0, 0);
XFreePixmap(md, pix);
// create standard cursor
standard_cursor = XCreateFontCursor(md, 130);
}
int dr_os_init(const int* parameter)
{
using_shm = parameter[0];
doing_sync = parameter[1];
// init time count
gettimeofday(&start, NULL);
return TRUE;
}
int dr_os_open(int w, int h, int bpp, int fullscreen)
{
XSetWindowAttributes attr;
width = w;
height = h;
md = XOpenDisplay(NULL);
if (md == NULL) {
fprintf(stderr, "Kann Display nicht oeffnen!");
exit(1);
}
ms = DefaultScreen(md);
display_depth = DefaultDepth(md, ms);
fprintf(stderr, "Using %d bpp\n", display_depth);
if (display_depth == 8) {
is_truecolor = FALSE;
} else if(display_depth >= 15) {
is_truecolor = TRUE;
fprintf(stderr,"Warning: using experimental HiColor/TrueColor mode\n");
} else {
fprintf(
stderr,
"Problem: Simutrans braucht ein 8 Bit pseudo color visual (256 Farben mit Palette)\n"
" oder ein direct color/true color visual (HiColor)\n"
);
exit(2);
}
mbg = WhitePixel(md, ms);
mfg = BlackPixel(md, ms);
mh.x = 0; /* Fenster sollte im sichtbaren Bereich liegen */
mh.y = 0;
mh.width = width;
mh.height = height;
// set window size hints
// min_size = size = max_size -> no resizing
mh.min_width = mh.max_width = width;
mh.min_height = mh.max_height = height;
mh.flags = PSize | PMinSize | PMaxSize;
fprintf(stderr, "Opening program window ...\n");
mw = XCreateSimpleWindow(
md, DefaultRootWindow(md),
mh.x, mh.y, mh.width, mh.height, 5,
mfg, mbg
);
XSetStandardProperties(md, mw, "Simutrans", "Simu", None, NULL, 0, NULL);
mgc =XCreateGC(md, mw, 0, 0);
XSetBackground(md, mgc, mbg);
XSetForeground(md, mgc, mfg);
XSelectInput(
md, mw,
VisibilityChangeMask |
ButtonPressMask |
ButtonReleaseMask |
KeyPressMask |
KeyReleaseMask |
PointerMotionMask
);
attr.backing_store = Always;
XChangeWindowAttributes(md, mw, CWBackingStore, &attr);
if (!is_truecolor) {
cmap = XCreateColormap(md, mw, DefaultVisual(md, ms), AllocNone);
XAllocColorCells(md, cmap, 0, planetab, 0, coltab, 256);
XSetWindowColormap(md, mw, cmap);
}
XMapRaised(md, mw);
init_cursors();
XDefineCursor(md, mw, standard_cursor);
sys_event.mx = width / 2;
sys_event.my = height / 2;
XSetForeground(md, mgc, 1);
return TRUE;
}
int dr_os_close(void)
{
XCloseDisplay(md);
return TRUE;
}
static XImage* texturimg;
static XShmSegmentInfo xshminfo;
// this is used if we need to fake an 8 bit array
static unsigned char* data8;
int dr_textur_resize(unsigned short** textur, int w, int h, int bpp)
{
// XXX TODO implement
return 0;
}
unsigned short* dr_textur_init(void)
{
int depth = display_depth;
if (!using_shm) {
if (depth == 24) depth = 32;
if (depth == 15) depth = 16;
texturimg = XCreateImage(
md, DefaultVisual(md, ms),
display_depth, ZPixmap, 0,
malloc(width * height * (depth / 8)),
width, height, depth, width * (depth / 8)
);
printf("Texturimage: %p\n, ", texturimg);
} else {
if (depth == 24) depth = 32;
xshminfo.shmid = shmget(
IPC_PRIVATE, width * height * (depth / 8), IPC_CREAT | 0777
);
texturimg = XShmCreateImage(
md, DefaultVisual(md, ms),
display_depth, ZPixmap, NULL, &xshminfo,
width, height
);
printf("Texturimage: %p, shmid: %d\n", texturimg, xshminfo.shmid);
texturimg->data = (char*)shmat(xshminfo.shmid, 0, 0);
printf("Data: %p\n", texturimg->data);
xshminfo.shmaddr = texturimg->data;
xshminfo.readOnly = False;
printf("Attach: %d\n", XShmAttach(md, &xshminfo));
}
if (is_truecolor) {
// fake an 8 bit array and convert data before displaying
data8 = malloc(width * height);
return data8;
} else {
return texturimg->data;
}
}
void dr_textur(int xp, int yp, int w, int h)
{
// clipping unten
if (yp + h > height) h = height - yp;
// if we are in truecolor we need to convert our image data
if (is_truecolor) {
const unsigned char * src = data8 + xp + yp * width;
const int skip = width - w;
if (display_depth <= 16) {
unsigned short * dest = (unsigned short*)texturimg->data + xp + yp * width;
int hc = h;
do {
const unsigned char* const end = src + w;
do {
*dest++ = colortrans[*src++];
*dest++ = colortrans[*src++];
*dest++ = colortrans[*src++];
*dest++ = colortrans[*src++];
} while (src < end);
dest += skip;
src += skip;
} while (--hc > 0);
} else {
unsigned int* dest = (unsigned int*)texturimg->data + xp + yp * width;
int hc = h;
do {
const unsigned char* const end = src + w;
do {
*dest++ = colortrans[*src++];
*dest++ = colortrans[*src++];
*dest++ = colortrans[*src++];
*dest++ = colortrans[*src++];
} while(src < end);
dest += skip;
src += skip;
} while (--hc > 0);
}
}
if (using_shm) {
#if 0
printf("%d %d %d %d\n", xp, yp, w, h);
#endif
XShmPutImage(md, mw, mgc, texturimg, xp, yp, xp, yp, w, h, 0);
} else {
XPutImage(md, mw, mgc, texturimg, xp, yp, xp, yp, w, h);
}
}
void dr_flush(void)
{
if (doing_sync) XSync(md, FALSE);
}
void dr_setRGB8multi(int first, int count, unsigned char* data)
{
int n;
for (n = 0; n < count; n++) {
XColor xc;
xc.pixel = n + first;
xc.flags = DoRed | DoGreen | DoBlue;
xc.red = data[n * 3 + 0] << 8;
xc.green = data[n * 3 + 1] << 8;
xc.blue = data[n * 3 + 2] << 8;
if (is_truecolor) {
XAllocColor(md, DefaultColormap(md, ms), &xc);
#if 0
printf("Storing color %d with value %lx\n", n, xc.pixel);
#endif
colortrans[n] = xc.pixel;
} else {
XStoreColor(md, cmap, &xc);
}
}
XFlush(md);
}
void show_pointer(int yesno)
{
XDefineCursor(md, mw, yesno ? standard_cursor : invisible_cursor);
}
void move_pointer(int x, int y)
{
XWarpPointer(md, None, mw, 0, 0, width, height, x, y);
}
void set_pointer(int loading)
{
// XXX TODO implement
}
int dr_screenshot(const char *filename)
{
// XXX TODO implement
return 0;
}
/*
* Hier sind die Funktionen zur Messageverarbeitung
*/
static void internal_GetEvents(int wait)
{
XEvent event;
if (wait) {
do {
XNextEvent(md, &event);
} while (
event.type == GraphicsExpose ||
event.type == NoExpose ||
(XEventsQueued(md, QueuedAlready) > 0 && event.type == MotionNotify)
);
} else {
do {
XNextEvent(md, &event);
} while (
event.type == GraphicsExpose ||
event.type == NoExpose ||
(XEventsQueued(md, QueuedAfterFlush) > 0 && event.type == MotionNotify)
);
}
switch (event.type) {
case ButtonPress:
sys_event.type = SIM_MOUSE_BUTTONS;
sys_event.mx = event.xbutton.x;
sys_event.my = event.xbutton.y;
switch (event.xbutton.button) {
case 1: sys_event.code = SIM_MOUSE_LEFTBUTTON; break;
case 2: sys_event.code = SIM_MOUSE_MIDBUTTON; break;
case 3: sys_event.code = SIM_MOUSE_RIGHTBUTTON; break;
case 4: sys_event.code = SIM_MOUSE_WHEELUP; break;
case 5: sys_event.code = SIM_MOUSE_WHEELDOWN; break;
}
break;
case ButtonRelease:
sys_event.type = SIM_MOUSE_BUTTONS;
sys_event.mx = event.xbutton.x;
sys_event.my = event.xbutton.y;
switch (event.xbutton.button) {
case 1: sys_event.code = SIM_MOUSE_LEFTUP; break;
case 2: sys_event.code = SIM_MOUSE_MIDUP; break;
case 3: sys_event.code = SIM_MOUSE_RIGHTUP; break;
}
break;
case KeyPress: {
KeySym mykey;
char text[4];
sys_event.type = SIM_KEYBOARD;
if (XLookupString(&event.xkey, text, 1, &mykey, 0)) {
sys_event.code = text[0];
} else {
sys_event.code = 0;
}
break;
}
case MotionNotify:
sys_event.type = SIM_MOUSE_MOVE;
sys_event.code = SIM_MOUSE_MOVED;
sys_event.mx = event.xmotion.x;
sys_event.my = event.xmotion.y;
break;
case VisibilityNotify:
dr_textur(0, 0, width, height);
sys_event.type = SIM_IGNORE_EVENT;
sys_event.code = 0;
break;
case KeyRelease:
sys_event.type = SIM_IGNORE_EVENT;
sys_event.code = 0;
break;
default:
sys_event.type = SIM_IGNORE_EVENT;
sys_event.code = 0;
break;
}
}
void GetEvents(void)
{
internal_GetEvents(TRUE);
}
void GetEventsNoWait(void)
{
int n = XEventsQueued(md, QueuedAfterFlush);
sys_event.type = SIM_NOEVENT;
sys_event.code = 0;
if (n > 0) internal_GetEvents(FALSE);
}
void ex_ord_update_mx_my(void)
{
// wird fuer X nicht benoetigt
}
unsigned long dr_time(void)
{
struct timeval now;
gettimeofday(&now, NULL);
return
(now.tv_sec - start.tv_sec) * 1000 +
(now.tv_usec - start.tv_usec) / 1000;
}
void dr_sleep(unsigned long usec)
{
if (usec > 1000) usleep(usec);
}
int main(int argc, char **argv)
{
return simu_main(argc, argv);
}