-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathsys_getmodes.cpp
2697 lines (2224 loc) · 81.8 KB
/
sys_getmodes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright 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
const char* loading = "materials/console/startup_loading.vtf";
if ( IsGamepadUI() )
loading = "materials/gamepadui/game_logo.vtf";
m_pLoadingTexture = LoadVTF( buf, loading );
if ( !m_pLoadingTexture )
{
Error( "Can't find background image '%s'\n", loading );
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 );
const char* loading = "console/startup_loading.vtf";
if ( IsGamepadUI() )
loading = "gamepadui/game_logo.vtf";
pVMTKeyValues = new KeyValues( "UnlitGeneric" );
pVMTKeyValues->SetString( "$basetexture", loading );
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 );
if ( !IsGamepadUI() )
DrawScreenSpaceRectangle( pLoadingMaterial, w-lw, h-lh+slide/2, lw, lh, 0, 0, lw-1, lh-1, lw, lh, NULL,1,1,depth-0.1 );
else
// TODO: Steam Deck
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 );
if(!IsGamepadUI())
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 );