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.

981 lines
23 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //--------------------------------------------------------------------------------------
  3. // TriangleASM.cpp
  4. //
  5. // Hijacked from samples, assimilated into valve app.
  6. //--------------------------------------------------------------------------------------
  7. #include "tier0\platform.h"
  8. #if !defined( _X360 )
  9. #include <windows.h>
  10. #endif
  11. #include "appframework\iappsystemgroup.h"
  12. #include "appframework\appframework.h"
  13. #include "tier0\dbg.h"
  14. #include "tier1\interface.h"
  15. #include "filesystem.h"
  16. #include "vstdlib\cvar.h"
  17. #include "filesystem_init.h"
  18. #include "tier1/utlbuffer.h"
  19. #include "icommandline.h"
  20. #include "datacache\idatacache.h"
  21. #include "datacache\imdlcache.h"
  22. #include "studio.h"
  23. #include "utlbuffer.h"
  24. #include "tier2\utlstreambuffer.h"
  25. #include "tier2\tier2.h"
  26. #include "tier3\tier3.h"
  27. #include "mathlib/mathlib.h"
  28. #include "inputsystem\iinputsystem.h"
  29. #include "vphysics_interface.h"
  30. #include "istudiorender.h"
  31. #include "studio.h"
  32. #include "tier1\KeyValues.h"
  33. #include "vgui\IVGui.h"
  34. #include "vguimatsurface\imatsystemsurface.h"
  35. #include "matsys_controls\matsyscontrols.h"
  36. #include "vgui\ILocalize.h"
  37. #include "vgui_controls\panel.h"
  38. #include "vgui_controls\label.h"
  39. #if defined( _X360 )
  40. #include "xbox\xbox_console.h"
  41. #include "xbox\xbox_win32stubs.h"
  42. #endif
  43. #include "materialsystem\imaterialsystem.h"
  44. #include "materialsystem\imesh.h"
  45. #include "materialsystem\materialsystem_config.h"
  46. #include "materialsystem\MaterialSystemUtil.h"
  47. #include "materialsystem\ishaderapi.h"
  48. #if !defined( _X360 )
  49. #include "xbox\xboxstubs.h"
  50. #endif
  51. #include "bone_setup.h"
  52. #include "tier0\memdbgon.h"
  53. bool g_bActive = true;
  54. extern SpewOutputFunc_t g_DefaultSpewFunc;
  55. // These must be turned on in order....
  56. #define USE_FILESYSTEM
  57. #define USE_MATERIALSYSTEM
  58. #define USE_VPHYSICS
  59. // Note: VPHYSICS is just used via the datacache to load a model's physics collision mesh
  60. // These can be turned on in any order
  61. #define USE_INPUTSYSTEM
  62. #define USE_VGUI
  63. #define USE_STUDIORENDER
  64. #pragma warning(disable:4189) // local variable is initialized but not referenced
  65. //-----------------------------------------------------------------------------
  66. // The application object
  67. //-----------------------------------------------------------------------------
  68. class CTest360App : public CDefaultAppSystemGroup<CSteamAppSystemGroup>
  69. {
  70. public:
  71. virtual bool Create();
  72. virtual bool PreInit();
  73. virtual int Main();
  74. virtual void Destroy();
  75. private:
  76. const char *GetAppName() { return "TEST360"; }
  77. void RenderScene();
  78. bool CreateMainWindow( int width, int height, bool fullscreen );
  79. #if defined( USE_MATERIALSYSTEM )
  80. bool SetupMaterialSystem();
  81. #endif
  82. #if defined( USE_STUDIORENDER )
  83. bool SetupStudioRender();
  84. bool LoadModel( const char *pModelName );
  85. matrix3x4_t* SetUpBones( studiohdr_t *pStudioHdr, const matrix3x4_t &modelMatrix );
  86. #endif
  87. #if defined( USE_VGUI )
  88. int InitializeVGUI( void );
  89. void ShutdownVGUI( void );
  90. #endif
  91. IPhysicsCollision *m_pCollision;
  92. IMaterialSystem *m_pMaterialSystem;
  93. IFileSystem *m_pFileSystem;
  94. int m_nWidth;
  95. int m_nHeight;
  96. float m_fAspect;
  97. float m_NearClip;
  98. float m_FarClip;
  99. float m_fov;
  100. HWND m_hWnd;
  101. studiohdr_t *m_pStudioHdr;
  102. studiohwdata_t *m_pStudioHWData;
  103. int m_nLod;
  104. float m_flTime;
  105. float m_flPlaybackRate;
  106. vgui::Panel *m_pMainPanel;
  107. };
  108. static float g_yaw;
  109. static float g_horizontalPan;
  110. static float g_verticalPan;
  111. static float g_zoom;
  112. static int g_sequence;
  113. static bool g_bWireframe;
  114. DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT( CTest360App );
  115. #if defined( USE_MATERIALSYSTEM )
  116. static void MaterialSystem_Warning( const char *fmt, ... )
  117. {
  118. va_list argptr;
  119. char msg[2048];
  120. va_start( argptr, fmt );
  121. Q_vsnprintf( msg, sizeof ( msg ), fmt, argptr );
  122. va_end( argptr );
  123. OutputDebugString( msg );
  124. }
  125. #endif
  126. #if defined( USE_MATERIALSYSTEM )
  127. static void MaterialSystem_Warning( char *fmt, ... )
  128. {
  129. va_list argptr;
  130. char msg[2048];
  131. va_start( argptr, fmt );
  132. Q_vsnprintf( msg, sizeof( msg ), fmt, argptr );
  133. va_end( argptr );
  134. OutputDebugString( msg );
  135. }
  136. #endif
  137. virtualmodel_t *studiohdr_t::GetVirtualModel( void ) const
  138. {
  139. if ( numincludemodels == 0 )
  140. return NULL;
  141. return g_pMDLCache->GetVirtualModelFast( this, (MDLHandle_t)virtualModel );
  142. }
  143. byte *studiohdr_t::GetAnimBlock( int i ) const
  144. {
  145. return g_pMDLCache->GetAnimBlock( (MDLHandle_t)virtualModel, i );
  146. }
  147. int studiohdr_t::GetAutoplayList( unsigned short **pOut ) const
  148. {
  149. return g_pMDLCache->GetAutoplayList( (MDLHandle_t)virtualModel, pOut );
  150. }
  151. const studiohdr_t *virtualgroup_t::GetStudioHdr( void ) const
  152. {
  153. return g_pMDLCache->GetStudioHdr( (MDLHandle_t)cache );
  154. }
  155. #if defined( USE_STUDIORENDER )
  156. matrix3x4_t* CTest360App::SetUpBones( studiohdr_t *pStudioHdr, const matrix3x4_t &shapeToWorld )
  157. {
  158. // Default to middle of the pose parameter range
  159. float pPoseParameter[MAXSTUDIOPOSEPARAM];
  160. for ( int i = 0; i < MAXSTUDIOPOSEPARAM; ++i )
  161. {
  162. pPoseParameter[i] = 0.5f;
  163. }
  164. CStudioHdr studioHdr( pStudioHdr, g_pMDLCache );
  165. int nFrameCount = Studio_MaxFrame( &studioHdr, g_sequence, pPoseParameter );
  166. if ( nFrameCount == 0 )
  167. {
  168. nFrameCount = 1;
  169. }
  170. float flCycle = ( m_flTime * m_flPlaybackRate ) / nFrameCount;
  171. // FIXME: We're always wrapping; may want to determing if we should clamp
  172. flCycle -= (int)(flCycle);
  173. int boneMask = BONE_USED_BY_VERTEX_AT_LOD( m_nLod );
  174. Vector pos[MAXSTUDIOBONES];
  175. Quaternion q[MAXSTUDIOBONES];
  176. IBoneSetup boneSetup( &studioHdr, boneMask, pPoseParameter );
  177. boneSetup.InitPose( pos, q );
  178. boneSetup.AccumulatePose( pos, q, g_sequence, flCycle, 1.0f, m_flTime, NULL );
  179. // FIXME: Try enabling this?
  180. // CalcAutoplaySequences( pStudioHdr, NULL, pos, q, pPoseParameter, BoneMask( ), flTime );
  181. // Root transform
  182. matrix3x4_t rootToWorld, temp;
  183. MatrixCopy( shapeToWorld, rootToWorld );
  184. matrix3x4_t *pBoneToWorld = g_pStudioRender->LockBoneMatrices( studioHdr.numbones() );
  185. for ( int i = 0; i < studioHdr.numbones(); i++ )
  186. {
  187. // If it's not being used, fill with NAN for errors
  188. if ( !(studioHdr.pBone( i )->flags & boneMask) )
  189. {
  190. int j, k;
  191. for (j = 0; j < 3; j++)
  192. {
  193. for (k = 0; k < 4; k++)
  194. {
  195. pBoneToWorld[i][j][k] = VEC_T_NAN;
  196. }
  197. }
  198. continue;
  199. }
  200. matrix3x4_t boneMatrix;
  201. QuaternionMatrix( q[i], boneMatrix );
  202. MatrixSetColumn( pos[i], 3, boneMatrix );
  203. if (studioHdr.pBone(i)->parent == -1)
  204. {
  205. ConcatTransforms (rootToWorld, boneMatrix, pBoneToWorld[i]);
  206. }
  207. else
  208. {
  209. ConcatTransforms (pBoneToWorld[ studioHdr.pBone(i)->parent ], boneMatrix, pBoneToWorld[i] );
  210. }
  211. }
  212. return pBoneToWorld;
  213. }
  214. #endif
  215. //--------------------------------------------------------------------------------------
  216. // LoadModel
  217. //
  218. //--------------------------------------------------------------------------------------
  219. #if defined( USE_STUDIORENDER )
  220. bool CTest360App::LoadModel( const char* pModelName )
  221. {
  222. MDLHandle_t hMdl = g_pMDLCache->FindMDL( pModelName );
  223. m_pStudioHdr = g_pMDLCache->GetStudioHdr( hMdl );
  224. g_pMDLCache->GetVertexData( hMdl );
  225. g_pMDLCache->FinishPendingLoads();
  226. g_pMDLCache->GetHardwareData( hMdl );
  227. g_pMDLCache->FinishPendingLoads();
  228. m_pStudioHWData = g_pMDLCache->GetHardwareData( hMdl );
  229. g_sequence = 0;
  230. m_nLod = 0;
  231. m_flPlaybackRate = 30.0;
  232. g_yaw = 0;
  233. g_zoom = -100;
  234. g_horizontalPan = 0;
  235. g_verticalPan = -30;
  236. return true;
  237. }
  238. #endif
  239. //--------------------------------------------------------------------------------------
  240. // SetupMaterialSystem
  241. //
  242. //--------------------------------------------------------------------------------------
  243. #if defined( USE_MATERIALSYSTEM )
  244. bool CTest360App::SetupMaterialSystem()
  245. {
  246. RECT rect;
  247. MaterialSystem_Config_t config;
  248. config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, IsPC() ? true : false );
  249. config.SetFlag( MATSYS_VIDCFG_FLAGS_NO_WAIT_FOR_VSYNC, 0 );
  250. config.m_VideoMode.m_Width = 0;
  251. config.m_VideoMode.m_Height = 0;
  252. config.m_VideoMode.m_Format = IMAGE_FORMAT_BGRX8888;
  253. config.m_VideoMode.m_RefreshRate = 0;
  254. config.dxSupportLevel = IsX360() ? 98 : 0;
  255. bool modeSet = m_pMaterialSystem->SetMode( m_hWnd, config );
  256. if ( !modeSet )
  257. {
  258. Error( "Failed to set mode\n" );
  259. return false;
  260. }
  261. m_pMaterialSystem->OverrideConfig( config, false );
  262. GetClientRect( m_hWnd, &rect );
  263. m_nWidth = rect.right;
  264. m_nHeight = rect.bottom;
  265. m_fAspect = (float)m_nWidth/(float)m_nHeight;
  266. m_NearClip = 8.0f;
  267. m_FarClip = 28400.0f;
  268. m_fov = 90;
  269. return true;
  270. }
  271. #endif
  272. //--------------------------------------------------------------------------------------
  273. // SetupStudioRender
  274. //
  275. //--------------------------------------------------------------------------------------
  276. #if defined( USE_STUDIORENDER )
  277. bool CTest360App::SetupStudioRender()
  278. {
  279. StudioRenderConfig_t config;
  280. memset( &config, 0, sizeof(config) );
  281. config.bEyeMove = false;
  282. config.bTeeth = true;
  283. config.bEyes = true;
  284. config.bFlex = true;
  285. config.fEyeShiftX = 0.0f;
  286. config.fEyeShiftY = 0.0f;
  287. config.fEyeShiftZ = 0.0f;
  288. config.fEyeSize = 0.0f;
  289. config.bNoHardware = false;
  290. config.bNoSoftware = false;
  291. config.bSoftwareSkin = false;
  292. config.bSoftwareLighting = false;
  293. config.drawEntities = true;
  294. config.bWireframe = false;
  295. config.SetNormals( false );
  296. config.SetTangentFrame( false );
  297. config.skin = 0;
  298. config.fullbright = 0;
  299. config.pConDPrintf = MaterialSystem_Warning;
  300. config.pConPrintf = MaterialSystem_Warning;
  301. config.bShowEnvCubemapOnly = false;
  302. g_pStudioRender->UpdateConfig( config );
  303. return true;
  304. }
  305. #endif
  306. //--------------------------------------------------------------------------------------
  307. // Render a model using the MaterialSystem
  308. //--------------------------------------------------------------------------------------
  309. void CTest360App::RenderScene()
  310. {
  311. m_flTime = Plat_FloatTime();
  312. #if defined( USE_MATERIALSYSTEM )
  313. CMatRenderContextPtr pRenderContext( m_pMaterialSystem );
  314. m_pMaterialSystem->BeginFrame();
  315. #endif
  316. #if defined( USE_STUDIORENDER )
  317. g_pStudioRender->BeginFrame();
  318. #endif
  319. #if defined( USE_MATERIALSYSTEM )
  320. pRenderContext->ClearColor3ub( 0, 0, 0 );
  321. pRenderContext->ClearBuffers( true, true );
  322. #endif
  323. #if defined( USE_STUDIORENDER )
  324. pRenderContext->Viewport( 0, 0, m_nWidth, m_nHeight );
  325. pRenderContext->MatrixMode( MATERIAL_PROJECTION );
  326. pRenderContext->LoadIdentity();
  327. pRenderContext->PerspectiveX( m_fov, m_fAspect, m_NearClip, m_FarClip );
  328. pRenderContext->MatrixMode( MATERIAL_VIEW );
  329. pRenderContext->LoadIdentity();
  330. pRenderContext->Translate( g_horizontalPan, g_verticalPan, g_zoom );
  331. pRenderContext->MatrixMode( MATERIAL_MODEL );
  332. pRenderContext->LoadIdentity();
  333. g_pStudioRender->SetLocalLights( 0, NULL );
  334. pRenderContext->SetAmbientLight( 1.0, 1.0, 1.0 );
  335. QAngle angles;
  336. angles[YAW] = 0;
  337. angles[PITCH] = -90 + g_yaw;
  338. angles[ROLL] = -90;
  339. matrix3x4_t cameraMatrix;
  340. AngleMatrix( angles, cameraMatrix );
  341. static Vector white[6] =
  342. {
  343. Vector( 1.0, 1.0, 1.0 ),
  344. Vector( 1.0, 1.0, 1.0 ),
  345. Vector( 1.0, 1.0, 1.0 ),
  346. Vector( 1.0, 1.0, 1.0 ),
  347. Vector( 1.0, 1.0, 1.0 ),
  348. Vector( 1.0, 1.0, 1.0 ),
  349. };
  350. g_pStudioRender->SetAmbientLightColors( white );
  351. matrix3x4_t *pBoneToWorld = SetUpBones( m_pStudioHdr, cameraMatrix );
  352. Vector modelOrigin( 0, 0, 0 );
  353. pRenderContext->MatrixMode( MATERIAL_MODEL );
  354. pRenderContext->PushMatrix();
  355. DrawModelInfo_t modelInfo;
  356. memset( &modelInfo, 0, sizeof( modelInfo ) );
  357. modelInfo.m_pStudioHdr = m_pStudioHdr;
  358. modelInfo.m_pHardwareData = m_pStudioHWData;
  359. modelInfo.m_Decals = STUDIORENDER_DECAL_INVALID;
  360. modelInfo.m_Skin = 0;
  361. modelInfo.m_Body = 0;
  362. modelInfo.m_HitboxSet = 0;
  363. modelInfo.m_pClientEntity = NULL;
  364. modelInfo.m_Lod = 0;
  365. modelInfo.m_ppColorMeshes = NULL;
  366. int drawFlags = 0;
  367. if ( g_bWireframe )
  368. {
  369. drawFlags |= STUDIORENDER_DRAW_WIREFRAME;
  370. }
  371. g_pStudioRender->DrawModel( NULL, modelInfo, pBoneToWorld, modelOrigin, drawFlags );
  372. pRenderContext->MatrixMode( MATERIAL_MODEL );
  373. pRenderContext->PopMatrix();
  374. #endif
  375. #if defined( USE_MATERIALSYSTEM )
  376. pRenderContext->Flush( true );
  377. #endif
  378. #if defined( USE_VGUI )
  379. vgui::ivgui()->RunFrame();
  380. vgui::surface()->PaintTraverseEx( vgui::surface()->GetEmbeddedPanel() );
  381. #endif
  382. #if defined( USE_STUDIORENDER )
  383. g_pStudioRender->EndFrame();
  384. #endif
  385. #if defined( USE_MATERIALSYSTEM )
  386. m_pMaterialSystem->EndFrame();
  387. m_pMaterialSystem->SwapBuffers();
  388. #endif
  389. }
  390. //--------------------------------------------------------------------------------------
  391. // Window Proc
  392. //--------------------------------------------------------------------------------------
  393. LRESULT CALLBACK WndProc( HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam )
  394. {
  395. switch ( iMsg )
  396. {
  397. case WM_CLOSE:
  398. g_bActive = false;
  399. break;
  400. case WM_DESTROY:
  401. PostQuitMessage( 0 );
  402. return 0L;
  403. case WM_XCONTROLLER_INSERTED:
  404. Msg( "Port %d: Gamepad Activated\n", wParam );
  405. break;
  406. case WM_XCONTROLLER_UNPLUGGED:
  407. Msg( "Port %d: Gamepad Unplugged\n", wParam );
  408. break;
  409. case WM_XCONTROLLER_KEY:
  410. // wParam == key
  411. // HIWORD( lParam ) = port
  412. // LOWWORD( lParam ) = sample
  413. Msg( "Port %d: Button %d %s\n", HIWORD( lParam ), wParam, LOWORD( lParam ) ? "Pressed" : "Released" );
  414. switch ( wParam )
  415. {
  416. case XK_BUTTON_RTRIGGER:
  417. if ( LOWORD( lParam ) )
  418. {
  419. g_zoom++;
  420. }
  421. break;
  422. case XK_BUTTON_LTRIGGER:
  423. if ( LOWORD( lParam ) )
  424. {
  425. g_zoom--;
  426. }
  427. break;
  428. case XK_BUTTON_DOWN:
  429. if ( LOWORD( lParam ) )
  430. {
  431. g_verticalPan += 2;
  432. }
  433. break;
  434. case XK_BUTTON_UP:
  435. if ( LOWORD( lParam ) )
  436. {
  437. g_verticalPan -= 2;
  438. }
  439. break;
  440. case XK_BUTTON_LEFT:
  441. if ( LOWORD( lParam ) )
  442. {
  443. g_horizontalPan += 2;
  444. }
  445. break;
  446. case XK_BUTTON_RIGHT:
  447. if ( LOWORD( lParam ) )
  448. {
  449. g_horizontalPan -= 2;
  450. }
  451. break;
  452. case XK_STICK2_LEFT:
  453. if ( LOWORD( lParam ) )
  454. {
  455. g_yaw += 5;
  456. }
  457. break;
  458. case XK_STICK2_RIGHT:
  459. if ( LOWORD( lParam ) )
  460. {
  461. g_yaw -= 5;
  462. }
  463. break;
  464. case XK_BUTTON_A:
  465. if ( LOWORD( lParam ) )
  466. {
  467. g_sequence++;
  468. }
  469. break;
  470. case XK_BUTTON_B:
  471. if ( LOWORD( lParam ) )
  472. {
  473. g_sequence--;
  474. if ( g_sequence < 0 )
  475. g_sequence = 0;
  476. }
  477. break;
  478. case XK_BUTTON_Y:
  479. if ( LOWORD( lParam ) )
  480. {
  481. g_bWireframe ^= 1;
  482. }
  483. break;
  484. }
  485. break;
  486. case WM_KEYDOWN:
  487. switch ( wParam )
  488. {
  489. case 'O':
  490. g_zoom++;
  491. break;
  492. case 'P':
  493. g_zoom--;
  494. break;
  495. case 'W':
  496. g_verticalPan += 2;
  497. break;
  498. case 'S':
  499. g_verticalPan -= 2;
  500. break;
  501. case 'A':
  502. g_horizontalPan += 2;
  503. break;
  504. case 'D':
  505. g_horizontalPan -= 2;
  506. break;
  507. case 'N':
  508. g_sequence--;
  509. if ( g_sequence < 0 )
  510. g_sequence = 0;
  511. break;
  512. case 'M':
  513. g_sequence++;
  514. break;
  515. }
  516. break;
  517. }
  518. return DefWindowProc( hWnd, iMsg, wParam, lParam );
  519. }
  520. //--------------------------------------------------------------------------------------
  521. // CreateMainWindow
  522. //
  523. //--------------------------------------------------------------------------------------
  524. bool CTest360App::CreateMainWindow( int width, int height, bool fullscreen )
  525. {
  526. HWND hwnd;
  527. WNDCLASSEX wndclass;
  528. DWORD dwStyle, dwExStyle;
  529. int x, y, sx, sy;
  530. if ( ( hwnd = FindWindow( GetAppName(), GetAppName() ) ) != NULL )
  531. {
  532. SetForegroundWindow( hwnd );
  533. return true;
  534. }
  535. wndclass.cbSize = sizeof (wndclass);
  536. wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  537. wndclass.lpfnWndProc = ::WndProc;
  538. wndclass.cbClsExtra = 0;
  539. wndclass.cbWndExtra = 0;
  540. wndclass.hInstance = (HINSTANCE)GetAppInstance();
  541. wndclass.hIcon = 0;
  542. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  543. wndclass.hbrBackground = (HBRUSH)COLOR_GRAYTEXT;
  544. wndclass.lpszMenuName = NULL;
  545. wndclass.lpszClassName = GetAppName();
  546. wndclass.hIconSm = 0;
  547. if ( !RegisterClassEx( &wndclass ) )
  548. {
  549. Error( "Window class registration failed\n" );
  550. return false;
  551. }
  552. if ( fullscreen )
  553. {
  554. dwExStyle = WS_EX_TOPMOST;
  555. dwStyle = WS_POPUP | WS_VISIBLE;
  556. }
  557. else
  558. {
  559. dwExStyle = 0;
  560. dwStyle = WS_CAPTION | WS_SYSMENU;
  561. }
  562. x = y = 0;
  563. sx = width;
  564. sy = height;
  565. hwnd = CreateWindowEx(
  566. dwExStyle,
  567. GetAppName(), // window class name
  568. GetAppName(), // window caption
  569. dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, // window style
  570. x, // initial x position
  571. y, // initial y position
  572. sx, // initial x size
  573. sy, // initial y size
  574. NULL, // parent window handle
  575. NULL, // window menu handle
  576. (HINSTANCE)GetAppInstance(),// program instance handle
  577. NULL); // creation parameter
  578. if ( hwnd == NULL )
  579. {
  580. ChangeDisplaySettings( 0, 0 );
  581. Error( "Window creation failed\n" );
  582. return false;
  583. }
  584. m_hWnd = hwnd;
  585. return true;
  586. }
  587. //-----------------------------------------------------------------------------
  588. // Create
  589. //-----------------------------------------------------------------------------
  590. bool CTest360App::Create()
  591. {
  592. AppSystemInfo_t appSystems[] =
  593. {
  594. #if defined( USE_STUDIORENDER )
  595. { "datacache.dll", DATACACHE_INTERFACE_VERSION },
  596. { "datacache.dll", MDLCACHE_INTERFACE_VERSION },
  597. #endif
  598. #if defined( USE_MATERIALSYSTEM )
  599. { "materialsystem.dll", MATERIAL_SYSTEM_INTERFACE_VERSION },
  600. #endif
  601. #if defined( USE_STUDIORENDER )
  602. { "studiorender.dll", STUDIO_RENDER_INTERFACE_VERSION },
  603. #endif
  604. #if defined( USE_INPUTSYSTEM )
  605. { "inputsystem.dll", INPUTSYSTEM_INTERFACE_VERSION },
  606. #endif
  607. #if defined( USE_VGUI )
  608. { "vgui2.dll", VGUI_IVGUI_INTERFACE_VERSION },
  609. { "vguimatsurface.dll", VGUI_SURFACE_INTERFACE_VERSION },
  610. #endif
  611. #if defined( USE_STUDIORENDER )
  612. { "vphysics.dll", VPHYSICS_INTERFACE_VERSION },
  613. #endif
  614. { "", "" }
  615. };
  616. MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f );
  617. SpewOutputFunc( g_DefaultSpewFunc );
  618. #if defined( USE_FILESYSTEM )
  619. // Add in the cvar factory
  620. AppModule_t cvarModule = LoadModule( VStdLib_GetICVarFactory() );
  621. AddSystem( cvarModule, VENGINE_CVAR_INTERFACE_VERSION );
  622. #endif
  623. #if defined( _X360 )
  624. // vxconsole - true will block (legacy behavior)
  625. XBX_InitConsoleMonitor( false );
  626. #endif
  627. if ( !AddSystems( appSystems ) )
  628. return false;
  629. #if defined( USE_FILESYSTEM )
  630. m_pFileSystem = (IFileSystem*)FindSystem( FILESYSTEM_INTERFACE_VERSION );
  631. if ( !m_pFileSystem )
  632. {
  633. Error( "Failed to find %s\n", FILESYSTEM_INTERFACE_VERSION );
  634. return false;
  635. }
  636. #endif
  637. #if defined( USE_VPHYSICS )
  638. m_pCollision = (IPhysicsCollision*)FindSystem( VPHYSICS_COLLISION_INTERFACE_VERSION );
  639. if ( !m_pCollision )
  640. {
  641. Error( "Failed to find %s\n", VPHYSICS_COLLISION_INTERFACE_VERSION );
  642. return false;
  643. }
  644. #endif
  645. #if defined( USE_MATERIALSYSTEM )
  646. m_pMaterialSystem = (IMaterialSystem*)FindSystem( MATERIAL_SYSTEM_INTERFACE_VERSION );
  647. if ( !m_pMaterialSystem )
  648. {
  649. Error( "Failed to find %s\n", MATERIAL_SYSTEM_INTERFACE_VERSION );
  650. return false;
  651. }
  652. #if defined( _X360 )
  653. m_pFileSystem->LoadModule( "shaderapidx9.dll" );
  654. #endif
  655. m_pMaterialSystem->SetShaderAPI( "shaderapidx9.dll" );
  656. #endif
  657. return true;
  658. }
  659. //-----------------------------------------------------------------------------
  660. // PreInit
  661. //-----------------------------------------------------------------------------
  662. bool CTest360App::PreInit()
  663. {
  664. #if defined( USE_FILESYSTEM )
  665. // Add paths...
  666. if ( !SetupSearchPaths( NULL, false, true ) )
  667. {
  668. Error( "Failed to setup search paths\n" );
  669. return false;
  670. }
  671. #endif
  672. CreateInterfaceFn factory = GetFactory();
  673. ConnectTier1Libraries( &factory, 1 );
  674. ConnectTier2Libraries( &factory, 1 );
  675. ConnectTier3Libraries( &factory, 1 );
  676. // Create the main program window and our viewport
  677. int w = 640;
  678. int h = 480;
  679. if ( IsX360() )
  680. {
  681. w = GetSystemMetrics( SM_CXSCREEN );
  682. h = GetSystemMetrics( SM_CYSCREEN );
  683. }
  684. if ( !CreateMainWindow( w, h, false ) )
  685. {
  686. ChangeDisplaySettings( 0, 0 );
  687. Error( "Unable to create main window\n" );
  688. return false;
  689. }
  690. ShowWindow( m_hWnd, SW_SHOWNORMAL );
  691. UpdateWindow( m_hWnd );
  692. SetForegroundWindow( m_hWnd );
  693. SetFocus( m_hWnd );
  694. return true;
  695. }
  696. //-----------------------------------------------------------------------------
  697. // Destroy
  698. //-----------------------------------------------------------------------------
  699. void CTest360App::Destroy()
  700. {
  701. }
  702. //-----------------------------------------------------------------------------
  703. // Purpose: Setup all our VGUI info
  704. //-----------------------------------------------------------------------------
  705. #if defined( USE_VGUI )
  706. static CreateInterfaceFn s_pFactoryList[3];
  707. void *VGuiFactory( const char *pName, int *pReturnCode )
  708. {
  709. for ( int i = 0; i < ARRAYSIZE( s_pFactoryList ); ++i )
  710. {
  711. void *pInterface = s_pFactoryList[i]( pName, pReturnCode );
  712. if ( pInterface )
  713. return pInterface;
  714. }
  715. return NULL;
  716. }
  717. int CTest360App::InitializeVGUI( void )
  718. {
  719. s_pFactoryList[0] = Sys_GetFactory( m_pFileSystem->LoadModule( "filesystem_stdio" ) );
  720. s_pFactoryList[1] = Sys_GetFactory( m_pFileSystem->LoadModule( "vguimatsurface" ) );
  721. s_pFactoryList[2] = Sys_GetFactory( m_pFileSystem->LoadModule( "vgui2" ) );
  722. int factorycount = ARRAYSIZE( s_pFactoryList );
  723. if ( !vgui::VGui_InitInterfacesList( "test360", s_pFactoryList, factorycount ) )
  724. return 3;
  725. vgui::ivgui()->Connect( VGuiFactory );
  726. vgui::ivgui()->Init();
  727. vgui::ivgui()->SetSleep(false);
  728. // Init the surface
  729. vgui::Panel *pPanel = new vgui::Panel( NULL, "TopPanel" );
  730. pPanel->SetBounds( 0, 0, m_nWidth, m_nHeight );
  731. pPanel->SetPaintBackgroundEnabled( false );
  732. pPanel->SetVisible(true);
  733. vgui::surface()->SetEmbeddedPanel(pPanel->GetVPanel());
  734. // load the scheme
  735. vgui::scheme()->LoadSchemeFromFile( "resource/clientscheme.res", NULL );
  736. // localization
  737. //vgui::localize()->AddFile( "resource/vgui_%language%.txt" );
  738. // Start vgui
  739. vgui::ivgui()->Start();
  740. // add a panel
  741. m_pMainPanel = new vgui::Panel( pPanel, "MainPanel" );
  742. SETUP_PANEL( m_pMainPanel );
  743. m_pMainPanel->SetBounds( 30, 30, 200, 100 );
  744. m_pMainPanel->SetBgColor( Color(255,255,0,255) );
  745. // add text
  746. vgui::Label *pLabel = new vgui::Label( m_pMainPanel, "Text", L"" );
  747. SETUP_PANEL( pLabel );
  748. vgui::HScheme scheme = vgui::scheme()->GetScheme( "ClientScheme" );
  749. // vgui::HFont hFont = vgui::scheme()->GetIScheme(scheme)->GetFont( "BudgetLabel" );
  750. // pLabel->SetFont( hFont );
  751. pLabel->SetText( L"This is text" );
  752. pLabel->SetFgColor( Color(0,0,0,255) );
  753. return 0;
  754. }
  755. #endif
  756. //-----------------------------------------------------------------------------
  757. // Purpose: Stop VGUI
  758. //-----------------------------------------------------------------------------
  759. #if defined( USE_VGUI )
  760. void CTest360App::ShutdownVGUI( void )
  761. {
  762. delete m_pMainPanel;
  763. // Shutdown
  764. vgui::surface()->Shutdown();
  765. }
  766. #endif
  767. //-----------------------------------------------------------------------------
  768. // Main
  769. //-----------------------------------------------------------------------------
  770. int CTest360App::Main()
  771. {
  772. #if defined( USE_MATERIALSYSTEM )
  773. if ( !SetupMaterialSystem() )
  774. {
  775. return 0;
  776. }
  777. #endif
  778. #if defined( USE_STUDIORENDER )
  779. if ( !SetupStudioRender() )
  780. {
  781. return 0;
  782. }
  783. #endif
  784. #if defined( USE_VGUI )
  785. int ret = InitializeVGUI();
  786. if ( ret != 0 )
  787. return ret;
  788. #endif
  789. const char *pArgVal;
  790. const char* pModelName = "models\\alyx.mdl";
  791. if ( CommandLine()->CheckParm( "-model", &pArgVal ) )
  792. {
  793. pModelName = pArgVal;
  794. }
  795. #if defined( USE_STUDIORENDER )
  796. // the easiest model to load - no anims
  797. //const char* pModelName = "models\\items\\item_item_crate.mdl";
  798. if ( !LoadModel( pModelName ) )
  799. {
  800. return 0;
  801. }
  802. #endif
  803. MSG msg;
  804. while ( g_bActive == TRUE )
  805. {
  806. while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  807. {
  808. TranslateMessage( &msg );
  809. DispatchMessage( &msg );
  810. }
  811. #if defined( USE_INPUTSYSTEM )
  812. g_pInputSystem->PollInputState();
  813. #endif
  814. RenderScene();
  815. }
  816. #if defined( USE_VGUI )
  817. ShutdownVGUI();
  818. #endif
  819. return 0;
  820. }