home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 7 / Test / Main.cpp next >
Encoding:
C/C++ Source or Header  |  2004-06-03  |  5.2 KB  |  150 lines

  1. //-----------------------------------------------------------------------------
  2. // System Includes
  3. //-----------------------------------------------------------------------------
  4. #include <windows.h>
  5.  
  6. //-----------------------------------------------------------------------------
  7. // Engine Includes
  8. //-----------------------------------------------------------------------------
  9. #include "..\Engine\Engine.h"
  10.  
  11. //-----------------------------------------------------------------------------
  12. // Test State Class
  13. //-----------------------------------------------------------------------------
  14. class TestState : public State
  15. {
  16. public:
  17.     //-------------------------------------------------------------------------
  18.     // Allows the test state to preform any pre-processing construction.
  19.     //-------------------------------------------------------------------------
  20.     virtual void Load()
  21.     {
  22.         m_font = new Font;
  23.         m_text[0] = 0;
  24.         m_connected = false;
  25.     };
  26.  
  27.     //-------------------------------------------------------------------------
  28.     // Allows the test state to preform any post-processing destruction.
  29.     //-------------------------------------------------------------------------
  30.     virtual void Close()
  31.     {
  32.         SAFE_DELETE( m_font );
  33.     };
  34.  
  35.     //-------------------------------------------------------------------------
  36.     // Returns the view setup details for the given frame.
  37.     //-------------------------------------------------------------------------
  38.     virtual void RequestViewer( ViewerSetup *viewer )
  39.     {
  40.         viewer->viewClearFlags = D3DCLEAR_TARGET;
  41.     }
  42.  
  43.     //-------------------------------------------------------------------------
  44.     // Updates the state.
  45.     //-------------------------------------------------------------------------
  46.     virtual void Update( float elapsed )
  47.     {
  48.         // Check if the user wants to host a session.
  49.         if( g_engine->GetInput()->GetKeyPress( DIK_H ) == true )
  50.             m_connected = g_engine->GetNetwork()->Host( "host player", "session", 8 );
  51.  
  52.         // Check if the user wants to enumerate the sessions on the network.
  53.         if( g_engine->GetInput()->GetKeyPress( DIK_E ) == true )
  54.             g_engine->GetNetwork()->EnumerateSessions();
  55.  
  56.         // Check if the user wants to join the first session.
  57.         if( g_engine->GetInput()->GetKeyPress( DIK_1 ) == true )
  58.             m_connected = g_engine->GetNetwork()->Join( "join player", 0 );
  59.  
  60.         // Check if the user wants to join the second session.
  61.         if( g_engine->GetInput()->GetKeyPress( DIK_2 ) == true )
  62.             m_connected = g_engine->GetNetwork()->Join( "join player", 1 );
  63.  
  64.         // Check if the user wants to join the third session.
  65.         if( g_engine->GetInput()->GetKeyPress( DIK_2 ) == true )
  66.             m_connected = g_engine->GetNetwork()->Join( "join player", 2 );
  67.  
  68.         // Check if the user wants to quit.
  69.         if( g_engine->GetInput()->GetKeyPress( DIK_Q ) == true )
  70.             PostQuitMessage( 0 );
  71.  
  72.         // Create the menu text.
  73.         strcpy( m_text, "\n\n\nH - Host Session\n\nE - Enumerate Sessions" );
  74.  
  75.         // Go through the list of sessions found on the local network.
  76.         char count = 0;
  77.         char name[MAX_PATH];
  78.         SessionInfo *session = g_engine->GetNetwork()->GetNextSession( true );
  79.         while( session != NULL )
  80.         {
  81.             // Add the session count.
  82.             sprintf( name, "\n   %d - ", ++count );
  83.             strcat( m_text, name );
  84.  
  85.             // Add the session name.
  86.             wcstombs( name, session->description.pwszSessionName, MAX_PATH );
  87.             strcat( m_text, name );
  88.  
  89.             // Go to the next session.
  90.             session = g_engine->GetNetwork()->GetNextSession();
  91.         }
  92.  
  93.         // Add the quit option to the menu text.
  94.         strcat( m_text, "\n\nQ - Quit" );
  95.  
  96.         // Add the connection status text.
  97.         if( m_connected == true )
  98.         {
  99.             if( g_engine->GetNetwork()->IsHost() == true )
  100.                 strcat( m_text, "\n\nCONNECTED - HOST" );
  101.             else
  102.                 strcat( m_text, "\n\nCONNECTED - CLIENT" );
  103.         }
  104.         else
  105.             strcat( m_text, "\n\nNOT CONNECTED" );
  106.     };
  107.  
  108.     //-------------------------------------------------------------------------
  109.     // Renders the state.
  110.     //-------------------------------------------------------------------------
  111.     virtual void Render()
  112.     {
  113.         m_font->Render( m_text, 0.0f, 0.0f );
  114.     };
  115.  
  116. private:
  117.     Font *m_font; // Font to display the menu options.
  118.     char m_text[MAX_PATH]; // Text for the menu options.
  119.     bool m_connected; // Indicates if the network object is connected.
  120. };
  121.  
  122. //-----------------------------------------------------------------------------
  123. // Application specific state setup.
  124. //-----------------------------------------------------------------------------
  125. void StateSetup()
  126. {
  127.     g_engine->AddState( new TestState, true );
  128. }
  129.  
  130. //-----------------------------------------------------------------------------
  131. // Entry point for the application.
  132. //-----------------------------------------------------------------------------
  133. int WINAPI WinMain( HINSTANCE instance, HINSTANCE prev, LPSTR cmdLine, int cmdShow )
  134. {
  135.     // A unique id for the test so it can be identified on a network.
  136.     GUID guid = { 0x9ca7996f, 0xebb4, 0x4e9b, { 0xba, 0x6b, 0xe6, 0x4b, 0x45, 0x7f, 0x59, 0x98 } };
  137.  
  138.     // Create the engine setup structure.
  139.     EngineSetup setup;
  140.     setup.instance = instance;
  141.     setup.guid = guid;
  142.     setup.name = "Network Test";
  143.     setup.StateSetup = StateSetup;
  144.  
  145.     // Create the engine (using the setup structure), then run it.
  146.     new Engine( &setup );
  147.     g_engine->Run();
  148.  
  149.     return true;
  150. }