Guest User

Untitled

a guest
Apr 21st, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.00 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////
  2. // File name: CApplication.cpp //
  3. // Created: 2008-03-25 14:02:06 //
  4. // //
  5. // //
  6. // Copyright © 2008 Tension Graphics AB //
  7. // //
  8. //////////////////////////////////////////////////////////////////////////////////////////
  9.  
  10. #include "CApplication.h"
  11. #include "CAnimationManager.h"
  12. #include "CCamera.h"
  13. #include "CConfigManager.h"
  14. #include "CLightManager.h"
  15. #include "CModelManager.h"
  16. #include "CWorldManager.h"
  17. #include "tgCDebugManager.h"
  18. #include "tgCFileSystem.h"
  19. #include "tgCTextureManager.h"
  20. #include "tgCTimer.h"
  21.  
  22.  
  23. // The name of the application
  24. const tgChar g_AppName[] = "Framework D3D9";
  25.  
  26.  
  27. ////////////////////////////// CommandQuitCB //////////////////////////////
  28. // //
  29. // Info: Console command to quit the application //
  30. // //
  31. //*////////////////////////////////////////////////////////////////////////
  32. static tgBool
  33. CommandQuitCB( tgSPluginConsoleCommand* pCommand )
  34. {
  35. tgCCore::GetInstance().SetQuit( TRUE );
  36.  
  37. return TRUE;
  38.  
  39. } // */ // CommandQuitCB
  40.  
  41.  
  42. ////////////////////////////// CApplication //////////////////////////////
  43. // //
  44. // Info:
  45. // //
  46. //*///////////////////////////////////////////////////////////////////////
  47. CApplication::CApplication( void )
  48. {
  49. // Clear everything
  50. m_pTimer = NULL;
  51.  
  52. m_p2DCamera = NULL;
  53. m_p3DCamera = NULL;
  54.  
  55. m_ScreenBorderSize = 0.0f;
  56.  
  57. m_LostDevice = FALSE;
  58.  
  59. } // */ // CApplication
  60.  
  61.  
  62. ////////////////////////////// ~CApplication //////////////////////////////
  63. // //
  64. // Info:
  65. // //
  66. //*////////////////////////////////////////////////////////////////////////
  67. CApplication::~CApplication( void )
  68. {
  69. // Destroy the timer
  70. if( m_pTimer )
  71. tgDelete m_pTimer;
  72.  
  73. // Destroy the 3D camera
  74. if( m_p3DCamera )
  75. tgDelete m_p3DCamera;
  76.  
  77. // Destroy the 2D camera
  78. if( m_p2DCamera )
  79. tgDelete m_p2DCamera;
  80.  
  81. // Destroy the light manager
  82. if( CLightManager::GetInstancePtr() )
  83. CLightManager::Deinitialize();
  84.  
  85. // Destroy the animation manager
  86. if( CAnimationManager::GetInstancePtr() )
  87. CAnimationManager::Deinitialize();
  88.  
  89. // Destroy the model manager
  90. if( CModelManager::GetInstancePtr() )
  91. CModelManager::Deinitialize();
  92.  
  93. // Destroy the world manager
  94. if( CWorldManager::GetInstancePtr() )
  95. CWorldManager::Deinitialize();
  96.  
  97. // Reset the message Proc's
  98. if( tgCPluginConsole::GetInstancePtr() )
  99. tgCPluginConsole::GetInstance().SetReturnProc( DefWindowProc );
  100. if( tgCPluginWindow::GetInstancePtr() )
  101. tgCPluginWindow::GetInstance().SetReturnProc( DefWindowProc );
  102.  
  103. // Destroy the skydome plugin
  104. if( tgCPluginSkydome::GetInstancePtr() )
  105. tgCPluginSkydome::Deinitialize();
  106.  
  107. // Destroy the input plugin
  108. if( tgCPluginInput::GetInstancePtr() )
  109. tgCPluginInput::Deinitialize();
  110.  
  111. // Destroy the console plugin
  112. if( tgCPluginConsole::GetInstancePtr() )
  113. tgCPluginConsole::Deinitialize();
  114.  
  115. // Destroy the device
  116. if( tgCD3D9::GetInstancePtr() )
  117. tgCD3D9::GetInstance().DestroyDevice();
  118.  
  119. // Destroy the window plugin
  120. if( tgCPluginWindow::GetInstancePtr() )
  121. tgCPluginWindow::Deinitialize();
  122.  
  123. // Destroy the core
  124. if( tgCCore::GetInstancePtr() )
  125. tgCCore::Deinitialize();
  126.  
  127. } // */ // ~CApplication
  128.  
  129.  
  130. ////////////////////////////// Create //////////////////////////////
  131. // //
  132. // Info: Create the application //
  133. // //
  134. //*/////////////////////////////////////////////////////////////////
  135. tgBool
  136. CApplication::Create( void )
  137. {
  138. // Create the core
  139. tgCCore::Initialize();
  140.  
  141. // Set the file path before trying to load any files
  142. tgCFileSystem::GetInstance().SetFilePath( "data/;" );
  143.  
  144. // Create the window plugin
  145. tgCPluginWindow::Initialize();
  146.  
  147. //////////////////////////////////////////////////////////////////////////
  148.  
  149. tgCCore& rCore = tgCCore::GetInstance();
  150. tgCD3D9& rD3D9 = tgCD3D9::GetInstance();
  151. tgCPluginWindow& rPluginWindow = tgCPluginWindow::GetInstance();
  152.  
  153. // Read settings from file
  154. tgSInt32 Width = 800;
  155. tgSInt32 Height = 600;
  156. tgSInt32 Fullscreen = 0;
  157. tgSInt32 VSync = 0;
  158. tgSInt32 RefreshRate = 60;
  159. tgSInt32 MultiSampleQuality = 0;
  160. CConfigManager::ReadSetting( "Settings.ini", "Global", "Width", NULL, &Width, NULL );
  161. CConfigManager::ReadSetting( "Settings.ini", "Global", "Height", NULL, &Height, NULL );
  162. CConfigManager::ReadSetting( "Settings.ini", "Global", "Fullscreen", NULL, &Fullscreen, NULL );
  163. CConfigManager::ReadSetting( "Settings.ini", "Global", "EnableVSync", NULL, &VSync, NULL );
  164. CConfigManager::ReadSetting( "Settings.ini", "Global", "RefreshRate", NULL, &RefreshRate, NULL );
  165. CConfigManager::ReadSetting( "Settings.ini", "Global", "MultiSampleQuality", NULL, &MultiSampleQuality, NULL );
  166.  
  167. // Set core window options and create the window
  168. rCore.SetWindowName( g_AppName );
  169. rCore.SetWindowWidth( ( tgUInt16 )Width );
  170. rCore.SetWindowHeight( ( tgUInt16 )Height );
  171. rCore.SetWindowFullscreen( Fullscreen != 0 );
  172. rCore.SetWindowVSync( VSync != 0 );
  173. rCore.SetWindowRefreshRate( ( tgUInt16 )RefreshRate ); // This is only used if VSync is on and only in fullscreen mode
  174. rCore.SetWindowHandle( rPluginWindow.Create() );
  175.  
  176. // Create the device
  177. if( !rD3D9.CreateDevice( MultiSampleQuality, tgPERFORMANCEDEVICE_NONE ) )
  178. return FALSE;
  179.  
  180. // Set the highest anisotropic filtering
  181. rCore.SetAnisotropy( rD3D9.GetMaxAnisotropy() );
  182.  
  183. // Calculate screen border size ( 95% visible screen )
  184. m_ScreenBorderSize.x = ( tgFloat )tgMathCeil( Width * 0.025f );
  185. m_ScreenBorderSize.y = ( tgFloat )tgMathCeil( Height * 0.025f );
  186.  
  187. // Set the texture path before trying to load any textures
  188. tgCTextureManager::GetInstance().SetPath( "textures/;models/textures/;worlds/textures/" );
  189.  
  190. // Set the default shader
  191. // tgCShaderManager::GetInstance().SetDefaultShader( "shader" );
  192.  
  193. // Set the default render callbacks
  194. // tgCModelManager::GetInstance().SetDefaultMeshRenderCallback( RenderCallback );
  195. // tgCWorldManager::GetInstance().SetDefaultMeshRenderCallback( RenderCallback );
  196.  
  197. //////////////////////////////////////////////////////////////////////////
  198.  
  199. // Create the console plugin
  200. tgCPluginConsole::Initialize();
  201. if( !tgCPluginConsole::GetInstance().Init() )
  202. return FALSE;
  203.  
  204. // Create the input plugin
  205. tgCPluginInput::Initialize();
  206.  
  207. // Create the skydome plugin
  208. tgCPluginSkydome::Initialize();
  209. if( !tgCPluginSkydome::GetInstance().Init() )
  210. return FALSE;
  211.  
  212. //////////////////////////////////////////////////////////////////////////
  213.  
  214. tgCPluginConsole& rPluginConsole = tgCPluginConsole::GetInstance();
  215. tgCPluginInput& rPluginInput = tgCPluginInput::GetInstance();
  216.  
  217. // Set the message Proc's
  218. rPluginWindow.SetReturnProc( rPluginConsole.GetProc() );
  219. rPluginConsole.SetReturnProc( rPluginInput.GetProc() );
  220.  
  221. //////////////////////////////////////////////////////////////////////////
  222.  
  223. // Create the world manager
  224. CWorldManager::Initialize();
  225.  
  226. // Create the model manager
  227. CModelManager::Initialize();
  228.  
  229. // Create the animation manager
  230. CAnimationManager::Initialize();
  231.  
  232. // Create the light manager
  233. CLightManager::Initialize();
  234.  
  235. //////////////////////////////////////////////////////////////////////////
  236.  
  237. // Create the 2d camera
  238. m_p2DCamera = tgNew CCamera();
  239. m_p2DCamera->Create( "2D Camera", 0, 0, Width, Height, 0.0f, 0.0f, -1.0f, 1.0f, tgCAMERA_PROJECTION_2D_ORTHO );
  240. m_p2DCamera->SetActiveInput( FALSE );
  241. m_p2DCamera->Update();
  242. m_p2DCamera->GetCamera()->ClearBuffers();
  243.  
  244. // Create the 3d camera
  245. m_p3DCamera = tgNew CCamera();
  246. m_p3DCamera->Create( "3D Camera", 0, 0, Width, Height, 45.0f, ( tgFloat )Width / ( tgFloat )Height, 0.1f, 1024.0f, tgCAMERA_PROJECTION_PERSPECTIVE );
  247. m_p3DCamera->SetPosition( tgCV3D( 0.0f, 0.0f, 0.0f ) );
  248. m_p3DCamera->SetRotation( tgCV3D( 0.0f, 0.0f, 0.0f ) );
  249. m_p3DCamera->SetMovementSpeed( 3.0f );
  250. m_p3DCamera->SetSensitivity( 0.2f );
  251. m_p3DCamera->SetInvertX( TRUE );
  252. m_p3DCamera->Update();
  253. m_p3DCamera->GetCamera()->ClearBuffers();
  254.  
  255. //////////////////////////////////////////////////////////////////////////
  256.  
  257. // Create timer
  258. m_pTimer = tgNew tgCTimer( TRUE );
  259.  
  260. //////////////////////////////////////////////////////////////////////////
  261.  
  262. // Create console commands to quit the app
  263. rPluginConsole.CreateCommand( "exit", "", TRUE, CommandQuitCB );
  264. rPluginConsole.CreateCommand( "quit", "", TRUE, CommandQuitCB );
  265.  
  266. //////////////////////////////////////////////////////////////////////////
  267.  
  268. // Run this twice to get rid of the first frame mouse movement
  269. tgCPluginInput::GetInstance().Update();
  270. tgCPluginInput::GetInstance().Update();
  271.  
  272. // Run this twice to get rid of the first frame high delta time
  273. m_pTimer->Update();
  274. m_pTimer->Update();
  275.  
  276. // Get reference to world manager instance
  277. CWorldManager& rWorldManager = CWorldManager::GetInstance();
  278.  
  279. // Load the world and set it to active
  280. const tgCWorld* pWorld = rWorldManager.LoadWorld( "worlds/world.tfw", "world" );
  281. rWorldManager.SetActiveWorld( pWorld );
  282.  
  283. return TRUE;
  284.  
  285. } // */ // Create
  286.  
  287.  
  288. ////////////////////////////// Run //////////////////////////////
  289. // //
  290. // Info: Application will loop in this function until end //
  291. // //
  292. //*//////////////////////////////////////////////////////////////
  293. void
  294. CApplication::Run( void )
  295. {
  296. // Keep looping until we should quit
  297. while( !tgCCore::GetInstance().GetQuit() )
  298. {
  299. // Check for memory errors
  300. tgMemoryCheckCorruption();
  301.  
  302. // Update
  303. Update();
  304.  
  305. // Render
  306. Render();
  307. }
  308.  
  309. } // */ // Run
  310.  
  311.  
  312. ///////////////////////////// RenderDebugObjects3D /////////////////////////////
  313. // //
  314. // Info: Render all 3d debug objects in scene //
  315. // //
  316. //*/////////////////////////////////////////////////////////////////////////////
  317. void
  318. CApplication::RenderDebugObjects3D( void )
  319. {
  320. tgCDebugManager& rDebugManager = tgCDebugManager::GetInstance();
  321.  
  322. // Render all 3d debug stuff
  323. rDebugManager.Render3D();
  324.  
  325. } // */ // RenderDebugObjects3D
  326.  
  327.  
  328. ///////////////////////////// RenderDebugObjects2D /////////////////////////////
  329. // //
  330. // Info: Render all 2d debug objects in scene //
  331. // //
  332. //*/////////////////////////////////////////////////////////////////////////////
  333. void
  334. CApplication::RenderDebugObjects2D( void )
  335. {
  336. tgCDebugManager& rDebugManager = tgCDebugManager::GetInstance();
  337.  
  338. //#ifndef FINAL
  339.  
  340. // Calculate a smooth average FPS
  341. static tgUInt32 NumFrames[ 8 ];
  342. static tgDouble FPS;
  343. static tgDouble Time;
  344. static tgUInt32 Counter;
  345.  
  346. NumFrames[ Counter ]++;
  347.  
  348. while( Time < m_pTimer->GetLifeTime() )
  349. {
  350. Time = Time + ( 1.0 / 8.0 );
  351. FPS = NumFrames[ 0 ] + NumFrames[ 1 ] + NumFrames[ 2 ] + NumFrames[ 3 ] + NumFrames[ 4 ] + NumFrames[ 5 ] + NumFrames[ 6 ] + NumFrames[ 7 ];
  352. Counter = ( Counter + 1 ) & 7;
  353. NumFrames[ Counter ] = 0;
  354. }
  355.  
  356. // Write the FPS on screen
  357. rDebugManager.AddText2D( m_ScreenBorderSize, tgCColor::White, "%4d FPS", ( tgUInt32 )FPS );
  358.  
  359. //#endif // FINAL
  360.  
  361. #ifdef DEBUG
  362.  
  363. // Write out the camera position
  364. const tgCV3D& rCamPos = m_p3DCamera->GetCamera()->GetTransform().GetMatrixWorld().Pos;
  365. rDebugManager.AddText2D( tgCColor::White, "Cam pos: %.2f %.2f %.2f", rCamPos.x, rCamPos.y, rCamPos.z );
  366.  
  367. #endif // DEBUG
  368.  
  369. // Render all 2d debug stuff
  370. rDebugManager.Render2D();
  371.  
  372. } // */ // RenderDebugObjects2D
  373.  
  374.  
  375. ////////////////////////////// ResetDevice //////////////////////////////
  376. // //
  377. // Info:
  378. // //
  379. //*//////////////////////////////////////////////////////////////////////
  380. void
  381. CApplication::ResetDevice( void )
  382. {
  383. tgCD3D9& rD3D9 = tgCD3D9::GetInstance();
  384.  
  385. // Release everything, reset the device and then restore everything again
  386. rD3D9.OnLostDevice();
  387. rD3D9.ResetDevice();
  388. rD3D9.OnResetDevice();
  389.  
  390. m_p3DCamera->GetCamera()->SetRenderTarget( rD3D9.GetDefaultRenderTarget() );
  391. m_p3DCamera->GetCamera()->SetDepthStencil( rD3D9.GetDefaultDepthStencil() );
  392.  
  393. m_p2DCamera->GetCamera()->SetRenderTarget( rD3D9.GetDefaultRenderTarget() );
  394. m_p2DCamera->GetCamera()->SetDepthStencil( rD3D9.GetDefaultDepthStencil() );
  395.  
  396. } // */ // ResetDevice
  397.  
  398.  
  399. //////////////////////////////////////////////////////////////////////////
  400.  
  401.  
  402. ////////////////////////////// Update //////////////////////////////
  403. // //
  404. // Info: Update //
  405. // //
  406. //*/////////////////////////////////////////////////////////////////
  407. void
  408. CApplication::Update( void )
  409. {
  410. tgCCore& rCore = tgCCore::GetInstance();
  411. tgCPluginInput& rPluginInput = tgCPluginInput::GetInstance();
  412.  
  413. // Sleep if window is not in focus
  414. if( !rCore.GetHasFocus() )
  415. Sleep( 50 );
  416.  
  417. // Update the input plugin
  418. rPluginInput.Update();
  419.  
  420. // Handle window messages
  421. rCore.HandleMessages();
  422.  
  423. // Update the timer
  424. m_pTimer->Update();
  425.  
  426. // Get the deltatime
  427. const tgFloat DeltaTime = ( tgFloat )m_pTimer->GetDeltaTime();
  428.  
  429. // Update the console plugin
  430. tgCPluginConsole::GetInstance().Update( DeltaTime );
  431.  
  432. // Update the skydome plugin
  433. tgCPluginSkydome::GetInstance().Update( DeltaTime );
  434.  
  435. // Update the light manager
  436. CLightManager::GetInstance().Update();
  437.  
  438. // Update the model manager
  439. CModelManager::GetInstance().Update();
  440.  
  441. // Update the 2d camera
  442. m_p2DCamera->Update();
  443.  
  444. // Handle the 3d camera input
  445. m_p3DCamera->HandleInput();
  446.  
  447. // Update the 3d camera
  448. m_p3DCamera->Update();
  449.  
  450. // Update the world manager
  451. CWorldManager::GetInstance().Update();
  452.  
  453. // Toggle fullscreen
  454. if( rPluginInput.KeyPressed( VK_F1 ) )
  455. {
  456. tgCD3D9::GetInstance().ToggleFullscreen();
  457. tgCPluginWindow::GetInstance().ToggleFullscreen();
  458. m_LostDevice = TRUE;
  459. }
  460.  
  461. // Take a screenshot
  462. if( rPluginInput.KeyPressed( VK_F11 ) )
  463. tgCD3D9::GetInstance().Screenshot( g_AppName );
  464.  
  465. // Exit application
  466. if( rPluginInput.KeyPressed( VK_ESCAPE ) )
  467. rCore.SetQuit( TRUE );
  468.  
  469. } // */ // Update
  470.  
  471.  
  472. ////////////////////////////// Render //////////////////////////////
  473. // //
  474. // Info: Render //
  475. // //
  476. //*/////////////////////////////////////////////////////////////////
  477. void
  478. CApplication::Render( void )
  479. {
  480. // Show the rendered buffer on screen
  481. if( FAILED( tgCD3D9::GetInstance().GetDevice()->Present( NULL, NULL, NULL, NULL ) ) )
  482. m_LostDevice = TRUE;
  483.  
  484. // Check if the device has been lost for some reason
  485. CheckLostDevice();
  486.  
  487. //////////////////////////////////////////////////////////////////////////
  488.  
  489. tgCCameraManager& rCameraManager = tgCCameraManager::GetInstance();
  490. tgCCamera& r3DCamera = *m_p3DCamera->GetCamera();
  491. tgCCamera& r2DCamera = *m_p2DCamera->GetCamera();
  492.  
  493. // Set 3d camera as the current one
  494. rCameraManager.SetCurrentCamera( r3DCamera );
  495.  
  496. // Begin the 3d camera render
  497. if( r3DCamera.BeginRender() )
  498. {
  499. // Render the skydome
  500. tgCPluginSkydome::GetInstance().Render();
  501.  
  502. // Render whole world ( all models within the world will also be rendered )
  503. CWorldManager::GetInstance().Render();
  504.  
  505. // Render non-world models
  506. CModelManager::GetInstance().Render();
  507.  
  508. // Render 3d debug objects
  509. RenderDebugObjects3D();
  510.  
  511. // End the camera update
  512. r3DCamera.EndRender();
  513. }
  514.  
  515. // Set 2d camera as the current one
  516. rCameraManager.SetCurrentCamera( r2DCamera );
  517.  
  518. // Begin the 2d camera render
  519. if( r2DCamera.BeginRender( tgCAMERA_CLEAR_DEPTH ) )
  520. {
  521. // Render 2d debug objects
  522. RenderDebugObjects2D();
  523.  
  524. // Render the console
  525. tgCPluginConsole::GetInstance().Render();
  526.  
  527. // End the 2d camera render
  528. r2DCamera.EndRender();
  529. }
  530.  
  531. // Set 3d camera as the current one
  532. rCameraManager.SetCurrentCamera( r3DCamera );
  533.  
  534. } // */ // Render
  535.  
  536.  
  537. ////////////////////////// CheckLostDevice /////////////////////////
  538. // //
  539. // Info: CheckLostDevice //
  540. // //
  541. //*/////////////////////////////////////////////////////////////////
  542. void
  543. CApplication::CheckLostDevice( void )
  544. {
  545. // Check if the device is lost
  546. if( ( tgCD3D9::GetInstance().GetDevice()->TestCooperativeLevel() ) == D3DERR_DEVICELOST )
  547. {
  548. // If it is lost, sleep to let everything catch up
  549. m_LostDevice = TRUE;
  550. Sleep( 100 );
  551. return;
  552. }
  553.  
  554. // If device is lost and not reset
  555. if( m_LostDevice )
  556. {
  557. if( ( tgCD3D9::GetInstance().GetDevice()->TestCooperativeLevel() ) == D3DERR_DEVICENOTRESET )
  558. {
  559. // Reset everything
  560. ResetDevice();
  561.  
  562. // The device is not lost anymore and have been reset
  563. m_LostDevice = FALSE;
  564. }
  565. }
  566.  
  567. } // */ // CheckLostDevice
Add Comment
Please, Sign In to add comment