forked from chocolate-doom/chocolate-doom
-
Notifications
You must be signed in to change notification settings - Fork 132
/
i_video.c
2056 lines (1644 loc) · 51.1 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
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005-2014 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.
//
// DESCRIPTION:
// DOOM graphics stuff for SDL.
//
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#include "SDL_opengl.h"
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
#include "crispy.h"
#include "config.h"
#include "d_loop.h"
#include "deh_str.h"
#include "doomtype.h"
#include "i_input.h"
#include "i_joystick.h"
#include "i_system.h"
#include "i_timer.h"
#include "i_video.h"
#include "m_argv.h"
#include "m_config.h"
#include "m_misc.h"
#include "tables.h"
#include "v_diskicon.h"
#include "v_video.h"
#include "w_wad.h"
#include "z_zone.h"
int SCREENWIDTH, SCREENHEIGHT, SCREENHEIGHT_4_3;
int NONWIDEWIDTH; // [crispy] non-widescreen SCREENWIDTH
int WIDESCREENDELTA; // [crispy] horizontal widescreen offset
// These are (1) the window (or the full screen) that our game is rendered to
// and (2) the renderer that scales the texture (see below) into this window.
static SDL_Window *screen;
static SDL_Renderer *renderer;
// Window title
static const char *window_title = "";
// These are (1) the 320x200x8 paletted buffer that we draw to (i.e. the one
// that holds I_VideoBuffer), (2) the 320x200x32 RGBA intermediate buffer that
// we blit the former buffer to, (3) the intermediate 320x200 texture that we
// load the RGBA buffer to and that we render into another texture (4) which
// is upscaled by an integer factor UPSCALE using "nearest" scaling and which
// in turn is finally rendered to screen using "linear" scaling.
#ifndef CRISPY_TRUECOLOR
static SDL_Surface *screenbuffer = NULL;
#endif
static SDL_Surface *argbbuffer = NULL;
static SDL_Texture *texture = NULL;
static SDL_Texture *texture_upscaled = NULL;
#ifndef CRISPY_TRUECOLOR
static SDL_Rect blit_rect = {
0,
0,
MAXWIDTH,
MAXHEIGHT
};
#endif
static uint32_t pixel_format;
// palette
#ifdef CRISPY_TRUECOLOR
static SDL_Texture *curpane = NULL;
static SDL_Texture *redpane = NULL;
static SDL_Texture *yelpane = NULL;
static SDL_Texture *grnpane = NULL;
static int pane_alpha;
static unsigned int rmask, gmask, bmask, amask; // [crispy] moved up here
static const uint8_t blend_alpha = 0xa8;
extern pixel_t* colormaps; // [crispy] evil hack to get FPS dots working as in Vanilla
#else
static SDL_Color palette[256];
#endif
static boolean palette_to_set;
// display has been set up?
static boolean initialized = false;
// disable mouse?
static boolean nomouse = false;
int usemouse = 1;
// Save screenshots in PNG format.
int png_screenshots = 1; // [crispy]
// SDL video driver name
char *video_driver = "";
// Window position:
char *window_position = "center";
// SDL display number on which to run.
int video_display = 0;
// Screen width and height, from configuration file.
int window_width = 800;
int window_height = 600;
// Fullscreen mode, 0x0 for SDL_WINDOW_FULLSCREEN_DESKTOP.
int fullscreen_width = 0, fullscreen_height = 0;
// Maximum number of pixels to use for intermediate scale buffer.
static int max_scaling_buffer_pixels = 16000000;
// Run in full screen mode? (int type for config code)
int fullscreen = true;
// Aspect ratio correction mode
int aspect_ratio_correct = true;
static int actualheight;
// Force integer scales for resolution-independent rendering
int integer_scaling = false;
// VGA Porch palette change emulation
int vga_porch_flash = false;
// Force software rendering, for systems which lack effective hardware
// acceleration
int force_software_renderer = false;
// Time to wait for the screen to settle on startup before starting the
// game (ms)
static int startup_delay = 1000;
// Grab the mouse? (int type for config code). nograbmouse_override allows
// this to be temporarily disabled via the command line.
static int grabmouse = true;
static boolean nograbmouse_override = false;
// The screen buffer; this is modified to draw things to the screen
pixel_t *I_VideoBuffer = NULL;
// If true, game is running as a screensaver
boolean screensaver_mode = false;
// Flag indicating whether the screen is currently visible:
// when the screen isnt visible, don't render the screen
boolean screenvisible = true;
// If true, we display dots at the bottom of the screen to
// indicate FPS.
static boolean display_fps_dots;
// If this is true, the screen is rendered but not blitted to the
// video buffer.
static boolean noblit;
// Callback function to invoke to determine whether to grab the
// mouse pointer.
static grabmouse_callback_t grabmouse_callback = NULL;
// Does the window currently have focus?
static boolean window_focused = true;
// Window resize state.
static boolean need_resize = false;
static unsigned int last_resize_time;
#define RESIZE_DELAY 500
// Gamma correction level to use
int usegamma = 0;
// Joystick/gamepad hysteresis
unsigned int joywait = 0;
// Icon RGB data and dimensions
static const unsigned int *icon_data;
static int icon_w;
static int icon_h;
static boolean MouseShouldBeGrabbed()
{
// never grab the mouse when in screensaver mode
if (screensaver_mode)
return false;
// if the window doesn't 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)
return true;
// Don't grab the mouse if mouse input is disabled
if (!usemouse || nomouse)
return false;
// if we specify not to grab the mouse, never grab
if (nograbmouse_override || !grabmouse)
return false;
// Invoke the grabmouse callback function to determine whether
// the mouse should be grabbed
if (grabmouse_callback != NULL)
{
return grabmouse_callback();
}
else
{
return true;
}
}
void I_SetGrabMouseCallback(grabmouse_callback_t func)
{
grabmouse_callback = func;
}
// Set the variable controlling FPS dots.
void I_DisplayFPSDots(boolean dots_on)
{
display_fps_dots = dots_on;
}
static void SetShowCursor(boolean show)
{
if (!screensaver_mode)
{
// When the cursor is hidden, grab the input.
// Relative mode implicitly hides the cursor.
SDL_SetRelativeMouseMode(!show);
SDL_GetRelativeMouseState(NULL, NULL);
}
}
void I_ShutdownGraphics(void)
{
if (initialized)
{
SetShowCursor(true);
SDL_QuitSubSystem(SDL_INIT_VIDEO);
initialized = false;
}
}
//
// I_StartFrame
//
void I_StartFrame (void)
{
// er?
}
// Adjust window_width / window_height variables to be an an aspect
// ratio consistent with the aspect_ratio_correct variable.
static void AdjustWindowSize(void)
{
if (aspect_ratio_correct || integer_scaling)
{
static int old_v_w, old_v_h;
if (old_v_w > 0 && old_v_h > 0)
{
int rendered_height;
// rendered height does not necessarily match window height
if (window_height * old_v_w > window_width * old_v_h)
rendered_height = (window_width * old_v_h + old_v_w - 1) / old_v_w;
else
rendered_height = window_height;
window_width = rendered_height * SCREENWIDTH / actualheight;
}
old_v_w = SCREENWIDTH;
old_v_h = actualheight;
#if 0
if (window_width * actualheight <= window_height * SCREENWIDTH)
{
// We round up window_height if the ratio is not exact; this leaves
// the result stable.
window_height = (window_width * actualheight + SCREENWIDTH - 1) / SCREENWIDTH;
}
else
{
window_width = window_height * SCREENWIDTH / actualheight;
}
#endif
}
}
static void HandleWindowEvent(SDL_WindowEvent *event)
{
int i;
switch (event->event)
{
#if 0 // SDL2-TODO
case SDL_ACTIVEEVENT:
// need to update our focus state
UpdateFocus();
break;
#endif
case SDL_WINDOWEVENT_EXPOSED:
palette_to_set = true;
break;
case SDL_WINDOWEVENT_RESIZED:
need_resize = true;
last_resize_time = SDL_GetTicks();
break;
// Don't render the screen when the window is minimized:
case SDL_WINDOWEVENT_MINIMIZED:
screenvisible = false;
break;
case SDL_WINDOWEVENT_MAXIMIZED:
case SDL_WINDOWEVENT_RESTORED:
screenvisible = true;
break;
// 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.
case SDL_WINDOWEVENT_FOCUS_GAINED:
window_focused = true;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
window_focused = false;
break;
// We want to save the user's preferred monitor to use for running the
// game, so that next time we're run we start on the same display. So
// every time the window is moved, find which display we're now on and
// update the video_display config variable.
case SDL_WINDOWEVENT_MOVED:
i = SDL_GetWindowDisplayIndex(screen);
if (i >= 0)
{
video_display = i;
}
break;
default:
break;
}
}
static boolean ToggleFullScreenKeyShortcut(SDL_Keysym *sym)
{
Uint16 flags = (KMOD_LALT | KMOD_RALT);
#if defined(__MACOSX__)
flags |= (KMOD_LGUI | KMOD_RGUI);
#endif
return (sym->scancode == SDL_SCANCODE_RETURN ||
sym->scancode == SDL_SCANCODE_KP_ENTER) && (sym->mod & flags) != 0;
}
static void I_ToggleFullScreen(void)
{
unsigned int flags = 0;
// TODO: Consider implementing fullscreen toggle for SDL_WINDOW_FULLSCREEN
// (mode-changing) setup. This is hard because we have to shut down and
// restart again.
if (fullscreen_width != 0 || fullscreen_height != 0)
{
return;
}
fullscreen = !fullscreen;
if (fullscreen)
{
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
SDL_SetWindowFullscreen(screen, flags);
if (!fullscreen)
{
AdjustWindowSize();
SDL_SetWindowSize(screen, window_width, window_height);
}
}
void I_GetEvent(void)
{
SDL_Event sdlevent;
SDL_PumpEvents();
while (SDL_PollEvent(&sdlevent))
{
switch (sdlevent.type)
{
case SDL_KEYDOWN:
if (ToggleFullScreenKeyShortcut(&sdlevent.key.keysym))
{
I_ToggleFullScreen();
break;
}
// deliberate fall-though
case SDL_KEYUP:
I_HandleKeyboardEvent(&sdlevent);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEWHEEL:
if (usemouse && !nomouse && window_focused)
{
I_HandleMouseEvent(&sdlevent);
}
break;
case SDL_QUIT:
if (screensaver_mode)
{
I_Quit();
}
else
{
event_t event;
event.type = ev_quit;
D_PostEvent(&event);
}
break;
case SDL_WINDOWEVENT:
if (sdlevent.window.windowID == SDL_GetWindowID(screen))
{
HandleWindowEvent(&sdlevent.window);
}
break;
default:
break;
}
}
}
//
// I_StartTic
//
void I_StartTic (void)
{
if (!initialized)
{
return;
}
I_GetEvent();
if (usemouse && !nomouse && window_focused)
{
I_ReadMouse();
}
if (joywait < I_GetTime())
{
I_UpdateJoystick();
}
}
//
// I_UpdateNoBlit
//
void I_UpdateNoBlit (void)
{
// what is this?
}
static void UpdateGrab(void)
{
static boolean currently_grabbed = false;
boolean grab;
grab = MouseShouldBeGrabbed();
if (screensaver_mode)
{
// Hide the cursor in screensaver mode
SetShowCursor(false);
}
else if (grab && !currently_grabbed)
{
SetShowCursor(false);
}
else if (!grab && currently_grabbed)
{
int screen_w, screen_h;
SetShowCursor(true);
// When releasing the mouse from grab, warp the mouse cursor to
// the bottom-right of the screen. This is a minimally distracting
// place for it to appear - we may only have released the grab
// because we're at an end of level intermission screen, for
// example.
SDL_GetWindowSize(screen, &screen_w, &screen_h);
SDL_WarpMouseInWindow(screen, screen_w - 16, screen_h - 16);
SDL_GetRelativeMouseState(NULL, NULL);
}
currently_grabbed = grab;
}
static void LimitTextureSize(int *w_upscale, int *h_upscale)
{
SDL_RendererInfo rinfo;
int orig_w, orig_h;
orig_w = *w_upscale;
orig_h = *h_upscale;
// Query renderer and limit to maximum texture dimensions of hardware:
if (SDL_GetRendererInfo(renderer, &rinfo) != 0)
{
I_Error("CreateUpscaledTexture: SDL_GetRendererInfo() call failed: %s",
SDL_GetError());
}
while (*w_upscale * SCREENWIDTH > rinfo.max_texture_width)
{
--*w_upscale;
}
while (*h_upscale * SCREENHEIGHT > rinfo.max_texture_height)
{
--*h_upscale;
}
if ((*w_upscale < 1 && rinfo.max_texture_width > 0) ||
(*h_upscale < 1 && rinfo.max_texture_height > 0))
{
I_Error("CreateUpscaledTexture: Can't create a texture big enough for "
"the whole screen! Maximum texture size %dx%d",
rinfo.max_texture_width, rinfo.max_texture_height);
}
// We limit the amount of texture memory used for the intermediate buffer,
// since beyond a certain point there are diminishing returns. Also,
// depending on the hardware there may be performance problems with very
// huge textures, so the user can use this to reduce the maximum texture
// size if desired.
if (max_scaling_buffer_pixels < SCREENWIDTH * SCREENHEIGHT)
{
I_Error("CreateUpscaledTexture: max_scaling_buffer_pixels too small "
"to create a texture buffer: %d < %d",
max_scaling_buffer_pixels, SCREENWIDTH * SCREENHEIGHT);
}
while (*w_upscale * *h_upscale * SCREENWIDTH * SCREENHEIGHT
> max_scaling_buffer_pixels)
{
if (*w_upscale > *h_upscale)
{
--*w_upscale;
}
else
{
--*h_upscale;
}
}
if (*w_upscale != orig_w || *h_upscale != orig_h)
{
printf("CreateUpscaledTexture: Limited texture size to %dx%d "
"(max %d pixels, max texture size %dx%d)\n",
*w_upscale * SCREENWIDTH, *h_upscale * SCREENHEIGHT,
max_scaling_buffer_pixels,
rinfo.max_texture_width, rinfo.max_texture_height);
}
}
static void CreateUpscaledTexture(boolean force)
{
int w, h;
int h_upscale, w_upscale;
static int h_upscale_old, w_upscale_old;
SDL_Texture *new_texture, *old_texture;
// Get the size of the renderer output. The units this gives us will be
// real world pixels, which are not necessarily equivalent to the screen's
// window size (because of highdpi).
if (SDL_GetRendererOutputSize(renderer, &w, &h) != 0)
{
I_Error("Failed to get renderer output size: %s", SDL_GetError());
}
// When the screen or window dimensions do not match the aspect ratio
// of the texture, the rendered area is scaled down to fit. Calculate
// the actual dimensions of the rendered area.
if (w * actualheight < h * SCREENWIDTH)
{
// Tall window.
h = w * actualheight / SCREENWIDTH;
}
else
{
// Wide window.
w = h * SCREENWIDTH / actualheight;
}
// Pick texture size the next integer multiple of the screen dimensions.
// If one screen dimension matches an integer multiple of the original
// resolution, there is no need to overscale in this direction.
w_upscale = (w + SCREENWIDTH - 1) / SCREENWIDTH;
h_upscale = (h + SCREENHEIGHT - 1) / SCREENHEIGHT;
// Minimum texture dimensions of 320x200.
if (w_upscale < 1)
{
w_upscale = 1;
}
if (h_upscale < 1)
{
h_upscale = 1;
}
LimitTextureSize(&w_upscale, &h_upscale);
// Create a new texture only if the upscale factors have actually changed.
if (h_upscale == h_upscale_old && w_upscale == w_upscale_old && !force)
{
return;
}
h_upscale_old = h_upscale;
w_upscale_old = w_upscale;
// Set the scaling quality for rendering the upscaled texture to "linear",
// which looks much softer and smoother than "nearest" but does a better
// job at downscaling from the upscaled texture to screen.
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
new_texture = SDL_CreateTexture(renderer,
pixel_format,
SDL_TEXTUREACCESS_TARGET,
w_upscale*SCREENWIDTH,
h_upscale*SCREENHEIGHT);
old_texture = texture_upscaled;
texture_upscaled = new_texture;
if (old_texture != NULL)
{
SDL_DestroyTexture(old_texture);
}
}
// [AM] Fractional part of the current tic, in the half-open
// range of [0.0, 1.0). Used for interpolation.
fixed_t fractionaltic;
//
// I_FinishUpdate
//
void I_FinishUpdate (void)
{
static int lasttic;
int tics;
int i;
if (!initialized)
return;
if (noblit)
return;
if (need_resize)
{
if (SDL_GetTicks() > last_resize_time + RESIZE_DELAY)
{
int flags;
// When the window is resized (we're not in fullscreen mode),
// save the new window size.
flags = SDL_GetWindowFlags(screen);
if ((flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == 0)
{
SDL_GetWindowSize(screen, &window_width, &window_height);
// Adjust the window by resizing again so that the window
// is the right aspect ratio.
AdjustWindowSize();
SDL_SetWindowSize(screen, window_width, window_height);
}
CreateUpscaledTexture(false);
need_resize = false;
palette_to_set = true;
}
else
{
return;
}
}
UpdateGrab();
#if 0 // SDL2-TODO
// 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;
#endif
// draws little dots on the bottom of the screen
if (display_fps_dots)
{
i = I_GetTime();
tics = i - lasttic;
lasttic = i;
if (tics > 20) tics = 20;
for (i=0 ; i<tics*4 ; i+=4)
#ifndef CRISPY_TRUECOLOR
I_VideoBuffer[ (SCREENHEIGHT-1)*SCREENWIDTH + i] = 0xff;
#else
I_VideoBuffer[ (SCREENHEIGHT-1)*SCREENWIDTH + i] = colormaps[0xff];
#endif
for ( ; i<20*4 ; i+=4)
#ifndef CRISPY_TRUECOLOR
I_VideoBuffer[ (SCREENHEIGHT-1)*SCREENWIDTH + i] = 0x0;
#else
I_VideoBuffer[ (SCREENHEIGHT-1)*SCREENWIDTH + i] = colormaps[0x0];
#endif
}
// [crispy] [AM] Real FPS counter
{
static int lastmili;
static int fpscount;
int mili;
fpscount++;
i = SDL_GetTicks();
mili = i - lastmili;
// Update FPS counter every second
if (mili >= 1000)
{
crispy->fps = (fpscount * 1000) / mili;
fpscount = 0;
lastmili = i;
}
}
// Draw disk icon before blit, if necessary.
V_DrawDiskIcon();
#ifndef CRISPY_TRUECOLOR
if (palette_to_set)
{
SDL_SetPaletteColors(screenbuffer->format->palette, palette, 0, 256);
palette_to_set = false;
if (vga_porch_flash)
{
// "flash" the pillars/letterboxes with palette changes, emulating
// VGA "porch" behaviour (GitHub issue #832)
SDL_SetRenderDrawColor(renderer, palette[0].r, palette[0].g,
palette[0].b, SDL_ALPHA_OPAQUE);
}
}
// Blit from the paletted 8-bit screen buffer to the intermediate
// 32-bit RGBA buffer that we can load into the texture.
SDL_LowerBlit(screenbuffer, &blit_rect, argbbuffer, &blit_rect);
#endif
// Update the intermediate texture with the contents of the RGBA buffer.
SDL_UpdateTexture(texture, NULL, argbbuffer->pixels, argbbuffer->pitch);
// Make sure the pillarboxes are kept clear each frame.
SDL_RenderClear(renderer);
if (crispy->smoothscaling && !force_software_renderer)
{
// Render this intermediate texture into the upscaled texture
// using "nearest" integer scaling.
SDL_SetRenderTarget(renderer, texture_upscaled);
SDL_RenderCopy(renderer, texture, NULL, NULL);
// Finally, render this upscaled texture to screen using linear scaling.
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderCopy(renderer, texture_upscaled, NULL, NULL);
}
else
{
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderCopy(renderer, texture, NULL, NULL);
}
#ifdef CRISPY_TRUECOLOR
if (curpane)
{
SDL_SetTextureAlphaMod(curpane, pane_alpha);
SDL_RenderCopy(renderer, curpane, NULL, NULL);
}
#endif
// Draw!
SDL_RenderPresent(renderer);
if (crispy->uncapped && !singletics)
{
// Limit framerate
if (crispy->fpslimit >= TICRATE)
{
uint64_t target_time = 1000000ull / crispy->fpslimit;
static uint64_t start_time;
while (1)
{
uint64_t current_time = I_GetTimeUS();
uint64_t elapsed_time = current_time - start_time;
uint64_t remaining_time = 0;
if (elapsed_time >= target_time)
{
start_time = current_time;
break;
}
remaining_time = target_time - elapsed_time;
if (remaining_time > 1000)
{
I_Sleep((remaining_time - 1000) / 1000);
}
}
}
// [AM] Figure out how far into the current tic we're in as a fixed_t.
fractionaltic = I_GetFracRealTime();
}
// Restore background and undo the disk indicator, if it was drawn.
V_RestoreDiskBackground();
}
//
// I_ReadScreen
//
void I_ReadScreen (pixel_t* scr)
{
memcpy(scr, I_VideoBuffer, SCREENWIDTH*SCREENHEIGHT*sizeof(*scr));
}
//
// I_SetPalette
//
// [crispy] intermediate gamma levels
byte **gamma2table = NULL;
void I_SetGammaTable (void)
{
int i;
gamma2table = malloc(9 * sizeof(*gamma2table));
// [crispy] 5 original gamma levels
for (i = 0; i < 5; i++)
{
gamma2table[2*i] = (byte *)gammatable[i];
}
// [crispy] 4 intermediate gamma levels
for (i = 0; i < 4; i++)
{
int j;
gamma2table[2*i+1] = malloc(256 * sizeof(**gamma2table));
for (j = 0; j < 256; j++)
{
gamma2table[2*i+1][j] = (gamma2table[2*i][j] + gamma2table[2*i+2][j]) / 2;
}
}
}
#ifndef CRISPY_TRUECOLOR
void I_SetPalette (byte *doompalette)
{
int i;
// [crispy] intermediate gamma levels
if (!gamma2table)
{
I_SetGammaTable();
}
for (i=0; i<256; ++i)
{
// Zero out the bottom two bits of each channel - the PC VGA
// controller only supports 6 bits of accuracy.
// [crispy] intermediate gamma levels
palette[i].r = gamma2table[usegamma][*doompalette++] & ~3;
palette[i].g = gamma2table[usegamma][*doompalette++] & ~3;
palette[i].b = gamma2table[usegamma][*doompalette++] & ~3;
}
palette_to_set = true;
}
// Given an RGB value, find the closest matching palette index.