home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 11 / Game / Game.cpp next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  4.8 KB  |  144 lines

  1. //-----------------------------------------------------------------------------
  2. // Game.h implementation.
  3. // Refer to the Game.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Main.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // Globals
  12. //-----------------------------------------------------------------------------
  13. Game *g_game = NULL;
  14.  
  15. SceneObject *object = NULL;
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Game class constructor.
  19. //-----------------------------------------------------------------------------
  20. Game::Game() : State( STATE_GAME )
  21. {
  22.     // Store a pointer to the game state in a global variable for easy access.
  23.     g_game = this;
  24.  
  25.     // Invalidate the score board font.
  26.     m_scoreBoardFont = NULL;
  27. }
  28.  
  29. //-----------------------------------------------------------------------------
  30. // Allows the game state to preform any pre-processing construction.
  31. //-----------------------------------------------------------------------------
  32. void Game::Load()
  33. {
  34.     // Hide the mouse cursor.
  35.     ShowCursor( false );
  36.  
  37.     // Load the crosshair material.
  38.     m_crosshair = g_engine->GetMaterialManager()->Add( "Crosshair.dds.txt", "./Assets/" );
  39.  
  40.     // Create the score board font.
  41.     m_scoreBoardFont = new Font( "Arial", 14, FW_BOLD );
  42.     m_scoreBoardNames[0] = 0;
  43.     m_scoreBoardFrags[0] = 0;
  44.     m_scoreBoardDeaths[0] = 0;
  45. }
  46.  
  47. //-----------------------------------------------------------------------------
  48. // Allows the game state to preform any post-processing destruction.
  49. //-----------------------------------------------------------------------------
  50. void Game::Close()
  51. {
  52.     // Show the mouse cursor.
  53.     ShowCursor( true );
  54.  
  55.     // Terminate the session.
  56.     g_engine->GetNetwork()->Terminate();
  57.  
  58.     // Destroy the scene.
  59.     g_engine->GetSceneManager()->DestroyScene();
  60.  
  61.     // Destroy the score board font.
  62.     SAFE_DELETE( m_scoreBoardFont );
  63.  
  64.     // Destroy the crosshair material.
  65.     g_engine->GetMaterialManager()->Remove( &m_crosshair );
  66. }
  67.  
  68. //-----------------------------------------------------------------------------
  69. // Returns the view setup details for the given frame.
  70. //-----------------------------------------------------------------------------
  71. void Game::RequestViewer( ViewerSetup *viewer )
  72. {
  73.     viewer->viewClearFlags = D3DCLEAR_ZBUFFER;
  74. }
  75.  
  76. //-----------------------------------------------------------------------------
  77. // Update the game state.
  78. //-----------------------------------------------------------------------------
  79. void Game::Update( float elapsed )
  80. {
  81.     // Check if the user is holding down the tab key.
  82.     if( g_engine->GetInput()->GetKeyPress( DIK_TAB, true ) == true )
  83.     {
  84.         // Build the score board text.
  85.         sprintf( m_scoreBoardNames, "PLAYER\n" );
  86.         sprintf( m_scoreBoardFrags, "FRAGS\n" );
  87.         sprintf( m_scoreBoardDeaths, "DEATHS\n" );
  88.     }
  89.  
  90.     // Check if the user wants to exit back to the menu.
  91.     if( g_engine->GetInput()->GetKeyPress( DIK_ESCAPE ) )
  92.         g_engine->ChangeState( STATE_MENU );
  93. }
  94.  
  95. //-----------------------------------------------------------------------------
  96. // Render the game state.
  97. //-----------------------------------------------------------------------------
  98. void Game::Render()
  99. {
  100.     // Ensure the scene is loaded.
  101.     if( g_engine->GetSceneManager()->IsLoaded() == false )
  102.         return;
  103.  
  104.     // If the user is holding down the tab key, then render the score board.
  105.     if( g_engine->GetInput()->GetKeyPress( DIK_TAB, true ) == true )
  106.     {
  107.         m_scoreBoardFont->Render( m_scoreBoardNames, 20, 100, 0xFFFF7700 );
  108.         m_scoreBoardFont->Render( m_scoreBoardFrags, 180, 100, 0xFFFF7700 );
  109.         m_scoreBoardFont->Render( m_scoreBoardDeaths, 260, 100, 0xFFFF7700 );
  110.     }
  111.  
  112.     // Draw the local player's crosshair in the centre of the screen.
  113.     g_engine->GetSprite()->Begin( D3DXSPRITE_ALPHABLEND );
  114.     g_engine->GetSprite()->Draw( m_crosshair->GetTexture(), NULL, NULL, &D3DXVECTOR3( g_engine->GetDisplayMode()->Width / 2.0f - 15.0f, g_engine->GetDisplayMode()->Height / 2.0f - 15.0f, 0.0f ), 0xFFFFFFFF );
  115.     g_engine->GetSprite()->End();
  116. }
  117.  
  118. //-----------------------------------------------------------------------------
  119. // Handles the game specific network messages. Called by the network object.
  120. //-----------------------------------------------------------------------------
  121. void Game::HandleNetworkMessage( ReceivedMessage *msg )
  122. {
  123.     // Process the received messaged based on its type.
  124.     switch( msg->msgid )
  125.     {
  126.         case MSGID_CREATE_PLAYER:
  127.         {
  128.             break;
  129.         }
  130.  
  131.         case MSGID_DESTROY_PLAYER:
  132.         {
  133.             break;
  134.         }
  135.  
  136.         case MSGID_TERMINATE_SESSION:
  137.         {
  138.             // Switch to the menu state.
  139.             g_engine->ChangeState( STATE_MENU );
  140.  
  141.             break;
  142.         }
  143.     }
  144. }