Team Fortress 2 Source Code as on 22/4/2020
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.

308 lines
8.4 KiB

  1. //========= Copyright Valve Corporation, 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. #if defined( _WIN32 ) && !defined( _X360 )
  11. #include <windows.h>
  12. #endif
  13. #include "appframework/vguimatsysapp.h"
  14. #include "filesystem.h"
  15. #include "materialsystem/imaterialsystem.h"
  16. #include "vgui/ivgui.h"
  17. #include "vgui/ISurface.h"
  18. #include "vgui_controls/controls.h"
  19. #include "vgui/ischeme.h"
  20. #include "vgui/ilocalize.h"
  21. #include "tier0/dbg.h"
  22. #include "tier0/icommandline.h"
  23. #include "materialsystem/materialsystem_config.h"
  24. #include "filesystem_init.h"
  25. #include "VGuiMatSurface/IMatSystemSurface.h"
  26. #include "inputsystem/iinputsystem.h"
  27. #include "tier3/tier3.h"
  28. //-----------------------------------------------------------------------------
  29. // Constructor
  30. //-----------------------------------------------------------------------------
  31. CVguiMatSysApp::CVguiMatSysApp()
  32. {
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Create all singleton systems
  36. //-----------------------------------------------------------------------------
  37. bool CVguiMatSysApp::Create()
  38. {
  39. AppSystemInfo_t appSystems[] =
  40. {
  41. { "inputsystem.dll", INPUTSYSTEM_INTERFACE_VERSION },
  42. { "materialsystem.dll", MATERIAL_SYSTEM_INTERFACE_VERSION },
  43. // NOTE: This has to occur before vgui2.dll so it replaces vgui2's surface implementation
  44. { "vguimatsurface.dll", VGUI_SURFACE_INTERFACE_VERSION },
  45. { "vgui2.dll", VGUI_IVGUI_INTERFACE_VERSION },
  46. // Required to terminate the list
  47. { "", "" }
  48. };
  49. if ( !AddSystems( appSystems ) )
  50. return false;
  51. IMaterialSystem *pMaterialSystem = (IMaterialSystem*)FindSystem( MATERIAL_SYSTEM_INTERFACE_VERSION );
  52. if ( !pMaterialSystem )
  53. {
  54. Warning( "CVguiMatSysApp::Create: Unable to connect to necessary interface!\n" );
  55. return false;
  56. }
  57. pMaterialSystem->SetShaderAPI( "shaderapidx9.dll" );
  58. return true;
  59. }
  60. void CVguiMatSysApp::Destroy()
  61. {
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Window management
  65. //-----------------------------------------------------------------------------
  66. void*CVguiMatSysApp::CreateAppWindow( char const *pTitle, bool bWindowed, int w, int h )
  67. {
  68. WNDCLASSEX wc;
  69. memset( &wc, 0, sizeof( wc ) );
  70. wc.cbSize = sizeof( wc );
  71. wc.style = CS_OWNDC | CS_DBLCLKS;
  72. wc.lpfnWndProc = DefWindowProc;
  73. wc.hInstance = (HINSTANCE)GetAppInstance();
  74. wc.lpszClassName = "Valve001";
  75. wc.hIcon = NULL; //LoadIcon( s_HInstance, MAKEINTRESOURCE( IDI_LAUNCHER ) );
  76. wc.hIconSm = wc.hIcon;
  77. RegisterClassEx( &wc );
  78. // Note, it's hidden
  79. DWORD style = WS_POPUP | WS_CLIPSIBLINGS;
  80. if ( bWindowed )
  81. {
  82. // Give it a frame
  83. style |= WS_OVERLAPPEDWINDOW;
  84. style &= ~WS_THICKFRAME;
  85. }
  86. // Never a max box
  87. style &= ~WS_MAXIMIZEBOX;
  88. RECT windowRect;
  89. windowRect.top = 0;
  90. windowRect.left = 0;
  91. windowRect.right = w;
  92. windowRect.bottom = h;
  93. // Compute rect needed for that size client area based on window style
  94. AdjustWindowRectEx(&windowRect, style, FALSE, 0);
  95. // Create the window
  96. void *hWnd = CreateWindow( wc.lpszClassName, pTitle, style, 0, 0,
  97. windowRect.right - windowRect.left, windowRect.bottom - windowRect.top,
  98. NULL, NULL, (HINSTANCE)GetAppInstance(), NULL );
  99. if (!hWnd)
  100. return NULL;
  101. int CenterX, CenterY;
  102. CenterX = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
  103. CenterY = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
  104. CenterX = (CenterX < 0) ? 0: CenterX;
  105. CenterY = (CenterY < 0) ? 0: CenterY;
  106. // In VCR modes, keep it in the upper left so mouse coordinates are always relative to the window.
  107. SetWindowPos( (HWND)hWnd, NULL, CenterX, CenterY, 0, 0,
  108. SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
  109. return hWnd;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Pump messages
  113. //-----------------------------------------------------------------------------
  114. void CVguiMatSysApp::AppPumpMessages()
  115. {
  116. g_pInputSystem->PollInputState();
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Sets up the game path
  120. //-----------------------------------------------------------------------------
  121. bool CVguiMatSysApp::SetupSearchPaths( const char *pStartingDir, bool bOnlyUseStartingDir, bool bIsTool )
  122. {
  123. if ( !BaseClass::SetupSearchPaths( pStartingDir, bOnlyUseStartingDir, bIsTool ) )
  124. return false;
  125. g_pFullFileSystem->AddSearchPath( GetGameInfoPath(), "SKIN", PATH_ADD_TO_HEAD );
  126. return true;
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Init, shutdown
  130. //-----------------------------------------------------------------------------
  131. bool CVguiMatSysApp::PreInit( )
  132. {
  133. if ( !BaseClass::PreInit() )
  134. return false;
  135. if ( !g_pFullFileSystem || !g_pMaterialSystem || !g_pInputSystem || !g_pMatSystemSurface )
  136. {
  137. Warning( "CVguiMatSysApp::PreInit: Unable to connect to necessary interface!\n" );
  138. return false;
  139. }
  140. // Add paths...
  141. if ( !SetupSearchPaths( NULL, false, true ) )
  142. return false;
  143. const char *pArg;
  144. int iWidth = 1024;
  145. int iHeight = 768;
  146. bool bWindowed = (CommandLine()->CheckParm( "-fullscreen" ) == NULL);
  147. if (CommandLine()->CheckParm( "-width", &pArg ))
  148. {
  149. iWidth = atoi( pArg );
  150. }
  151. if (CommandLine()->CheckParm( "-height", &pArg ))
  152. {
  153. iHeight = atoi( pArg );
  154. }
  155. m_nWidth = iWidth;
  156. m_nHeight = iHeight;
  157. m_HWnd = CreateAppWindow( GetAppName(), bWindowed, iWidth, iHeight );
  158. if ( !m_HWnd )
  159. return false;
  160. g_pInputSystem->AttachToWindow( m_HWnd );
  161. g_pMatSystemSurface->AttachToWindow( m_HWnd );
  162. // NOTE: If we specifically wanted to use a particular shader DLL, we set it here...
  163. //m_pMaterialSystem->SetShaderAPI( "shaderapidx8" );
  164. // Get the adapter from the command line....
  165. const char *pAdapterString;
  166. int adapter = 0;
  167. if (CommandLine()->CheckParm( "-adapter", &pAdapterString ))
  168. {
  169. adapter = atoi( pAdapterString );
  170. }
  171. int adapterFlags = 0;
  172. if ( CommandLine()->CheckParm( "-ref" ) )
  173. {
  174. adapterFlags |= MATERIAL_INIT_REFERENCE_RASTERIZER;
  175. }
  176. if ( AppUsesReadPixels() )
  177. {
  178. adapterFlags |= MATERIAL_INIT_ALLOCATE_FULLSCREEN_TEXTURE;
  179. }
  180. g_pMaterialSystem->SetAdapter( adapter, adapterFlags );
  181. return true;
  182. }
  183. void CVguiMatSysApp::PostShutdown()
  184. {
  185. if ( g_pMatSystemSurface && g_pInputSystem )
  186. {
  187. g_pMatSystemSurface->AttachToWindow( NULL );
  188. g_pInputSystem->DetachFromWindow( );
  189. }
  190. BaseClass::PostShutdown();
  191. }
  192. //-----------------------------------------------------------------------------
  193. // Gets the window size
  194. //-----------------------------------------------------------------------------
  195. int CVguiMatSysApp::GetWindowWidth() const
  196. {
  197. return m_nWidth;
  198. }
  199. int CVguiMatSysApp::GetWindowHeight() const
  200. {
  201. return m_nHeight;
  202. }
  203. //-----------------------------------------------------------------------------
  204. // Returns the window
  205. //-----------------------------------------------------------------------------
  206. void* CVguiMatSysApp::GetAppWindow()
  207. {
  208. return m_HWnd;
  209. }
  210. //-----------------------------------------------------------------------------
  211. // Sets the video mode
  212. //-----------------------------------------------------------------------------
  213. bool CVguiMatSysApp::SetVideoMode( )
  214. {
  215. MaterialSystem_Config_t config;
  216. if ( CommandLine()->CheckParm( "-fullscreen" ) )
  217. {
  218. config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, false );
  219. }
  220. else
  221. {
  222. config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, true );
  223. }
  224. if ( CommandLine()->CheckParm( "-resizing" ) )
  225. {
  226. config.SetFlag( MATSYS_VIDCFG_FLAGS_RESIZING, true );
  227. }
  228. if ( CommandLine()->CheckParm( "-mat_vsync" ) )
  229. {
  230. config.SetFlag( MATSYS_VIDCFG_FLAGS_NO_WAIT_FOR_VSYNC, false );
  231. }
  232. config.m_nAASamples = CommandLine()->ParmValue( "-mat_antialias", 1 );
  233. config.m_nAAQuality = CommandLine()->ParmValue( "-mat_aaquality", 0 );
  234. config.m_VideoMode.m_Width = config.m_VideoMode.m_Height = 0;
  235. config.m_VideoMode.m_Format = IMAGE_FORMAT_BGRX8888;
  236. config.m_VideoMode.m_RefreshRate = 0;
  237. config.SetFlag( MATSYS_VIDCFG_FLAGS_STENCIL, true );
  238. bool modeSet = g_pMaterialSystem->SetMode( m_HWnd, config );
  239. if (!modeSet)
  240. {
  241. Error( "Unable to set mode\n" );
  242. return false;
  243. }
  244. g_pMaterialSystem->OverrideConfig( config, false );
  245. return true;
  246. }
  247. #endif // _WIN32