-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathi_video.pas
1268 lines (1121 loc) · 37 KB
/
i_video.pas
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
//------------------------------------------------------------------------------
//
// FPCDoom - Port of Doom to Free Pascal Compiler
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 2004-2007 by Jim Valavanis
// Copyright (C) 2017-2022 by Jim Valavanis
//
// 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.
//
//------------------------------------------------------------------------------
// E-Mail: [email protected]
// Site : https://sourceforge.net/projects/fpcdoom/
//------------------------------------------------------------------------------
{$I FPCDoom.inc}
unit i_video;
interface
uses
SysUtils,
Windows,
d_fpc;
//==============================================================================
//
// I_DetectNativeScreenResolution
//
//==============================================================================
procedure I_DetectNativeScreenResolution;
//==============================================================================
// I_InitGraphics
//
// Called by D_DoomMain,
// determines the hardware configuration
// and sets up the video mode
//
//==============================================================================
procedure I_InitGraphics;
//==============================================================================
//
// I_ChangeFullScreen
//
//==============================================================================
procedure I_ChangeFullScreen(const dofull, doexclusive: boolean);
//==============================================================================
//
// I_ShutDownGraphics
//
//==============================================================================
procedure I_ShutDownGraphics;
//==============================================================================
// I_SetPalette
//
// Takes full 8 bit values.
//
//==============================================================================
procedure I_SetPalette(const palette: PByteArray);
//==============================================================================
//
// I_FinishUpdate
//
//==============================================================================
procedure I_FinishUpdate;
//==============================================================================
//
// I_ReadScreen32
//
//==============================================================================
procedure I_ReadScreen32(dest: pointer);
//==============================================================================
//
// I_RestoreWindowPos
//
//==============================================================================
procedure I_RestoreWindowPos(const fs: Boolean);
var
fixstallhack: boolean = true;
type
displaymode_t = record
width, height: integer;
bpp: integer;
end;
displaymode_tArray = array[0..$FF] of displaymode_t;
Pdisplaymode_tArray = ^displaymode_tArray;
var
displaymodes: Pdisplaymode_tArray = nil;
numdisplaymodes: integer = 0;
//==============================================================================
//
// I_DisplayModeIndex
//
//==============================================================================
function I_DisplayModeIndex(const w, h: integer): integer;
//==============================================================================
//
// I_NearestDisplayModeIndex
//
//==============================================================================
function I_NearestDisplayModeIndex(const w, h: integer): integer;
var
vid_pillarbox_pct: integer;
vid_letterbox_pct: integer;
const
PILLARLETTER_MIN = 0;
PILLARLETTER_MAX = 50;
implementation
uses
doomdef,
DirectX,
i_system,
i_main,
i_input,
r_hires,
v_video;
var
g_pDD: IDirectDraw7 = nil; // DirectDraw object
g_pDDSPrimary: IDirectDrawSurface7 = nil; // DirectDraw primary surface
g_pDDScreen: IDirectDrawSurface7 = nil; // DirectDraw surface
g_pDDClipper: IDirectDrawClipper = nil; // Clipper
var
bpp: integer;
var
s_alttab_disabled: boolean = false;
var
screen16: PWordArray;
screen: PLongWordArray;
oscreen: pointer;
//==============================================================================
//
// I_GetWindowClientOffset
//
//==============================================================================
procedure I_GetWindowClientOffset(var dw, dh: integer);
var
rw, rc: TRect;
begin
GetClientRect(hMainWnd, rc);
GetWindowRect(hMainWnd, rw);
dw := (rw.Right - rw.Left) - (rc.Right - rc.Left);
dh := (rw.Bottom - rw.Top) - (rc.Bottom - rc.Top);
end;
//==============================================================================
//
// I_GetWindowOffset
//
//==============================================================================
procedure I_GetWindowOffset(var dw, dh: integer);
var
rw, rc: TRect;
border: integer;
begin
GetClientRect(hMainWnd, rc);
GetWindowRect(hMainWnd, rw);
border := ((rw.Right - rw.Left) - (rc.Right - rc.Left)) div 2;
dw := rw.Right - rc.Right - border;
dh := rw.Bottom - rc.Bottom - border;
end;
//==============================================================================
//
// I_GetWindowPosition
//
//==============================================================================
procedure I_GetWindowPosition(var dw, dh: integer);
var
rw: TRect;
begin
GetWindowRect(hMainWnd, rw);
dw := rw.Left;
dh := rw.Top;
end;
//==============================================================================
//
// I_RestoreWindowPos
//
//==============================================================================
procedure I_RestoreWindowPos(const fs: Boolean);
var
dw, dh: integer;
begin
if fs then
begin
SetWindowLong(hMainWnd, GWL_STYLE, WINDOW_STYLE_FS);
SetWindowPos(hMainWnd, HWND_NOTOPMOST, 0, 0, WINDOWWIDTH, WINDOWHEIGHT, SWP_SHOWWINDOW);
end
else
begin
SetWindowLong(hMainWnd, GWL_STYLE, WINDOW_STYLE_W);
SetWindowPos(hMainWnd, HWND_NOTOPMOST, windowxpos, windowypos, WINDOWWIDTH, WINDOWHEIGHT, SWP_SHOWWINDOW);
I_GetWindowClientOffset(dw, dh);
SetWindowPos(hMainWnd, HWND_NOTOPMOST, windowxpos, windowypos, WINDOWWIDTH + dw, WINDOWHEIGHT + dh, SWP_SHOWWINDOW);
end;
end;
//==============================================================================
//
// I_SetClipper
//
//==============================================================================
procedure I_SetClipper(const fs: boolean);
begin
if fs then
g_pDDSPrimary.SetClipper(nil)
else
g_pDDSPrimary.SetClipper(g_pDDClipper);
end;
//==============================================================================
//
// I_DisableAltTab
//
//==============================================================================
procedure I_DisableAltTab;
var
old: Boolean;
begin
if s_alttab_disabled then
Exit;
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
if isLibrary then
RegisterHotKey(0, $C000, MOD_ALT, VK_TAB)
else
RegisterHotKey(0, 0, MOD_ALT, VK_TAB)
end
else
SystemParametersInfo(SPI_SCREENSAVERRUNNING, 1, @old, 0);
s_alttab_disabled := true;
end;
//==============================================================================
//
// I_EnableAltTab
//
//==============================================================================
procedure I_EnableAltTab;
var
old: Boolean;
begin
if s_alttab_disabled then
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
if isLibrary then
UnregisterHotKey(0, $C000)
else
UnregisterHotKey(0, 0)
end
else
SystemParametersInfo(SPI_SCREENSAVERRUNNING, 0, @old, 0);
s_alttab_disabled := false;
end;
end;
var
allocscreensize: integer;
//==============================================================================
//
// I_ShutDownGraphics
//
//==============================================================================
procedure I_ShutDownGraphics;
begin
I_ClearInterface(IInterface(g_pDDScreen));
I_ClearInterface(IInterface(g_pDDSPrimary));
I_ClearInterface(IInterface(g_pDD));
I_ClearInterface(IInterface(g_pDDClipper));
I_EnableAltTab;
{$IFNDEF FPC}displaymodes := {$ENDIF}realloc(displaymodes, numdisplaymodes * SizeOf(displaymode_t), 0);
numdisplaymodes := 0;
memfree(oscreen, allocscreensize);
if screen16 <> nil then
memfree(screen16, SCREENWIDTH * SCREENHEIGHT * 2);
end;
var
stallhack: boolean;
type
finishupdateparms_t = record
start, stop: integer;
end;
Pfinishupdateparms_t = ^finishupdateparms_t;
//==============================================================================
// I_FinishUpdate8
//
// I_FinishUpdate
//
//==============================================================================
procedure I_FinishUpdate8(parms: Pfinishupdateparms_t);
var
destl: PLongWord;
destw: PWord;
pixel: LongWord;
r, g, b: LongWord;
src: PByte;
srcstop: PByte;
begin
src := @(screens[SCN_FG][parms.start]);
srcstop := @(screens[SCN_FG][parms.stop]);
if bpp = 32 then
begin
destl := @screen[parms.start];
while PCAST(src) < PCAST(srcstop) do
begin
destl^ := curpal[src^];
inc(destl);
inc(src);
end;
end
else if bpp = 16 then
begin
destw := @screen16[parms.start];
while PCAST(src) < PCAST(srcstop) do
begin
pixel := curpal[src^];
r := (pixel shr 19) and 31;
g := (pixel shr 11) and 31;
b := (pixel shr 3) and 31;
destw^ := (r shl 11) or (g shl 6) or b;
inc(destw);
inc(src);
end;
end;
end;
//==============================================================================
//
// I_FinishUpdate16
//
//==============================================================================
procedure I_FinishUpdate16;
var
i: integer;
destw: PWord;
pixel: LongWord;
r, g, b: LongWord;
srcl: PLongWord;
begin
destw := @screen16[0];
srcl := @screen[0];
for i := 0 to SCREENWIDTH * SCREENHEIGHT - 1 do
begin
pixel := srcl^;
r := (pixel shr 19) and 31;
g := (pixel shr 11) and 31;
b := (pixel shr 3) and 31;
destw^ := (r shl 11) or (g shl 6) or b;
inc(destw);
inc(srcl);
end;
end;
var
oldstretch: boolean = false;
old_pillarbox_pct: integer = -1;
old_letterbox_pct: integer = -1;
old_windowwidth: integer = -1;
old_windowheight: integer = -1;
old_fullscreen: boolean = false;
old_fullscreenexclusive: boolean = false;
//==============================================================================
//
// I_FinishUpdate
//
//==============================================================================
procedure I_FinishUpdate;
var
srcrect: TRect;
destrect: TRect;
blackrect: TRect;
oldcolor: LongWord;
parms1: finishupdateparms_t;
stretch: boolean;
hpan, vpan: integer;
dw, dh: integer;
begin
if (hMainWnd = 0) or (screens[SCN_FG] = nil) or (screen32 = nil) then
exit;
if videomode = vm32bit then
begin
if bpp = 16 then
begin
// JVAL
// Internal hi-color rendering engine works in 32 bits
// If we have a 16 bit depth desktop we get a bit slower performance ....
I_FinishUpdate16;
end;
// if bpp = 32 <- we don't do nothing, directly drawing was performed
end
else
begin
parms1.start := 0;
parms1.stop := SCREENWIDTH * SCREENHEIGHT - 1;
I_FinishUpdate8(@parms1);
end;
vid_pillarbox_pct := ibetween(vid_pillarbox_pct, PILLARLETTER_MIN, PILLARLETTER_MAX);
vid_letterbox_pct := ibetween(vid_letterbox_pct, PILLARLETTER_MIN, PILLARLETTER_MAX);
srcrect.Left := 0;
srcrect.Top := 0;
srcrect.Right := SCREENWIDTH;
srcrect.Bottom := SCREENHEIGHT;
stretch := stallhack and fixstallhack and (WINDOWHEIGHT = SCREENHEIGHT);
if not stretch then
stretch := (WINDOWWIDTH <> SCREENWIDTH) or (WINDOWHEIGHT <> SCREENHEIGHT) or
(vid_pillarbox_pct <> 0) or (vid_letterbox_pct <> 0) or
(fullscreen <> old_fullscreen) or (fullscreenexclusive <> old_fullscreenexclusive);
if stretch or not fullscreen then
begin
hpan := Trunc(vid_pillarbox_pct * WINDOWWIDTH / 100 / 2);
vpan := Trunc(vid_letterbox_pct * WINDOWHEIGHT / 100 / 2);
if not oldstretch or
(vid_pillarbox_pct <> old_pillarbox_pct) or
(vid_letterbox_pct <> old_letterbox_pct) or
(old_windowwidth <> WINDOWWIDTH) or
(old_windowheight <> WINDOWHEIGHT) or
(fullscreen <> old_fullscreen) or (fullscreenexclusive <> old_fullscreenexclusive) then
begin
if bpp = 16 then
begin
oldcolor := screen16[0];
screen16[0] := 0;
end
else
begin
oldcolor := screen32[0];
screen32[0] := 0;
end;
blackrect.Left := 0;
blackrect.Top := 0;
blackrect.Right := 1;
blackrect.Bottom := 1;
if hpan <> 0 then
begin
destrect.Left := 0;
destrect.Top := 0;
destrect.Right := hpan;
destrect.Bottom := WINDOWHEIGHT;
if g_pDDSPrimary.Blt(destrect, g_pDDScreen, blackrect, DDBLTFAST_DONOTWAIT or DDBLTFAST_NOCOLORKEY, PDDBltFX(0)^) = DDERR_SURFACELOST then
g_pDDSPrimary.Restore;
destrect.Left := WINDOWWIDTH - hpan;
destrect.Top := 0;
destrect.Right := WINDOWWIDTH;
destrect.Bottom := WINDOWHEIGHT;
if g_pDDSPrimary.Blt(destrect, g_pDDScreen, blackrect, DDBLTFAST_DONOTWAIT or DDBLTFAST_NOCOLORKEY, PDDBltFX(0)^) = DDERR_SURFACELOST then
g_pDDSPrimary.Restore;
end;
if vpan <> 0 then
begin
destrect.Left := hpan;
destrect.Top := 0;
destrect.Right := WINDOWWIDTH - hpan;
destrect.Bottom := vpan;
if g_pDDSPrimary.Blt(destrect, g_pDDScreen, blackrect, DDBLTFAST_DONOTWAIT or DDBLTFAST_NOCOLORKEY, PDDBltFX(0)^) = DDERR_SURFACELOST then
g_pDDSPrimary.Restore;
destrect.Left := hpan;
destrect.Top := WINDOWHEIGHT - vpan;
destrect.Right := WINDOWWIDTH - hpan;
destrect.Bottom := WINDOWHEIGHT;
if g_pDDSPrimary.Blt(destrect, g_pDDScreen, blackrect, DDBLTFAST_DONOTWAIT or DDBLTFAST_NOCOLORKEY, PDDBltFX(0)^) = DDERR_SURFACELOST then
g_pDDSPrimary.Restore;
end;
if bpp = 16 then
screen16[0] := oldcolor
else
screen32[0] := oldcolor;
oldstretch := true;
old_pillarbox_pct := vid_pillarbox_pct;
old_letterbox_pct := vid_letterbox_pct;
old_windowwidth := WINDOWWIDTH;
old_windowheight := WINDOWHEIGHT;
old_fullscreen := fullscreen;
old_fullscreenexclusive := fullscreenexclusive;
end;
destrect.Left := hpan;
destrect.Top := vpan;
destrect.Right := WINDOWWIDTH - hpan;
destrect.Bottom := WINDOWHEIGHT - vpan;
if not fullscreen then
begin
I_GetWindowPosition(windowxpos, windowypos);
I_GetWindowOffset(dw, dh);
Inc(destrect.Left, dw);
Inc(destrect.Right, dw);
Inc(destrect.Top, dh);
Inc(destrect.Bottom, dh);
end;
if g_pDDSPrimary.Blt(destrect, g_pDDScreen, srcrect, DDBLTFAST_DONOTWAIT or DDBLTFAST_NOCOLORKEY, PDDBltFX(0)^) = DDERR_SURFACELOST then
g_pDDSPrimary.Restore;
end
else
begin
if g_pDDSPrimary.BltFast(0, 0, g_pDDScreen, srcrect, DDBLTFAST_DONOTWAIT or DDBLTFAST_NOCOLORKEY) = DDERR_SURFACELOST then
g_pDDSPrimary.Restore;
oldstretch := false;
end;
end;
//==============================================================================
//
// Palette stuff.
//
// I_SetPalette
//
//==============================================================================
procedure I_SetPalette(const palette: PByteArray);
var
dest: PLongWord;
src: PByteArray;
curgamma: PByteArray;
begin
dest := @curpal[0];
src := palette;
curgamma := @gammatable[usegamma];
while PCAST(src) < PCAST(@palette[256 * 3]) do
begin
dest^ := (LongWord(curgamma[src[0]]) shl 16) or
(LongWord(curgamma[src[1]]) shl 8) or
(LongWord(curgamma[src[2]]));
inc(dest);
src := pOp(src, 3);
end;
end;
//==============================================================================
//
// I_AdjustWindowMode
//
//==============================================================================
function I_AdjustWindowMode: boolean;
begin
result := false;
if WINDOWWIDTH > NATIVEWIDTH then
begin
WINDOWWIDTH := NATIVEWIDTH;
result := true;
end;
if WINDOWHEIGHT > NATIVEHEIGHT then
begin
WINDOWHEIGHT := NATIVEHEIGHT;
result := true;
end;
end;
//==============================================================================
//
// I_MemoryStallHack
//
//==============================================================================
function I_MemoryStallHack: boolean;
// JVAL: Memory stall can dramatically reduce performance in inc operation of
// esi register of value 4096 etc
// e.g.
// mov [esp], 4096 (=SCREENWIDTH(=1024) * SizeOf(LongWord)(=4)
// add esi, [esp]
// The above code is dramatically slower than:
// mov [esp], 4088 (=SCREENWIDTH(=1022) * SizeOf(LongWord)(=4)
// add esi, [esp]
// It's crazy!
begin
if (SCREENWIDTH = 1024) or (SCREENWIDTH = 1152) or (SCREENWIDTH = 1280) then
begin
dec(SCREENWIDTH, 2);
stallhack := true;
end
else
stallhack := false;
result := stallhack;
end;
//==============================================================================
//
// SortDisplayModes
//
//==============================================================================
procedure SortDisplayModes;
function sortvalue(const idx: integer): double;
begin
result := displaymodes[idx].width + displaymodes[idx].height / 1000000
end;
procedure qsort(l, r: Integer);
var
i, j: Integer;
tmp: displaymode_t;
rover: double;
begin
repeat
i := l;
j := r;
rover := sortvalue((l + r) shr 1);
repeat
while sortvalue(i) < rover do
inc(i);
while sortvalue(j) > rover do
dec(j);
if i <= j then
begin
tmp := displaymodes[i];
displaymodes[i] := displaymodes[j];
displaymodes[j] := tmp;
inc(i);
dec(j);
end;
until i > j;
if l < j then
qsort(l, j);
l := i;
until i >= r;
end;
begin
if numdisplaymodes > 0 then
qsort(0, numdisplaymodes - 1);
end;
//==============================================================================
//
// I_DisplayModeIndex
//
//==============================================================================
function I_DisplayModeIndex(const w, h: integer): integer;
var
i: integer;
begin
result := -1;
if displaymodes = nil then
exit;
for i := 0 to numdisplaymodes - 1 do
if (displaymodes[i].width = w) and (displaymodes[i].height = h) then
begin
result := i;
exit;
end;
end;
//==============================================================================
//
// I_NearestDisplayModeIndex
//
//==============================================================================
function I_NearestDisplayModeIndex(const w, h: integer): integer;
var
i: integer;
dist: double;
mindist: double;
begin
result := I_DisplayModeIndex(w, h);
if result >= 0 then
exit;
mindist := 1000000000000.0;
for i := 0 to numdisplaymodes - 1 do
begin
dist := sqrt(sqr(displaymodes[i].width - SCREENWIDTH) + sqr(displaymodes[i].height - SCREENHEIGHT));
if SCREENWIDTH < displaymodes[i].width then
dist := dist + 50.0;
if SCREENHEIGHT < displaymodes[i].height then
dist := dist + 50.0;
if dist < mindist then
begin
mindist := dist;
result := i;
end;
end;
end;
//==============================================================================
//
// IsAvailableScreenResolution
//
//==============================================================================
function IsAvailableScreenResolution(const w, h: integer): boolean;
begin
result := I_DisplayModeIndex(w, h) >= 0;
end;
//==============================================================================
//
// I_EnumDisplayModes
//
//==============================================================================
procedure I_EnumDisplayModes;
var
dm: TDevMode;
i: integer;
begin
if displaymodes <> nil then
memfree(displaymodes, numdisplaymodes * SizeOf(displaymode_t));
numdisplaymodes := 0;
i := 0;
while EnumDisplaySettings(nil, i, dm) do
begin
if (dm.dmPelsWidth >= 320) and (dm.dmPelsHeight >= 200) and (dm.dmBitsPerPel = 32) and not IsAvailableScreenResolution(dm.dmPelsWidth, dm.dmPelsHeight) then
begin
{$IFNDEF FPC}displaymodes := {$ENDIF}realloc(displaymodes, numdisplaymodes * SizeOf(displaymode_t), (numdisplaymodes + 1) * SizeOf(displaymode_t));
displaymodes[numdisplaymodes].width := dm.dmPelsWidth;
displaymodes[numdisplaymodes].height := dm.dmPelsHeight;
displaymodes[numdisplaymodes].bpp := dm.dmBitsPerPel;
inc(numdisplaymodes);
end;
Inc(i);
end;
if numdisplaymodes = 0 then
begin
while EnumDisplaySettings(nil, i, dm) do
begin
if (dm.dmPelsWidth >= 640) and (dm.dmPelsHeight >= 400) and (dm.dmBitsPerPel >= 16) and not IsAvailableScreenResolution(dm.dmPelsWidth, dm.dmPelsHeight) then
begin
{$IFNDEF FPC}displaymodes := {$ENDIF}realloc(displaymodes, numdisplaymodes * SizeOf(displaymode_t), (numdisplaymodes + 1) * SizeOf(displaymode_t));
displaymodes[numdisplaymodes].width := dm.dmPelsWidth;
displaymodes[numdisplaymodes].height := dm.dmPelsHeight;
displaymodes[numdisplaymodes].bpp := dm.dmBitsPerPel;
inc(numdisplaymodes);
end;
Inc(i);
end;
end;
if numdisplaymodes = 0 then
begin
displaymodes := malloc(SizeOf(displaymode_t));
displaymodes[0].width := 320;
displaymodes[0].height := 200;
displaymodes[0].bpp := 32;
displaymodes[1].width := 640;
displaymodes[1].height := 400;
displaymodes[1].bpp := 32;
numdisplaymodes := 2;
end;
SortDisplayModes;
end;
//==============================================================================
//
// I_DoFindWindowSize
//
//==============================================================================
procedure I_DoFindWindowSize(const dofull, doexclusive: boolean);
var
i: integer;
dist: double;
mindist: double;
idx: integer;
begin
if dofull and not doexclusive then
begin
WINDOWWIDTH := NATIVEWIDTH;
WINDOWHEIGHT := NATIVEHEIGHT;
exit;
end;
if not dofull then
begin
WINDOWWIDTH := SCREENWIDTH;
WINDOWHEIGHT := SCREENHEIGHT;
exit;
end;
for i := 0 to numdisplaymodes - 1 do
if displaymodes[i].width = SCREENWIDTH then
if displaymodes[i].height = SCREENHEIGHT then
begin
WINDOWWIDTH := SCREENWIDTH;
WINDOWHEIGHT := SCREENHEIGHT;
exit;
end;
mindist := 1000000000000.0;
idx := -1;
for i := 0 to numdisplaymodes - 1 do
begin
dist := sqrt(sqr(displaymodes[i].width - SCREENWIDTH) + sqr(displaymodes[i].height - SCREENHEIGHT));
if SCREENWIDTH < displaymodes[i].width then
dist := dist + 50.0;
if SCREENHEIGHT < displaymodes[i].height then
dist := dist + 50.0;
if dist < mindist then
begin
mindist := dist;
idx := i;
end;
end;
if idx >= 0 then
begin
WINDOWWIDTH := displaymodes[idx].width;
WINDOWHEIGHT := displaymodes[idx].height;
exit;
end;
WINDOWWIDTH := NATIVEWIDTH;
WINDOWHEIGHT := NATIVEHEIGHT;
end;
//==============================================================================
//
// I_FindWindowSize
//
//==============================================================================
procedure I_FindWindowSize(const dofull, doexclusive: boolean);
begin
I_DoFindWindowSize(dofull, doexclusive);
printf('I_FindWindowSize: Set window size at (%d, %d)'#13#10, [WINDOWWIDTH, WINDOWHEIGHT]);
end;
//==============================================================================
//
// I_DetectNativeScreenResolution
//
//==============================================================================
procedure I_DetectNativeScreenResolution;
begin
NATIVEWIDTH := GetSystemMetrics(SM_CXSCREEN);
NATIVEHEIGHT := GetSystemMetrics(SM_CYSCREEN);
end;
var
isexclusive: boolean = false;
//==============================================================================
//
// I_SetCooperativeLevel
//
//==============================================================================
function I_SetCooperativeLevel(const exclusive: boolean): HResult;
begin
if exclusive then
result := g_pDD.SetCooperativeLevel(hMainWnd, DDSCL_ALLOWMODEX or DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN)
else
result := g_pDD.SetCooperativeLevel(hMainWnd, DDSCL_NORMAL);
isexclusive := exclusive;
end;
const
ERROR_OFFSET = 20;
//==============================================================================
// I_InitGraphics
//
// Called by D_DoomMain,
// determines the hardware configuration
// and sets up the video mode
//
//==============================================================================
procedure I_InitGraphics;
var
hres: HRESULT;
ddsd: DDSURFACEDESC2;
procedure I_ErrorInitGraphics(const procname: string);
begin
I_Error('I_InitGraphics(): %s failed, result = %d', [procname, hres]);
end;
begin
if g_pDD <> nil then
exit;
printf('I_InitGraphics: Initialize directdraw.' + #13#10);
I_EnumDisplayModes;
///////////////////////////////////////////////////////////////////////////
// Create the main DirectDraw object
///////////////////////////////////////////////////////////////////////////
hres := DirectDrawCreateEx(nil, g_pDD, IID_IDirectDraw7, nil);
if hres <> DD_OK then
I_ErrorInitGraphics('DirectDrawCreateEx');
///////////////////////////////////////////////////////////////////////////
// Create the clipper using the DirectDraw object
///////////////////////////////////////////////////////////////////////////
hres := g_pDD.CreateClipper(0, g_pDDClipper, nil);
if hres <> DD_OK then
I_ErrorInitGraphics('CreateClipper');
///////////////////////////////////////////////////////////////////////////
// Assign your window's HWND to the clipper
///////////////////////////////////////////////////////////////////////////
hres := g_pDDClipper.SetHWnd(0, hMainWnd);
if hres <> DD_OK then
I_ErrorInitGraphics('g_pDDClipper.SetHWnd');
if fullscreen then
begin
I_FindWindowSize(true, fullscreenexclusive);
// Get exclusive mode
hres := I_SetCooperativeLevel(fullscreenexclusive);
if hres <> DD_OK then
I_ErrorInitGraphics('SetCooperativeLevel');
if fullscreenexclusive then
begin
// Set the video mode to WINDOWWIDTH x WINDOWHEIGHT x 32
hres := g_pDD.SetDisplayMode(WINDOWWIDTH, WINDOWHEIGHT, 32, 0, 0);
if hres <> DD_OK then
begin
// Fullscreen mode failed, trying window mode
fullscreen := false;
I_AdjustWindowMode;
I_RestoreWindowPos(fullscreen);
I_Warning('SetDisplayMode(): Failed to fullscreen %dx%dx%d, trying window mode...'#13#10,
[WINDOWWIDTH, WINDOWHEIGHT, 32]);
printf('Window Mode %dx%d' + #13#10, [WINDOWWIDTH, WINDOWHEIGHT]);
hres := I_SetCooperativeLevel(false);
if hres <> DD_OK then
begin
I_Warning('SetDisplayMode(): Failed to window mode %dx%d...' + #13#10, [WINDOWWIDTH, WINDOWHEIGHT]);
WINDOWWIDTH := 640;
WINDOWHEIGHT := 480;
V_ReInit;
hres := g_pDD.SetDisplayMode(WINDOWWIDTH, WINDOWHEIGHT, 32, 0, 0);
if hres <> DD_OK then
I_ErrorInitGraphics('SetDisplayMode');
printf('SetDisplayMode(): %dx%d...'#13#10, [WINDOWWIDTH, WINDOWHEIGHT]);
end;
end
else
I_DisableAltTab;
end;
end
else
begin
I_FindWindowSize(false, false);
I_AdjustWindowMode;