-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdl_driver.c
More file actions
2563 lines (2097 loc) · 67.8 KB
/
sdl_driver.c
File metadata and controls
2563 lines (2097 loc) · 67.8 KB
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* An SDL replacement for BUILD's VESA code.
*
* Written by Ryan C. Gordon. (icculus@clutteredmind.org)
*
* Please do NOT harrass Ken Silverman about any code modifications
* (including this file) to BUILD.
*/
/*
* "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman
* Ken Silverman's official web site: "http://www.advsys.net/ken"
* See the included license file "BUILDLIC.TXT" for license info.
* This file IS NOT A PART OF Ken Silverman's original release
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef PLATFORM_WIN32
#include <sys/param.h>
#endif
#include "platform.h"
#if (!defined PLATFORM_SUPPORTS_SDL)
#error This platform apparently does not use SDL. Do not compile this.
#endif
#include "SDL.h"
#include "build.h"
#include "display.h"
#include "pragmas.h"
#include "engine.h"
#if (defined USE_OPENGL)
#include "buildgl.h"
#endif
/* need VirtualProtect() from win32 API... */
#if ((defined PLATFORM_WIN32) && (defined USE_I386_ASM))
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif
typedef enum
{
RENDERER_SOFTWARE,
RENDERER_OPENGL3D,
RENDERER_TOTAL
} sdl_renderer_type;
const char *renderer_name[RENDERER_TOTAL];
#define ENVRSTR_RENDERER_SOFTWARE "software"
#define ENVRSTR_RENDERER_OPENGL3D "opengl3d"
static sdl_renderer_type renderer = RENDERER_SOFTWARE;
/* !!! ugh. Clean this up. */
#if (!defined __WATCOMC__)
#include "a.h"
#else
extern long setvlinebpl(long);
#pragma aux setvlinebpl parm [eax];
#endif /* __WATCOMC__ */
#include "cache1d.h"
/*
* !!! I'd like this to be temporary. --ryan.
* !!! That is, the self-modifying part, so I can ditch the mprotect() stuff.
*/
#if ((defined PLATFORM_UNIX) && (defined USE_I386_ASM))
#include <sys/mman.h>
#include <limits.h>
#ifndef PAGESIZE
#define PAGESIZE 4096
#endif
#endif
#ifdef PLATFORM_MACOSX
#include <CoreServices/CoreServices.h>
#endif
/*
* !!! remove the surface_end checks, for speed's sake. They are a
* !!! needed safety right now. --ryan.
*/
#define DEFAULT_MAXRESWIDTH 1600
#define DEFAULT_MAXRESHEIGHT 1200
#define UNLOCK_SURFACE_AND_RETURN if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); return;
static unsigned char tempbuf[MAXWALLS];
static char pcxheader[128] =
{
0xa,0x5,0x1,0x8,0x0,0x0,0x0,0x0,0x3f,0x1,0xc7,0x0,
0x40,0x1,0xc8,0x0,0x0,0x0,0x0,0x8,0x8,0x8,0x10,0x10,
0x10,0x18,0x18,0x18,0x20,0x20,0x20,0x28,0x28,0x28,0x30,0x30,
0x30,0x38,0x38,0x38,0x40,0x40,0x40,0x48,0x48,0x48,0x50,0x50,
0x50,0x58,0x58,0x58,0x60,0x60,0x60,0x68,0x68,0x68,0x70,0x70,
0x70,0x78,0x78,0x78,0x0,0x1,0x40,0x1,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
};
static char vgapal16[48] =
{
00,00,00,00,00,42,00,42,00,00,42,42,42,00,00,42,00,42,42,21,00,42,42,42,
21,21,21,21,21,63,21,63,21,21,63,63,63,21,21,63,21,63,63,63,21,63,63,63,
};
int _argc = 0;
char **_argv = NULL;
/* !!! move these elsewhere? */
long xres, yres, bytesperline, frameplace, imageSize, maxpages;
char *screen, vesachecked;
long buffermode, origbuffermode, linearmode;
char permanentupdate = 0, vgacompatible;
SDL_Surface *surface = NULL; /* This isn't static so that we can use it elsewhere AH */
static int debug_hall_of_mirrors = 0;
static Uint32 sdl_flags = SDL_HWPALETTE;
static long mouse_x = 0;
static long mouse_y = 0;
static long mouse_relative_x = 0;
static long mouse_relative_y = 0;
static short mouse_buttons = 0;
static int mouse_grabbed = 0;
static unsigned int lastkey = 0;
static SDL_TimerID primary_timer = NULL;
/* so we can make use of setcolor16()... - DDOI */
static unsigned char drawpixel_color=0;
/* These hold the colors for the 256 color palette when in 16-color mode - DDOI */
static char backup_palette[48];
static int in_egapalette = 0;
/* The standard EGA palette in BUILD format - DDOI */
static char egapalette[] = { 0, 0, 0,
0, 0, 42,
0, 42, 0,
0, 42, 42,
42, 0, 0,
42, 0, 42,
42, 21, 0,
42, 42, 42,
21, 21, 21,
21, 21, 63,
21, 63, 21,
21, 63, 63,
63, 21, 21,
63, 21, 63,
63, 63, 21,
63, 63, 63
};
static unsigned int scancodes[SDLK_LAST];
static long last_render_ticks = 0;
long total_render_time = 1;
long total_rendered_frames = 0;
static char *titlelong = NULL;
static char *titleshort = NULL;
void restore256_palette (void);
void set16color_palette (void);
static FILE *_sdl_debug_file = NULL;
#ifdef DC
#define sdldebug(fmt,args...) printf("BUILDSDL: " fmt "\n",##args)
#else
static __inline void __out_sdldebug(const char *subsystem,
const char *fmt, va_list ap)
{
fprintf(_sdl_debug_file, "%s: ", subsystem);
vfprintf(_sdl_debug_file, fmt, ap);
fprintf(_sdl_debug_file, "\n");
fflush(_sdl_debug_file);
} /* __out_sdldebug */
static void sdldebug(const char *fmt, ...)
{
va_list ap;
if (_sdl_debug_file)
{
va_start(ap, fmt);
__out_sdldebug("BUILDSDL", fmt, ap);
va_end(ap);
} /* if */
} /* sdldebug */
#endif
#if (defined USE_OPENGL)
void sgldebug(const char *fmt, ...)
{
va_list ap;
if (_sdl_debug_file)
{
va_start(ap, fmt);
__out_sdldebug("BUILDSDL/GL", fmt, ap);
va_end(ap);
} /* if */
} /* sgldebug */
#endif
static void __append_sdl_surface_flag(SDL_Surface *_surface, char *str,
size_t strsize, Uint32 flag,
const char *flagstr)
{
if (_surface->flags & flag)
{
if ( (strlen(str) + strlen(flagstr)) >= (strsize - 1) )
strcpy(str + (strsize - 5), " ...");
else
strcat(str, flagstr);
} /* if */
} /* append_sdl_surface_flag */
#define append_sdl_surface_flag(a, b, c, fl) __append_sdl_surface_flag(a, b, c, fl, " " #fl)
#define print_tf_state(str, val) sdldebug("%s: {%s}", str, (val) ? "true" : "false" )
static void output_surface_info(SDL_Surface *_surface)
{
const SDL_VideoInfo *info;
char f[256];
if (!_sdl_debug_file)
return;
if (_surface == NULL)
{
sdldebug("-WARNING- You've got a NULL screen surface!");
}
else
{
f[0] = '\0';
sdldebug("screen surface is (%dx%dx%dbpp).",
_surface->w, _surface->h, _surface->format->BitsPerPixel);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_SWSURFACE);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_HWSURFACE);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_ASYNCBLIT);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_ANYFORMAT);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_HWPALETTE);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_DOUBLEBUF);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_FULLSCREEN);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_OPENGL);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_OPENGLBLIT);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_RESIZABLE);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_NOFRAME);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_HWACCEL);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_SRCCOLORKEY);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_RLEACCELOK);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_RLEACCEL);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_SRCALPHA);
append_sdl_surface_flag(_surface, f, sizeof (f), SDL_PREALLOC);
if (f[0] == '\0')
strcpy(f, " (none)");
sdldebug("New vidmode flags:%s", f);
info = SDL_GetVideoInfo();
assert(info != NULL);
print_tf_state("hardware surface available", info->hw_available);
print_tf_state("window manager available", info->wm_available);
print_tf_state("accelerated hardware->hardware blits", info->blit_hw);
print_tf_state("accelerated hardware->hardware colorkey blits", info->blit_hw_CC);
print_tf_state("accelerated hardware->hardware alpha blits", info->blit_hw_A);
print_tf_state("accelerated software->hardware blits", info->blit_sw);
print_tf_state("accelerated software->hardware colorkey blits", info->blit_sw_CC);
print_tf_state("accelerated software->hardware alpha blits", info->blit_sw_A);
print_tf_state("accelerated color fills", info->blit_fill);
sdldebug("video memory: (%d)", info->video_mem);
} /* else */
} /* output_surface_info */
static void output_driver_info(void)
{
char buffer[256];
if (!_sdl_debug_file)
return;
sdldebug("Using BUILD renderer \"%s\".", renderer_name[renderer]);
if (SDL_VideoDriverName(buffer, sizeof (buffer)) == NULL)
{
sdldebug("-WARNING- SDL_VideoDriverName() returned NULL!");
} /* if */
else
{
sdldebug("Using SDL video driver \"%s\".", buffer);
} /* else */
} /* output_driver_info */
static Uint8 *get_framebuffer(void)
{
assert(renderer != RENDERER_OPENGL3D);
if (renderer == RENDERER_SOFTWARE)
return((Uint8 *) surface->pixels);
else if (renderer == RENDERER_OPENGL3D)
return((Uint8 *) frameplace);
return(NULL);
} /* get_framebuffer */
int using_opengl(void)
{
return(renderer == RENDERER_OPENGL3D);
} /* using_opengl */
/*
* !!! This is almost an entire copy of the original setgamemode().
* !!! Figure out what is needed for just 2D mode, and separate that
* !!! out. Then, place the original setgamemode() back into engine.c,
* !!! and remove our simple implementation (and this function.)
* !!! Just be sure to keep the non-DOS things, like the window's
* !!! titlebar caption. --ryan.
*/
static char screenalloctype = 255;
static void init_new_res_vars(int davidoption)
{
int i = 0;
int j = 0;
setupmouse();
SDL_WM_SetCaption(titlelong, titleshort);
xdim = xres = surface->w;
ydim = yres = surface->h;
bytesperline = surface->w;
vesachecked = 1;
vgacompatible = 1;
linearmode = 1;
qsetmode = surface->h;
activepage = visualpage = 0;
horizlookup = horizlookup2 = NULL;
if (renderer == RENDERER_OPENGL3D)
frameplace = (long) NULL;
else
frameplace = (long) ( ((Uint8 *) surface->pixels) );
if (screen != NULL)
{
if (screenalloctype == 0) kkfree((void *)screen);
if (screenalloctype == 1) suckcache((long *)screen);
screen = NULL;
} /* if */
if (davidoption != -1)
{
switch(vidoption)
{
case 1:i = xdim*ydim; break;
case 2: xdim = 320; ydim = 200; i = xdim*ydim; break;
case 6: xdim = 320; ydim = 200; i = 131072; break;
default: assert(0);
}
j = ydim*4*sizeof(long); /* Leave room for horizlookup&horizlookup2 */
screenalloctype = 0;
if ((screen = (char *)kkmalloc(i+(j<<1))) == NULL)
{
allocache((long *)&screen,i+(j<<1),&permanentlock);
screenalloctype = 1;
}
/* !!! FIXME: Should screen get allocated above if in opengl3d mode? */
if (renderer == RENDERER_OPENGL3D)
frameplace = (long) NULL;
else
{
frameplace = FP_OFF(screen);
horizlookup = (long *)(frameplace+i);
horizlookup2 = (long *)(frameplace+i+j);
} /* else */
} /* if */
j = 0;
for(i = 0; i <= ydim; i++)
{
ylookup[i] = j;
j += bytesperline;
} /* for */
horizycent = ((ydim*4)>>1);
/* Force drawrooms to call dosetaspect & recalculate stuff */
oxyaspect = oxdimen = oviewingrange = -1;
setvlinebpl(bytesperline);
if (davidoption != -1)
{
setview(0L,0L,xdim-1,ydim-1);
clearallviews(0L);
} /* if */
setbrightness((char) curbrightness, (unsigned char *) &palette[0]);
if (searchx < 0) { searchx = halfxdimen; searchy = (ydimen>>1); }
} /* init_new_res_vars */
static void go_to_new_vid_mode(int vidoption, int w, int h)
{
getvalidvesamodes();
SDL_ClearError();
surface = SDL_SetVideoMode(w, h, 8, sdl_flags);
if (surface == NULL)
{
fprintf(stderr, "BUILDSDL: Failed to set %dx%d video mode!\n"
"BUILDSDL: SDL_Error() says [%s].\n",
w, h, SDL_GetError());
SDL_Quit();
exit(13);
} /* if */
output_surface_info(surface);
init_new_res_vars(vidoption);
} /* go_to_new_vid_mode */
static __inline int sdl_mouse_button_filter(SDL_MouseButtonEvent const *event)
{
/*
* What bits BUILD expects:
* 0 left button pressed if 1
* 1 right button pressed if 1
* 2 middle button pressed if 1
*
* (That is, this is what Int 33h (AX=0x05) returns...)
*
* additionally 3&4 are set for the mouse wheel
*/
Uint8 button = event->button;
if (button >= sizeof (mouse_buttons) * 8)
return(0);
if (button == SDL_BUTTON_RIGHT)
button = SDL_BUTTON_MIDDLE;
else if (button == SDL_BUTTON_MIDDLE)
button = SDL_BUTTON_RIGHT;
if (((const SDL_MouseButtonEvent*)event)->state)
mouse_buttons |= 1<<(button-1);
else if (button != 4 && button != 5)
mouse_buttons ^= 1<<(button-1);
#if 0
Uint8 bmask = SDL_GetMouseState(NULL, NULL);
mouse_buttons = 0;
if (bmask & SDL_BUTTON_LMASK) mouse_buttons |= 1;
if (bmask & SDL_BUTTON_RMASK) mouse_buttons |= 2;
if (bmask & SDL_BUTTON_MMASK) mouse_buttons |= 4;
#endif
return(0);
} /* sdl_mouse_up_filter */
static int sdl_mouse_motion_filter(SDL_Event const *event)
{
if (surface == NULL)
return(0);
if (event->type == SDL_JOYBALLMOTION)
{
mouse_relative_x = event->jball.xrel/100;
mouse_relative_y = event->jball.yrel/100;
mouse_x += mouse_relative_x;
mouse_y += mouse_relative_y;
} /* if */
else
{
if (mouse_grabbed)
{
mouse_relative_x += event->motion.xrel;
mouse_relative_y += event->motion.yrel;
mouse_x += mouse_relative_x;
mouse_y += mouse_relative_y;
} /* if */
else
{
mouse_relative_x += event->motion.x - mouse_x;
mouse_relative_y += event->motion.y - mouse_y;
mouse_x = event->motion.x;
mouse_y = event->motion.y;
} /* else */
} /* else */
if (mouse_x < 0) mouse_x = 0;
if (mouse_x > surface->w) mouse_x = surface->w;
if (mouse_y < 0) mouse_y = 0;
if (mouse_y > surface->h) mouse_y = surface->h;
return(0);
} /* sdl_mouse_motion_filter */
/**
* Attempt to flip the video surface to fullscreen or windowed mode.
* Attempts to maintain the surface's state, but makes no guarantee
* that pointers (i.e., the surface's pixels field) will be the same
* after this call.
*
* Caveats: Your surface pointers will be changing; if you have any other
* copies laying about, they are invalidated.
*
* Do NOT call this from an SDL event filter on Windows. You can
* call it based on the return values from SDL_PollEvent, etc, just
* not during the function you passed to SDL_SetEventFilter().
*
* Thread safe? Likely not.
*
* @param surface pointer to surface ptr to toggle. May be different
* pointer on return. MAY BE NULL ON RETURN IF FAILURE!
* @param flags pointer to flags to set on surface. The value pointed
* to will be XOR'd with SDL_FULLSCREEN before use. Actual
* flags set will be filled into pointer. Contents are
* undefined on failure. Can be NULL, in which case the
* surface's current flags are used.
* @return non-zero on success, zero on failure.
*/
static int attempt_fullscreen_toggle(SDL_Surface **surface, Uint32 *flags)
{
long framesize = 0;
void *pixels = NULL;
SDL_Rect clip;
Uint32 tmpflags = 0;
int w = 0;
int h = 0;
int bpp = 0;
int grabmouse = (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON);
int showmouse = SDL_ShowCursor(-1);
#if 0
SDL_Color *palette = NULL;
int ncolors = 0;
#endif
sdldebug("attempting to toggle fullscreen flag...");
if ( (!surface) || (!(*surface)) ) /* don't try if there's no surface. */
{
sdldebug("Null surface (?!). Not toggling fullscreen flag.");
return(0);
} /* if */
if (SDL_WM_ToggleFullScreen(*surface))
{
sdldebug("SDL_WM_ToggleFullScreen() seems to work on this system.");
if (flags)
*flags ^= SDL_FULLSCREEN;
return(1);
} /* if */
#if !PLATFORM_MACOSX
if ( !(SDL_GetVideoInfo()->wm_available) )
{
sdldebug("No window manager. Not toggling fullscreen flag.");
return(0);
} /* if */
#endif
sdldebug("toggling fullscreen flag The Hard Way...");
tmpflags = (*surface)->flags;
w = (*surface)->w;
h = (*surface)->h;
bpp = (*surface)->format->BitsPerPixel;
if (flags == NULL) /* use the surface's flags. */
flags = &tmpflags;
SDL_GetClipRect(*surface, &clip);
/* save the contents of the screen. */
if ( (!(tmpflags & SDL_OPENGL)) && (!(tmpflags & SDL_OPENGLBLIT)) )
{
framesize = (w * h) * ((*surface)->format->BytesPerPixel);
pixels = malloc(framesize);
if (pixels == NULL)
return(0);
memcpy(pixels, (*surface)->pixels, framesize);
} /* if */
#if 0
if ((*surface)->format->palette != NULL)
{
ncolors = (*surface)->format->palette->ncolors;
palette = malloc(ncolors * sizeof (SDL_Color));
if (palette == NULL)
{
free(pixels);
return(0);
} /* if */
memcpy(palette, (*surface)->format->palette->colors,
ncolors * sizeof (SDL_Color));
} /* if */
#endif
if (grabmouse)
SDL_WM_GrabInput(SDL_GRAB_OFF);
SDL_ShowCursor(1);
*surface = SDL_SetVideoMode(w, h, bpp, (*flags) ^ SDL_FULLSCREEN);
if (*surface != NULL)
*flags ^= SDL_FULLSCREEN;
else /* yikes! Try to put it back as it was... */
{
*surface = SDL_SetVideoMode(w, h, bpp, tmpflags);
if (*surface == NULL) /* completely screwed. */
{
if (pixels != NULL)
free(pixels);
if (palette != NULL)
free(palette);
return(0);
} /* if */
} /* if */
/* Unfortunately, you lose your OpenGL image until the next frame... */
if (pixels != NULL)
{
memcpy((*surface)->pixels, pixels, framesize);
free(pixels);
} /* if */
#if 0
if (palette != NULL)
{
/* !!! FIXME : No idea if that flags param is right. */
SDL_SetPalette(*surface, SDL_LOGPAL, palette, 0, ncolors);
free(palette);
} /* if */
#else
setbrightness((char) curbrightness, (unsigned char *) &palette[0]);
#endif
SDL_SetClipRect(*surface, &clip);
if (grabmouse)
SDL_WM_GrabInput(SDL_GRAB_ON);
SDL_ShowCursor(showmouse);
output_surface_info(*surface);
return(1);
} /* attempt_fullscreen_toggle */
/*
* The windib driver can't alert us to the keypad enter key, which
* Ken's code depends on heavily. It sends it as the same key as the
* regular return key. These users will have to hit SHIFT-ENTER,
* which we check for explicitly, and give the engine a keypad enter
* enter event.
*/
static __inline int handle_keypad_enter_hack(const SDL_Event *event)
{
static int kp_enter_hack = 0;
int retval = 0;
if (event->key.keysym.sym == SDLK_RETURN)
{
if (event->key.state == SDL_PRESSED)
{
if (event->key.keysym.mod & KMOD_SHIFT)
{
kp_enter_hack = 1;
lastkey = scancodes[SDLK_KP_ENTER];
retval = 1;
} /* if */
} /* if */
else /* key released */
{
if (kp_enter_hack)
{
kp_enter_hack = 0;
lastkey = scancodes[SDLK_KP_ENTER];
retval = 1;
} /* if */
} /* if */
} /* if */
return(retval);
} /* handle_keypad_enter_hack */
static int sdl_key_filter(const SDL_Event *event)
{
SDL_GrabMode grab_mode = SDL_GRAB_OFF;
int extended;
int tmp;
#if PLATFORM_MACOSX /* Apple-Q */
{
static Uint32 cmdqticks = 0;
if ( (event->key.keysym.sym == SDLK_q) &&
(event->key.state == SDL_PRESSED) &&
(event->key.keysym.mod & KMOD_META) )
{
Uint32 t = SDL_GetTicks();
if (t - cmdqticks < 500) /* 2 hits within .5 second? */
{
SDL_Quit();
exit(0);
}
cmdqticks = t;
}
}
#endif
if ( (event->key.keysym.sym == SDLK_g) &&
(event->key.state == SDL_PRESSED) &&
(event->key.keysym.mod & KMOD_CTRL) )
{
mouse_grabbed = ((mouse_grabbed) ? 0 : 1);
if (mouse_grabbed)
grab_mode = SDL_GRAB_ON;
SDL_WM_GrabInput(grab_mode);
return(0);
} /* if */
else if ( ( (event->key.keysym.sym == SDLK_RETURN) ||
(event->key.keysym.sym == SDLK_KP_ENTER) ) &&
(event->key.state == SDL_PRESSED) &&
(event->key.keysym.mod & KMOD_ALT) )
{
tmp = ((void *) frameplace == (void *) surface->pixels);
attempt_fullscreen_toggle(&surface, &sdl_flags);
assert(surface != NULL);
if (tmp)
frameplace = (long) surface->pixels;
return(0);
} /* if */
if (!handle_keypad_enter_hack(event))
lastkey = scancodes[event->key.keysym.sym];
if (lastkey == 0x0000) /* No DOS equivalent defined. */
return(0);
extended = ((lastkey & 0xFF00) >> 8);
if (extended != 0)
{
lastkey = extended;
keyhandler();
lastkey = (scancodes[event->key.keysym.sym] & 0xFF);
} /* if */
if (event->key.state == SDL_RELEASED)
lastkey += 128; /* +128 signifies that the key is released in DOS. */
keyhandler();
return(0);
} /* sdl_key_filter */
static int root_sdl_event_filter(const SDL_Event *event)
{
switch (event->type)
{
case SDL_KEYUP:
case SDL_KEYDOWN:
return(sdl_key_filter(event));
case SDL_JOYBALLMOTION:
case SDL_MOUSEMOTION:
return(sdl_mouse_motion_filter(event));
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
return(sdl_mouse_button_filter((const SDL_MouseButtonEvent*)event));
case SDL_QUIT:
/* !!! rcg TEMP */
SDL_Quit();
exit(0);
} /* switch */
return(1);
} /* root_sdl_event_filter */
static void handle_events(void)
{
SDL_Event event;
while (SDL_PollEvent(&event))
root_sdl_event_filter(&event);
} /* handle_events */
/* bleh...public version... */
void _handle_events(void)
{
handle_events();
} /* _handle_events */
static SDL_Joystick *joystick = NULL;
void _joystick_init(void)
{
const char *envr = getenv(BUILD_SDLJOYSTICK);
int favored = 0;
int numsticks;
int i;
if (joystick != NULL)
{
sdldebug("Joystick appears to be already initialized.");
sdldebug("...deinitializing for stick redetection...");
_joystick_deinit();
} /* if */
if ((envr != NULL) && (strcmp(envr, "none") == 0))
{
sdldebug("Skipping joystick detection/initialization at user request");
return;
} /* if */
sdldebug("Initializing SDL joystick subsystem...");
sdldebug(" (export environment variable BUILD_SDLJOYSTICK=none to skip)");
if (SDL_Init(SDL_INIT_JOYSTICK) != 0)
{
sdldebug("SDL_Init(SDL_INIT_JOYSTICK) failed: [%s].", SDL_GetError());
return;
} /* if */
numsticks = SDL_NumJoysticks();
sdldebug("SDL sees %d joystick%s.", numsticks, numsticks == 1 ? "" : "s");
if (numsticks == 0)
return;
for (i = 0; i < numsticks; i++)
{
const char *stickname = SDL_JoystickName(i);
if ((envr != NULL) && (strcmp(envr, stickname) == 0))
favored = i;
sdldebug("Stick #%d: [%s]", i, stickname);
} /* for */
sdldebug("Using Stick #%d.", favored);
if ((envr == NULL) && (numsticks > 1))
sdldebug("Set BUILD_SDLJOYSTICK to one of the above names to change.");
joystick = SDL_JoystickOpen(favored);
if (joystick == NULL)
{
sdldebug("Joystick #%d failed to init: %s", favored, SDL_GetError());
return;
} /* if */
sdldebug("Joystick initialized. %d axes, %d buttons, %d hats, %d balls.",
SDL_JoystickNumAxes(joystick), SDL_JoystickNumButtons(joystick),
SDL_JoystickNumHats(joystick), SDL_JoystickNumBalls(joystick));
SDL_JoystickEventState(SDL_QUERY);
} /* _joystick_init */
void _joystick_deinit(void)
{
if (joystick != NULL)
{
sdldebug("Closing joystick device...");
SDL_JoystickClose(joystick);
sdldebug("Joystick device closed. Deinitializing SDL subsystem...");
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
sdldebug("SDL joystick subsystem deinitialized.");
joystick = NULL;
} /* if */
} /* _joystick_deinit */
int _joystick_update(void)
{
if (joystick == NULL)
return(0);
SDL_JoystickUpdate();
return(1);
} /* _joystick_update */
int _joystick_axis(int axis)
{
if (joystick == NULL)
return(0);
return(SDL_JoystickGetAxis(joystick, axis));
} /* _joystick_axis */
int _joystick_hat(int hat)
{
if (joystick == NULL)
{
return(-1);
}
return(SDL_JoystickGetHat(joystick, hat));
} /* _joystick_axis */
int _joystick_button(int button)
{
if (joystick == NULL)
return(0);
return(SDL_JoystickGetButton(joystick, button) != 0);
} /* _joystick_button */
unsigned char _readlastkeyhit(void)
{
return(lastkey);
} /* _readlastkeyhit */
/* !!! I'd like this to be temporary. --ryan. */
#if (defined USE_I386_ASM)
# if (defined PLATFORM_UNIX)
# define PROT_R_W_X (PROT_READ | PROT_WRITE | PROT_EXEC)
# elif (defined PLATFORM_WIN32)
# ifndef PAGESIZE
# define PAGESIZE 4096
# endif
# define PROT_R_W_X PAGE_EXECUTE_READWRITE
static int mprotect(void *ptr, size_t len, int prot)
{
BOOL rc;
DWORD old = 0;
rc = VirtualProtect(ptr, len, prot, &old);
return(rc == 0);
}
#endif
#ifdef USE_I386_ASM
int mprotect_align(const void *addr, size_t len, int prot)
{
int retval;
unsigned long l = (unsigned long) addr;
l -= (l % PAGESIZE);
retval = mprotect((void *) l, len * 2, prot);
assert(retval == 0);
return(retval);
} /* mprotect_align */
void unprotect_ASM_pages(void)
{
mprotect_align((const void *) asm_sethlinesizes, PAGESIZE, PROT_R_W_X);
mprotect_align((const void *) asm_setpalookupaddress, PAGESIZE, PROT_R_W_X);
mprotect_align((const void *) asm_setuphlineasm4, PAGESIZE, PROT_R_W_X);