-
Notifications
You must be signed in to change notification settings - Fork 2
/
vi_sdl.c
525 lines (456 loc) · 8.94 KB
/
vi_sdl.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
#include "wl_def.h"
#include "SDL/SDL.h"
static SDL_Surface *surface;
static unsigned myint sdl_palettemode;
byte framebuffer[128 * 64 / 2];
extern void keyboard_handler(myint code, myint press);
extern boolean InternalKeyboard[NumCodes];
myint main (myint argc, char *argv[])
{
return WolfMain(argc, argv);
}
void DisplayTextSplash(byte *text);
/*
==========================
=
= Quit
=
==========================
*/
void Quit(const char *error)
{
#ifndef EMBEDDED
memptr screen = NULL;
if (!error || !*error) {
CA_CacheGrChunk(ORDERSCREEN);
screen = grsegs[ORDERSCREEN];
WriteConfig();
} else if (error) {
CA_CacheGrChunk(ERRORSCREEN);
screen = grsegs[ERRORSCREEN];
}
ShutdownId();
if (screen) {
//DisplayTextSplash(screen);
}
#endif
if (error && *error) {
fprintf(stderr, "Quit: %s\n", error);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
void VL_WaitVBL(myint vbls)
{
unsigned long last = get_TimeCount() + vbls;
while (last > get_TimeCount()) ;
}
void VW_UpdateScreen()
{
#ifdef EMBEDDED
byte *out;
byte *in;
int x;
int y;
byte c;
out = surface->pixels;
in = framebuffer;
for (y = 0; y < 64; y++)
{
for (x = 0; x < 64; x++)
{
c = *(in++);
out[(x << 1)] = c >> 4;
out[(x << 1) + 1] = c & 0xf;
}
out += surface->pitch;
}
#endif
SDL_Flip(surface);
#ifndef EMBEDDED
gfxbuf = surface->pixels;
#endif
}
/*
=======================
=
= VL_Startup
=
=======================
*/
void VL_Startup()
{
const SDL_VideoInfo *vinfo;
myint flags;
#ifndef EMBEDDED
vwidth = 320;
vheight = 200;
if (MS_CheckParm("x2")) {
vwidth *= 2;
vheight *= 2;
} else if (MS_CheckParm("x3")) {
vwidth *= 3;
vheight *= 3;
}
#endif
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0) {
Quit("Couldn't init SDL");
}
vinfo = SDL_GetVideoInfo();
sdl_palettemode = (vinfo->vfmt->BitsPerPixel == 8) ? (SDL_PHYSPAL|SDL_LOGPAL) : SDL_LOGPAL;
flags = SDL_SWSURFACE|SDL_HWPALETTE|SDL_DOUBLEBUF;
#ifndef EMBEDDED
if (MS_CheckParm("fullscreen"))
flags |= SDL_FULLSCREEN;
#endif
surface = SDL_SetVideoMode(vwidth, vheight, 8, flags);
if (surface == NULL) {
SDL_Quit();
Quit("(SDL) Couldn't set video mode");
}
#ifndef EMBEDDED
gfxbuf = surface->pixels;
#else
{
SDL_Color colors[256];
myint i;
for (i = 0; i < 15; i++)
{
colors[i].r = i << 4;
colors[i].g = i << 4;
colors[i].b = i << 4;
}
SDL_SetPalette(surface, sdl_palettemode, colors, 0, 16);
}
#endif
if (surface->flags & SDL_FULLSCREEN)
SDL_ShowCursor(0);
SDL_WM_SetCaption(GAMENAME, GAMENAME);
}
/*
=======================
=
= VL_Shutdown
=
=======================
*/
void VL_Shutdown()
{
SDL_Quit();
}
/* ======================================================================== */
#ifndef EMBEDDED
/*
=================
=
= VL_SetPalette
=
=================
*/
void VL_SetPalette(const byte *palette)
{
SDL_Color colors[256];
myint i;
VL_WaitVBL(1);
for (i = 0; i < 256; i++)
{
colors[i].r = palette[i*3+0] << 2;
colors[i].g = palette[i*3+1] << 2;
colors[i].b = palette[i*3+2] << 2;
}
SDL_SetPalette(surface, sdl_palettemode, colors, 0, 256);
}
/*
=================
=
= VL_GetPalette
=
=================
*/
void VL_GetPalette(byte *palette)
{
myint i;
for (i=0;i<256;i++)
{
palette[i*3+0] = surface->format->palette->colors[i].r >> 2;
palette[i*3+1] = surface->format->palette->colors[i].g >> 2;
palette[i*3+2] = surface->format->palette->colors[i].b >> 2;
}
}
#endif
static myint XKeysymToScancode(unsigned myint keysym)
{
switch (keysym) {
case SDLK_KP_ENTER:
case SDLK_RETURN:
return sc_Enter;
case SDLK_ESCAPE:
return sc_Escape;
case SDLK_SPACE:
return sc_Space;
case SDLK_BACKSPACE:
return sc_BackSpace;
case SDLK_TAB:
return sc_Tab;
case SDLK_LALT:
case SDLK_RALT:
return sc_Alt;
case SDLK_LCTRL:
case SDLK_RCTRL:
return sc_Control;
case SDLK_CAPSLOCK:
return sc_CapsLock;
case SDLK_LSHIFT:
return sc_LShift;
case SDLK_RSHIFT:
return sc_RShift;
case SDLK_UP:
case SDLK_KP8:
return sc_UpArrow;
case SDLK_DOWN:
case SDLK_KP2:
return sc_DownArrow;
case SDLK_LEFT:
case SDLK_KP4:
return sc_LeftArrow;
case SDLK_RIGHT:
case SDLK_KP6:
return sc_RightArrow;
case SDLK_HOME:
return sc_Home;
case SDLK_END:
return sc_End;
case SDLK_PAGEUP:
return sc_PgUp;
case SDLK_PAGEDOWN:
return sc_PgDn;
case SDLK_INSERT:
return sc_Insert;
case SDLK_DELETE:
return sc_Delete;
case SDLK_F1:
return sc_F1;
case SDLK_F2:
return sc_F2;
case SDLK_F3:
return sc_F3;
case SDLK_F4:
return sc_F4;
case SDLK_F5:
return sc_F5;
case SDLK_F6:
return sc_F6;
case SDLK_F7:
return sc_F7;
case SDLK_F8:
return sc_F8;
case SDLK_F9:
return sc_F9;
case SDLK_F10:
return sc_F10;
case SDLK_F11:
return sc_F11;
case SDLK_F12:
return sc_F12;
case SDLK_1:
return sc_1;
case SDLK_2:
return sc_2;
case SDLK_3:
return sc_3;
case SDLK_4:
return sc_4;
case SDLK_5:
return sc_5;
case SDLK_6:
return sc_6;
case SDLK_7:
return sc_7;
case SDLK_8:
return sc_8;
case SDLK_9:
return sc_9;
case SDLK_0:
return sc_0;
case SDLK_a:
return sc_A;
case SDLK_b:
return sc_B;
case SDLK_c:
return sc_C;
case SDLK_d:
return sc_D;
case SDLK_e:
return sc_E;
case SDLK_f:
return sc_F;
case SDLK_g:
return sc_G;
case SDLK_h:
return sc_H;
case SDLK_i:
return sc_I;
case SDLK_j:
return sc_J;
case SDLK_k:
return sc_K;
case SDLK_l:
return sc_L;
case SDLK_m:
return sc_M;
case SDLK_n:
return sc_N;
case SDLK_o:
return sc_O;
case SDLK_p:
return sc_P;
case SDLK_q:
return sc_Q;
case SDLK_r:
return sc_R;
case SDLK_s:
return sc_S;
case SDLK_t:
return sc_T;
case SDLK_u:
return sc_U;
case SDLK_v:
return sc_V;
case SDLK_w:
return sc_W;
case SDLK_x:
return sc_X;
case SDLK_y:
return sc_Y;
case SDLK_z:
return sc_Z;
case SDLK_PAUSE:
return 0xE1;
default:
return sc_None;
}
}
void INL_Update()
{
SDL_Event event;
if (SDL_PollEvent(&event)) {
do {
switch(event.type) {
case SDL_KEYDOWN:
keyboard_handler(XKeysymToScancode(event.key.keysym.sym), 1);
break;
case SDL_KEYUP:
keyboard_handler(XKeysymToScancode(event.key.keysym.sym), 0);
break;
case SDL_QUIT:
Quit(NULL);
break;
default:
break;
}
} while (SDL_PollEvent(&event));
}
#ifndef EMBEDDED
if (InternalKeyboard[sc_Alt] &&
(!DebouncedKeyboard[sc_Return] && InternalKeyboard[sc_Return])) {
SDL_GrabMode gm;
SDL_WM_ToggleFullScreen(surface);
gm = SDL_WM_GrabInput(SDL_GRAB_QUERY);
if (gm == SDL_GRAB_OFF && !(surface->flags & SDL_FULLSCREEN))
SDL_ShowCursor(1);
else
SDL_ShowCursor(0);
}
if (InternalKeyboard[sc_Control] &&
(!DebouncedKeyboard[sc_G] && InternalKeyboard[sc_G])) {
SDL_GrabMode gm;
gm = SDL_WM_GrabInput(SDL_GRAB_QUERY);
SDL_WM_GrabInput((gm == SDL_GRAB_ON) ? SDL_GRAB_OFF : SDL_GRAB_ON);
gm = SDL_WM_GrabInput(SDL_GRAB_QUERY);
if (gm == SDL_GRAB_OFF && !(surface->flags & SDL_FULLSCREEN))
SDL_ShowCursor(1);
else
SDL_ShowCursor(0);
}
#endif
/* ctrl-z for iconify window? */
}
void IN_GetMouseDelta(myint *dx, myint *dy)
{
myint x, y;
SDL_GetRelativeMouseState(&x, &y);
if (dx)
*dx = x;
if (dy)
*dy = y;
}
byte IN_MouseButtons()
{
Uint8 state;
byte retr;
state = SDL_GetMouseState(NULL, NULL);
retr = 0;
if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) {
retr |= 1;
}
if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
retr |= 2;
}
if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) {
retr |= 4;
}
return retr;
}
#ifndef EMBEDDED
/*
===================
=
= IN_JoyButtons
=
===================
*/
byte IN_JoyButtons()
{
return 0;
}
#endif
///////////////////////////////////////////////////////////////////////////
//
// IN_GetJoyAbs() - Reads the absolute position of the specified joystick
//
///////////////////////////////////////////////////////////////////////////
void IN_GetJoyAbs(word joy,word *xp,word *yp)
{
*xp = 0;
*yp = 0;
}
///////////////////////////////////////////////////////////////////////////
//
// INL_GetJoyDelta() - Returns the relative movement of the specified
// joystick (from +/-127)
//
///////////////////////////////////////////////////////////////////////////
void INL_GetJoyDelta(word joy,myint *dx,myint *dy)
{
*dx = 0;
*dy = 0;
}
///////////////////////////////////////////////////////////////////////////
//
// INL_GetJoyButtons() - Returns the button status of the specified
// joystick
//
///////////////////////////////////////////////////////////////////////////
word INL_GetJoyButtons(word joy)
{
return 0;
}
///////////////////////////////////////////////////////////////////////////
//
// IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale()
// to set up scaling values
//
///////////////////////////////////////////////////////////////////////////
void IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy)
{
}