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
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//===========================================================================//
#if defined( USE_SDL )
#undef PROTECTED_THINGS_ENABLE
#include "SDL.h"
#include "SDL_syswm.h"
#endif
#if defined( _WIN32 ) && !defined( _X360 )
#include "winlite.h"
#elif defined(POSIX)
typedef void *HDC;
#endif
#include "appframework/ilaunchermgr.h"
#include "basetypes.h"
#include "sysexternal.h"
#include "cmd.h"
#include "modelloader.h"
#include "gl_matsysiface.h"
#include "vmodes.h"
#include "modes.h"
#include "ivideomode.h"
#include "igame.h"
#include "iengine.h"
#include "engine_launcher_api.h"
#include "iregistry.h"
#include "common.h"
#include "tier0/icommandline.h"
#include "cl_main.h"
#include "filesystem_engine.h"
#include "host.h"
#include "gl_model_private.h"
#include "bitmap/tgawriter.h"
#include "vtf/vtf.h"
#include "materialsystem/materialsystem_config.h"
#include "materialsystem/itexture.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
#include "jpeglib/jpeglib.h"
#include "vgui/ISurface.h"
#include "vgui_controls/Controls.h"
#include "gl_shader.h"
#include "sys_dll.h"
#include "materialsystem/imaterial.h"
#include "IHammer.h"
#include "sourcevr/isourcevirtualreality.h"
#include "tier2/tier2.h"
#include "tier2/renderutils.h"
#include "tier0/etwprof.h"
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#else
#include "xbox/xboxstubs.h"
#endif
#include "video/ivideoservices.h"
#if !defined(NO_STEAM)
#include "cl_steamauth.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
void CL_GetBackgroundLevelName(char *pszBackgroundName, int bufSize, bool bMapName);
void ClientDLL_HudVidInit( void );
ConVar cl_savescreenshotstosteam( "cl_savescreenshotstosteam", "0", FCVAR_HIDDEN, "Saves screenshots to the Steam's screenshot library" );
ConVar cl_screenshotusertag( "cl_screenshotusertag", "", FCVAR_HIDDEN, "User to tag in the screenshot" );
ConVar cl_screenshotlocation( "cl_screenshotlocation", "", FCVAR_HIDDEN, "Location to tag the screenshot with" );
//-----------------------------------------------------------------------------
// HDRFIXME: move this somewhere else.
//-----------------------------------------------------------------------------
static void PFMWrite( float *pFloatImage, const char *pFilename, int width, int height )
{
FileHandle_t fp;
fp = g_pFileSystem->Open( pFilename, "wb" );
g_pFileSystem->FPrintf( fp, "PF\n%d %d\n-1.000000\n", width, height );
int i;
for( i = height-1; i >= 0; i-- )
{
float *pRow = &pFloatImage[3 * width * i];
g_pFileSystem->Write( pRow, width * sizeof( float ) * 3, fp );
}
g_pFileSystem->Close( fp );
}
//-----------------------------------------------------------------------------
// Purpose: Functionality shared by all video modes
//-----------------------------------------------------------------------------
class CVideoMode_Common : public IVideoMode
{
public:
CVideoMode_Common( void );
virtual ~CVideoMode_Common( void );
// Methods of IVideoMode
virtual bool Init( );
virtual void Shutdown( void );
virtual vmode_t *GetMode( int num );
virtual int GetModeCount( void );
virtual bool IsWindowedMode( void ) const;
virtual void UpdateWindowPosition( void );
virtual void RestoreVideo( void );
virtual void ReleaseVideo( void );
virtual void DrawNullBackground( void *hdc, int w, int h );
virtual void InvalidateWindow();
virtual void DrawStartupGraphic();
virtual bool CreateGameWindow( int nWidth, int nHeight, bool bWindowed );
virtual int GetModeWidth( void ) const;
virtual int GetModeHeight( void ) const;
virtual int GetModeStereoWidth() const;
virtual int GetModeStereoHeight() const;
virtual int GetModeUIWidth() const OVERRIDE;
virtual int GetModeUIHeight() const OVERRIDE;
virtual const vrect_t &GetClientViewRect( ) const;
virtual void SetClientViewRect( const vrect_t &viewRect );
virtual void MarkClientViewRectDirty();
virtual void TakeSnapshotTGA( const char *pFileName );
virtual void TakeSnapshotTGARect( const char *pFilename, int x, int y, int w, int h, int resampleWidth, int resampleHeight, bool bPFM, CubeMapFaceIndex_t faceIndex );
virtual void WriteMovieFrame( const MovieInfo_t& info );
virtual void TakeSnapshotJPEG( const char *pFileName, int quality );
virtual bool TakeSnapshotJPEGToBuffer( CUtlBuffer& buf, int quality );
protected:
bool GetInitialized( ) const;
void SetInitialized( bool init );
void AdjustWindow( int nWidth, int nHeight, int nBPP, bool bWindowed );
void ResetCurrentModeForNewResolution( int width, int height, bool bWindowed );
int GetModeBPP( ) const { return 32; }
void DrawStartupVideo();
void ComputeStartupGraphicName( char *pBuf, int nBufLen );
void WriteScreenshotToSteam( uint8 *pImage, int cubImage, int width, int height );
void AddScreenshotToSteam( const char *pchFilenameJpeg, int width, int height );
#if !defined(NO_STEAM)
void ApplySteamScreenshotTags( ScreenshotHandle hScreenshot );
#endif
// Finds the video mode in the list of video modes
int FindVideoMode( int nDesiredWidth, int nDesiredHeight, bool bWindowed );
// Purpose: Returns the optimal refresh rate for the specified mode
int GetRefreshRateForMode( const vmode_t *pMode );
// Inline accessors
vmode_t& DefaultVideoMode();
vmode_t& RequestedWindowVideoMode();
private:
// Purpose: Loads the startup graphic
void SetupStartupGraphic();
void CenterEngineWindow(void *hWndCenter, int width, int height);
void DrawStartupGraphic( HWND window );
void BlitGraphicToHDC(HDC hdc, byte *rgba, int imageWidth, int imageHeight, int x0, int y0, int x1, int y1);
void BlitGraphicToHDCWithAlpha(HDC hdc, byte *rgba, int imageWidth, int imageHeight, int x0, int y0, int x1, int y1);
IVTFTexture *LoadVTF( CUtlBuffer &temp, const char *szFileName );
void RecomputeClientViewRect();
// Overridden by derived classes
virtual void ReleaseFullScreen( void );
virtual void ChangeDisplaySettingsToFullscreen( int nWidth, int nHeight, int nBPP );
virtual void ReadScreenPixels( int x, int y, int w, int h, void *pBuffer, ImageFormat format );
// PFM screenshot methods
ITexture *GetBuildCubemaps16BitTexture( void );
ITexture *GetFullFrameFB0( void );
void BlitHiLoScreenBuffersTo16Bit( void );
void TakeSnapshotPFMRect( const char *pFilename, int x, int y, int w, int h, int resampleWidth, int resampleHeight, CubeMapFaceIndex_t faceIndex );
protected:
enum
{
#if !defined( _X360 )
MAX_MODE_LIST = 512
#else
MAX_MODE_LIST = 2
#endif
};
enum
{
VIDEO_MODE_DEFAULT = -1,
VIDEO_MODE_REQUESTED_WINDOW_SIZE = -2,
CUSTOM_VIDEO_MODES = 2
};
// Master mode list
int m_nNumModes;
vmode_t m_rgModeList[MAX_MODE_LIST];
vmode_t m_nCustomModeList[CUSTOM_VIDEO_MODES];
bool m_bInitialized;
bool m_bPlayedStartupVideo;
// Renderable surface information
int m_nModeWidth;
int m_nModeHeight;
int m_nStereoWidth;
int m_nStereoHeight;
int m_nUIWidth;
int m_nUIHeight;
int m_nVROverrideX;
int m_nVROverrideY;
#if defined( USE_SDL )
int m_nRenderWidth;
int m_nRenderHeight;
#endif
bool m_bWindowed;
bool m_bSetModeOnce;
bool m_bVROverride;
// Client view rectangle
vrect_t m_ClientViewRect;
bool m_bClientViewRectDirty;
// loading image
IVTFTexture *m_pBackgroundTexture;
IVTFTexture *m_pLoadingTexture;
};
//-----------------------------------------------------------------------------
// Inline accessors
//-----------------------------------------------------------------------------
inline vmode_t& CVideoMode_Common::DefaultVideoMode()
{
return m_nCustomModeList[ - VIDEO_MODE_DEFAULT - 1 ];
}
inline vmode_t& CVideoMode_Common::RequestedWindowVideoMode()
{
return m_nCustomModeList[ - VIDEO_MODE_REQUESTED_WINDOW_SIZE - 1 ];
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CVideoMode_Common::CVideoMode_Common( void )
{
m_nNumModes = 0;
m_bInitialized = false;
DefaultVideoMode().width = 640;
DefaultVideoMode().height = 480;
DefaultVideoMode().bpp = 32;
DefaultVideoMode().refreshRate = 0;
RequestedWindowVideoMode().width = -1;
RequestedWindowVideoMode().height = -1;
RequestedWindowVideoMode().bpp = 32;
RequestedWindowVideoMode().refreshRate = 0;
m_bClientViewRectDirty = false;
m_pBackgroundTexture = NULL;
m_pLoadingTexture = NULL;
m_bWindowed = false;
m_nModeWidth = IsPC() ? 1024 : 640;
m_nModeHeight = IsPC() ? 768 : 480;
m_bVROverride = false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CVideoMode_Common::~CVideoMode_Common( void )
{
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CVideoMode_Common::GetInitialized( void ) const
{
return m_bInitialized;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : init -
//-----------------------------------------------------------------------------
void CVideoMode_Common::SetInitialized( bool init )
{
m_bInitialized = init;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CVideoMode_Common::IsWindowedMode( void ) const
{
return m_bWindowed;
}
//-----------------------------------------------------------------------------
// Returns the video mode width + height.
//-----------------------------------------------------------------------------
int CVideoMode_Common::GetModeWidth( void ) const
{
return m_nModeWidth;
}
int CVideoMode_Common::GetModeHeight( void ) const
{
return m_nModeHeight;
}
//-----------------------------------------------------------------------------
// Returns the video mode width + height for one stereo view.
//-----------------------------------------------------------------------------
int CVideoMode_Common::GetModeStereoWidth( void ) const
{
return m_nStereoWidth;
}
int CVideoMode_Common::GetModeStereoHeight( void ) const
{
return m_nStereoHeight;
}
//-----------------------------------------------------------------------------
// Returns the video mode full screen UI width + height.
//-----------------------------------------------------------------------------
int CVideoMode_Common::GetModeUIWidth( void ) const
{
return m_nUIWidth;
}
int CVideoMode_Common::GetModeUIHeight( void ) const
{
return m_nUIHeight;
}
//-----------------------------------------------------------------------------
// Returns the enumerated video mode
//-----------------------------------------------------------------------------
vmode_t *CVideoMode_Common::GetMode( int num )
{
if ( num < 0 )
return &m_nCustomModeList[-num - 1];
if ( num >= m_nNumModes )
return &DefaultVideoMode();
return &m_rgModeList[num];
}
//-----------------------------------------------------------------------------
// Returns the number of fullscreen video modes
//-----------------------------------------------------------------------------
int CVideoMode_Common::GetModeCount( void )
{
return m_nNumModes;
}
//-----------------------------------------------------------------------------
// Purpose: Compares video modes so we can sort the list
// Input : *arg1 -
// *arg2 -
// Output : static int
//-----------------------------------------------------------------------------
static int __cdecl VideoModeCompare( const void *arg1, const void *arg2 )
{
vmode_t *m1, *m2;
m1 = (vmode_t *)arg1;
m2 = (vmode_t *)arg2;
if ( m1->width < m2->width )
{
return -1;
}
if ( m1->width == m2->width )
{
if ( m1->height < m2->height )
{
return -1;
}
if ( m1->height > m2->height )
{
return 1;
}
return 0;
}
return 1;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CVideoMode_Common::Init( )
{
return true;
}
//-----------------------------------------------------------------------------
// Finds the video mode in the list of video modes
//-----------------------------------------------------------------------------
int CVideoMode_Common::FindVideoMode( int nDesiredWidth, int nDesiredHeight, bool bWindowed )
{
#if defined( USE_SDL )
// If we want to scale the 3D portion of the game and leave the UI at the same res, then
// re-enable this code. Not that on retina displays the UI will be super small and that
// should probably be fixed.
#if 0
static ConVarRef mat_viewportscale( "mat_viewportscale" );
if ( !bWindowed )
{
m_nRenderWidth = nDesiredWidth;
m_nRenderHeight = nDesiredHeight;
uint nWidth, nHeight, nRefreshHz;
g_pLauncherMgr->GetNativeDisplayInfo( -1, nWidth, nHeight, nRefreshHz );
for ( int i = 0; i < m_nNumModes; i++)
{
if ( m_rgModeList[i].width != ( int )nWidth )
{
continue;
}
if ( m_rgModeList[i].height != ( int )nHeight )
{
continue;
}
if ( m_rgModeList[i].refreshRate != ( int )nRefreshHz )
{
continue;
}
mat_viewportscale.SetValue( ( float )nDesiredWidth / ( float )nWidth );
return i;
}
Assert( 0 ); // we should have found our native resolution, why not???
}
else
{
mat_viewportscale.SetValue( 1.0f );
}
#endif // 0
#endif // USE_SDL
// Check the default window size..
if ( ( nDesiredWidth == DefaultVideoMode().width) && (nDesiredHeight == DefaultVideoMode().height) )
return VIDEO_MODE_DEFAULT;
// Check the requested window size, but only if we're running windowed
if ( bWindowed )
{
if ( ( nDesiredWidth == RequestedWindowVideoMode().width) && (nDesiredHeight == RequestedWindowVideoMode().height) )
return VIDEO_MODE_REQUESTED_WINDOW_SIZE;
}
int i;
int iOK = VIDEO_MODE_DEFAULT;
for ( i = 0; i < m_nNumModes; i++)
{
// Match width first
if ( m_rgModeList[i].width != nDesiredWidth )
continue;
iOK = i;
if ( m_rgModeList[i].height != nDesiredHeight )
continue;
// Found a decent match
break;
}
// No match, use mode 0
if ( i >= m_nNumModes )
{
if ( iOK != VIDEO_MODE_DEFAULT )
{
i = iOK;
}
else
{
i = 0;
}
}
return i;
}
//-----------------------------------------------------------------------------
// Choose the actual video mode based on the available modes
//-----------------------------------------------------------------------------
void CVideoMode_Common::ResetCurrentModeForNewResolution( int nWidth, int nHeight, bool bWindowed )
{
// Fill in vid structure for the mode
int nGameMode = FindVideoMode( nWidth, nHeight, bWindowed );
vmode_t *pMode = GetMode( nGameMode );
// default to non-VR values
m_bWindowed = bWindowed;
m_nModeWidth = pMode->width;
m_nModeHeight = pMode->height;
m_nUIWidth = pMode->width;
m_nUIHeight = pMode->height;
m_nStereoWidth = pMode->width;
m_nStereoHeight = pMode->height;
// assume we won't be overriding the position
m_bVROverride = false;
if ( UseVR() || ShouldForceVRActive() )
{
g_pSourceVR->GetViewportBounds( ISourceVirtualReality::VREye_Left, NULL, NULL, &m_nStereoWidth, &m_nStereoHeight );
VRRect_t vrBounds;
if( g_pSourceVR->GetDisplayBounds( &vrBounds ) )
{
ConVarRef vr_force_windowed( "vr_force_windowed" );
RequestedWindowVideoMode().width = m_nModeWidth = vrBounds.nWidth;
RequestedWindowVideoMode().height = m_nModeHeight = vrBounds.nHeight;
m_bVROverride = true;
m_bWindowed = vr_force_windowed.GetBool();
// This is the smallest size the the UI in source games can handle.
m_nUIWidth = 640;
m_nUIHeight = 480;
#if defined( WIN32 ) && !defined( USE_SDL )
m_nVROverrideX = vrBounds.nX;
m_nVROverrideY = vrBounds.nY;
#elif defined( USE_SDL )
for ( int i = 0; i < SDL_GetNumVideoDisplays(); i++ )
{
SDL_Rect sdlRect;
SDL_GetDisplayBounds( i, &sdlRect );
if( sdlRect.x == vrBounds.nX && sdlRect.y == vrBounds.nY
&& sdlRect.w == vrBounds.nWidth && sdlRect.h == vrBounds.nHeight )
{
static ConVarRef sdl_displayindex( "sdl_displayindex" );
sdl_displayindex.SetValue( i );
break;
}
}
#endif
}
}
else if( materials->GetCurrentConfigForVideoCard().m_nVRModeAdapter == materials->GetCurrentAdapter() )
{
// if we aren't in VR mode but we do have a VR mode adapter set, we must not be full
// screen because that would show up on the HMD
m_bWindowed = true;
}
}
//-----------------------------------------------------------------------------
// Creates the game window, plays the startup movie
//-----------------------------------------------------------------------------
bool CVideoMode_Common::CreateGameWindow( int nWidth, int nHeight, bool bWindowed )
{
COM_TimestampedLog( "CVideoMode_Common::Init CreateGameWindow" );
if ( ShouldForceVRActive() )
{
// First make sure we're running a compatible version of DirectX
ConVarRef mat_dxlevel( "mat_dxlevel" );
if ( mat_dxlevel.IsValid() )
{
if ( mat_dxlevel.GetInt() < 90 )
{
Msg( "VR mode does not work with DirectX8.\nPlease use at least \"-dxlevel 90\" or higher.\n" );
return false;
}
}
VRRect_t bounds;
g_pSourceVR->GetDisplayBounds( &bounds );
nWidth = bounds.nWidth;
nHeight = bounds.nHeight;
m_nVROverrideX = bounds.nX;
m_nVROverrideY = bounds.nY;
}
// This allows you to have a window of any size.
// Requires you to set both width and height for the window and
// that you start in windowed mode
if ( bWindowed && nWidth && nHeight )
{
// FIXME: There's some ordering issues related to the config record
// and reading the command-line. Would be nice for just one place where this is done.
RequestedWindowVideoMode().width = nWidth;
RequestedWindowVideoMode().height = nHeight;
}
if ( !InEditMode() )
{
// Fill in vid structure for the mode.
// Note: ModeWidth/Height may *not* match requested nWidth/nHeight
ResetCurrentModeForNewResolution( nWidth, nHeight, bWindowed );
COM_TimestampedLog( "CreateGameWindow - Start" );
// When running in stand-alone mode, create your own window
if ( !game->CreateGameWindow() )
return false;
COM_TimestampedLog( "CreateGameWindow - Finish" );
// Re-size and re-center the window
AdjustWindow( GetModeWidth(), GetModeHeight(), GetModeBPP(), IsWindowedMode() );
COM_TimestampedLog( "SetMode - Start" );
// Set the mode and let the materialsystem take over
if ( !SetMode( GetModeWidth(), GetModeHeight(), IsWindowedMode() ) )
return false;
#if defined( USE_SDL ) && 0
static ConVarRef mat_viewportscale( "mat_viewportscale" );
if ( !bWindowed )
{
m_nRenderWidth = nWidth;
m_nRenderHeight = nHeight;
mat_viewportscale.SetValue( ( float )nWidth / ( float )GetModeWidth() );
}
#endif
COM_TimestampedLog( "SetMode - Finish" );
// Play our videos for the background after the render device has been initialized
DrawStartupVideo();
COM_TimestampedLog( "DrawStartupGraphic - Start" );
// Play our videos or display our temp image for the background
DrawStartupGraphic();
COM_TimestampedLog( "DrawStartupGraphic - Finish" );
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: loads a vtf, through the temporary buffer
//-----------------------------------------------------------------------------
IVTFTexture *CVideoMode_Common::LoadVTF( CUtlBuffer &temp, const char *szFileName )
{
if ( !g_pFileSystem->ReadFile( szFileName, NULL, temp ) )
return NULL;
IVTFTexture *texture = CreateVTFTexture();
if ( !texture->Unserialize( temp ) )
{
Error( "Invalid or corrupt background texture %s\n", szFileName );
return NULL;
}
texture->ConvertImageFormat( IMAGE_FORMAT_RGBA8888, false );
return texture;
}
//-----------------------------------------------------------------------------
// Computes the startup graphic name
//-----------------------------------------------------------------------------
void CVideoMode_Common::ComputeStartupGraphicName( char *pBuf, int nBufLen )
{
char szBackgroundName[_MAX_PATH];
CL_GetBackgroundLevelName( szBackgroundName, sizeof(szBackgroundName), false );
float aspectRatio = (float)GetModeStereoWidth() / GetModeStereoHeight();
if ( aspectRatio >= 1.6f )
{
// use the widescreen version
Q_snprintf( pBuf, nBufLen, "materials/console/%s_widescreen.vtf", szBackgroundName );
}
else
{
Q_snprintf( pBuf, nBufLen, "materials/console/%s.vtf", szBackgroundName );
}
if ( !g_pFileSystem->FileExists( pBuf, "GAME" ) )
{
Q_strncpy( pBuf, ( aspectRatio >= 1.6f ) ? "materials/console/background01_widescreen.vtf" : "materials/console/background01.vtf", nBufLen );
}
}
//-----------------------------------------------------------------------------
// Writes a screenshot to Steam screenshot library given an RGB buffer
// and applies any tags that have been set for it
//-----------------------------------------------------------------------------
void CVideoMode_Common::WriteScreenshotToSteam( uint8 *pImage, int cubImage, int width, int height )
{
#if !defined(NO_STEAM)
if ( cl_savescreenshotstosteam.GetBool() )
{
if ( Steam3Client().SteamScreenshots() )
{
ScreenshotHandle hScreenshot = Steam3Client().SteamScreenshots()->WriteScreenshot( pImage, cubImage, width, height );
ApplySteamScreenshotTags( hScreenshot );
}
}
cl_screenshotusertag.SetValue(0);
cl_screenshotlocation.SetValue("");
#endif
}
//-----------------------------------------------------------------------------
// Adds a screenshot to the Steam screenshot library from disk
// and applies any tags that have been set for it
//-----------------------------------------------------------------------------
void CVideoMode_Common::AddScreenshotToSteam( const char *pchFilename, int width, int height )
{
#if !defined(NO_STEAM)
if ( cl_savescreenshotstosteam.GetBool() )
{
if ( Steam3Client().SteamScreenshots() )
{
ScreenshotHandle hScreenshot = Steam3Client().SteamScreenshots()->AddScreenshotToLibrary( pchFilename, NULL, width, height );
ApplySteamScreenshotTags( hScreenshot );
}
}
cl_screenshotusertag.SetValue(0);
cl_screenshotlocation.SetValue("");
#endif
}
//-----------------------------------------------------------------------------
// Applies tags to a screenshot for the Steam screenshot library, which are
// passed in through convars
//-----------------------------------------------------------------------------
#if !defined(NO_STEAM)
void CVideoMode_Common::ApplySteamScreenshotTags( ScreenshotHandle hScreenshot )
{
if ( hScreenshot != INVALID_SCREENSHOT_HANDLE )
{
if ( cl_screenshotusertag.GetBool() )
{
if ( Steam3Client().SteamUtils() )
{
CSteamID steamID( cl_screenshotusertag.GetInt(), Steam3Client().SteamUtils()->GetConnectedUniverse(), k_EAccountTypeIndividual );
Steam3Client().SteamScreenshots()->TagUser( hScreenshot, steamID );
}
}
const char *pchLocation = cl_screenshotlocation.GetString();
if ( pchLocation && pchLocation[0] )
{
Steam3Client().SteamScreenshots()->SetLocation( hScreenshot, pchLocation );
}
}
}
#endif
void CVideoMode_Common::SetupStartupGraphic()
{
COM_TimestampedLog( "CVideoMode_Common::Init SetupStartupGraphic" );
char szBackgroundName[_MAX_PATH];
CL_GetBackgroundLevelName( szBackgroundName, sizeof(szBackgroundName), false );
// get the image to load
char material[_MAX_PATH];
CUtlBuffer buf;
float aspectRatio = (float)GetModeWidth() / GetModeHeight();
if ( aspectRatio >= 1.6f )
{
// use the widescreen version
Q_snprintf( material, sizeof(material),
"materials/console/%s_widescreen.vtf", szBackgroundName );
}
else
{
Q_snprintf( material, sizeof(material),
"materials/console/%s.vtf", szBackgroundName );
}
// load in the background vtf
buf.Clear();
m_pBackgroundTexture = LoadVTF( buf, material );
if ( !m_pBackgroundTexture )
{
// fallback to opening just the default background
m_pBackgroundTexture = LoadVTF( buf, ( aspectRatio >= 1.6f ) ? "materials/console/background01_widescreen.vtf" : "materials/console/background01.vtf" );
if ( !m_pBackgroundTexture )
{
Error( "Can't find background image '%s'\n", material );
return;
}
}
// loading.vtf
buf.Clear(); // added this Clear() because we saw cases where LoadVTF was not emptying the buf fully in the above section
m_pLoadingTexture = LoadVTF( buf, "materials/console/startup_loading.vtf" );
if ( !m_pLoadingTexture )
{
Error( "Can't find background image materials/console/startup_loading.vtf\n" );
return;
}
}
//-----------------------------------------------------------------------------
// Purpose: Renders the startup video into the HWND
//-----------------------------------------------------------------------------
void CVideoMode_Common::DrawStartupVideo()
{
if ( IsX360() )
return;
CETWScope timer( "CVideoMode_Common::DrawStartupGraphic" );
// render an avi, if we have one
if ( !m_bPlayedStartupVideo && !InEditMode() && !ShouldForceVRActive() )
{
game->PlayStartupVideos();
m_bPlayedStartupVideo = true;
}
}
//-----------------------------------------------------------------------------
// Purpose: Renders the startup graphic into the HWND
//-----------------------------------------------------------------------------
void CVideoMode_Common::DrawStartupGraphic()
{
if ( IsX360() )
return;
char debugstartup = CommandLine()->FindParm("-debugstartupscreen");
SetupStartupGraphic();
if ( !m_pBackgroundTexture || !m_pLoadingTexture )
return;
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
char pStartupGraphicName[MAX_PATH];
ComputeStartupGraphicName( pStartupGraphicName, sizeof(pStartupGraphicName) );
if(debugstartup)
{
// slam the startup graphic name for sanity - take your pick
strcpy( pStartupGraphicName, "materials/console/background01.vtf");
//strcpy( pStartupGraphicName, "materials/console/testramp.vtf");
}
// Allocate a white material
KeyValues *pVMTKeyValues = new KeyValues( "UnlitGeneric" );
pVMTKeyValues->SetString( "$basetexture", pStartupGraphicName + 10 );
pVMTKeyValues->SetInt( "$ignorez", 1 );
pVMTKeyValues->SetInt( "$nofog", 1 );
pVMTKeyValues->SetInt( "$no_fullbright", 1 );
pVMTKeyValues->SetInt( "$nocull", 1 );
IMaterial *pMaterial = g_pMaterialSystem->CreateMaterial( "__background", pVMTKeyValues );
pVMTKeyValues = new KeyValues( "UnlitGeneric" );
pVMTKeyValues->SetString( "$basetexture", "Console/startup_loading.vtf" );
pVMTKeyValues->SetInt( "$translucent", 1 );
pVMTKeyValues->SetInt( "$ignorez", 1 );
pVMTKeyValues->SetInt( "$nofog", 1 );
pVMTKeyValues->SetInt( "$no_fullbright", 1 );
pVMTKeyValues->SetInt( "$nocull", 1 );
IMaterial *pLoadingMaterial = g_pMaterialSystem->CreateMaterial( "__loading", pVMTKeyValues );
int w = GetModeStereoWidth();
int h = GetModeStereoHeight();
int tw = m_pBackgroundTexture->Width();
int th = m_pBackgroundTexture->Height();
int lw = m_pLoadingTexture->Width();
int lh = m_pLoadingTexture->Height();
if (debugstartup)
{
for ( int repeat = 0; repeat<100000; repeat++)
{
pRenderContext->Viewport( 0, 0, w, h );
pRenderContext->DepthRange( 0, 1 );
pRenderContext->ClearColor3ub( 0, (repeat & 0x7) << 3, 0 );
pRenderContext->ClearBuffers( true, true, true );
pRenderContext->SetToneMappingScaleLinear( Vector(1,1,1) );
if(1) // draw normal BK
{
float depth = 0.55f;
int slide = (repeat) % 200; // 100 down and 100 up
if (slide > 100)
{
slide = 200-slide; // aka 100-(slide-100).
}
// stop sliding about
slide = 0;
DrawScreenSpaceRectangle( pMaterial, 0, 0+slide, w, h-50, 0, 0, tw-1, th-1, tw, th, NULL,1,1,depth );
DrawScreenSpaceRectangle( pLoadingMaterial, w-lw, h-lh+slide/2, lw, lh, 0, 0, lw-1, lh-1, lw, lh, NULL,1,1,depth-0.1 );
}
if(0)
{
// draw a grid too
int grid_size = 8;
float depthacc = 0.0;
float depthinc = 1.0 / (float)((grid_size * grid_size)+1);
for( int x = 0; x<grid_size; x++)
{
float cornerx = ((float)x) * 20.0f;
for( int y=0; y<grid_size; y++)
{
float cornery = ((float)y) * 20.0f;
//if (! ((x^y) & 1) )
{
DrawScreenSpaceRectangle( pMaterial, 10.0f+cornerx,10.0f+ cornery, 15, 15, 0, 0, tw-1, th-1, tw, th, NULL,1,1, depthacc );
}
depthacc += depthinc;
}
}
}
g_pMaterialSystem->SwapBuffers();
}
}
else
{
pRenderContext->Viewport( 0, 0, w, h );
pRenderContext->DepthRange( 0, 1 );
pRenderContext->SetToneMappingScaleLinear( Vector(1,1,1) );
float depth = 0.5f;
// Make sure we clear both front & back buffer.
for (int i = 0; i < 2; ++i)
{
pRenderContext->ClearColor3ub( 0, 0, 0 );
pRenderContext->ClearBuffers( true, true, true );
DrawScreenSpaceRectangle( pMaterial, 0, 0, w, h, 0, 0, tw-1, th-1, tw, th, NULL,1,1,depth );
DrawScreenSpaceRectangle( pLoadingMaterial, w-lw, h-lh, lw, lh, 0, 0, lw-1, lh-1, lw, lh, NULL,1,1,depth );
g_pMaterialSystem->SwapBuffers();
}
}
#ifdef DX_TO_GL_ABSTRACTION
g_pMaterialSystem->DoStartupShaderPreloading();
#endif
pMaterial->Release();
pLoadingMaterial->Release();
// release graphics
DestroyVTFTexture( m_pBackgroundTexture );
m_pBackgroundTexture = NULL;
DestroyVTFTexture( m_pLoadingTexture );
m_pLoadingTexture = NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Blits an image to the loading window hdc
//-----------------------------------------------------------------------------
void CVideoMode_Common::BlitGraphicToHDCWithAlpha(HDC hdc, byte *rgba, int imageWidth, int imageHeight, int x0, int y0, int x1, int y1)
{
#ifdef WIN32
if ( IsX360() )
return;
int x = x0;
int y = y0;
int wide = x1 - x0;
int tall = y1 - y0;
Assert(imageWidth == wide && imageHeight == tall);
int texwby4 = imageWidth << 2;
for ( int v = 0; v < tall; v++ )
{
int *src = (int *)(rgba + (v * texwby4));
int xaccum = 0;
for ( int u = 0; u < wide; u++ )
{
byte *xsrc = (byte *)(src + xaccum);
if (xsrc[3])
{
::SetPixel(hdc, x + u, y + v, RGB(xsrc[0], xsrc[1], xsrc[2]));
}
xaccum += 1;
}
}
#else
Assert( !"Impl me" );
#endif
}
void CVideoMode_Common::InvalidateWindow()
{
if ( CommandLine()->FindParm( "-noshaderapi" ) )
{
#if defined( USE_SDL )
SDL_Event fake;
memset(&fake, '\0', sizeof (SDL_Event));
fake.type = SDL_WINDOWEVENT;
fake.window.windowID = SDL_GetWindowID( (SDL_Window *) g_pLauncherMgr->GetWindowRef() );
fake.window.event = SDL_WINDOWEVENT_EXPOSED;
SDL_PushEvent(&fake);
#else
InvalidateRect( (HWND)game->GetMainWindow(), NULL, FALSE );
#endif
}
}
void CVideoMode_Common::DrawNullBackground( void *hHDC, int w, int h )
{
#ifdef WIN32
if ( IsX360() )
return;
HDC hdc = (HDC)hHDC;
// Show a message if running without renderer..
if ( CommandLine()->FindParm( "-noshaderapi" ) )
{
HFONT fnt = CreateFontA( -18,
0,
0,
0,
FW_NORMAL,
FALSE,
FALSE,
FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
DEFAULT_PITCH,
"Arial" );
HFONT oldFont = (HFONT)SelectObject( hdc, fnt );
int oldBkMode = SetBkMode( hdc, TRANSPARENT );
COLORREF oldFgColor = SetTextColor( hdc, RGB( 255, 255, 255 ) );
HBRUSH br = CreateSolidBrush( RGB( 0, 0, 0 ) );
HBRUSH oldBr = (HBRUSH)SelectObject( hdc, br );
Rectangle( hdc, 0, 0, w, h );
RECT rc;
rc.left = 0;
rc.top = 0;
rc.right = w;
rc.bottom = h;
DrawText( hdc, "Running with -noshaderapi", -1, &rc, DT_NOPREFIX | DT_VCENTER | DT_CENTER | DT_SINGLELINE );
rc.top = rc.bottom - 30;
if ( host_state.worldmodel != NULL )
{
rc.left += 10;
DrawText( hdc, modelloader->GetName( host_state.worldmodel ), -1, &rc, DT_NOPREFIX | DT_VCENTER | DT_SINGLELINE );
}
SetTextColor( hdc, oldFgColor );
SelectObject( hdc, oldBr );
SetBkMode( hdc, oldBkMode );
SelectObject( hdc, oldFont );
DeleteObject( br );
DeleteObject( fnt );
}
#else
Assert( !"Impl me" );
#endif
}
#ifndef _WIN32
typedef unsigned char BYTE;
typedef signed long LONG;
typedef unsigned long ULONG;
typedef char * LPSTR;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
/* constants for the biCompression field */
#define BI_RGB 0L
#define BI_RLE8 1L
#define BI_RLE4 2L
#define BI_BITFIELDS 3L
#if 0
typedef struct _GUID
{
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID;
#endif
typedef GUID UUID;
#endif //WIN32
//-----------------------------------------------------------------------------
// Purpose: Blits an image to the loading window hdc
//-----------------------------------------------------------------------------
void CVideoMode_Common::BlitGraphicToHDC(HDC hdc, byte *rgba, int imageWidth, int imageHeight, int x0, int y0, int x1, int y1)
{
if ( IsX360() )
return;
#ifdef WIN32
int x = x0;
int y = y0;
int wide = x1 - x0;
int tall = y1 - y0;
// Needs to be a multiple of 4
int dibwide = ( wide + 3 ) & ~3;
Assert(rgba);
int texwby4 = imageWidth << 2;
double st = Plat_FloatTime();
void *destBits = NULL;
HBITMAP bm;
BITMAPINFO bmi;
Q_memset( &bmi, 0, sizeof( bmi ) );
BITMAPINFOHEADER *hdr = &bmi.bmiHeader;
hdr->biSize = sizeof( *hdr );
hdr->biWidth = dibwide;
hdr->biHeight = -tall; // top down bitmap
hdr->biBitCount = 24;
hdr->biPlanes = 1;
hdr->biCompression = BI_RGB;
hdr->biSizeImage = dibwide * tall * 3;
hdr->biXPelsPerMeter = 3780;
hdr->biYPelsPerMeter = 3780;
// Create a "source" DC
HDC tempDC = CreateCompatibleDC( hdc );
// Create the dibsection bitmap
bm = CreateDIBSection
(
tempDC, // handle to DC
&bmi, // bitmap data
DIB_RGB_COLORS, // data type indicator
&destBits, // bit values
NULL, // handle to file mapping object
0 // offset to bitmap bit values
);
// Select it into the source DC
HBITMAP oldBitmap = (HBITMAP)SelectObject( tempDC, bm );
// Setup for bilinaer filtering. If we don't do this filter here, there will be a big
// annoying pop when it switches to the vguimatsurface version of the background.
// We leave room for 14 bits of integer precision, so the image can be up to 16k x 16k.
const int BILINEAR_FIX_SHIFT = 17;
const int BILINEAR_FIX_MUL = (1 << BILINEAR_FIX_SHIFT);
#define FIXED_BLEND( a, b, out, frac ) \
out[0] = (a[0]*frac + b[0]*(BILINEAR_FIX_MUL-frac)) >> BILINEAR_FIX_SHIFT; \
out[1] = (a[1]*frac + b[1]*(BILINEAR_FIX_MUL-frac)) >> BILINEAR_FIX_SHIFT; \
out[2] = (a[2]*frac + b[2]*(BILINEAR_FIX_MUL-frac)) >> BILINEAR_FIX_SHIFT;
float eps = 0.001f;
float uMax = imageWidth - 1 - eps;
float vMax = imageHeight - 1 - eps;
int fixedBilinearV = 0;
int bilinearUInc = (int)( (uMax / (dibwide-1)) * BILINEAR_FIX_MUL );
int bilinearVInc = (int)( (vMax / (tall-1)) * BILINEAR_FIX_MUL );
for ( int v = 0; v < tall; v++ )
{
int iBilinearV = fixedBilinearV >> BILINEAR_FIX_SHIFT;
int fixedFractionV = fixedBilinearV & (BILINEAR_FIX_MUL-1);
fixedBilinearV += bilinearVInc;
int fixedBilinearU = 0;
byte *dest = (byte *)destBits + ( ( y + v ) * dibwide + x ) * 3;
for ( int u = 0; u < dibwide; u++, dest+=3 )
{
int iBilinearU = fixedBilinearU >> BILINEAR_FIX_SHIFT;
int fixedFractionU = fixedBilinearU & (BILINEAR_FIX_MUL-1);
fixedBilinearU += bilinearUInc;
Assert( iBilinearU >= 0 && iBilinearU+1 < imageWidth );
Assert( iBilinearV >= 0 && iBilinearV+1 < imageHeight );
byte *srcTopLine = rgba + iBilinearV * texwby4;
byte *srcBottomLine = rgba + (iBilinearV+1) * texwby4;
byte *xsrc[4] = {
srcTopLine + (iBilinearU+0)*4, srcTopLine + (iBilinearU+1)*4,
srcBottomLine + (iBilinearU+0)*4, srcBottomLine + (iBilinearU+1)*4 };
int topColor[3], bottomColor[3], finalColor[3];
FIXED_BLEND( xsrc[1], xsrc[0], topColor, fixedFractionU );
FIXED_BLEND( xsrc[3], xsrc[2], bottomColor, fixedFractionU );
FIXED_BLEND( bottomColor, topColor, finalColor, fixedFractionV );
// Windows wants the colors in reverse order.
dest[0] = finalColor[2];
dest[1] = finalColor[1];
dest[2] = finalColor[0];
}
}
// Now do the Blt
BitBlt( hdc, 0, 0, dibwide, tall, tempDC, 0, 0, SRCCOPY );
// This only draws if running -noshaderapi
DrawNullBackground( hdc, dibwide, tall );
// Restore the old Bitmap
SelectObject( tempDC, oldBitmap );
// Destroy the temporary DC
DeleteDC( tempDC );
// Destroy the DIBSection bitmap
DeleteObject( bm );
double elapsed = Plat_FloatTime() - st;
COM_TimestampedLog( "BlitGraphicToHDC: new ver took %.4f", elapsed );
#else
Assert( !"Impl me" );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: This is called in response to a WM_MOVE message
//-----------------------------------------------------------------------------
void CVideoMode_Common::UpdateWindowPosition( void )
{
int x, y, w, h;
// Get the window from the game ( right place for it? )
game->GetWindowRect( &x, &y, &w, &h );
#ifdef WIN32
RECT window_rect;
window_rect.left = x;
window_rect.right = x + w;
window_rect.top = y;
window_rect.bottom = y + h;
#endif
// NOTE: We need to feed this back into the video mode stuff
// esp. in Resizing window mode.
}
void CVideoMode_Common::ChangeDisplaySettingsToFullscreen( int nWidth, int nHeight, int nBPP )
{
}
void CVideoMode_Common::ReleaseFullScreen( void )
{
}
//-----------------------------------------------------------------------------
// Purpose: Returns the optimal refresh rate for the specified mode
//-----------------------------------------------------------------------------
int CVideoMode_Common::GetRefreshRateForMode( const vmode_t *pMode )
{
int nRefreshRate = pMode->refreshRate;
// FIXME: We should only read this once, at the beginning
// override the refresh rate from the command-line maybe
nRefreshRate = CommandLine()->ParmValue( "-freq", nRefreshRate );
nRefreshRate = CommandLine()->ParmValue( "-refresh", nRefreshRate );
nRefreshRate = CommandLine()->ParmValue( "-refreshrate", nRefreshRate );
return nRefreshRate;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *mode -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
void CVideoMode_Common::AdjustWindow( int nWidth, int nHeight, int nBPP, bool bWindowed )
{
if ( g_bTextMode )
return;
// Use Change Display Settings to go full screen
ChangeDisplaySettingsToFullscreen( nWidth, nHeight, nBPP );
RECT WindowRect;
WindowRect.top = 0;
WindowRect.left = 0;
WindowRect.right = nWidth;
WindowRect.bottom = nHeight;
#ifndef USE_SDL
#ifndef _X360
// Get window style
DWORD style = GetWindowLong( (HWND)game->GetMainWindow(), GWL_STYLE );
DWORD exStyle = GetWindowLong( (HWND)game->GetMainWindow(), GWL_EXSTYLE );
if ( bWindowed )
{
// Give it a frame (pretty much WS_OVERLAPPEDWINDOW except for we do not modify the
// flags corresponding to resizing-frame and maximize-box)
if( !CommandLine()->FindParm( "-noborder" ) && !m_bVROverride )
{
style |= WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
}
else
{
style &= ~WS_OVERLAPPEDWINDOW;
}
// remove topmost flag
exStyle &= ~WS_EX_TOPMOST;
SetWindowLong( (HWND)game->GetMainWindow(), GWL_EXSTYLE, exStyle );
}
else
{
// Remove window border going into fullscreen mode to avoid Vista sizing issues when DWM is enabled
style &= ~WS_OVERLAPPEDWINDOW;
}
SetWindowLong( (HWND)game->GetMainWindow(), GWL_STYLE, style );
// Compute rect needed for that size client area based on window style
AdjustWindowRectEx( &WindowRect, style, FALSE, exStyle );
#endif
// Prepare to set window pos, which is required when toggling between topmost and not window flags
HWND hWndAfter = NULL;
DWORD dwSwpFlags = 0;
#ifndef _X360
{
if ( bWindowed )
{
hWndAfter = HWND_NOTOPMOST;
}
else
{
hWndAfter = HWND_TOPMOST;
}
dwSwpFlags = SWP_FRAMECHANGED;
}
#else
{
dwSwpFlags = SWP_NOZORDER;
}
#endif
// Move the window to 0, 0 and the new true size
SetWindowPos( (HWND)game->GetMainWindow(),
hWndAfter,
0, 0, WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
SWP_NOREDRAW | dwSwpFlags );
#endif // !USE_SDL
// Now center
CenterEngineWindow( game->GetMainWindow(),
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top );
#if defined( USE_SDL )
g_pLauncherMgr->SetWindowFullScreen( !bWindowed, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top );
CenterEngineWindow( game->GetMainWindow(),
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top );
g_pLauncherMgr->SizeWindow( WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top );
if( bWindowed )
{
SDL_Window* win = (SDL_Window*)g_pLauncherMgr->GetWindowRef();
if ( m_bVROverride || CommandLine()->FindParm( "-noborder" ) )
SDL_SetWindowBordered( win, SDL_FALSE );
else
SDL_SetWindowBordered( win, SDL_TRUE );
}
#endif
game->SetWindowSize( nWidth, nHeight );
// Make sure we have updated window information
UpdateWindowPosition();
MarkClientViewRectDirty();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CVideoMode_Common::Shutdown( void )
{
ReleaseFullScreen();
game->DestroyGameWindow();
if ( !GetInitialized() )
return;
SetInitialized( false );
}
//-----------------------------------------------------------------------------
// Gets/sets the client view rectangle
//-----------------------------------------------------------------------------
const vrect_t &CVideoMode_Common::GetClientViewRect( ) const
{
const_cast<CVideoMode_Common*>(this)->RecomputeClientViewRect();
return m_ClientViewRect;
}
void CVideoMode_Common::SetClientViewRect( const vrect_t &viewRect )
{
m_ClientViewRect = viewRect;
}
//-----------------------------------------------------------------------------
// Marks the client view rect dirty
//-----------------------------------------------------------------------------
void CVideoMode_Common::MarkClientViewRectDirty()
{
m_bClientViewRectDirty = true;
}
void CVideoMode_Common::RecomputeClientViewRect()
{
if ( !InEditMode() )
{
if ( !m_bClientViewRectDirty )
return;
}
m_bClientViewRectDirty = false;
int nWidth, nHeight;
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->GetRenderTargetDimensions( nWidth, nHeight );
m_ClientViewRect.width = nWidth;
m_ClientViewRect.height = nHeight;
m_ClientViewRect.x = 0;
m_ClientViewRect.y = 0;
if (!nWidth || !nHeight)
{
// didn't successfully get the screen size, try again next frame
// window is probably minimized
m_bClientViewRectDirty = true;
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : hWndCenter -
// width -
// height -
// Output : static void
//-----------------------------------------------------------------------------
void CVideoMode_Common::CenterEngineWindow( void *hWndCenter, int width, int height)
{
int CenterX, CenterY;
#if defined(USE_SDL)
// Get the displayindex, and center our window on that display.
static ConVarRef sdl_displayindex( "sdl_displayindex" );
int displayindex = sdl_displayindex.IsValid() ? sdl_displayindex.GetInt() : 0;
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode( displayindex, &mode );
const int wide = mode.w;
const int tall = mode.h;
CenterX = (wide - width) / 2;
CenterY = (tall - height) / 2;
CenterX = (CenterX < 0) ? 0: CenterX;
CenterY = (CenterY < 0) ? 0: CenterY;
// tweak the x and w positions if the user species them on the command-line
CenterX = CommandLine()->ParmValue( "-x", CenterX );
CenterY = CommandLine()->ParmValue( "-y", CenterY );
// also check for the negated form (since it is hard to say "-x -1000")
int negx = CommandLine()->ParmValue( "-negx", 0 );
if (negx > 0)
{
CenterX = -negx;
}
int negy = CommandLine()->ParmValue( "-negy", 0 );
if (negy > 0)
{
CenterY = -negy;
}
SDL_Rect rect = { 0, 0, 0, 0 };
SDL_GetDisplayBounds( displayindex, &rect );
CenterX += rect.x;
CenterY += rect.y;
game->SetWindowXY( CenterX, CenterY );
g_pLauncherMgr->MoveWindow( CenterX, CenterY );
#else
if ( IsPC() )
{
// In windowed mode go through game->GetDesktopInfo because system metrics change
// when going fullscreen vs windowed.
// Use system metrics for fullscreen or when game didn't have a chance to initialize.
int cxScreen = 0, cyScreen = 0, refreshRate = 0;
if ( !( WS_EX_TOPMOST & ::GetWindowLong( (HWND)hWndCenter, GWL_EXSTYLE ) ) && m_bWindowed )
{
game->GetDesktopInfo( cxScreen, cyScreen, refreshRate );
}
if ( !cxScreen || !cyScreen )
{
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
}
// Compute top-left corner offset
CenterX = (cxScreen - width) / 2;
CenterY = (cyScreen - height) / 2;
CenterX = (CenterX < 0) ? 0: CenterX;
CenterY = (CenterY < 0) ? 0: CenterY;
}
else
{
CenterX = 0;
CenterY = 0;
}
if( m_bVROverride )
{
CenterX = m_nVROverrideX;
CenterY = m_nVROverrideY;
}
// tweak the x and w positions if the user species them on the command-line
CenterX = CommandLine()->ParmValue( "-x", CenterX );
CenterY = CommandLine()->ParmValue( "-y", CenterY );
game->SetWindowXY( CenterX, CenterY );
SetWindowPos ( (HWND)hWndCenter, NULL, CenterX, CenterY, 0, 0,
SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
#endif
}
//-----------------------------------------------------------------------------
// Handle alt-tab
//-----------------------------------------------------------------------------
void CVideoMode_Common::RestoreVideo( void )
{
}
void CVideoMode_Common::ReleaseVideo( void )
{
}
//-----------------------------------------------------------------------------
// Read screen pixels
//-----------------------------------------------------------------------------
void CVideoMode_Common::ReadScreenPixels( int x, int y, int w, int h, void *pBuffer, ImageFormat format )
{
int nBytes = ImageLoader::GetMemRequired( w, h, 1, format, false );
memset( pBuffer, 0, nBytes );
}
//-----------------------------------------------------------------------------
// Purpose: Write vid.buffer out as a .tga file
//-----------------------------------------------------------------------------
void CVideoMode_Common::TakeSnapshotTGA( const char *pFilename )
{
// bitmap bits
uint8 *pImage = new uint8[ GetModeStereoWidth() * 3 * GetModeStereoHeight() ];
// Get Bits from the material system
ReadScreenPixels( 0, 0, GetModeStereoWidth(), GetModeStereoHeight(), pImage, IMAGE_FORMAT_RGB888 );
CUtlBuffer outBuf;
if ( TGAWriter::WriteToBuffer( pImage, outBuf, GetModeStereoWidth(), GetModeStereoHeight(), IMAGE_FORMAT_RGB888,
IMAGE_FORMAT_RGB888 ) )
{
if ( !g_pFileSystem->WriteFile( pFilename, NULL, outBuf ) )
{
Warning( "Couldn't write bitmap data snapshot to file %s.\n", pFilename );
}
else
{
char szPath[MAX_PATH];
szPath[0] = 0;
if ( g_pFileSystem->GetLocalPath( pFilename, szPath, sizeof(szPath) ) )
{
AddScreenshotToSteam( szPath, GetModeStereoWidth(), GetModeStereoHeight() );
}
}
}
delete[] pImage;
}
//-----------------------------------------------------------------------------
// PFM screenshot helpers
//-----------------------------------------------------------------------------
ITexture *CVideoMode_Common::GetBuildCubemaps16BitTexture( void )
{
return materials->FindTexture( "_rt_BuildCubemaps16bit", TEXTURE_GROUP_RENDER_TARGET );
}
ITexture *CVideoMode_Common::GetFullFrameFB0( void )
{
return materials->FindTexture( "_rt_FullFrameFB", TEXTURE_GROUP_RENDER_TARGET );
}
void CVideoMode_Common::BlitHiLoScreenBuffersTo16Bit( void )
{
if ( IsX360() )
{
// FIXME: this breaks in 480p due to (at least) the multisampled depth buffer (need to cache, clear and restore the depth target)
Assert( 0 );
return;
}
IMaterial *pHDRCombineMaterial = materials->FindMaterial( "dev/hdrcombineto16bit", TEXTURE_GROUP_OTHER, true );
// if( IsErrorMaterial( pHDRCombineMaterial ) )
// {
// Assert( 0 );
// return;
// }
CMatRenderContextPtr pRenderContext( materials );
ITexture *pSaveRenderTarget;
pSaveRenderTarget = pRenderContext->GetRenderTarget();
int oldX, oldY, oldW, oldH;
pRenderContext->GetViewport( oldX, oldY, oldW, oldH );
pRenderContext->SetRenderTarget( GetBuildCubemaps16BitTexture() );
int width, height;
pRenderContext->GetRenderTargetDimensions( width, height );
pRenderContext->Viewport( 0, 0, width, height );
pRenderContext->DrawScreenSpaceQuad( pHDRCombineMaterial );
pRenderContext->SetRenderTarget( pSaveRenderTarget );
pRenderContext->Viewport( oldX, oldY, oldW, oldH );
}
void GetCubemapOffset( CubeMapFaceIndex_t faceIndex, int &x, int &y, int &faceDim )
{
int fbWidth, fbHeight;
materials->GetBackBufferDimensions( fbWidth, fbHeight );
if( fbWidth * 4 > fbHeight * 3 )
{
faceDim = fbHeight / 3;
}
else
{
faceDim = fbWidth / 4;
}
switch( faceIndex )
{
case CUBEMAP_FACE_RIGHT:
x = 2;
y = 1;
break;
case CUBEMAP_FACE_LEFT:
x = 0;
y = 1;
break;
case CUBEMAP_FACE_BACK:
x = 1;
y = 1;
break;
case CUBEMAP_FACE_FRONT:
x = 3;
y = 1;
break;
case CUBEMAP_FACE_UP:
x = 2;
y = 0;
break;
case CUBEMAP_FACE_DOWN:
x = 2;
y = 2;
break;
NO_DEFAULT
}
x *= faceDim;
y *= faceDim;
}
//-----------------------------------------------------------------------------
// Takes a snapshot to PFM
//-----------------------------------------------------------------------------
void CVideoMode_Common::TakeSnapshotPFMRect( const char *pFilename, int x, int y, int w, int h, int resampleWidth, int resampleHeight, CubeMapFaceIndex_t faceIndex )
{
if ( IsX360() )
{
// FIXME: this breaks in 480p due to (at least) the multisampled depth buffer (need to cache, clear and restore the depth target)
Assert( 0 );
return;
}
if ( g_pMaterialSystemHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
{
Warning( "Unable to take PFM screenshots if HDR isn't enabled!\n" );
return;
}
// hack
// resampleWidth = w;
// resampleHeight = h;
// bitmap bits
float16 *pImage = ( float16 * )malloc( w * h * ImageLoader::SizeInBytes( IMAGE_FORMAT_RGBA16161616F ) );
float *pImage1 = ( float * )malloc( w * h * ImageLoader::SizeInBytes( IMAGE_FORMAT_RGB323232F ) );
CMatRenderContextPtr pRenderContext( materials );
// Save the current render target.
ITexture *pSaveRenderTarget = pRenderContext->GetRenderTarget();
// Set this as the render target so that we can read it.
pRenderContext->SetRenderTarget( GetFullFrameFB0() );
// Get Bits from the material system
ReadScreenPixels( x, y, w, h, pImage, IMAGE_FORMAT_RGBA16161616F );
// Draw what we just grabbed to the screen
pRenderContext->SetRenderTarget( NULL);
int scrw, scrh;
pRenderContext->GetRenderTargetDimensions( scrw, scrh );
pRenderContext->Viewport( 0, 0, scrw,scrh );
int offsetX, offsetY, faceDim;
GetCubemapOffset( faceIndex, offsetX, offsetY, faceDim );
pRenderContext->DrawScreenSpaceRectangle( materials->FindMaterial( "dev/copyfullframefb", "" ),
offsetX, offsetY, faceDim, faceDim, 0, 0, w-1, h-1, scrw, scrh );
// Restore the render target.
pRenderContext->SetRenderTarget( pSaveRenderTarget );
// convert from float16 to float32
ImageLoader::ConvertImageFormat( ( unsigned char * )pImage, IMAGE_FORMAT_RGBA16161616F,
( unsigned char * )pImage1, IMAGE_FORMAT_RGB323232F,
w, h );
Assert( w == h ); // garymcthack - this only works for square images
float *pFloatImage = ( float * )malloc( resampleWidth * resampleHeight * ImageLoader::SizeInBytes( IMAGE_FORMAT_RGB323232F ) );
ImageLoader::ResampleInfo_t info;
info.m_pSrc = ( unsigned char * )pImage1;
info.m_pDest = ( unsigned char * )pFloatImage;
info.m_nSrcWidth = w;
info.m_nSrcHeight = h;
info.m_nDestWidth = resampleWidth;
info.m_nDestHeight = resampleHeight;
info.m_flSrcGamma = 1.0f;
info.m_flDestGamma = 1.0f;
if( !ImageLoader::ResampleRGB323232F( info ) )
{
Sys_Error( "Can't resample\n" );
}
PFMWrite( pFloatImage, pFilename, resampleWidth, resampleHeight );
free( pImage1 );
free( pImage );
free( pFloatImage );
}
//-----------------------------------------------------------------------------
// Takes a snapshot
//-----------------------------------------------------------------------------
void CVideoMode_Common::TakeSnapshotTGARect( const char *pFilename, int x, int y, int w, int h, int resampleWidth, int resampleHeight, bool bPFM, CubeMapFaceIndex_t faceIndex )
{
if ( IsX360() )
{
Assert( 0 );
return;
}
if ( bPFM )
{
TakeSnapshotPFMRect( pFilename, x, y, w, h, resampleWidth, resampleHeight, faceIndex );
return;
}
// bitmap bits
uint8 *pImage = new uint8[ w * h * 4 ];
uint8 *pImage1 = new uint8[ resampleWidth * resampleHeight * 4 ];
// Get Bits from the material system
ReadScreenPixels( x, y, w, h, pImage, IMAGE_FORMAT_RGBA8888 );
Assert( w == h ); // garymcthack - this only works for square images
ImageLoader::ResampleInfo_t info;
info.m_pSrc = pImage;
info.m_pDest = pImage1;
info.m_nSrcWidth = w;
info.m_nSrcHeight = h;
info.m_nDestWidth = resampleWidth;
info.m_nDestHeight = resampleHeight;
info.m_flSrcGamma = 1.0f;
info.m_flDestGamma = 1.0f;
if( !ImageLoader::ResampleRGBA8888( info ) )
{
Sys_Error( "Can't resample\n" );
}
CUtlBuffer outBuf;
if ( TGAWriter::WriteToBuffer( pImage1, outBuf, resampleWidth, resampleHeight, IMAGE_FORMAT_RGBA8888, IMAGE_FORMAT_RGBA8888 ) )
{
if ( !g_pFileSystem->WriteFile( pFilename, NULL, outBuf ) )
{
Error( "Couldn't write bitmap data snapshot to file %s.\n", pFilename );
}
else
{
DevMsg( "Screenshot: %dx%d saved to '%s'.\n", w, h, pFilename );
}
}
delete[] pImage1;
delete[] pImage;
materials->SwapBuffers();
}
//-----------------------------------------------------------------------------
// Purpose: Writes the data in *data to the sequentially number .bmp file filename
// Input : *filename -
// width -
// height -
// depth -
// *data -
// Output : static void
//-----------------------------------------------------------------------------
static void VID_ProcessMovieFrame( const MovieInfo_t& info, bool jpeg, const char *filename, int width, int height, byte *data )
{
CUtlBuffer outBuf;
bool bSuccess = false;
if ( jpeg )
{
bSuccess = videomode->TakeSnapshotJPEGToBuffer( outBuf, info.jpeg_quality );
}
else
{
bSuccess = TGAWriter::WriteToBuffer( data, outBuf, width, height, IMAGE_FORMAT_BGR888, IMAGE_FORMAT_RGB888 );
}
if ( bSuccess )
{
if ( !g_pFileSystem->WriteFile( filename, NULL, outBuf ) )
{
Warning( "Couldn't write movie snapshot to file %s.\n", filename );
Cbuf_AddText( "endmovie\n" );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Store current frame to numbered .bmp file
// Input : *pFilename -
//-----------------------------------------------------------------------------
extern IVideoRecorder *g_pVideoRecorder;
void CVideoMode_Common::WriteMovieFrame( const MovieInfo_t& info )
{
char const *pMovieName = info.moviename;
int nMovieFrame = info.movieframe;
if ( g_LostVideoMemory )
return;
if ( !pMovieName[0] )
{
Cbuf_AddText( "endmovie\n" );
ConMsg( "Tried to write movie buffer with no filename set!\n" );
return;
}
int imagesize = GetModeStereoWidth() * GetModeStereoHeight();
BGR888_t *hp = new BGR888_t[ imagesize ];
if ( hp == NULL )
{
Sys_Error( "Couldn't allocate bitmap header to snapshot.\n" );
}
// Get Bits from material system
ReadScreenPixels( 0, 0, GetModeStereoWidth(), GetModeStereoHeight(), hp, IMAGE_FORMAT_BGR888 );
// Store frame to disk
if ( info.DoTga() )
{
VID_ProcessMovieFrame( info, false, va( "%s%04d.tga", pMovieName, nMovieFrame ),
GetModeStereoWidth(), GetModeStereoHeight(), (unsigned char*)hp );
}
if ( info.DoJpg() )
{
VID_ProcessMovieFrame( info, true, va( "%s%04d.jpg", pMovieName, nMovieFrame ),
GetModeStereoWidth(), GetModeStereoHeight(), (unsigned char*)hp );
}
if ( info.DoVideo() )
{
g_pVideoRecorder->AppendVideoFrame( hp );
}
delete[] hp;
}
//-----------------------------------------------------------------------------
// Purpose: Expanded data destination object for CUtlBuffer output
//-----------------------------------------------------------------------------
struct JPEGDestinationManager_t
{
struct jpeg_destination_mgr pub; // public fields
CUtlBuffer *pBuffer; // target/final buffer
byte *buffer; // start of temp buffer
};
// choose an efficiently bufferaable size
#define OUTPUT_BUF_SIZE 4096
//-----------------------------------------------------------------------------
// Purpose: Initialize destination --- called by jpeg_start_compress
// before any data is actually written.
//-----------------------------------------------------------------------------
METHODDEF(void) init_destination (j_compress_ptr cinfo)
{
JPEGDestinationManager_t *dest = ( JPEGDestinationManager_t *) cinfo->dest;
// Allocate the output buffer --- it will be released when done with image
dest->buffer = (byte *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
OUTPUT_BUF_SIZE * sizeof(byte));
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
//-----------------------------------------------------------------------------
// Purpose: Empty the output buffer --- called whenever buffer fills up.
// Input : boolean -
//-----------------------------------------------------------------------------
METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
{
JPEGDestinationManager_t *dest = ( JPEGDestinationManager_t * ) cinfo->dest;
CUtlBuffer *buf = dest->pBuffer;
// Add some data
buf->Put( dest->buffer, OUTPUT_BUF_SIZE );
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
//-----------------------------------------------------------------------------
// Purpose: Terminate destination --- called by jpeg_finish_compress
// after all data has been written. Usually needs to flush buffer.
//
// NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
// application must deal with any cleanup that should happen even
// for error exit.
//-----------------------------------------------------------------------------
METHODDEF(void) term_destination (j_compress_ptr cinfo)
{
JPEGDestinationManager_t *dest = (JPEGDestinationManager_t *) cinfo->dest;
size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
CUtlBuffer *buf = dest->pBuffer;
/* Write any data remaining in the buffer */
if (datacount > 0)
{
buf->Put( dest->buffer, datacount );
}
}
//-----------------------------------------------------------------------------
// Purpose: Set up functions for writing data to a CUtlBuffer instead of FILE *
//-----------------------------------------------------------------------------
GLOBAL(void) jpeg_UtlBuffer_dest (j_compress_ptr cinfo, CUtlBuffer *pBuffer )
{
JPEGDestinationManager_t *dest;
/* The destination object is made permanent so that multiple JPEG images
* can be written to the same file without re-executing jpeg_stdio_dest.
* This makes it dangerous to use this manager and a different destination
* manager serially with the same JPEG object, because their private object
* sizes may be different. Caveat programmer.
*/
if (cinfo->dest == NULL) { /* first time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(JPEGDestinationManager_t));
}
dest = ( JPEGDestinationManager_t * ) cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
dest->pBuffer = pBuffer;
}
bool CVideoMode_Common::TakeSnapshotJPEGToBuffer( CUtlBuffer& buf, int quality )
{
#if !defined( _X360 )
if ( g_LostVideoMemory )
return false;
// Validate quality level
quality = clamp( quality, 1, 100 );
// Allocate space for bits
uint8 *pImage = new uint8[ GetModeStereoWidth() * 3 * GetModeStereoHeight() ];
if ( !pImage )
{
Msg( "Unable to allocate %i bytes for image\n", GetModeStereoWidth() * 3 * GetModeStereoHeight() );
return false;
}
// Get Bits from the material system
ReadScreenPixels( 0, 0, GetModeStereoWidth(), GetModeStereoHeight(), pImage, IMAGE_FORMAT_RGB888 );
JSAMPROW row_pointer[1]; // pointer to JSAMPLE row[s]
int row_stride; // physical row width in image buffer
// stderr handler
struct jpeg_error_mgr jerr;
// compression data structure
struct jpeg_compress_struct cinfo;
row_stride = GetModeStereoWidth() * 3; // JSAMPLEs per row in image_buffer
// point at stderr
cinfo.err = jpeg_std_error(&jerr);
// create compressor
jpeg_create_compress(&cinfo);
// Hook CUtlBuffer to compression
jpeg_UtlBuffer_dest(&cinfo, &buf );
// image width and height, in pixels
cinfo.image_width = GetModeStereoWidth();
cinfo.image_height = GetModeStereoHeight();
// RGB is 3 componnent
cinfo.input_components = 3;
// # of color components per pixel
cinfo.in_color_space = JCS_RGB;
// Apply settings
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE );
// Start compressor
jpeg_start_compress(&cinfo, TRUE);
// Write scanlines
while ( cinfo.next_scanline < cinfo.image_height )
{
row_pointer[ 0 ] = &pImage[ cinfo.next_scanline * row_stride ];
jpeg_write_scanlines( &cinfo, row_pointer, 1 );
}
// Finalize image
jpeg_finish_compress(&cinfo);
// Cleanup
jpeg_destroy_compress(&cinfo);
delete[] pImage;
#else
// not supporting
Assert( 0 );
#endif
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Write vid.buffer out as a .jpg file
// Input : *pFilename -
//-----------------------------------------------------------------------------
void CVideoMode_Common::TakeSnapshotJPEG( const char *pFilename, int quality )
{
#if !defined( _X360 )
Assert( pFilename );
// Output buffer
CUtlBuffer buf( 0, 0 );
TakeSnapshotJPEGToBuffer( buf, quality );
int finalSize = 0;
FileHandle_t fh = g_pFileSystem->Open( pFilename, "wb" );
if ( FILESYSTEM_INVALID_HANDLE != fh )
{
g_pFileSystem->Write( buf.Base(), buf.TellPut(), fh );
finalSize = g_pFileSystem->Tell( fh );
g_pFileSystem->Close( fh );
}
// Show info to console.
char orig[ 64 ];
char final[ 64 ];
Q_strncpy( orig, Q_pretifymem( GetModeStereoWidth() * 3 * GetModeStereoHeight(), 2 ), sizeof( orig ) );
Q_strncpy( final, Q_pretifymem( finalSize, 2 ), sizeof( final ) );
Msg( "Wrote '%s': %s (%dx%d) compresssed (quality %i) to %s\n",
pFilename, orig, GetModeStereoWidth(), GetModeStereoHeight(), quality, final );
if ( finalSize > 0 )
{
char szPath[MAX_PATH];
szPath[0] = 0;
if ( g_pFileSystem->GetLocalPath( pFilename, szPath, sizeof(szPath) ) )
{
AddScreenshotToSteam( szPath, GetModeStereoWidth(), GetModeStereoHeight() );
}
}
#else
Assert( 0 );
#endif
}
//-----------------------------------------------------------------------------
// The version of the VideoMode class for the material system
//-----------------------------------------------------------------------------
class CVideoMode_MaterialSystem: public CVideoMode_Common
{
public:
typedef CVideoMode_Common BaseClass;
CVideoMode_MaterialSystem( );
virtual bool Init( );
virtual void Shutdown( void );
virtual void SetGameWindow( void *hWnd );
virtual bool SetMode( int nWidth, int nHeight, bool bWindowed );
virtual void ReleaseVideo( void );
virtual void RestoreVideo( void );
virtual void AdjustForModeChange( void );
virtual void ReadScreenPixels( int x, int y, int w, int h, void *pBuffer, ImageFormat format );
private:
virtual void ReleaseFullScreen( void );
virtual void ChangeDisplaySettingsToFullscreen( int nWidth, int nHeight, int nBPP );
#ifdef WIN32
int m_nLastCDSWidth;
int m_nLastCDSHeight;
int m_nLastCDSBPP;
int m_nLastCDSFreq;
#endif
};
static void VideoMode_AdjustForModeChange( void )
{
( ( CVideoMode_MaterialSystem * )videomode )->AdjustForModeChange();
}
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
CVideoMode_MaterialSystem::CVideoMode_MaterialSystem( )
{
#ifdef WIN32
m_nLastCDSWidth = 0;
m_nLastCDSHeight = 0;
m_nLastCDSBPP = 0;
m_nLastCDSFreq = 0;
#endif
}
//-----------------------------------------------------------------------------
// Initialization
//-----------------------------------------------------------------------------
bool CVideoMode_MaterialSystem::Init( )
{
m_bSetModeOnce = false;
m_bPlayedStartupVideo = false;
// we only support 32-bit rendering.
int bitsperpixel = 32;
bool bAllowSmallModes = false;
if ( CommandLine()->FindParm( "-small" ) )
{
bAllowSmallModes = true;
}
int nAdapter = materials->GetCurrentAdapter();
int nModeCount = materials->GetModeCount( nAdapter );
int nDesktopWidth, nDesktopHeight, nDesktopRefresh;
game->GetDesktopInfo( nDesktopWidth, nDesktopHeight, nDesktopRefresh );
for ( int i = 0; i < nModeCount; i++ )
{
MaterialVideoMode_t info;
materials->GetModeInfo( nAdapter, i, info );
if ( info.m_Width < 640 || info.m_Height < 480 )
{
if ( !bAllowSmallModes )
continue;
}
// make sure we don't already have this mode listed
bool bAlreadyInList = false;
for ( int j = 0; j < m_nNumModes; j++ )
{
if ( info.m_Width == m_rgModeList[ j ].width && info.m_Height == m_rgModeList[ j ].height )
{
// in VR mode we want the highest refresh rate, without regard for the desktop refresh rate
if ( UseVR() || ShouldForceVRActive() )
{
if ( info.m_RefreshRate > m_rgModeList[j].refreshRate )
{
m_rgModeList[j].refreshRate = info.m_RefreshRate;
}
}
else
{
// choose the highest refresh rate available for each mode up to the desktop rate
// if the new mode is valid and current is invalid or not as high, choose the new one
if ( info.m_RefreshRate <= nDesktopRefresh && (m_rgModeList[j].refreshRate > nDesktopRefresh || m_rgModeList[j].refreshRate < info.m_RefreshRate) )
{
m_rgModeList[j].refreshRate = info.m_RefreshRate;
}
}
bAlreadyInList = true;
break;
}
}
if ( bAlreadyInList )
continue;
m_rgModeList[ m_nNumModes ].width = info.m_Width;
m_rgModeList[ m_nNumModes ].height = info.m_Height;
m_rgModeList[ m_nNumModes ].bpp = bitsperpixel;
// NOTE: Don't clamp this to the desktop rate because we want to be sure we've only added
// modes that the adapter can do and maybe the desktop rate isn't available in this mode
m_rgModeList[ m_nNumModes ].refreshRate = info.m_RefreshRate;
if ( ++m_nNumModes >= MAX_MODE_LIST )
break;
}
// Sort modes for easy searching later
if ( m_nNumModes > 1 )
{
qsort( (void *)&m_rgModeList[0], m_nNumModes, sizeof(vmode_t), VideoModeCompare );
}
materials->AddModeChangeCallBack( &VideoMode_AdjustForModeChange );
SetInitialized( true );
return true;
}
void CVideoMode_MaterialSystem::Shutdown()
{
materials->RemoveModeChangeCallBack( &VideoMode_AdjustForModeChange );
BaseClass::Shutdown();
}
//-----------------------------------------------------------------------------
// Sets the video mode
//-----------------------------------------------------------------------------
bool CVideoMode_MaterialSystem::SetMode( int nWidth, int nHeight, bool bWindowed )
{
// Necessary for mode selection to work
int nFoundMode = FindVideoMode( nWidth, nHeight, bWindowed );
vmode_t *pMode = GetMode( nFoundMode );
// update current video state
MaterialSystem_Config_t config = *g_pMaterialSystemConfig;
config.m_VideoMode.m_Width = pMode->width;
config.m_VideoMode.m_Height = pMode->height;
// make sure VR mode is up to date
config.SetFlag( MATSYS_VIDCFG_FLAGS_VR_MODE, UseVR() || ShouldForceVRActive() );
if ( ShouldForceVRActive() )
{
config.m_nVRModeAdapter = materials->GetCurrentAdapter();
}
#ifdef SWDS
config.m_VideoMode.m_RefreshRate = 60;
#else
config.m_VideoMode.m_RefreshRate = GetRefreshRateForMode( pMode );
#endif
config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, bWindowed );
#if defined( _X360 )
XVIDEO_MODE videoMode;
XGetVideoMode( &videoMode );
if ( videoMode.fIsWideScreen )
{
extern ConVar r_aspectratio;
r_aspectratio.SetValue( 16.0f/9.0f );
}
config.SetFlag( MATSYS_VIDCFG_FLAGS_SCALE_TO_OUTPUT_RESOLUTION, (DWORD)nWidth != videoMode.dwDisplayWidth || (DWORD)nHeight != videoMode.dwDisplayHeight );
if ( nHeight == 480 || nWidth == 576 )
{
// Use 2xMSAA for standard def (see mat_software_aa_strength for fake hi-def aa)
// FIXME: shuffle the EDRAM surfaces to allow 4xMSAA for standard def
// (they would overlap & trash each other with the current arrangement)
// NOTE: This should affect 640x480 and 848x480 (which is also used for 640x480 widescreen), and PAL 640x576
config.m_nAASamples = 2;
}
#endif
// FIXME: This is trash. We have to do *different* things depending on how we're setting the mode!
if ( !m_bSetModeOnce )
{
//Debugger();
if ( !materials->SetMode( (void*)game->GetMainDeviceWindow(), config ) )
return false;
m_bSetModeOnce = true;
InitStartupScreen();
return true;
}
// update the config
OverrideMaterialSystemConfig( config );
return true;
}
//-----------------------------------------------------------------------------
// Called by the material system when mode changes after a call to OverrideConfig
//-----------------------------------------------------------------------------
void CVideoMode_MaterialSystem::AdjustForModeChange( void )
{
if ( InEditMode() )
return;
// get previous size
int nOldUIWidth = GetModeUIWidth();
int nOldUIHeight = GetModeUIHeight();
// Get the new mode info from the config record
int nNewWidth = g_pMaterialSystemConfig->m_VideoMode.m_Width;
int nNewHeight = g_pMaterialSystemConfig->m_VideoMode.m_Height;
bool bWindowed = g_pMaterialSystemConfig->Windowed();
// reset the window size
CMatRenderContextPtr pRenderContext( materials );
ResetCurrentModeForNewResolution( nNewWidth, nNewHeight, bWindowed );
AdjustWindow( GetModeWidth(), GetModeHeight(), GetModeBPP(), IsWindowedMode() );
MarkClientViewRectDirty();
pRenderContext->Viewport( 0, 0, GetModeStereoWidth(), GetModeStereoHeight() );
// fixup vgui
vgui::surface()->OnScreenSizeChanged( nOldUIWidth, nOldUIHeight );
// Re-init the HUD
ClientDLL_HudVidInit();
}
//-----------------------------------------------------------------------------
// Sets the game window in editor mode
//-----------------------------------------------------------------------------
void CVideoMode_MaterialSystem::SetGameWindow( void *hWnd )
{
if ( hWnd == NULL )
{
// No longer confine rendering into this view
materials->SetView( NULL );
return;
}
// When running in edit mode, just use hammer's window
game->SetGameWindow( (HWND)hWnd );
// FIXME: Move this code into the _MaterialSystem version of CVideoMode
// In editor mode, the mode width + height is equal to the desktop width + height
MaterialVideoMode_t mode;
materials->GetDisplayMode( mode );
m_bWindowed = true;
m_nModeWidth = mode.m_Width;
m_nModeHeight = mode.m_Height;
materials->SetView( game->GetMainDeviceWindow() );
}
//-----------------------------------------------------------------------------
// Called when we lose the video buffer (alt-tab)
//-----------------------------------------------------------------------------
void CVideoMode_MaterialSystem::ReleaseVideo( void )
{
if ( IsX360() )
return;
if ( IsWindowedMode() )
return;
ReleaseFullScreen();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CVideoMode_MaterialSystem::RestoreVideo( void )
{
if ( IsX360() )
return;
if ( IsWindowedMode() )
return;
#if defined( USE_SDL )
SDL_ShowWindow( (SDL_Window*)game->GetMainWindow() );
#else
ShowWindow( (HWND)game->GetMainWindow(), SW_SHOWNORMAL );
#endif
AdjustWindow( GetModeWidth(), GetModeHeight(), GetModeBPP(), IsWindowedMode() );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CVideoMode_MaterialSystem::ReleaseFullScreen( void )
{
if ( IsX360() )
return;
if ( IsWindowedMode() )
return;
#if !defined( USE_SDL )
// Hide the main window
if ( m_nLastCDSWidth != 0 )
ChangeDisplaySettings( NULL, 0 );
ShowWindow( (HWND)game->GetMainWindow(), SW_MINIMIZE );
m_nLastCDSWidth = 0;
m_nLastCDSHeight = 0;
m_nLastCDSBPP = 0;
m_nLastCDSFreq = 0;
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Use Change Display Settings to go Full Screen
//-----------------------------------------------------------------------------
void CVideoMode_MaterialSystem::ChangeDisplaySettingsToFullscreen( int nWidth, int nHeight, int nBPP )
{
if ( IsX360() )
return;
if ( IsWindowedMode() )
return;
#if defined( WIN32 ) && !defined( USE_SDL )
DEVMODE dm;
memset(&dm, 0, sizeof(dm));
dm.dmSize = sizeof( dm );
dm.dmPelsWidth = nWidth;
dm.dmPelsHeight = nHeight;
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
dm.dmBitsPerPel = nBPP;
// FIXME: Fix direct reference of refresh rate from config record
int freq = g_pMaterialSystemConfig->m_VideoMode.m_RefreshRate;
if ( freq >= 60 )
{
dm.dmDisplayFrequency = freq;
dm.dmFields |= DM_DISPLAYFREQUENCY;
}
#if defined(IS_WINDOWS_PC)
DEVMODE dmCurrent;
if ( EnumDisplaySettings( materials->GetDisplayDeviceName(), ENUM_CURRENT_SETTINGS, &dmCurrent ) &&
dmCurrent.dmBitsPerPel == dm.dmBitsPerPel &&
dmCurrent.dmPelsWidth == dm.dmPelsWidth &&
dmCurrent.dmPelsHeight == dm.dmPelsHeight &&
( (dm.dmFields & DM_DISPLAYFREQUENCY) == 0 || dmCurrent.dmDisplayFrequency == dm.dmDisplayFrequency ) )
{
return;
}
#endif
if ( nWidth == m_nLastCDSWidth && nHeight == m_nLastCDSHeight && nBPP == m_nLastCDSBPP && freq == m_nLastCDSFreq )
return;
m_nLastCDSWidth = nWidth;
m_nLastCDSHeight = nHeight;
m_nLastCDSBPP = nBPP;
m_nLastCDSFreq = freq;
ChangeDisplaySettingsEx( materials->GetDisplayDeviceName(), &dm, NULL, CDS_FULLSCREEN, NULL );
#elif defined( USE_SDL )
g_pLauncherMgr->SetWindowFullScreen( true, nWidth, nHeight );
#else
if ( !HushAsserts() )
{
Assert( !"Impl me" );
}
#endif
}
void CVideoMode_MaterialSystem::ReadScreenPixels( int x, int y, int w, int h, void *pBuffer, ImageFormat format )
{
if ( !g_LostVideoMemory )
{
bool bReadPixelsFromFrontBuffer = g_pMaterialSystemHardwareConfig->ReadPixelsFromFrontBuffer();
if( bReadPixelsFromFrontBuffer )
{
Shader_SwapBuffers();
}
CMatRenderContextPtr pRenderContext( materials );
Rect_t rect;
rect.x = x;
rect.y = y;
rect.width = w;
rect.height = h;
pRenderContext->ReadPixelsAndStretch( &rect, &rect, (unsigned char*)pBuffer, format, w * ImageLoader::SizeInBytes( format ) );
if( bReadPixelsFromFrontBuffer )
{
Shader_SwapBuffers();
}
}
else
{
int nBytes = ImageLoader::GetMemRequired( w, h, 1, format, false );
memset( pBuffer, 0, nBytes );
}
}
//-----------------------------------------------------------------------------
// Class factory
//-----------------------------------------------------------------------------
IVideoMode *videomode = ( IVideoMode * )NULL;
void VideoMode_Create( )
{
videomode = new CVideoMode_MaterialSystem;
Assert( videomode );
}
void VideoMode_Destroy()
{
if ( videomode )
{
CVideoMode_MaterialSystem *pVideoMode_MS = static_cast<CVideoMode_MaterialSystem*>(videomode);
delete pVideoMode_MS;
videomode = NULL;
}
}
|