This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
/
i_video.c
1533 lines (1297 loc) · 38 KB
/
i_video.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
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
*
* PrBoom: a Doom port merged with LxDoom and LSDLDoom
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* Copyright (C) 1999-2006 by
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
* Copyright 2005, 2006 by
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
*
* 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.
*
* DESCRIPTION:
* DOOM graphics stuff for SDL
*
*-----------------------------------------------------------------------------
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <windows.h>
#endif // _WIN32
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "SDL.h"
//e6y
#ifdef _WIN32
#include <SDL_syswm.h>
#endif
#include "m_argv.h"
#include "doomstat.h"
#include "doomdef.h"
#include "doomtype.h"
#include "v_video.h"
#include "r_draw.h"
#include "r_things.h"
#include "r_plane.h"
#include "r_main.h"
#include "f_wipe.h"
#include "d_main.h"
#include "d_event.h"
#include "d_deh.h"
#include "i_joy.h"
#include "i_video.h"
#include "i_capture.h"
#include "z_zone.h"
#include "s_sound.h"
#include "sounds.h"
#include "w_wad.h"
#include "st_stuff.h"
#include "am_map.h"
#include "g_game.h"
#include "lprintf.h"
#ifdef GL_DOOM
#include "gl_struct.h"
#endif
#include "e6y.h"//e6y
#include "i_main.h"
//e6y: new mouse code
static SDL_Cursor* cursors[2] = {NULL, NULL};
dboolean window_focused;
int mouse_currently_grabbed = true;
// Window resize state.
static void ApplyWindowResize(SDL_Event *resize_event);
const char *sdl_video_window_pos;
static void ActivateMouse(void);
static void DeactivateMouse(void);
//static int AccelerateMouse(int val);
static void I_ReadMouse(void);
static dboolean MouseShouldBeGrabbed();
static void UpdateFocus(void);
int gl_colorbuffer_bits=16;
int gl_depthbuffer_bits=16;
extern void M_QuitDOOM(int choice);
int use_fullscreen;
int desired_fullscreen;
int render_vsync;
int screen_multiply;
int render_screen_multiply;
SDL_Surface *screen;
static SDL_Surface *buffer;
SDL_Window *sdl_window;
SDL_Renderer *sdl_renderer;
static SDL_Texture *sdl_texture;
static SDL_Texture *sdl_texture_upscaled;
static SDL_GLContext sdl_glcontext;
unsigned int windowid = 0;
SDL_Rect src_rect = { 0, 0, 0, 0 };
SDL_Rect dst_rect = { 0, 0, 0, 0 };
////////////////////////////////////////////////////////////////////////////
// Input code
int leds_always_off = 0; // Expected by m_misc, not relevant
// Mouse handling
extern int usemouse; // config file var
static dboolean mouse_enabled; // usemouse, but can be overriden by -nomouse
video_mode_t I_GetModeFromString(const char *modestr);
/////////////////////////////////////////////////////////////////////////////////
// Keyboard handling
//
// Translates the key currently in key
//
static int I_TranslateKey(SDL_Keysym* key)
{
int rc = 0;
switch (key->sym) {
case SDLK_LEFT: rc = KEYD_LEFTARROW; break;
case SDLK_RIGHT: rc = KEYD_RIGHTARROW; break;
case SDLK_DOWN: rc = KEYD_DOWNARROW; break;
case SDLK_UP: rc = KEYD_UPARROW; break;
case SDLK_ESCAPE: rc = KEYD_ESCAPE; break;
case SDLK_RETURN: rc = KEYD_ENTER; break;
case SDLK_TAB: rc = KEYD_TAB; break;
case SDLK_F1: rc = KEYD_F1; break;
case SDLK_F2: rc = KEYD_F2; break;
case SDLK_F3: rc = KEYD_F3; break;
case SDLK_F4: rc = KEYD_F4; break;
case SDLK_F5: rc = KEYD_F5; break;
case SDLK_F6: rc = KEYD_F6; break;
case SDLK_F7: rc = KEYD_F7; break;
case SDLK_F8: rc = KEYD_F8; break;
case SDLK_F9: rc = KEYD_F9; break;
case SDLK_F10: rc = KEYD_F10; break;
case SDLK_F11: rc = KEYD_F11; break;
case SDLK_F12: rc = KEYD_F12; break;
case SDLK_BACKSPACE: rc = KEYD_BACKSPACE; break;
case SDLK_DELETE: rc = KEYD_DEL; break;
case SDLK_INSERT: rc = KEYD_INSERT; break;
case SDLK_PAGEUP: rc = KEYD_PAGEUP; break;
case SDLK_PAGEDOWN: rc = KEYD_PAGEDOWN; break;
case SDLK_HOME: rc = KEYD_HOME; break;
case SDLK_END: rc = KEYD_END; break;
case SDLK_PAUSE: rc = KEYD_PAUSE; break;
case SDLK_EQUALS: rc = KEYD_EQUALS; break;
case SDLK_MINUS: rc = KEYD_MINUS; break;
case SDLK_KP_0: rc = KEYD_KEYPAD0; break;
case SDLK_KP_1: rc = KEYD_KEYPAD1; break;
case SDLK_KP_2: rc = KEYD_KEYPAD2; break;
case SDLK_KP_3: rc = KEYD_KEYPAD3; break;
case SDLK_KP_4: rc = KEYD_KEYPAD4; break;
case SDLK_KP_5: rc = KEYD_KEYPAD5; break;
case SDLK_KP_6: rc = KEYD_KEYPAD6; break;
case SDLK_KP_7: rc = KEYD_KEYPAD7; break;
case SDLK_KP_8: rc = KEYD_KEYPAD8; break;
case SDLK_KP_9: rc = KEYD_KEYPAD9; break;
case SDLK_KP_PLUS: rc = KEYD_KEYPADPLUS; break;
case SDLK_KP_MINUS: rc = KEYD_KEYPADMINUS; break;
case SDLK_KP_DIVIDE: rc = KEYD_KEYPADDIVIDE; break;
case SDLK_KP_MULTIPLY: rc = KEYD_KEYPADMULTIPLY; break;
case SDLK_KP_ENTER: rc = KEYD_KEYPADENTER; break;
case SDLK_KP_PERIOD: rc = KEYD_KEYPADPERIOD; break;
case SDLK_LSHIFT:
case SDLK_RSHIFT: rc = KEYD_RSHIFT; break;
case SDLK_LCTRL:
case SDLK_RCTRL: rc = KEYD_RCTRL; break;
case SDLK_LALT:
case SDLK_LGUI:
case SDLK_RALT:
case SDLK_RGUI: rc = KEYD_RALT; break;
case SDLK_CAPSLOCK: rc = KEYD_CAPSLOCK; break;
case SDLK_PRINTSCREEN: rc = KEYD_PRINTSC; break;
default: rc = key->sym; break;
}
return rc;
}
/////////////////////////////////////////////////////////////////////////////////
// Main input code
/* cph - pulled out common button code logic */
//e6y static
int I_SDLtoDoomMouseState(Uint32 buttonstate)
{
return 0
| (buttonstate & SDL_BUTTON(1) ? 1 : 0)
| (buttonstate & SDL_BUTTON(2) ? 2 : 0)
| (buttonstate & SDL_BUTTON(3) ? 4 : 0)
| (buttonstate & SDL_BUTTON(6) ? 8 : 0)
| (buttonstate & SDL_BUTTON(7) ? 16 : 0)
;
}
static void I_GetEvent(void)
{
event_t event;
SDL_Event SDLEvent;
SDL_Event *Event = &SDLEvent;
static int mwheeluptic = 0, mwheeldowntic = 0;
while (SDL_PollEvent(Event))
{
switch (Event->type) {
case SDL_KEYDOWN:
#ifdef MACOSX
if (Event->key.keysym.mod & KMOD_META)
{
// Switch windowed<->fullscreen if pressed <Command-F>
if (Event->key.keysym.sym == SDLK_f)
{
V_ToggleFullscreen();
break;
}
}
#else
if (Event->key.keysym.mod & KMOD_LALT)
{
// Prevent executing action on Alt-Tab
if (Event->key.keysym.sym == SDLK_TAB)
{
break;
}
// Switch windowed<->fullscreen if pressed Alt-Enter
else if (Event->key.keysym.sym == SDLK_RETURN)
{
V_ToggleFullscreen();
break;
}
// Immediately exit on Alt+F4 ("Boss Key")
else if (Event->key.keysym.sym == SDLK_F4)
{
I_SafeExit(0);
break;
}
}
#endif
event.type = ev_keydown;
event.data1 = I_TranslateKey(&Event->key.keysym);
D_PostEvent(&event);
break;
case SDL_KEYUP:
{
event.type = ev_keyup;
event.data1 = I_TranslateKey(&Event->key.keysym);
D_PostEvent(&event);
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (mouse_enabled && window_focused)
{
event.type = ev_mouse;
event.data1 = I_SDLtoDoomMouseState(SDL_GetMouseState(NULL, NULL));
event.data2 = event.data3 = 0;
D_PostEvent(&event);
}
break;
case SDL_MOUSEWHEEL:
if (mouse_enabled && window_focused)
{
if (Event->wheel.y > 0)
{
event.type = ev_keydown;
event.data1 = KEYD_MWHEELUP;
mwheeldowntic = gametic;
D_PostEvent(&event);
}
else if (Event->wheel.y < 0)
{
event.type = ev_keydown;
event.data1 = KEYD_MWHEELDOWN;
mwheeluptic = gametic;
D_PostEvent(&event);
}
}
break;
case SDL_MOUSEMOTION:
if (mouse_enabled && window_focused)
{
event.type = ev_mouse;
event.data1 = I_SDLtoDoomMouseState(Event->motion.state);
event.data2 = Event->motion.xrel << 4;
event.data3 = -Event->motion.yrel << 4;
D_PostEvent(&event);
}
break;
case SDL_WINDOWEVENT:
if (Event->window.windowID == windowid)
{
switch (Event->window.event)
{
case SDL_WINDOWEVENT_FOCUS_GAINED:
case SDL_WINDOWEVENT_FOCUS_LOST:
UpdateFocus();
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
ApplyWindowResize(Event);
break;
}
}
break;
case SDL_QUIT:
S_StartSound(NULL, sfx_swtchn);
M_QuitDOOM(0);
default:
break;
}
}
if(mwheeluptic && mwheeluptic + 1 < gametic)
{
event.type = ev_keyup;
event.data1 = KEYD_MWHEELUP;
D_PostEvent(&event);
mwheeluptic = 0;
}
if(mwheeldowntic && mwheeldowntic + 1 < gametic)
{
event.type = ev_keyup;
event.data1 = KEYD_MWHEELDOWN;
D_PostEvent(&event);
mwheeldowntic = 0;
}
}
//
// I_StartTic
//
void I_StartTic (void)
{
I_GetEvent();
I_ReadMouse();
I_PollJoystick();
}
//
// I_StartFrame
//
void I_StartFrame (void)
{
}
//
// I_InitInputs
//
static void I_InitInputs(void)
{
static Uint8 empty_cursor_data = 0;
int nomouse_parm = M_CheckParm("-nomouse");
// check if the user wants to use the mouse
mouse_enabled = usemouse && !nomouse_parm;
SDL_PumpEvents();
// Save the default cursor so it can be recalled later
cursors[0] = SDL_GetCursor();
// Create an empty cursor
cursors[1] = SDL_CreateCursor(&empty_cursor_data, &empty_cursor_data, 8, 1, 0, 0);
if (mouse_enabled)
{
MouseAccelChanging();
}
I_InitJoystick();
}
/////////////////////////////////////////////////////////////////////////////
// I_SkipFrame
//
// Returns true if it thinks we can afford to skip this frame
inline static dboolean I_SkipFrame(void)
{
static int frameno;
frameno++;
switch (gamestate) {
case GS_LEVEL:
if (!paused)
return false;
default:
// Skip odd frames
return (frameno & 1) ? true : false;
}
}
///////////////////////////////////////////////////////////
// Palette stuff.
//
static void I_UploadNewPalette(int pal, int force)
{
// This is used to replace the current 256 colour cmap with a new one
// Used by 256 colour PseudoColor modes
// Array of SDL_Color structs used for setting the 256-colour palette
static SDL_Color* colours;
static int cachedgamma;
static size_t num_pals;
if (V_GetMode() == VID_MODEGL)
return;
if ((colours == NULL) || (cachedgamma != usegamma) || force) {
int pplump = W_GetNumForName("PLAYPAL");
int gtlump = (W_CheckNumForName)("GAMMATBL",ns_prboom);
register const byte * palette = (const byte*)W_CacheLumpNum(pplump);
register const byte * const gtable = (const byte *)W_CacheLumpNum(gtlump) + 256*(cachedgamma = usegamma);
register int i;
num_pals = W_LumpLength(pplump) / (3*256);
num_pals *= 256;
if (!colours) {
// First call - allocate and prepare colour array
colours = (SDL_Color*)malloc(sizeof(*colours)*num_pals);
}
// set the colormap entries
for (i=0 ; (size_t)i<num_pals ; i++) {
colours[i].r = gtable[palette[0]];
colours[i].g = gtable[palette[1]];
colours[i].b = gtable[palette[2]];
palette += 3;
}
W_UnlockLumpNum(pplump);
W_UnlockLumpNum(gtlump);
num_pals/=256;
}
#ifdef RANGECHECK
if ((size_t)pal >= num_pals)
I_Error("I_UploadNewPalette: Palette number out of range (%d>=%d)",
pal, num_pals);
#endif
SDL_SetPaletteColors(screen->format->palette, colours+256*pal, 0, 256);
}
//////////////////////////////////////////////////////////////////////////////
// Graphics API
void I_ShutdownGraphics(void)
{
SDL_FreeCursor(cursors[1]);
DeactivateMouse();
}
//
// I_UpdateNoBlit
//
void I_UpdateNoBlit (void)
{
}
//
// I_FinishUpdate
//
static int newpal = 0;
#define NO_PALETTE_CHANGE 1000
void I_FinishUpdate (void)
{
//e6y: new mouse code
UpdateGrab();
// The screen wipe following pressing the exit switch on a level
// is noticably jerkier with I_SkipFrame
// if (I_SkipFrame())return;
#ifdef MONITOR_VISIBILITY
//!!if (!(SDL_GetAppState()&SDL_APPACTIVE)) {
//!! return;
//!!}
#endif
#ifdef GL_DOOM
if (V_GetMode() == VID_MODEGL) {
// proff 04/05/2000: swap OpenGL buffers
gld_Finish();
return;
}
#endif
if ((screen_multiply > 1) || SDL_MUSTLOCK(screen)) {
int h;
byte *src;
byte *dest;
if (SDL_LockSurface(screen) < 0) {
lprintf(LO_INFO,"I_FinishUpdate: %s\n", SDL_GetError());
return;
}
dest=(byte*)screen->pixels;
src=screens[0].data;
h=screen->h;
for (; h>0; h--)
{
memcpy(dest,src,SCREENWIDTH*V_GetPixelDepth()); //e6y
dest+=screen->pitch;
src+=screens[0].byte_pitch;
}
SDL_UnlockSurface(screen);
}
/* Update the display buffer (flipping video pages if supported)
* If we need to change palette, that implicitely does a flip */
if (newpal != NO_PALETTE_CHANGE) {
I_UploadNewPalette(newpal, false);
newpal = NO_PALETTE_CHANGE;
}
// Blit from the paletted 8-bit screen buffer to the intermediate
// 32-bit RGBA buffer that we can load into the texture.
SDL_LowerBlit(screen, &src_rect, buffer, &src_rect);
// Update the intermediate texture with the contents of the RGBA buffer.
SDL_UpdateTexture(sdl_texture, &src_rect, buffer->pixels, buffer->pitch);
// Make sure the pillarboxes are kept clear each frame.
SDL_RenderClear(sdl_renderer);
if (screen_multiply > 1)
{
// Render this intermediate texture into the upscaled texture
// using "nearest" integer scaling.
SDL_SetRenderTarget(sdl_renderer, sdl_texture_upscaled);
SDL_RenderCopy(sdl_renderer, sdl_texture, &src_rect, NULL);
// Finally, render this upscaled texture to screen using linear scaling.
SDL_SetRenderTarget(sdl_renderer, NULL);
SDL_RenderCopy(sdl_renderer, sdl_texture_upscaled, NULL, NULL);
}
else
{
SDL_RenderCopy(sdl_renderer, sdl_texture, &src_rect, NULL);
}
// Draw!
SDL_RenderPresent(sdl_renderer);
}
//
// I_ScreenShot - moved to i_sshot.c
//
//
// I_SetPalette
//
void I_SetPalette (int pal)
{
newpal = pal;
}
// I_PreInitGraphics
static void I_ShutdownSDL(void)
{
SDL_Quit();
return;
}
void I_PreInitGraphics(void)
{
int p;
// Initialize SDL
unsigned int flags = 0;
if (!(M_CheckParm("-nodraw") && M_CheckParm("-nosound")))
flags = SDL_INIT_VIDEO;
#ifdef PRBOOM_DEBUG
flags |= SDL_INIT_NOPARACHUTE;
#endif
p = SDL_Init(flags);
if (p < 0)
{
I_Error("Could not initialize SDL [%s]", SDL_GetError());
}
atexit(I_ShutdownSDL);
}
// e6y: resolution limitation is removed
void I_InitBuffersRes(void)
{
R_InitMeltRes();
R_InitSpritesRes();
R_InitBuffersRes();
R_InitPlanesRes();
R_InitVisplanesRes();
}
#define MAX_RESOLUTIONS_COUNT 128
const char *screen_resolutions_list[MAX_RESOLUTIONS_COUNT] = {NULL};
const char *screen_resolution = NULL;
//
// I_GetScreenResolution
// Get current resolution from the config variable (WIDTHxHEIGHT format)
// 640x480 if screen_resolution variable has wrong data
//
void I_GetScreenResolution(void)
{
int width, height;
desired_screenwidth = 640;
desired_screenheight = 480;
if (screen_resolution)
{
if (sscanf(screen_resolution, "%dx%d", &width, &height) == 2)
{
desired_screenwidth = width;
desired_screenheight = height;
}
}
}
// make sure the canonical resolutions are always available
static const struct {
const int w, h;
} canonicals[] = {
{640, 480}, // Doom 95
{640, 400}, // MBF
{320, 240}, // Doom 95
{320, 200}, // Vanilla Doom
};
static const int num_canonicals = sizeof(canonicals)/sizeof(*canonicals);
//
// I_FillScreenResolutionsList
// Get all the supported screen resolutions
// and fill the list with them
//
static void I_FillScreenResolutionsList(void)
{
int display_index = 0;
SDL_DisplayMode mode;
int i, j, list_size, current_resolution_index, count;
char mode_name[256];
// do it only once
if (screen_resolutions_list[0])
{
return;
}
if (desired_screenwidth == 0 || desired_screenheight == 0)
{
I_GetScreenResolution();
}
// Don't call SDL_ListModes if SDL has not been initialized
count = 0;
if (!nodrawers)
count = SDL_GetNumDisplayModes(display_index);
list_size = 0;
current_resolution_index = -1;
// on success, SDL_GetNumDisplayModes() always returns at least 1
if (count > 0)
{
// -2 for the desired resolution and for NULL
count = MIN(count, MAX_RESOLUTIONS_COUNT - 2 - num_canonicals);
for(i = count - 1 + num_canonicals; i >= 0; i--)
{
int in_list = false;
// make sure the canonical resolutions are always available
if (i > count - 1)
{
mode.w = canonicals[i - count].w;
mode.h = canonicals[i - count].h;
}
else
{
SDL_GetDisplayMode(display_index, i, &mode);
}
doom_snprintf(mode_name, sizeof(mode_name), "%dx%d", mode.w, mode.h);
for(j = 0; j < list_size; j++)
{
if (!strcmp(mode_name, screen_resolutions_list[j]))
{
in_list = true;
break;
}
}
if (!in_list)
{
screen_resolutions_list[list_size] = strdup(mode_name);
if (mode.w == desired_screenwidth && mode.h == desired_screenheight)
{
current_resolution_index = list_size;
}
list_size++;
}
}
screen_resolutions_list[list_size] = NULL;
}
if (list_size == 0)
{
doom_snprintf(mode_name, sizeof(mode_name), "%dx%d", desired_screenwidth, desired_screenheight);
screen_resolutions_list[0] = strdup(mode_name);
current_resolution_index = 0;
list_size = 1;
}
if (current_resolution_index == -1)
{
doom_snprintf(mode_name, sizeof(mode_name), "%dx%d", desired_screenwidth, desired_screenheight);
// make it first
list_size++;
for(i = list_size - 1; i > 0; i--)
{
screen_resolutions_list[i] = screen_resolutions_list[i - 1];
}
screen_resolutions_list[0] = strdup(mode_name);
current_resolution_index = 0;
}
screen_resolutions_list[list_size] = NULL;
screen_resolution = screen_resolutions_list[current_resolution_index];
}
// e6y
// GLBoom use this function for trying to set the closest supported resolution if the requested mode can't be set correctly.
// For example glboom.exe -geom 1025x768 -nowindow will set 1024x768.
// It should be used only for fullscreen modes.
static void I_ClosestResolution (int *width, int *height)
{
int display_index = 0;
int twidth, theight;
int cwidth = 0, cheight = 0;
int i, count;
unsigned int closest = UINT_MAX;
unsigned int dist;
if (!SDL_WasInit(SDL_INIT_VIDEO))
return;
count = SDL_GetNumDisplayModes(display_index);
if (count > 0)
{
for(i=0; i<count; ++i)
{
SDL_DisplayMode mode;
SDL_GetDisplayMode(display_index, i, &mode);
twidth = mode.w;
theight = mode.h;
if (twidth == *width && theight == *height)
return;
//if (iteration == 0 && (twidth < *width || theight < *height))
// continue;
dist = (twidth - *width) * (twidth - *width) +
(theight - *height) * (theight - *height);
if (dist < closest)
{
closest = dist;
cwidth = twidth;
cheight = theight;
}
}
if (closest != 4294967295u)
{
*width = cwidth;
*height = cheight;
return;
}
}
}
int process_affinity_mask;
int process_priority;
// e6y
// It is a simple test of CPU cache misses.
unsigned int I_TestCPUCacheMisses(int width, int height, unsigned int mintime)
{
int i, k;
char *s, *d, *ps, *pd;
unsigned int tickStart;
s = (char*)malloc(width * height);
d = (char*)malloc(width * height);
tickStart = SDL_GetTicks();
k = 0;
do
{
ps = s;
pd = d;
for(i = 0; i < height; i++)
{
pd[0] = ps[0];
pd += width;
ps += width;
}
k++;
}
while (SDL_GetTicks() - tickStart < mintime);
free(d);
free(s);
return k;
}
// CPhipps -
// I_CalculateRes
// Calculates the screen resolution, possibly using the supplied guide
void I_CalculateRes(int width, int height)
{
// e6y
// GLBoom will try to set the closest supported resolution
// if the requested mode can't be set correctly.
// For example glboom.exe -geom 1025x768 -nowindow will set 1024x768.
// It affects only fullscreen modes.
if (V_GetMode() == VID_MODEGL) {
if ( desired_fullscreen )
{
I_ClosestResolution(&width, &height);
}
SCREENWIDTH = width;
SCREENHEIGHT = height;
SCREENPITCH = SCREENWIDTH;
} else {
unsigned int count1, count2;
int pitch1, pitch2;
SCREENWIDTH = width;//(width+15) & ~15;
SCREENHEIGHT = height;
// e6y
// Trying to optimise screen pitch for reducing of CPU cache misses.
// It is extremally important for wiping in software.
// I have ~20x improvement in speed with using 1056 instead of 1024 on Pentium4
// and only ~10% for Core2Duo
if (1)
{
unsigned int mintime = 100;
int w = (width+15) & ~15;
pitch1 = w * V_GetPixelDepth();
pitch2 = w * V_GetPixelDepth() + 32;
count1 = I_TestCPUCacheMisses(pitch1, SCREENHEIGHT, mintime);
count2 = I_TestCPUCacheMisses(pitch2, SCREENHEIGHT, mintime);
lprintf(LO_INFO, "I_CalculateRes: trying to optimize screen pitch\n");
lprintf(LO_INFO, " test case for pitch=%d is processed %d times for %d msec\n", pitch1, count1, mintime);
lprintf(LO_INFO, " test case for pitch=%d is processed %d times for %d msec\n", pitch2, count2, mintime);
SCREENPITCH = (count2 > count1 ? pitch2 : pitch1);
lprintf(LO_INFO, " optimized screen pitch is %d\n", SCREENPITCH);
}
else
{
SCREENPITCH = SCREENWIDTH * V_GetPixelDepth();
}
}
// e6y: processing of screen_multiply
{
int factor = ((V_GetMode() == VID_MODEGL) ? 1 : render_screen_multiply);
REAL_SCREENWIDTH = SCREENWIDTH * factor;
REAL_SCREENHEIGHT = SCREENHEIGHT * factor;
REAL_SCREENPITCH = SCREENPITCH * factor;
}
}
// CPhipps -
// I_InitScreenResolution
// Sets the screen resolution
// e6y: processing of screen_multiply
void I_InitScreenResolution(void)
{
int i, p, w, h;
char c, x;
video_mode_t mode;
int init = (sdl_window == NULL);
I_GetScreenResolution();
if (init)
{
//e6y: ability to change screen resolution from GUI
I_FillScreenResolutionsList();
// Video stuff
if ((p = M_CheckParm("-width")))
if (myargv[p+1])
desired_screenwidth = atoi(myargv[p+1]);
if ((p = M_CheckParm("-height")))
if (myargv[p+1])
desired_screenheight = atoi(myargv[p+1]);
if ((p = M_CheckParm("-fullscreen")))
use_fullscreen = 1;
if ((p = M_CheckParm("-nofullscreen")))
use_fullscreen = 0;
// e6y
// New command-line options for setting a window (-window)
// or fullscreen (-nowindow) mode temporarily which is not saved in cfg.
// It works like "-geom" switch
desired_fullscreen = use_fullscreen;
if ((p = M_CheckParm("-window")))
desired_fullscreen = 0;
if ((p = M_CheckParm("-nowindow")))
desired_fullscreen = 1;
// e6y
// change the screen size for the current session only
// syntax: -geom WidthxHeight[w|f]
// examples: -geom 320x200f, -geom 640x480w, -geom 1024x768
w = desired_screenwidth;
h = desired_screenheight;
if (!(p = M_CheckParm("-geom")))
p = M_CheckParm("-geometry");
if (p && p + 1 < myargc)
{
int count = sscanf(myargv[p+1], "%d%c%d%c", &w, &x, &h, &c);
// at least width and height must be specified
// restoring original values if not
if (count < 3 || tolower(x) != 'x')
{