summaryrefslogtreecommitdiff
path: root/src/i_video.c
blob: 62ad2c7977f499ab44aca3aaf0b98278eb6cb4c5 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// $Id: i_video.c 536 2006-05-29 10:38:27Z fraggle $
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// $Log$
// Revision 1.43  2006/01/12 01:34:48  fraggle
// Combine mouse motion for tics into single events.
//
// Revision 1.42  2006/01/02 20:27:45  fraggle
// Clear the screen AFTER initialising the loading disk buffer, so that
// bits of loading disk are not visible on the initial screen melt.
//
// Revision 1.41  2006/01/02 00:06:30  fraggle
// Make functions static.  Remove unused variable.
//
// Revision 1.40  2005/10/17 19:46:22  fraggle
// Guard against multiple video shutdowns better.  Fix crash due to improper
// screen clear at startup.
//
// Revision 1.39  2005/10/16 20:55:50  fraggle
// Fix the '-cdrom' command-line option.
//
// Revision 1.38  2005/10/15 22:50:57  fraggle
// Fix pink icon on startup
//
// Revision 1.37  2005/10/15 15:59:14  fraggle
// Map mouse buttons correctly.
//
// Revision 1.36  2005/10/15 15:45:03  fraggle
// Check the return code from SDL_LockSurface to ensure a surface has been
// properly locked.  Fixes crash when switching applications while running
// fullscreen.
//
// Revision 1.35  2005/10/02 03:16:29  fraggle
// ENDOOM support using text mode emulation
//
// Revision 1.34  2005/10/02 03:03:40  fraggle
// Make sure loading disk is only shown if the display is initialised
//
// Revision 1.33  2005/09/27 22:33:42  fraggle
// Always use SDL_Flip to update the screen.  Fixes problems in Windows when
// running fullscreen, introduced by fixes to the disk icon code.
//
// Revision 1.32  2005/09/26 21:44:30  fraggle
// Fix melting crap on startup - oops
//
// Revision 1.31  2005/09/25 00:31:32  fraggle
// Fix disk icon appearing before palette is set (pink disk!)
// Cleanup and commenting
//
// Revision 1.30  2005/09/24 23:44:49  fraggle
// Enforce sane screenmultiply values
//
// Revision 1.29  2005/09/24 23:41:07  fraggle
// Fix "loading" icon for all video modes
//
// Revision 1.28  2005/09/24 22:04:03  fraggle
// Add application icon to running program
//
// Revision 1.27  2005/09/17 20:50:46  fraggle
// Mouse acceleration code to emulate old DOS drivers
//
// Revision 1.26  2005/09/14 21:55:47  fraggle
// Lock surfaces properly when we have to (fixes crash under Windows 98)
//
// Revision 1.25  2005/09/11 20:25:56  fraggle
// Second configuration file to allow chocolate doom-specific settings.
// Adjust some existing command line logic (for graphics settings and
// novert) to adjust for this.
//
// Revision 1.24  2005/09/08 22:05:17  fraggle
// Allow alt-tab away while running fullscreen
//
// Revision 1.23  2005/09/07 20:44:23  fraggle
// Fix up names of functions
// Make the quit button work (pops up the "quit doom?" prompt).
// Fix focus detection to release the mouse and ignore mouse events
// when window is not focused.
//
// Revision 1.22  2005/09/04 23:18:30  fraggle
// Remove dead code.  Cope with the screen not having width == pitch.  Lock
// the SDL screen surface properly. Rewrite 2x scaling code.
//
// Revision 1.21  2005/09/01 00:01:36  fraggle
// -nograbmouse option
//
// Revision 1.20  2005/08/31 23:58:28  fraggle
// smarter mouse grabbing for windowed mode
//
// Revision 1.19  2005/08/31 21:35:42  fraggle
// Display the game name in the title bar.  Move game start code to later
// in initialisation because of the IWAD detection changes.
//
// Revision 1.18  2005/08/10 08:45:35  fraggle
// Remove "if (french)" stuff, FRENCH define, detect french wad automatically
//
// Revision 1.17  2005/08/07 20:01:00  fraggle
// Clear the screen on startup
//
// Revision 1.16  2005/08/07 03:09:33  fraggle
// Fix gamma correction
//
// Revision 1.15  2005/08/07 02:59:23  fraggle
// Clear disk image when loading at startup
//
// Revision 1.14  2005/08/06 17:30:30  fraggle
// Only change palette on screen updates
//
// Revision 1.13  2005/08/04 22:23:07  fraggle
// Use zone memory function.  Add command line options
//
// Revision 1.12  2005/08/04 19:54:56  fraggle
// Use keysym value rather than unicode value (fixes problems with shift
// key)
//
// Revision 1.11  2005/08/04 18:42:15  fraggle
// Silence compiler warnings
//
// Revision 1.10  2005/08/04 01:13:46  fraggle
// Loading disk
//
// Revision 1.9  2005/08/03 22:19:52  fraggle
// Set some flags to fix palette and improve performance
//
// Revision 1.8  2005/08/03 21:58:02  fraggle
// Working scale*2
//
// Revision 1.7  2005/07/25 20:50:55  fraggle
// mouse
//
// Revision 1.6  2005/07/24 02:14:04  fraggle
// Move to SDL for graphics.
// Translate key scancodes to correct internal format when reading
// settings from config file - backwards compatible with config files
// for original exes
//
// Revision 1.5  2005/07/23 21:32:47  fraggle
// Add missing errno.h, fix crash on startup when no IWAD present
//
// Revision 1.4  2005/07/23 19:17:11  fraggle
// Use ANSI-standard limit constants.  Remove LINUX define.
//
// Revision 1.3  2005/07/23 17:27:04  fraggle
// Stop crash on shutdown
//
// Revision 1.2  2005/07/23 16:44:55  fraggle
// Update copyright to GNU GPL
//
// Revision 1.1.1.1  2005/07/23 16:19:58  fraggle
// Initial import
//
//
// DESCRIPTION:
//	DOOM graphics stuff for X11, UNIX.
//
//-----------------------------------------------------------------------------

static const char
rcsid[] = "$Id: i_video.c 536 2006-05-29 10:38:27Z fraggle $";

#include <SDL.h>
#include <ctype.h>
#include <math.h>

#include "chocolate_doom_icon.c"

#include "config.h"
#include "doomdef.h"
#include "doomstat.h"
#include "d_main.h"
#include "i_system.h"
#include "i_timer.h"
#include "m_argv.h"
#include "m_swap.h"
#include "s_sound.h"
#include "sounds.h"
#include "v_video.h"
#include "w_wad.h"
#include "z_zone.h"

// Alternate screenheight for letterbox mode

#define LETTERBOX_SCREENHEIGHT 240

enum
{
    FULLSCREEN_OFF,
    FULLSCREEN_ON,
    FULLSCREEN_LETTERBOX,
};

extern void M_QuitDOOM();

static SDL_Surface *screen;

// palette
static SDL_Color palette[256];
static boolean palette_to_set;

static int windowwidth, windowheight;

// display has been set up?

static boolean initialised = false;

// disable mouse?

static boolean nomouse = false;
extern int usemouse;

// if true, screens[0] is screen->pixel

static boolean native_surface;

// Automatically adjust video settings if the selected mode is 
// not a valid video mode.

int autoadjust_video_settings = 1;

// Run in full screen mode?  (int type for config code)

int fullscreen = FULLSCREEN_ON;

// Time to wait for the screen to settle on startup before starting the
// game (ms)

int startup_delay = 0;

// Grab the mouse? (int type for config code)

int grabmouse = true;

// Flag indicating whether the screen is currently visible:
// when the screen isnt visible, don't render the screen

boolean screenvisible;

// Blocky mode,
// replace each 320x200 pixel with screenmultiply*screenmultiply pixels.
// According to Dave Taylor, it still is a bonehead thing
// to use ....

int screenmultiply = 1;

// disk image data and background overwritten by the disk to be
// restored by EndRead

static byte *disk_image = NULL;
static int disk_image_w, disk_image_h;
static byte *saved_background;
static boolean window_focused;

// Mouse acceleration
//
// This emulates some of the behavior of DOS mouse drivers by increasing
// the speed when the mouse is moved fast.
//
// The mouse input values are input directly to the game, but when
// the values exceed the value of mouse_threshold, they are multiplied
// by mouse_acceleration to increase the speed.

float mouse_acceleration = 2.0;
int mouse_threshold = 10;

static boolean MouseShouldBeGrabbed()
{
    // if the window doesnt have focus, never grab it

    if (!window_focused)
        return false;

    // always grab the mouse when full screen (dont want to 
    // see the mouse pointer)

    if (fullscreen != FULLSCREEN_OFF)
        return true;

    // if we specify not to grab the mouse, never grab
 
    if (!grabmouse)
        return false;

    // when menu is active or game is paused, release the mouse 
 
    if (menuactive || paused)
        return false;

    // only grab mouse when playing levels (but not demos)

    return (gamestate == GS_LEVEL) && !demoplayback;
}

// Update the value of window_focused when we get a focus event
//
// We try to make ourselves be well-behaved: the grab on the mouse
// is removed if we lose focus (such as a popup window appearing),
// and we dont move the mouse around if we aren't focused either.

static void UpdateFocus(void)
{
    Uint8 state;

    state = SDL_GetAppState();

    // We should have input (keyboard) focus and be visible 
    // (not minimised)

    window_focused = (state & SDL_APPINPUTFOCUS) && (state & SDL_APPACTIVE);

    // Should the screen be grabbed?

    screenvisible = (state & SDL_APPACTIVE) != 0;
}

static void LoadDiskImage(void)
{
    patch_t *disk;
    int y;

    if (M_CheckParm("-cdrom") > 0)
        disk = (patch_t *) W_CacheLumpName("STCDROM", PU_STATIC);
    else
        disk = (patch_t *) W_CacheLumpName("STDISK", PU_STATIC);

    V_DrawPatch(0, 0, 0, disk);
    disk_image_w = SHORT(disk->width);
    disk_image_h = SHORT(disk->height);

    disk_image = Z_Malloc(disk_image_w * disk_image_h, PU_STATIC, NULL);
    saved_background = Z_Malloc(disk_image_w * disk_image_h, PU_STATIC, NULL);

    for (y=0; y<disk_image_h; ++y) 
    {
        memcpy(disk_image + disk_image_w * y,
               screens[0] + SCREENWIDTH * y,
               disk_image_w);
        memset(screens[0] + SCREENWIDTH * y, 0, disk_image_w);
    }

    Z_Free(disk);
}

//
// Translates the SDL key
//

static int TranslateKey(SDL_keysym *sym)
{
    switch(sym->sym)
    {
      case SDLK_LEFT:	return KEY_LEFTARROW;
      case SDLK_RIGHT:	return KEY_RIGHTARROW;
      case SDLK_DOWN:	return KEY_DOWNARROW;
      case SDLK_UP:	return KEY_UPARROW;
      case SDLK_ESCAPE:	return KEY_ESCAPE;
      case SDLK_RETURN:	return KEY_ENTER;
      case SDLK_TAB:	return KEY_TAB;
      case SDLK_F1:	return KEY_F1;
      case SDLK_F2:	return KEY_F2;
      case SDLK_F3:	return KEY_F3;
      case SDLK_F4:	return KEY_F4;
      case SDLK_F5:	return KEY_F5;
      case SDLK_F6:	return KEY_F6;
      case SDLK_F7:	return KEY_F7;
      case SDLK_F8:	return KEY_F8;
      case SDLK_F9:	return KEY_F9;
      case SDLK_F10:	return KEY_F10;
      case SDLK_F11:	return KEY_F11;
      case SDLK_F12:	return KEY_F12;
	
      case SDLK_BACKSPACE: return KEY_BACKSPACE;
      case SDLK_DELETE:	return KEY_DEL;

      case SDLK_PAUSE:	return KEY_PAUSE;

      case SDLK_EQUALS:
      case SDLK_KP_EQUALS:	return KEY_EQUALS;

      case SDLK_MINUS:          return KEY_MINUS;

      case SDLK_LSHIFT:
      case SDLK_RSHIFT:
	return KEY_RSHIFT;
	
      case SDLK_LCTRL:
      case SDLK_RCTRL:
	return KEY_RCTRL;
	
      case SDLK_LALT:
      case SDLK_LMETA:
      case SDLK_RALT:
      case SDLK_RMETA:
        return KEY_RALT;

      case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
      case SDLK_SCROLLOCK: return KEY_SCRLCK;

      case SDLK_KP0: return KEYP_0;
      case SDLK_KP1: return KEYP_1;
      case SDLK_KP2: return KEYP_2;
      case SDLK_KP3: return KEYP_3;
      case SDLK_KP4: return KEYP_4;
      case SDLK_KP5: return KEYP_5;
      case SDLK_KP6: return KEYP_6;
      case SDLK_KP7: return KEYP_7;
      case SDLK_KP8: return KEYP_8;
      case SDLK_KP9: return KEYP_9;

      case SDLK_HOME: return KEY_HOME;
      case SDLK_INSERT: return KEY_INS;
      case SDLK_END: return KEY_END;
      case SDLK_PAGEUP: return KEY_PGUP;
      case SDLK_PAGEDOWN: return KEY_PGDN;
      case SDLK_KP_MULTIPLY: return KEYP_MULTIPLY;
      case SDLK_KP_PLUS: return KEYP_PLUS;
      case SDLK_KP_MINUS: return KEYP_MINUS;
      case SDLK_KP_DIVIDE: return KEYP_DIVIDE;

      default:
        return tolower(sym->sym);
    }
}

void I_ShutdownGraphics(void)
{
    if (initialised)
    {
        SDL_ShowCursor(1);
        SDL_WM_GrabInput(SDL_GRAB_OFF);

        SDL_QuitSubSystem(SDL_INIT_VIDEO);
    
        initialised = false;
    }
}



//
// I_StartFrame
//
void I_StartFrame (void)
{
    // er?

}

static int MouseButtonState(void)
{
    Uint8 state = SDL_GetMouseState(NULL, NULL);
    int result = 0;

    // Note: button "0" is left, button "1" is right,
    // button "2" is middle for Doom.  This is different
    // to how SDL sees things.

    if (state & SDL_BUTTON(1))
        result |= 1;
    if (state & SDL_BUTTON(3))
        result |= 2;
    if (state & SDL_BUTTON(2))
        result |= 4;

    return result;
}

static int AccelerateMouse(int val)
{
    if (val < 0)
        return -AccelerateMouse(-val);

    if (val > mouse_threshold)
    {
        return (val - mouse_threshold) * mouse_acceleration + mouse_threshold;
    }
    else
    {
        return val;
    }
}

void I_GetEvent(void)
{
    SDL_Event sdlevent;
    event_t event;

    // possibly not needed
    
    SDL_PumpEvents();

    // put event-grabbing stuff in here
    
    while (SDL_PollEvent(&sdlevent))
    {
        // ignore mouse events when the window is not focused

        if (!window_focused 
         && (sdlevent.type == SDL_MOUSEMOTION
          || sdlevent.type == SDL_MOUSEBUTTONDOWN
          || sdlevent.type == SDL_MOUSEBUTTONUP))
        {
            continue;
        }

        // process event
        
        switch (sdlevent.type)
        {
            case SDL_KEYDOWN:
                event.type = ev_keydown;
                event.data1 = TranslateKey(&sdlevent.key.keysym);
		event.data2 = sdlevent.key.keysym.unicode;
                D_PostEvent(&event);
                break;
            case SDL_KEYUP:
                event.type = ev_keyup;
                event.data1 = TranslateKey(&sdlevent.key.keysym);
                D_PostEvent(&event);
                break;
                /*
            case SDL_MOUSEMOTION:
                event.type = ev_mouse;
                event.data1 = MouseButtonState();
                event.data2 = AccelerateMouse(sdlevent.motion.xrel);
                event.data3 = -AccelerateMouse(sdlevent.motion.yrel);
                D_PostEvent(&event);
                break;
                */
            case SDL_MOUSEBUTTONDOWN:
		if (usemouse && !nomouse)
		{
                    event.type = ev_mouse;
                    event.data1 = MouseButtonState();
                    event.data2 = event.data3 = 0;
                    D_PostEvent(&event);
		}
                break;
            case SDL_MOUSEBUTTONUP:
		if (usemouse && !nomouse)
		{
                    event.type = ev_mouse;
                    event.data1 = MouseButtonState();
                    event.data2 = event.data3 = 0;
                    D_PostEvent(&event);
		}
                break;
            case SDL_QUIT:
                // bring up the "quit doom?" prompt
                S_StartSound(NULL,sfx_swtchn);
                M_QuitDOOM(0);
                break;
            case SDL_ACTIVEEVENT:
                // need to update our focus state
                UpdateFocus();
                break;
            default:
                break;
        }
    }
}

//
// Read the change in mouse state to generate mouse motion events
//
// This is to combine all mouse movement for a tic into one mouse
// motion event.

static void I_ReadMouse(void)
{
    int x, y;
    event_t ev;

    SDL_GetRelativeMouseState(&x, &y);

    if (x != 0 || y != 0) 
    {
        ev.type = ev_mouse;
        ev.data1 = MouseButtonState();
        ev.data2 = AccelerateMouse(x);
        ev.data3 = -AccelerateMouse(y);
        
        D_PostEvent(&ev);
    }
}

//
// I_StartTic
//
void I_StartTic (void)
{
    I_GetEvent();

    if (usemouse && !nomouse)
    {
        I_ReadMouse();
    }
}


//
// I_UpdateNoBlit
//
void I_UpdateNoBlit (void)
{
    // what is this?
}

static void UpdateGrab(void)
{
    static boolean currently_grabbed = false;
    boolean grab;

    grab = MouseShouldBeGrabbed();

    if (grab && !currently_grabbed)
    {
        SDL_ShowCursor(0);
        SDL_WM_GrabInput(SDL_GRAB_ON);
    }

    if (!grab && currently_grabbed)
    {
        SDL_ShowCursor(1);
        SDL_WM_GrabInput(SDL_GRAB_OFF);
    }

    currently_grabbed = grab;

}

// Update a small portion of the screen
//
// Does 2x stretching and buffer blitting if neccessary

static void BlitArea(int x1, int y1, int x2, int y2)
{
    int w = x2 - x1;
    int y_offset;

    // Y offset when running in letterbox mode

    if (fullscreen == FULLSCREEN_LETTERBOX)
    {
        y_offset = (LETTERBOX_SCREENHEIGHT - SCREENHEIGHT) / 2;
    }
    else
    {
        y_offset = 0;
    }
    
    // Need to byte-copy from buffer into the screen buffer

    if (screenmultiply == 1 && !native_surface)
    {
        byte *bufp, *screenp;
        int y;
        int pitch;

        if (SDL_LockSurface(screen) >= 0)
        {
            pitch = screen->pitch;
            bufp = screens[0] + y1 * SCREENWIDTH + x1;
            screenp = (byte *) screen->pixels + (y1 + y_offset) * pitch + x1;
    
            for (y=y1; y<y2; ++y)
            {
                memcpy(screenp, bufp, w);
                screenp += pitch;
                bufp += SCREENWIDTH;
            }
    
            SDL_UnlockSurface(screen);
        }
    }

    // scales the screen size before blitting it

    if (screenmultiply == 2)
    {
        byte *bufp, *screenp, *screenp2;
        int x, y;
        int pitch;

        if (SDL_LockSurface(screen) >= 0)
        {
            pitch = screen->pitch * 2;
            bufp = screens[0] + y1 * SCREENWIDTH + x1;
            screenp = (byte *) screen->pixels 
                    + (y1 + y_offset) * pitch 
                    + x1 * 2;
            screenp2 = screenp + screen->pitch;
    
            for (y=y1; y<y2; ++y)
            {
                byte *sp, *sp2, *bp;
                sp = screenp;
                sp2 = screenp2;
                bp = bufp;
    
                for (x=x1; x<x2; ++x)
                {
                    *sp++ = *bp;  *sp++ = *bp;
                    *sp2++ = *bp; *sp2++ = *bp;
                    ++bp;
                }
                screenp += pitch;
                screenp2 += pitch;
                bufp += SCREENWIDTH;
            }
    
            SDL_UnlockSurface(screen);
        }
    }

    if (screenmultiply == 3)
    {
        byte *bufp, *screenp, *screenp2, *screenp3;
        int x, y;
        int pitch;

        if (SDL_LockSurface(screen) >= 0)
        {
            pitch = screen->pitch * 3;
            bufp = screens[0] + y1 * SCREENWIDTH + x1;
            screenp = (byte *) screen->pixels 
                    + (y1 + y_offset) * pitch 
                    + x1 * 3;
            screenp2 = screenp + screen->pitch;
            screenp3 = screenp2 + screen->pitch;
    
            for (y=y1; y<y2; ++y)
            {
                byte *sp, *sp2, *sp3, *bp;
                sp = screenp;
                sp2 = screenp2;
                sp3 = screenp3;
                bp = bufp;
    
                for (x=x1; x<x2; ++x)
                {
                    *sp++ = *bp;  *sp++ = *bp;  *sp++ = *bp;
                    *sp2++ = *bp; *sp2++ = *bp; *sp2++ = *bp;
                    *sp3++ = *bp; *sp3++ = *bp; *sp3++ = *bp;
                    ++bp;
                }
                screenp += pitch;
                screenp2 += pitch;
                screenp3 += pitch;
                bufp += SCREENWIDTH;
            }
    
            SDL_UnlockSurface(screen);
        }
    }

    if (screenmultiply == 4)
    {
        byte *bufp, *screenp, *screenp2, *screenp3, *screenp4;
        int x, y;
        int pitch;

        if (SDL_LockSurface(screen) >= 0)
        {
            pitch = screen->pitch * 4;
            bufp = screens[0] + y1 * SCREENWIDTH + x1;
            screenp = (byte *) screen->pixels 
                    + (y1 + y_offset) * pitch 
                    + x1 * 4;
            screenp2 = screenp + screen->pitch;
            screenp3 = screenp2 + screen->pitch;
            screenp4 = screenp3 + screen->pitch;
    
            for (y=y1; y<y2; ++y)
            {
                byte *sp, *sp2, *sp3, *sp4, *bp;
                sp = screenp;
                sp2 = screenp2;
                sp3 = screenp3;
                sp4 = screenp4;
                bp = bufp;
    
                for (x=x1; x<x2; ++x)
                {
                    *sp++ = *bp;  *sp++ = *bp;  *sp++ = *bp;  *sp++ = *bp;
                    *sp2++ = *bp; *sp2++ = *bp; *sp2++ = *bp; *sp2++ = *bp;
                    *sp3++ = *bp; *sp3++ = *bp; *sp3++ = *bp; *sp3++ = *bp;
                    *sp4++ = *bp; *sp4++ = *bp; *sp4++ = *bp; *sp4++ = *bp;
                    ++bp;
                }
                screenp += pitch;
                screenp2 += pitch;
                screenp3 += pitch;
                screenp4 += pitch;
                bufp += SCREENWIDTH;
            }
    
            SDL_UnlockSurface(screen);
        }
    }
}

static void UpdateRect(int x1, int y1, int x2, int y2)
{
    // Do stretching and blitting

    BlitArea(x1, y1, x2, y2);

    // Update the area

    SDL_UpdateRect(screen, 
                   x1 * screenmultiply, 
                   y1 * screenmultiply, 
                   (x2-x1) * screenmultiply, 
                   (y2-y1) * screenmultiply);
}

void I_BeginRead(void)
{
    int y;

    if (!initialised || disk_image == NULL)
        return;

    // save background and copy the disk image in

    for (y=0; y<disk_image_h; ++y)
    {
        byte *screenloc = 
               screens[0] 
                 + (SCREENHEIGHT - 1 - disk_image_h + y) * SCREENWIDTH
                 + (SCREENWIDTH - 1 - disk_image_w);

        memcpy(saved_background + y * disk_image_w,
               screenloc,
               disk_image_w);
        memcpy(screenloc, disk_image + y * disk_image_w, disk_image_w);
    }

    UpdateRect(SCREENWIDTH - disk_image_w, SCREENHEIGHT - disk_image_h,
               SCREENWIDTH, SCREENHEIGHT);
}

void I_EndRead(void)
{
    int y;

    if (!initialised || disk_image == NULL)
        return;

    // save background and copy the disk image in

    for (y=0; y<disk_image_h; ++y)
    {
        byte *screenloc = 
               screens[0] 
                 + (SCREENHEIGHT - 1 - disk_image_h + y) * SCREENWIDTH
                 + (SCREENWIDTH - 1 - disk_image_w);

        memcpy(screenloc, saved_background + y * disk_image_w, disk_image_w);
    }

    UpdateRect(SCREENWIDTH - disk_image_w, SCREENHEIGHT - disk_image_h,
               SCREENWIDTH, SCREENHEIGHT);
}

//
// I_FinishUpdate
//
void I_FinishUpdate (void)
{
    static int	lasttic;
    int		tics;
    int		i;
    // UNUSED static unsigned char *bigscreen=0;

    if (!initialised)
        return;

    if (noblit)
        return;
    
    UpdateGrab();

    // Don't update the screen if the window isn't visible.
    // Not doing this breaks under Windows when we alt-tab away 
    // while fullscreen.

    if (!(SDL_GetAppState() & SDL_APPACTIVE))
        return;

    // draws little dots on the bottom of the screen
    if (devparm)
    {

	i = I_GetTime();
	tics = i - lasttic;
	lasttic = i;
	if (tics > 20) tics = 20;

	for (i=0 ; i<tics*2 ; i+=4)
	    screens[0][ (SCREENHEIGHT-1)*SCREENWIDTH + i] = 0xff;
	for ( ; i<20*4 ; i+=4)
	    screens[0][ (SCREENHEIGHT-1)*SCREENWIDTH + i] = 0x0;
    
    }

    // draw to screen

    BlitArea(0, 0, SCREENWIDTH, SCREENHEIGHT);
    
    // If we have a palette to set, the act of setting the palette
    // updates the screen

    if (palette_to_set ||true)
    {
        SDL_SetColors(screen, palette, 0, 256);
        palette_to_set = 0;
    }
    else
    {
        SDL_Flip(screen);
    }
}


//
// I_ReadScreen
//
void I_ReadScreen (byte* scr)
{
    memcpy (scr, screens[0], SCREENWIDTH*SCREENHEIGHT);
}


//
// I_SetPalette
//
void I_SetPalette (byte *doompalette)
{
    int i;

    for (i=0; i<256; ++i) 
    {
        palette[i].r = gammatable[usegamma][*doompalette++];
        palette[i].g = gammatable[usegamma][*doompalette++];
        palette[i].b = gammatable[usegamma][*doompalette++];
    }

    palette_to_set = 1;
}

// 
// Set the window caption
//

void I_SetWindowCaption(void)
{
    char *buf;

    buf = Z_Malloc(strlen(gamedescription) + strlen(PACKAGE_STRING) + 10, 
                   PU_STATIC, NULL);
    sprintf(buf, "%s - %s", gamedescription, PACKAGE_STRING);

    SDL_WM_SetCaption(buf, NULL);

    Z_Free(buf);
}

// Set the application icon

void I_SetWindowIcon(void)
{
    SDL_Surface *surface;

    surface = SDL_CreateRGBSurfaceFrom(chocolate_doom_data,
                                       chocolate_doom_w,
                                       chocolate_doom_h,
                                       24,
                                       chocolate_doom_w * 3,
                                       0xff << 0,
                                       0xff << 8,
                                       0xff << 16,
                                       0);

    SDL_WM_SetIcon(surface, NULL);
    SDL_FreeSurface(surface);
}

// Get window dimensions for the current settings

static void GetWindowDimensions(int *windowwidth, int *windowheight)
{
    *windowwidth = SCREENWIDTH * screenmultiply;

    if (fullscreen == FULLSCREEN_LETTERBOX)
        *windowheight = LETTERBOX_SCREENHEIGHT * screenmultiply;
    else
        *windowheight = SCREENHEIGHT * screenmultiply;
}

// Check if the screen mode for the current settings is in the list available
// Not all machines support running in 320x200/640x400 (only support 4:3)
// Some don't even support modes below 640x480.

static boolean CheckValidFSMode(void)
{
    SDL_Rect **modes;
    int i;
    int w, h;

    GetWindowDimensions(&w, &h);

    modes = SDL_ListModes(NULL, SDL_FULLSCREEN);

    for (i=0; modes[i]; ++i)
    {
        if (w == modes[i]->w && h == modes[i]->h)
            return true;
    }

    // not found

    return false;
}

void I_InitGraphics(void)
{
    SDL_Event dummy;
    int flags = 0;

    SDL_Init(SDL_INIT_VIDEO);

    flags |= SDL_SWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;

    // mouse grabbing

    if (M_CheckParm("-grabmouse"))
    {
        grabmouse = true;
    }
    else if (M_CheckParm("-nograbmouse"))
    {
        grabmouse = false;
    }

    // default to fullscreen mode, allow override with command line
    // nofullscreen because we love prboom

    if (M_CheckParm("-window") || M_CheckParm("-nofullscreen"))
    {
        fullscreen = FULLSCREEN_OFF;
    }
    else if (M_CheckParm("-fullscreen"))
    {
        fullscreen = FULLSCREEN_ON;
    }

    if (fullscreen != FULLSCREEN_OFF)
    {
        flags |= SDL_FULLSCREEN;
    }

    nomouse = M_CheckParm("-nomouse") > 0;

    // scale-by-2 mode
 
    if (M_CheckParm("-1"))
    {
        screenmultiply = 1;
    }
    else if (M_CheckParm("-2"))
    {
        screenmultiply = 2;
    }
    else if (M_CheckParm("-3"))
    {
        screenmultiply = 3;
    }
    else if (M_CheckParm("-4"))
    {
        screenmultiply = 4;
    }

    if (screenmultiply < 1)
        screenmultiply = 1;
    if (screenmultiply > 4)
        screenmultiply = 4;

    if (fullscreen && autoadjust_video_settings)
    {
        int oldw, oldh;
        int old_fullscreen, old_screenmultiply;

        GetWindowDimensions(&oldw, &oldh);
        old_screenmultiply = screenmultiply;
        old_fullscreen = fullscreen;

        if (!CheckValidFSMode() && screenmultiply == 1 
         && fullscreen == FULLSCREEN_ON)
        {
            // 320x200 is not valid.

            // Try turning on letterbox mode - avoid doubling up
            // the screen if possible

            fullscreen = FULLSCREEN_LETTERBOX;

            if (!CheckValidFSMode())
            {
                // That doesn't work. Change it back.

                fullscreen = FULLSCREEN_ON;
            }
        }

        if (!CheckValidFSMode() && screenmultiply == 1)
        {
            // Try doubling up the screen to 640x400

            screenmultiply = 2;
        }

        if (!CheckValidFSMode() && fullscreen == FULLSCREEN_ON)
        {
            // This is not a valid mode.  Try turning on letterbox mode

            fullscreen = FULLSCREEN_LETTERBOX;
        }

        if (old_fullscreen != fullscreen 
         || old_screenmultiply != screenmultiply)
        {
            printf("I_InitGraphics: %ix%i resolution is not supported "
                   "on this machine. \n", oldw, oldh);
            printf("I_InitGraphics: Video settings adjusted to "
                   "compensate:\n");
            
            if (fullscreen != old_fullscreen)
                printf("\tletterbox mode on (fullscreen=2)\n");
            if (screenmultiply != old_screenmultiply)
                printf("\tscreenmultiply=%i\n", screenmultiply);
            
            printf("NOTE: Your video settings have been adjusted.  "
                   "To disable this behavior,\n"
                   "set autoadjust_video_settings to 0 in your "
                   "configuration file.\n");
        }
        
        if (!CheckValidFSMode())
        {
            printf("I_InitGraphics: WARNING: Unable to find a valid "
                   "fullscreen video mode to run in.\n");
        }
    }

    GetWindowDimensions(&windowwidth, &windowheight);

    screen = SDL_SetVideoMode(windowwidth, windowheight, 8, flags);

    if (screen == NULL)
    {
        I_Error("Error setting video mode: %s\n", SDL_GetError());
    }

    // Start with a clear black screen
    // (screen will be flipped after we set the palette)

    if (SDL_LockSurface(screen) >= 0)
    {
        byte *screenpixels;
        int y;

        screenpixels = (byte *) screen->pixels;

        for (y=0; y<screen->h; ++y)
            memset(screenpixels + screen->pitch * y, 0, screen->w);

        SDL_UnlockSurface(screen);
    }
    
    // Set the palette

    I_SetPalette (W_CacheLumpName ("PLAYPAL",PU_CACHE));
    SDL_SetColors(screen, palette, 0, 256);

    // Setup title and icon

    I_SetWindowCaption();
    I_SetWindowIcon();

    UpdateFocus();
    UpdateGrab();

    // On some systems, it takes a second or so for the screen to settle
    // after changing modes.  We include the option to add a delay when
    // setting the screen mode, so that the game doesn't start immediately
    // with the player unable to see anything.

    if (fullscreen)
    {
        SDL_Delay(startup_delay);
    }

    // Check if we have a native surface we can use
    // If we have to lock the screen, draw to a buffer and copy
    // Likewise if the screen pitch is not the same as the width
    // If we have to multiply, drawing is done to a separate 320x200 buf

    native_surface = !SDL_MUSTLOCK(screen) 
                  && screenmultiply == 1 
                  && screen->pitch == SCREENWIDTH;

    // If not, allocate a buffer and copy from that buffer to the 
    // screen when we do an update

    if (native_surface)
    {
	screens[0] = (unsigned char *) (screen->pixels);

        if (fullscreen == FULLSCREEN_LETTERBOX)
        {
            screens[0] += ((LETTERBOX_SCREENHEIGHT - SCREENHEIGHT) * screen->pitch) / 2;
        }
    }
    else
    {
	screens[0] = (unsigned char *) Z_Malloc (SCREENWIDTH * SCREENHEIGHT, PU_STATIC, NULL);
    }

    // Loading from disk icon

    LoadDiskImage();

    memset(screens[0], 0, SCREENWIDTH * SCREENHEIGHT);

    // We need SDL to give us translated versions of keys as well

    SDL_EnableUNICODE(1);

    // clear out any events waiting at the start
  
    while (SDL_PollEvent(&dummy));

    initialised = true;
}