-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.c
1937 lines (1507 loc) · 50.8 KB
/
display.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
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 "platform.h"
#define BUILD_NOMOUSEGRAB "BUILD_NOMOUSEGRAB"
#define BUILD_WINDOWED "BUILD_WINDOWED"
#define BUILD_SDLDEBUG "BUILD_SDLDEBUG"
#define BUILD_RENDERER "BUILD_RENDERER"
#define BUILD_GLLIBRARY "BUILD_GLLIBRARY"
#define BUILD_USERSCREENRES "BUILD_USERSCREENRES"
#define BUILD_MAXSCREENRES "BUILD_MAXSCREENRES"
#define BUILD_HALLOFMIRRORS "BUILD_HALLOFMIRRORS"
#define BUILD_GLDUMP "BUILD_GLDUMP"
#define BUILD_SDLJOYSTICK "BUILD_SDLJOYSTICK"
#include "SDL.h"
#include "build.h"
#include "display.h"
#include "fixedPoint_math.h"
#include "engine.h"
#include "network.h"
#include "mmulti_unstable.h"
#include "mmulti_stable.h"
#include "network.h"
#include "icon.h"
// NATIVE TIMER FUNCTION DECLARATION
/*
FCS: The timer section sadly uses Native high precision calls to implement timer functions.
QueryPerformanceFrequency and QueryPerformanceCounter
it seems SDL precision was not good enough (or rather using unaccurate OS functions) to replicate
a DOS timer.
*/
int TIMER_GetPlatformTicksInOneSecond(int64_t* t);
void TIMER_GetPlatformTicks(int64_t* t);
//END // NATIVE TIMER FUNCTION DECLARATION
#if ((defined PLATFORM_WIN32))
#include <windows.h>
#endif
#include "draw.h"
#include "cache.h"
/*
* !!! 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;
/* !!! move these elsewhere? */
int32_t xres, yres, bytesperline, imageSize, maxpages;
uint8_t* frameplace;
//The frambuffer address
uint8_t* frameoffset;
uint8_t *screen, vesachecked;
int32_t buffermode, origbuffermode, linearmode;
uint8_t permanentupdate = 0, vgacompatible;
SDL_Surface *surface = NULL; /* This isn't static so that we can use it elsewhere AH */
static uint32_t sdl_flags = SDL_HWPALETTE;
static int32_t mouse_relative_x = 0;
static int32_t mouse_relative_y = 0;
static short mouse_buttons = 0;
static unsigned int lastkey = 0;
/* so we can make use of setcolor16()... - DDOI */
static uint8_t drawpixel_color=0;
static uint32_t scancodes[SDLK_LAST];
static int32_t last_render_ticks = 0;
int32_t total_render_time = 1;
int32_t total_rendered_frames = 0;
static char *titleNameLong = NULL;
static char *titleNameShort = NULL;
void restore256_palette (void);
void set16color_palette (void);
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 */
}
#define append_sdl_surface_flag(a, b, c, fl) __append_sdl_surface_flag(a, b, c, fl, " " #fl)
#define print_tf_state(str, val) printf("%s: {%s}\n", str, (val) ? "true" : "false" )
static void output_surface_info(SDL_Surface *_surface)
{
const SDL_VideoInfo *info;
char f[256];
if (_surface == NULL)
{
printf("-WARNING- You've got a NULL screen surface!");
}
else
{
f[0] = '\0';
printf("screen surface is (%dx%dx%dbpp).\n",_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)");
printf("New vidmode flags:%s.\n", 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);
printf("video memory: (%d),\n", info->video_mem);
*/
}
}
static void output_driver_info(void)
{
char buffer[256];
if (SDL_VideoDriverName(buffer, sizeof (buffer)) == NULL){
printf("-WARNING- SDL_VideoDriverName() returned NULL!");
} /* if */
else
{
printf("Using SDL video driver \"%s\".", buffer);
} /* else */
} /* output_driver_info */
void* get_framebuffer(void){
return((Uint8 *) surface->pixels);
}
/*
* !!! 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 uint8_t screenalloctype = 255;
static void init_new_res_vars(int32_t davidoption)
{
int i = 0;
int j = 0;
setupmouse();
SDL_WM_SetCaption(titleNameLong, titleNameShort);
xdim = xres = surface->w;
ydim = yres = surface->h;
printf("init_new_res_vars %d %d\n",xdim,ydim);
bytesperline = surface->w;
vesachecked = 1;
vgacompatible = 1;
linearmode = 1;
qsetmode = surface->h;
activepage = visualpage = 0;
frameoffset = frameplace = (uint8_t*)surface->pixels;
if (screen != NULL)
{
if (screenalloctype == 0) kkfree((void *)screen);
if (screenalloctype == 1) suckcache((int32_t *)screen);
screen = NULL;
} /* if */
switch(vidoption)
{
case 1:i = xdim*ydim; break;
case 2: xdim = 320; ydim = 200; i = xdim*ydim; break;
default: assert(0);
}
j = ydim*4*sizeof(int32_t); /* Leave room for horizlookup&horizlookup2 */
if(horizlookup)
free(horizlookup);
if(horizlookup2)
free(horizlookup2);
horizlookup = (int32_t*)malloc(j);
horizlookup2 = (int32_t*)malloc(j);
j = 0;
//Build lookup table (X screespace -> frambuffer offset.
for(i = 0; i <= ydim; i++)
{
ylookup[i] = j;
j += bytesperline;
}
horizycent = ((ydim*4)>>1);
/* Force drawrooms to call dosetaspect & recalculate stuff */
oxyaspect = oxdimen = oviewingrange = -1;
//Let the Assembly module how many pixels to skip when drawing a column
setBytesPerLine(bytesperline);
setview(0L,0L,xdim-1,ydim-1);
setbrightness(curbrightness, palette);
if (searchx < 0) {
searchx = halfxdimen;
searchy = (ydimen>>1);
}
}
static void go_to_new_vid_mode(int davidoption, int w, int h)
{
getvalidvesamodes();
SDL_ClearError();
// don't do SDL_SetVideoMode if SDL_WM_SetIcon not called. See sdl doc for SDL_WM_SetIcon
surface = SDL_SetVideoMode(w, h, 8, sdl_flags);
if (surface == NULL)
{
Error(EXIT_FAILURE, "BUILDSDL: Failed to set %dx%d video mode!\n"
"BUILDSDL: SDL_Error() says [%s].\n",
w, h, SDL_GetError());
} /* if */
output_surface_info(surface);
init_new_res_vars(davidoption); // dont be confused between vidoption (global) and davidoption
}
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 bits 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;
}
else
{
if (SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON)
{
mouse_relative_x += event->motion.xrel;
mouse_relative_y += event->motion.yrel;
//printf("sdl_mouse_motion_filter: mrx=%d, mry=%d, mx=%d, my=%d\n",
// mouse_relative_x, mouse_relative_y, event->motion.xrel, event->motion.yrel);
// mouse_relative_* is already reset in
// readmousexy(). It must not be
// reset here because calling this function does not mean
// we always handle the mouse.
// FIX_00001: Mouse speed is uneven and slower in windowed mode vs fullscreen mode.
}
else
mouse_relative_x = mouse_relative_y = 0;
}
return(0);
} /* sdl_mouse_motion_filter */
/*
* 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 */
void fullscreen_toggle_and_change_driver(void)
{
// FIX_00002: New Toggle Windowed/FullScreen system now simpler and will
// dynamically change for Windib or Directx driver. Windowed/Fullscreen
// toggle also made available from menu.
// Replace attempt_fullscreen_toggle(SDL_Surface **surface, Uint32 *flags)
int32_t x,y;
x = surface->w;
y = surface->h;
BFullScreen =!BFullScreen;
SDL_QuitSubSystem(SDL_INIT_VIDEO);
_platform_init(0, NULL, "Duke Nukem 3D", "Duke3D");
_setgamemode(ScreenMode,x,y);
//vscrn();
return;
}
static int sdl_key_filter(const SDL_Event *event)
{
int extended;
if ( (event->key.keysym.sym == SDLK_m) &&
(event->key.state == SDL_PRESSED) &&
(event->key.keysym.mod & KMOD_CTRL) )
{
// FIX_00005: Mouse pointer can be toggled on/off (see mouse menu or use CTRL-M)
// This is usefull to move the duke window when playing in window mode.
if (SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON)
{
SDL_WM_GrabInput(SDL_GRAB_OFF);
SDL_ShowCursor(1);
}
else
{
SDL_WM_GrabInput(SDL_GRAB_ON);
SDL_ShowCursor(0);
}
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) )
{ fullscreen_toggle_and_change_driver();
// hack to discard the ALT key...
lastkey=scancodes[SDLK_RALT]>>8; // extended
keyhandler();
lastkey=(scancodes[SDLK_RALT]&0xff)+0x80; // Simulating Key up
keyhandler();
lastkey=(scancodes[SDLK_LALT]&0xff)+0x80; // Simulating Key up (not extended)
keyhandler();
SDL_SetModState(KMOD_NONE); // SDL doesnt see we are releasing the ALT-ENTER keys
return(0);
}
if (!handle_keypad_enter_hack(event))
lastkey = scancodes[event->key.keysym.sym];
// printf("key.keysym.sym=%d\n", 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:
// FIX_00003: Pause mode is now fully responsive - (Thx to Jonathon Fowler tips)
if(event->key.keysym.sym == SDLK_PAUSE)
break;
case SDL_KEYDOWN:
return(sdl_key_filter(event));
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
{
//Do Nothing
//printf("Joybutton UP/DOWN\n");
//return(sdl_joystick_button_filter((const SDL_MouseButtonEvent*)event));
return 0;
}
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 */
Error(EXIT_SUCCESS, "Exit through SDL\n");
default:
//printf("This event is not handled: %d\n",event->type);
break;
} /* 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)
{
printf("Joystick appears to be already initialized.\n");
printf("...deinitializing for stick redetection...\n");
_joystick_deinit();
} /* if */
if ((envr != NULL) && (strcmp(envr, "none") == 0))
{
printf("Skipping joystick detection/initialization at user request\n");
return;
} /* if */
printf("Initializing SDL joystick subsystem...");
printf(" (export environment variable BUILD_SDLJOYSTICK=none to skip)\n");
if (SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE) != 0)
{
printf("SDL_Init(SDL_INIT_JOYSTICK) failed: [%s].\n", SDL_GetError());
return;
} /* if */
numsticks = SDL_NumJoysticks();
printf("SDL sees %d joystick%s.\n", 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;
printf("Stick #%d: [%s]\n", i, stickname);
} /* for */
printf("Using Stick #%d.", favored);
if ((envr == NULL) && (numsticks > 1))
printf("Set BUILD_SDLJOYSTICK to one of the above names to change.\n");
joystick = SDL_JoystickOpen(favored);
if (joystick == NULL)
{
printf("Joystick #%d failed to init: %s\n", favored, SDL_GetError());
return;
} /* if */
printf("Joystick initialized. %d axes, %d buttons, %d hats, %d balls.\n",
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)
{
printf("Closing joystick device...\n");
SDL_JoystickClose(joystick);
printf("Joystick device closed. Deinitializing SDL subsystem...\n");
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
printf("SDL joystick subsystem deinitialized.\n");
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 */
uint8_t _readlastkeyhit(void)
{
return(lastkey);
} /* _readlastkeyhit */
#if (!defined __DATE__)
#define __DATE__ "a long, int32_t time ago"
#endif
static void output_sdl_versions(void)
{
const SDL_version *linked_ver = SDL_Linked_Version();
SDL_version compiled_ver;
SDL_VERSION(&compiled_ver);
printf("SDL display driver for the BUILD engine initializing.\n");
printf(" sdl_driver.c by Ryan C. Gordon (icculus@clutteredmind.org).\n");
printf("Compiled %s against SDL version %d.%d.%d ...\n", __DATE__,
compiled_ver.major, compiled_ver.minor, compiled_ver.patch);
printf("Linked SDL version is %d.%d.%d ...\n",
linked_ver->major, linked_ver->minor, linked_ver->patch);
} /* output_sdl_versions */
/* lousy -ansi flag. :) */
static char *string_dupe(const char *str)
{
char *retval = malloc(strlen(str) + 1);
if (retval != NULL)
strcpy(retval, str);
return(retval);
} /* string_dupe */
void _platform_init(int argc, char **argv, const char *title, const char *iconName)
{
int i;
int64_t timeElapsed;
char dummyString[4096];
// FIX_00061: "ERROR: Two players have the same random ID" too frequent cuz of internet windows times
TIMER_GetPlatformTicks(&timeElapsed);
srand(timeElapsed&0xFFFFFFFF);
Setup_UnstableNetworking();
// Look through the command line args
for(i = 0; i < argc; i++)
{
if(argv[i][0] == '-' )
{
if(strcmpi(argv[i], "-netmode_stable") == 0)
{
//fullscreen = 1;
//TODO:
//TODO ( "[Todo: handle -netmode <int>]" )
Setup_StableNetworking();
}
}
}
#ifdef __APPLE__
SDL_putenv("SDL_VIDEODRIVER=Quartz");
#endif
if (SDL_Init(SDL_INIT_VIDEO) == -1){
Error(EXIT_FAILURE, "BUILDSDL: SDL_Init() failed!\nBUILDSDL: SDL_GetError() says \"%s\".\n", SDL_GetError());
}
// Set up the correct renderer
// Becarfull setenv can't reach dll in VC++
// A way to proceed is to integrate the SDL libs
// in the exe instead.
// FIX_00004: SDL.dll and SDL_Mixer.dll are now integrated within the exe
// (this also makes the Windib/Directx driver switching easier with SDL)
// This requires to recompile the whole sdl and sdl mixer with the lib
// switch instead of the default dll switch.
putenv("SDL_VIDEO_CENTERED=1");
if (title == NULL)
title = "BUILD";
if (iconName == NULL)
iconName = "BUILD";
titleNameLong = string_dupe(title);
titleNameShort = string_dupe(iconName);
sdl_flags = BFullScreen ? SDL_FULLSCREEN : 0;
sdl_flags |= SDL_HWPALETTE;
memset(scancodes, '\0', sizeof (scancodes));
scancodes[SDLK_ESCAPE] = 0x01;
scancodes[SDLK_1] = 0x02;
scancodes[SDLK_2] = 0x03;
scancodes[SDLK_3] = 0x04;
scancodes[SDLK_4] = 0x05;
scancodes[SDLK_5] = 0x06;
scancodes[SDLK_6] = 0x07;
scancodes[SDLK_7] = 0x08;
scancodes[SDLK_8] = 0x09;
scancodes[SDLK_9] = 0x0A;
scancodes[SDLK_0] = 0x0B;
scancodes[SDLK_MINUS] = 0x0C; /* was 0x4A */
scancodes[SDLK_EQUALS] = 0x0D; /* was 0x4E */
scancodes[SDLK_BACKSPACE] = 0x0E;
scancodes[SDLK_TAB] = 0x0F;
scancodes[SDLK_q] = 0x10;
scancodes[SDLK_w] = 0x11;
scancodes[SDLK_e] = 0x12;
scancodes[SDLK_r] = 0x13;
scancodes[SDLK_t] = 0x14;
scancodes[SDLK_y] = 0x15;
scancodes[SDLK_u] = 0x16;
scancodes[SDLK_i] = 0x17;
scancodes[SDLK_o] = 0x18;
scancodes[SDLK_p] = 0x19;
scancodes[SDLK_LEFTBRACKET] = 0x1A;
scancodes[SDLK_RIGHTBRACKET] = 0x1B;
scancodes[SDLK_RETURN] = 0x1C;
scancodes[SDLK_LCTRL] = 0x1D;
scancodes[SDLK_a] = 0x1E;
scancodes[SDLK_s] = 0x1F;
scancodes[SDLK_d] = 0x20;
scancodes[SDLK_f] = 0x21;
scancodes[SDLK_g] = 0x22;
scancodes[SDLK_h] = 0x23;
scancodes[SDLK_j] = 0x24;
scancodes[SDLK_k] = 0x25;
scancodes[SDLK_l] = 0x26;
scancodes[SDLK_SEMICOLON] = 0x27;
scancodes[SDLK_QUOTE] = 0x28;
scancodes[SDLK_BACKQUOTE] = 0x29;
scancodes[SDLK_LSHIFT] = 0x2A;
scancodes[SDLK_BACKSLASH] = 0x2B;
scancodes[SDLK_z] = 0x2C;
scancodes[SDLK_x] = 0x2D;
scancodes[SDLK_c] = 0x2E;
scancodes[SDLK_v] = 0x2F;
scancodes[SDLK_b] = 0x30;
scancodes[SDLK_n] = 0x31;
scancodes[SDLK_m] = 0x32;
scancodes[SDLK_COMMA] = 0x33;
scancodes[SDLK_PERIOD] = 0x34;
scancodes[SDLK_SLASH] = 0x35;
scancodes[SDLK_RSHIFT] = 0x36;
scancodes[SDLK_KP_MULTIPLY] = 0x37;
scancodes[SDLK_LALT] = 0x38;
scancodes[SDLK_SPACE] = 0x39;
scancodes[SDLK_CAPSLOCK] = 0x3A;
scancodes[SDLK_F1] = 0x3B;
scancodes[SDLK_F2] = 0x3C;
scancodes[SDLK_F3] = 0x3D;
scancodes[SDLK_F4] = 0x3E;
scancodes[SDLK_F5] = 0x3F;
scancodes[SDLK_F6] = 0x40;
scancodes[SDLK_F7] = 0x41;
scancodes[SDLK_F8] = 0x42;
scancodes[SDLK_F9] = 0x43;
scancodes[SDLK_F10] = 0x44;
scancodes[SDLK_NUMLOCK] = 0x45;
scancodes[SDLK_SCROLLOCK] = 0x46;
scancodes[SDLK_KP7] = 0x47;
scancodes[SDLK_KP8] = 0x48;
scancodes[SDLK_KP9] = 0x49;
scancodes[SDLK_KP_MINUS] = 0x4A;
scancodes[SDLK_KP4] = 0x4B;
scancodes[SDLK_KP5] = 0x4C;
scancodes[SDLK_KP6] = 0x4D;
scancodes[SDLK_KP_PLUS] = 0x4E;
scancodes[SDLK_KP1] = 0x4F;
scancodes[SDLK_KP2] = 0x50;
scancodes[SDLK_KP3] = 0x51;
scancodes[SDLK_KP0] = 0x52;
scancodes[SDLK_KP_PERIOD] = 0x53;
scancodes[SDLK_F11] = 0x57;
scancodes[SDLK_F12] = 0x58;
scancodes[SDLK_PAUSE] = 0x59; /* SBF - technically incorrect */
scancodes[SDLK_KP_ENTER] = 0xE01C;
scancodes[SDLK_RCTRL] = 0xE01D;
scancodes[SDLK_KP_DIVIDE] = 0xE035;
scancodes[SDLK_PRINT] = 0xE037; /* SBF - technically incorrect */
scancodes[SDLK_SYSREQ] = 0xE037; /* SBF - for windows... */
scancodes[SDLK_RALT] = 0xE038;
scancodes[SDLK_HOME] = 0xE047;
scancodes[SDLK_UP] = 0xE048;
scancodes[SDLK_PAGEUP] = 0xE049;
scancodes[SDLK_LEFT] = 0xE04B;
scancodes[SDLK_RIGHT] = 0xE04D;
scancodes[SDLK_END] = 0xE04F;
scancodes[SDLK_DOWN] = 0xE050;
scancodes[SDLK_PAGEDOWN] = 0xE051;
scancodes[SDLK_INSERT] = 0xE052;
scancodes[SDLK_DELETE] = 0xE053;
output_sdl_versions();
output_driver_info();
printf("Video Driver: '%s'.\n", SDL_VideoDriverName(dummyString, 20));
}
// Capture BMP of the current frame
int screencapture(char *filename, uint8_t inverseit)
{
// FIX_00006: better naming system for screenshots + message when pic is taken.
// Use ./screenshots folder. Screenshot code rerwritten. Faster and
// makes smaller files. Doesn't freeze or lag the game anymore.
SDL_SaveBMP(surface, filename);
return 0;
} /* screencapture */
void setvmode(int mode)
{
if (mode == 0x3) /* text mode. */
{
SDL_QuitSubSystem(SDL_INIT_VIDEO);
return;
} else
printf("setvmode(0x%x) is unsupported in SDL driver.\n", mode);
}
int _setgamemode(uint8_t davidoption, int32_t daxdim, int32_t daydim)
{
int validated, i;
SDL_Surface *image;
Uint32 colorkey;
// don't override higher-res app icon on OS X
#ifndef PLATFORM_MACOSX
// Install icon
image = SDL_LoadBMP_RW(SDL_RWFromMem(iconBMP, sizeof(iconBMP)), 1);
colorkey = 0; // index in this image to be transparent
SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
SDL_WM_SetIcon(image,NULL);
#endif
if (daxdim > MAXXDIM || daydim > MAXYDIM)
{
printf("Resolution %dx%d is too high. Changed to %dx%d\n", daxdim, daydim, MAXXDIM,MAXYDIM);
daxdim = MAXXDIM;
daydim = MAXYDIM;
}
getvalidvesamodes();
validated = 0;
for(i=0; i<validmodecnt; i++){
if(validmodexdim[i] == daxdim && validmodeydim[i] == daydim)
validated = 1;
}
if(!validated){
printf("Resolution %dx%d unsupported. Changed to 640x480\n", daxdim, daydim);
daxdim = 640;
daydim = 480;
}
go_to_new_vid_mode((int) davidoption, daxdim, daydim);
qsetmode = 200;
last_render_ticks = getticks();
return(0);
} /* setgamemode */
static int get_dimensions_from_str(const char *str, int32_t *_w, int32_t *_h)
{
char *xptr = NULL;
char *ptr = NULL;
int32_t w = -1;
int32_t h = -1;
if (str == NULL)
return(0);
xptr = strchr(str, 'x');
if (xptr == NULL)
return(0);
w = strtol(str, &ptr, 10);
if (ptr != xptr)
return(0);
xptr++;
h = strtol(xptr, &ptr, 10);