Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////
- // File name: CApplication.cpp //
- // Created: 2008-03-25 14:02:06 //
- // //
- // //
- // Copyright © 2008 Tension Graphics AB //
- // //
- //////////////////////////////////////////////////////////////////////////////////////////
- #include "CApplication.h"
- #include "CAnimationManager.h"
- #include "CCamera.h"
- #include "CConfigManager.h"
- #include "CLightManager.h"
- #include "CModelManager.h"
- #include "CWorldManager.h"
- #include "tgCDebugManager.h"
- #include "tgCFileSystem.h"
- #include "tgCTextureManager.h"
- #include "tgCTimer.h"
- // The name of the application
- const tgChar g_AppName[] = "Framework D3D9";
- ////////////////////////////// CommandQuitCB //////////////////////////////
- // //
- // Info: Console command to quit the application //
- // //
- //*////////////////////////////////////////////////////////////////////////
- static tgBool
- CommandQuitCB( tgSPluginConsoleCommand* pCommand )
- {
- tgCCore::GetInstance().SetQuit( TRUE );
- return TRUE;
- } // */ // CommandQuitCB
- ////////////////////////////// CApplication //////////////////////////////
- // //
- // Info:
- // //
- //*///////////////////////////////////////////////////////////////////////
- CApplication::CApplication( void )
- {
- // Clear everything
- m_pTimer = NULL;
- m_p2DCamera = NULL;
- m_p3DCamera = NULL;
- m_ScreenBorderSize = 0.0f;
- m_LostDevice = FALSE;
- } // */ // CApplication
- ////////////////////////////// ~CApplication //////////////////////////////
- // //
- // Info:
- // //
- //*////////////////////////////////////////////////////////////////////////
- CApplication::~CApplication( void )
- {
- // Destroy the timer
- if( m_pTimer )
- tgDelete m_pTimer;
- // Destroy the 3D camera
- if( m_p3DCamera )
- tgDelete m_p3DCamera;
- // Destroy the 2D camera
- if( m_p2DCamera )
- tgDelete m_p2DCamera;
- // Destroy the light manager
- if( CLightManager::GetInstancePtr() )
- CLightManager::Deinitialize();
- // Destroy the animation manager
- if( CAnimationManager::GetInstancePtr() )
- CAnimationManager::Deinitialize();
- // Destroy the model manager
- if( CModelManager::GetInstancePtr() )
- CModelManager::Deinitialize();
- // Destroy the world manager
- if( CWorldManager::GetInstancePtr() )
- CWorldManager::Deinitialize();
- // Reset the message Proc's
- if( tgCPluginConsole::GetInstancePtr() )
- tgCPluginConsole::GetInstance().SetReturnProc( DefWindowProc );
- if( tgCPluginWindow::GetInstancePtr() )
- tgCPluginWindow::GetInstance().SetReturnProc( DefWindowProc );
- // Destroy the skydome plugin
- if( tgCPluginSkydome::GetInstancePtr() )
- tgCPluginSkydome::Deinitialize();
- // Destroy the input plugin
- if( tgCPluginInput::GetInstancePtr() )
- tgCPluginInput::Deinitialize();
- // Destroy the console plugin
- if( tgCPluginConsole::GetInstancePtr() )
- tgCPluginConsole::Deinitialize();
- // Destroy the device
- if( tgCD3D9::GetInstancePtr() )
- tgCD3D9::GetInstance().DestroyDevice();
- // Destroy the window plugin
- if( tgCPluginWindow::GetInstancePtr() )
- tgCPluginWindow::Deinitialize();
- // Destroy the core
- if( tgCCore::GetInstancePtr() )
- tgCCore::Deinitialize();
- } // */ // ~CApplication
- ////////////////////////////// Create //////////////////////////////
- // //
- // Info: Create the application //
- // //
- //*/////////////////////////////////////////////////////////////////
- tgBool
- CApplication::Create( void )
- {
- // Create the core
- tgCCore::Initialize();
- // Set the file path before trying to load any files
- tgCFileSystem::GetInstance().SetFilePath( "data/;" );
- // Create the window plugin
- tgCPluginWindow::Initialize();
- //////////////////////////////////////////////////////////////////////////
- tgCCore& rCore = tgCCore::GetInstance();
- tgCD3D9& rD3D9 = tgCD3D9::GetInstance();
- tgCPluginWindow& rPluginWindow = tgCPluginWindow::GetInstance();
- // Read settings from file
- tgSInt32 Width = 800;
- tgSInt32 Height = 600;
- tgSInt32 Fullscreen = 0;
- tgSInt32 VSync = 0;
- tgSInt32 RefreshRate = 60;
- tgSInt32 MultiSampleQuality = 0;
- CConfigManager::ReadSetting( "Settings.ini", "Global", "Width", NULL, &Width, NULL );
- CConfigManager::ReadSetting( "Settings.ini", "Global", "Height", NULL, &Height, NULL );
- CConfigManager::ReadSetting( "Settings.ini", "Global", "Fullscreen", NULL, &Fullscreen, NULL );
- CConfigManager::ReadSetting( "Settings.ini", "Global", "EnableVSync", NULL, &VSync, NULL );
- CConfigManager::ReadSetting( "Settings.ini", "Global", "RefreshRate", NULL, &RefreshRate, NULL );
- CConfigManager::ReadSetting( "Settings.ini", "Global", "MultiSampleQuality", NULL, &MultiSampleQuality, NULL );
- // Set core window options and create the window
- rCore.SetWindowName( g_AppName );
- rCore.SetWindowWidth( ( tgUInt16 )Width );
- rCore.SetWindowHeight( ( tgUInt16 )Height );
- rCore.SetWindowFullscreen( Fullscreen != 0 );
- rCore.SetWindowVSync( VSync != 0 );
- rCore.SetWindowRefreshRate( ( tgUInt16 )RefreshRate ); // This is only used if VSync is on and only in fullscreen mode
- rCore.SetWindowHandle( rPluginWindow.Create() );
- // Create the device
- if( !rD3D9.CreateDevice( MultiSampleQuality, tgPERFORMANCEDEVICE_NONE ) )
- return FALSE;
- // Set the highest anisotropic filtering
- rCore.SetAnisotropy( rD3D9.GetMaxAnisotropy() );
- // Calculate screen border size ( 95% visible screen )
- m_ScreenBorderSize.x = ( tgFloat )tgMathCeil( Width * 0.025f );
- m_ScreenBorderSize.y = ( tgFloat )tgMathCeil( Height * 0.025f );
- // Set the texture path before trying to load any textures
- tgCTextureManager::GetInstance().SetPath( "textures/;models/textures/;worlds/textures/" );
- // Set the default shader
- // tgCShaderManager::GetInstance().SetDefaultShader( "shader" );
- // Set the default render callbacks
- // tgCModelManager::GetInstance().SetDefaultMeshRenderCallback( RenderCallback );
- // tgCWorldManager::GetInstance().SetDefaultMeshRenderCallback( RenderCallback );
- //////////////////////////////////////////////////////////////////////////
- // Create the console plugin
- tgCPluginConsole::Initialize();
- if( !tgCPluginConsole::GetInstance().Init() )
- return FALSE;
- // Create the input plugin
- tgCPluginInput::Initialize();
- // Create the skydome plugin
- tgCPluginSkydome::Initialize();
- if( !tgCPluginSkydome::GetInstance().Init() )
- return FALSE;
- //////////////////////////////////////////////////////////////////////////
- tgCPluginConsole& rPluginConsole = tgCPluginConsole::GetInstance();
- tgCPluginInput& rPluginInput = tgCPluginInput::GetInstance();
- // Set the message Proc's
- rPluginWindow.SetReturnProc( rPluginConsole.GetProc() );
- rPluginConsole.SetReturnProc( rPluginInput.GetProc() );
- //////////////////////////////////////////////////////////////////////////
- // Create the world manager
- CWorldManager::Initialize();
- // Create the model manager
- CModelManager::Initialize();
- // Create the animation manager
- CAnimationManager::Initialize();
- // Create the light manager
- CLightManager::Initialize();
- //////////////////////////////////////////////////////////////////////////
- // Create the 2d camera
- m_p2DCamera = tgNew CCamera();
- m_p2DCamera->Create( "2D Camera", 0, 0, Width, Height, 0.0f, 0.0f, -1.0f, 1.0f, tgCAMERA_PROJECTION_2D_ORTHO );
- m_p2DCamera->SetActiveInput( FALSE );
- m_p2DCamera->Update();
- m_p2DCamera->GetCamera()->ClearBuffers();
- // Create the 3d camera
- m_p3DCamera = tgNew CCamera();
- m_p3DCamera->Create( "3D Camera", 0, 0, Width, Height, 45.0f, ( tgFloat )Width / ( tgFloat )Height, 0.1f, 1024.0f, tgCAMERA_PROJECTION_PERSPECTIVE );
- m_p3DCamera->SetPosition( tgCV3D( 0.0f, 0.0f, 0.0f ) );
- m_p3DCamera->SetRotation( tgCV3D( 0.0f, 0.0f, 0.0f ) );
- m_p3DCamera->SetMovementSpeed( 3.0f );
- m_p3DCamera->SetSensitivity( 0.2f );
- m_p3DCamera->SetInvertX( TRUE );
- m_p3DCamera->Update();
- m_p3DCamera->GetCamera()->ClearBuffers();
- //////////////////////////////////////////////////////////////////////////
- // Create timer
- m_pTimer = tgNew tgCTimer( TRUE );
- //////////////////////////////////////////////////////////////////////////
- // Create console commands to quit the app
- rPluginConsole.CreateCommand( "exit", "", TRUE, CommandQuitCB );
- rPluginConsole.CreateCommand( "quit", "", TRUE, CommandQuitCB );
- //////////////////////////////////////////////////////////////////////////
- // Run this twice to get rid of the first frame mouse movement
- tgCPluginInput::GetInstance().Update();
- tgCPluginInput::GetInstance().Update();
- // Run this twice to get rid of the first frame high delta time
- m_pTimer->Update();
- m_pTimer->Update();
- // Get reference to world manager instance
- CWorldManager& rWorldManager = CWorldManager::GetInstance();
- // Load the world and set it to active
- const tgCWorld* pWorld = rWorldManager.LoadWorld( "worlds/world.tfw", "world" );
- rWorldManager.SetActiveWorld( pWorld );
- return TRUE;
- } // */ // Create
- ////////////////////////////// Run //////////////////////////////
- // //
- // Info: Application will loop in this function until end //
- // //
- //*//////////////////////////////////////////////////////////////
- void
- CApplication::Run( void )
- {
- // Keep looping until we should quit
- while( !tgCCore::GetInstance().GetQuit() )
- {
- // Check for memory errors
- tgMemoryCheckCorruption();
- // Update
- Update();
- // Render
- Render();
- }
- } // */ // Run
- ///////////////////////////// RenderDebugObjects3D /////////////////////////////
- // //
- // Info: Render all 3d debug objects in scene //
- // //
- //*/////////////////////////////////////////////////////////////////////////////
- void
- CApplication::RenderDebugObjects3D( void )
- {
- tgCDebugManager& rDebugManager = tgCDebugManager::GetInstance();
- // Render all 3d debug stuff
- rDebugManager.Render3D();
- } // */ // RenderDebugObjects3D
- ///////////////////////////// RenderDebugObjects2D /////////////////////////////
- // //
- // Info: Render all 2d debug objects in scene //
- // //
- //*/////////////////////////////////////////////////////////////////////////////
- void
- CApplication::RenderDebugObjects2D( void )
- {
- tgCDebugManager& rDebugManager = tgCDebugManager::GetInstance();
- //#ifndef FINAL
- // Calculate a smooth average FPS
- static tgUInt32 NumFrames[ 8 ];
- static tgDouble FPS;
- static tgDouble Time;
- static tgUInt32 Counter;
- NumFrames[ Counter ]++;
- while( Time < m_pTimer->GetLifeTime() )
- {
- Time = Time + ( 1.0 / 8.0 );
- FPS = NumFrames[ 0 ] + NumFrames[ 1 ] + NumFrames[ 2 ] + NumFrames[ 3 ] + NumFrames[ 4 ] + NumFrames[ 5 ] + NumFrames[ 6 ] + NumFrames[ 7 ];
- Counter = ( Counter + 1 ) & 7;
- NumFrames[ Counter ] = 0;
- }
- // Write the FPS on screen
- rDebugManager.AddText2D( m_ScreenBorderSize, tgCColor::White, "%4d FPS", ( tgUInt32 )FPS );
- //#endif // FINAL
- #ifdef DEBUG
- // Write out the camera position
- const tgCV3D& rCamPos = m_p3DCamera->GetCamera()->GetTransform().GetMatrixWorld().Pos;
- rDebugManager.AddText2D( tgCColor::White, "Cam pos: %.2f %.2f %.2f", rCamPos.x, rCamPos.y, rCamPos.z );
- #endif // DEBUG
- // Render all 2d debug stuff
- rDebugManager.Render2D();
- } // */ // RenderDebugObjects2D
- ////////////////////////////// ResetDevice //////////////////////////////
- // //
- // Info:
- // //
- //*//////////////////////////////////////////////////////////////////////
- void
- CApplication::ResetDevice( void )
- {
- tgCD3D9& rD3D9 = tgCD3D9::GetInstance();
- // Release everything, reset the device and then restore everything again
- rD3D9.OnLostDevice();
- rD3D9.ResetDevice();
- rD3D9.OnResetDevice();
- m_p3DCamera->GetCamera()->SetRenderTarget( rD3D9.GetDefaultRenderTarget() );
- m_p3DCamera->GetCamera()->SetDepthStencil( rD3D9.GetDefaultDepthStencil() );
- m_p2DCamera->GetCamera()->SetRenderTarget( rD3D9.GetDefaultRenderTarget() );
- m_p2DCamera->GetCamera()->SetDepthStencil( rD3D9.GetDefaultDepthStencil() );
- } // */ // ResetDevice
- //////////////////////////////////////////////////////////////////////////
- ////////////////////////////// Update //////////////////////////////
- // //
- // Info: Update //
- // //
- //*/////////////////////////////////////////////////////////////////
- void
- CApplication::Update( void )
- {
- tgCCore& rCore = tgCCore::GetInstance();
- tgCPluginInput& rPluginInput = tgCPluginInput::GetInstance();
- // Sleep if window is not in focus
- if( !rCore.GetHasFocus() )
- Sleep( 50 );
- // Update the input plugin
- rPluginInput.Update();
- // Handle window messages
- rCore.HandleMessages();
- // Update the timer
- m_pTimer->Update();
- // Get the deltatime
- const tgFloat DeltaTime = ( tgFloat )m_pTimer->GetDeltaTime();
- // Update the console plugin
- tgCPluginConsole::GetInstance().Update( DeltaTime );
- // Update the skydome plugin
- tgCPluginSkydome::GetInstance().Update( DeltaTime );
- // Update the light manager
- CLightManager::GetInstance().Update();
- // Update the model manager
- CModelManager::GetInstance().Update();
- // Update the 2d camera
- m_p2DCamera->Update();
- // Handle the 3d camera input
- m_p3DCamera->HandleInput();
- // Update the 3d camera
- m_p3DCamera->Update();
- // Update the world manager
- CWorldManager::GetInstance().Update();
- // Toggle fullscreen
- if( rPluginInput.KeyPressed( VK_F1 ) )
- {
- tgCD3D9::GetInstance().ToggleFullscreen();
- tgCPluginWindow::GetInstance().ToggleFullscreen();
- m_LostDevice = TRUE;
- }
- // Take a screenshot
- if( rPluginInput.KeyPressed( VK_F11 ) )
- tgCD3D9::GetInstance().Screenshot( g_AppName );
- // Exit application
- if( rPluginInput.KeyPressed( VK_ESCAPE ) )
- rCore.SetQuit( TRUE );
- } // */ // Update
- ////////////////////////////// Render //////////////////////////////
- // //
- // Info: Render //
- // //
- //*/////////////////////////////////////////////////////////////////
- void
- CApplication::Render( void )
- {
- // Show the rendered buffer on screen
- if( FAILED( tgCD3D9::GetInstance().GetDevice()->Present( NULL, NULL, NULL, NULL ) ) )
- m_LostDevice = TRUE;
- // Check if the device has been lost for some reason
- CheckLostDevice();
- //////////////////////////////////////////////////////////////////////////
- tgCCameraManager& rCameraManager = tgCCameraManager::GetInstance();
- tgCCamera& r3DCamera = *m_p3DCamera->GetCamera();
- tgCCamera& r2DCamera = *m_p2DCamera->GetCamera();
- // Set 3d camera as the current one
- rCameraManager.SetCurrentCamera( r3DCamera );
- // Begin the 3d camera render
- if( r3DCamera.BeginRender() )
- {
- // Render the skydome
- tgCPluginSkydome::GetInstance().Render();
- // Render whole world ( all models within the world will also be rendered )
- CWorldManager::GetInstance().Render();
- // Render non-world models
- CModelManager::GetInstance().Render();
- // Render 3d debug objects
- RenderDebugObjects3D();
- // End the camera update
- r3DCamera.EndRender();
- }
- // Set 2d camera as the current one
- rCameraManager.SetCurrentCamera( r2DCamera );
- // Begin the 2d camera render
- if( r2DCamera.BeginRender( tgCAMERA_CLEAR_DEPTH ) )
- {
- // Render 2d debug objects
- RenderDebugObjects2D();
- // Render the console
- tgCPluginConsole::GetInstance().Render();
- // End the 2d camera render
- r2DCamera.EndRender();
- }
- // Set 3d camera as the current one
- rCameraManager.SetCurrentCamera( r3DCamera );
- } // */ // Render
- ////////////////////////// CheckLostDevice /////////////////////////
- // //
- // Info: CheckLostDevice //
- // //
- //*/////////////////////////////////////////////////////////////////
- void
- CApplication::CheckLostDevice( void )
- {
- // Check if the device is lost
- if( ( tgCD3D9::GetInstance().GetDevice()->TestCooperativeLevel() ) == D3DERR_DEVICELOST )
- {
- // If it is lost, sleep to let everything catch up
- m_LostDevice = TRUE;
- Sleep( 100 );
- return;
- }
- // If device is lost and not reset
- if( m_LostDevice )
- {
- if( ( tgCD3D9::GetInstance().GetDevice()->TestCooperativeLevel() ) == D3DERR_DEVICENOTRESET )
- {
- // Reset everything
- ResetDevice();
- // The device is not lost anymore and have been reset
- m_LostDevice = FALSE;
- }
- }
- } // */ // CheckLostDevice
Add Comment
Please, Sign In to add comment