Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.3 KiB

  1. //=========== (C) Copyright 1999 Valve, L.L.C. All rights reserved. ===========
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. //=============================================================================
  9. #ifdef _WIN32
  10. #include "appframework/matsysapp.h"
  11. #include "FileSystem.h"
  12. #include "materialsystem/IMaterialSystem.h"
  13. #include "tier0/dbg.h"
  14. #include "tier0/icommandline.h"
  15. #include "materialsystem/MaterialSystem_Config.h"
  16. #include "filesystem_init.h"
  17. #include "inputsystem/iinputsystem.h"
  18. #include "tier2/tier2.h"
  19. #include "videocfg/videocfg.h"
  20. //-----------------------------------------------------------------------------
  21. // Constructor
  22. //-----------------------------------------------------------------------------
  23. CMatSysApp::CMatSysApp()
  24. {
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Create all singleton systems
  28. //-----------------------------------------------------------------------------
  29. bool CMatSysApp::Create()
  30. {
  31. AppSystemInfo_t appSystems[] =
  32. {
  33. { "inputsystem.dll", INPUTSYSTEM_INTERFACE_VERSION },
  34. { "materialsystem.dll", MATERIAL_SYSTEM_INTERFACE_VERSION },
  35. // Required to terminate the list
  36. { "", "" }
  37. };
  38. if ( !AddSystems( appSystems ) )
  39. return false;
  40. IMaterialSystem *pMaterialSystem = (IMaterialSystem*)FindSystem( MATERIAL_SYSTEM_INTERFACE_VERSION );
  41. if ( !pMaterialSystem )
  42. {
  43. Warning( "CMatSysApp::Create: Unable to connect to necessary interface!\n" );
  44. return false;
  45. }
  46. pMaterialSystem->SetShaderAPI( "shaderapidx9.dll" );
  47. return true;
  48. }
  49. void CMatSysApp::Destroy()
  50. {
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Pump messages
  54. //-----------------------------------------------------------------------------
  55. void CMatSysApp::AppPumpMessages()
  56. {
  57. g_pInputSystem->PollInputState();
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Sets up the game path
  61. //-----------------------------------------------------------------------------
  62. bool CMatSysApp::SetupSearchPaths( const char *pStartingDir, bool bOnlyUseStartingDir, bool bIsTool )
  63. {
  64. if ( !BaseClass::SetupSearchPaths( pStartingDir, bOnlyUseStartingDir, bIsTool ) )
  65. return false;
  66. g_pFullFileSystem->AddSearchPath( GetGameInfoPath(), "SKIN", PATH_ADD_TO_HEAD );
  67. return true;
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Init, shutdown
  71. //-----------------------------------------------------------------------------
  72. bool CMatSysApp::PreInit( )
  73. {
  74. if ( !BaseClass::PreInit() )
  75. return false;
  76. if ( !g_pFullFileSystem || !g_pMaterialSystem || !g_pInputSystem )
  77. {
  78. Warning( "CMatSysApp::PreInit: Unable to connect to necessary interface!\n" );
  79. return false;
  80. }
  81. // Add paths...
  82. if ( !SetupSearchPaths( NULL, false, true ) )
  83. return false;
  84. const char *pArg;
  85. int iWidth = 1024;
  86. int iHeight = 768;
  87. bool bWindowed = (CommandLine()->CheckParm( "-fullscreen" ) == NULL);
  88. if (CommandLine()->CheckParm( "-width", &pArg ))
  89. {
  90. iWidth = atoi( pArg );
  91. }
  92. if (CommandLine()->CheckParm( "-height", &pArg ))
  93. {
  94. iHeight = atoi( pArg );
  95. }
  96. m_nWidth = iWidth;
  97. m_nHeight = iHeight;
  98. m_HWnd = CreateAppWindow( GetAppInstance(), GetAppName(), bWindowed, iWidth, iHeight, false );
  99. if ( !m_HWnd )
  100. return false;
  101. g_pInputSystem->AttachToWindow( m_HWnd );
  102. // NOTE: If we specifically wanted to use a particular shader DLL, we set it here...
  103. //m_pMaterialSystem->SetShaderAPI( "shaderapidx8" );
  104. // Get the adapter from the command line....
  105. const char *pAdapterString;
  106. int adapter = 0;
  107. if (CommandLine()->CheckParm( "-adapter", &pAdapterString ))
  108. {
  109. adapter = atoi( pAdapterString );
  110. }
  111. int adapterFlags = 0;
  112. if ( CommandLine()->CheckParm( "-ref" ) )
  113. {
  114. adapterFlags |= MATERIAL_INIT_REFERENCE_RASTERIZER;
  115. }
  116. if ( AppUsesReadPixels() )
  117. {
  118. adapterFlags |= MATERIAL_INIT_ALLOCATE_FULLSCREEN_TEXTURE;
  119. }
  120. g_pMaterialSystem->SetAdapter( adapter, adapterFlags );
  121. return true;
  122. }
  123. void CMatSysApp::PostShutdown()
  124. {
  125. if ( g_pInputSystem )
  126. {
  127. g_pInputSystem->DetachFromWindow( );
  128. }
  129. BaseClass::PostShutdown();
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Gets the window size
  133. //-----------------------------------------------------------------------------
  134. int CMatSysApp::GetWindowWidth() const
  135. {
  136. return m_nWidth;
  137. }
  138. int CMatSysApp::GetWindowHeight() const
  139. {
  140. return m_nHeight;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Returns the window
  144. //-----------------------------------------------------------------------------
  145. void* CMatSysApp::GetAppWindow()
  146. {
  147. return m_HWnd;
  148. }
  149. // Replace first underscore (if any) with \0 and return
  150. // This handles special mods like tf_movies, l4d_movies, tf_comics
  151. // As a result, such mods will use the gpu_level settings etc from the base mod
  152. void StripModSuffix( char *pModName )
  153. {
  154. int i = 0;
  155. while ( pModName[i] != '\0' ) // Walk to the end of the string
  156. {
  157. if ( pModName[i] == '_') // If we hit an underscore
  158. {
  159. pModName[i] = '\0'; // Terminate the string here and bail out
  160. return;
  161. }
  162. i++;
  163. }
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Sets the video mode
  167. //-----------------------------------------------------------------------------
  168. bool CMatSysApp::SetVideoMode( )
  169. {
  170. MaterialSystem_Config_t config;
  171. if ( CommandLine()->CheckParm( "-fullscreen" ) )
  172. {
  173. config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, false );
  174. }
  175. else
  176. {
  177. config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, true );
  178. }
  179. if ( CommandLine()->CheckParm( "-resizing" ) )
  180. {
  181. config.SetFlag( MATSYS_VIDCFG_FLAGS_RESIZING, true );
  182. }
  183. if ( CommandLine()->CheckParm( "-mat_vsync" ) )
  184. {
  185. config.SetFlag( MATSYS_VIDCFG_FLAGS_NO_WAIT_FOR_VSYNC, false );
  186. }
  187. config.m_nAASamples = CommandLine()->ParmValue( "-mat_antialias", 1 );
  188. config.m_nAAQuality = CommandLine()->ParmValue( "-mat_aaquality", 0 );
  189. if ( CommandLine()->FindParm( "-csm_quality_level" ) )
  190. {
  191. int nCSMQuality = CommandLine()->ParmValue( "-csm_quality_level", CSMQUALITY_VERY_LOW );
  192. config.m_nCSMQuality = (CSMQualityMode_t)clamp( nCSMQuality, CSMQUALITY_VERY_LOW, CSMQUALITY_TOTAL_MODES - 1 );
  193. }
  194. config.m_VideoMode.m_Width = config.m_VideoMode.m_Height = 0;
  195. config.m_VideoMode.m_Format = IMAGE_FORMAT_BGRX8888;
  196. config.m_VideoMode.m_RefreshRate = 0;
  197. config.SetFlag( MATSYS_VIDCFG_FLAGS_STENCIL, true );
  198. bool modeSet = g_pMaterialSystem->SetMode( m_HWnd, config );
  199. if (!modeSet)
  200. {
  201. Error( "Unable to set mode\n" );
  202. return false;
  203. }
  204. char pModPath[MAX_PATH];
  205. V_snprintf( pModPath, sizeof(pModPath), "" );
  206. g_pFullFileSystem->GetSearchPath( "MOD", false, pModPath, sizeof( pModPath ) );
  207. // Construct the mod name so we can use the mod-specific encrypted config files
  208. char pModName[32];
  209. V_StripTrailingSlash( pModPath );
  210. V_FileBase( pModPath, pModName, sizeof( pModName ) );
  211. StripModSuffix( pModName );
  212. // Just use the highest levels in non-game apps
  213. UpdateSystemLevel( CPU_LEVEL_HIGH, GPU_LEVEL_VERYHIGH, MEM_LEVEL_HIGH, GPU_MEM_LEVEL_HIGH, false, pModName );
  214. g_pMaterialSystem->OverrideConfig( config, false );
  215. return true;
  216. }
  217. #endif // _WIN32