home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 9 / Test / Main.cpp next >
Encoding:
C/C++ Source or Header  |  2004-07-01  |  4.5 KB  |  120 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.         // Create an animated object, and play one of its animations.
  23.         m_character = new AnimatedObject( "Marine.x", "./Assets/" );
  24.         m_character->SetTranslation( -100.0f, 0.0f, 0.0f );
  25.         m_character->SetRotation( 0.0f, 3.14f, 0.0f );
  26.         m_character->PlayAnimation( 1, 0.0f );
  27.  
  28.         // Create a scene object that will be used as a camera.
  29.         m_viewer = new SceneObject;
  30.         m_viewer->SetTranslation( 0.0f, 0.0f, -400.0f );
  31.         m_viewer->SetFriction( 5.0f );
  32.  
  33.         // Create a test spawner object.
  34.         m_spawner = new SpawnerObject( "Spawner.txt", "./Assets/" );
  35.         m_spawner->SetTranslation( 100.0f, -50.0f, 0.0f );
  36.     };
  37.  
  38.     //-------------------------------------------------------------------------
  39.     // Allows the test state to preform any post-processing destruction.
  40.     //-------------------------------------------------------------------------
  41.     virtual void Close()
  42.     {
  43.         SAFE_DELETE( m_character );
  44.         SAFE_DELETE( m_viewer );
  45.         SAFE_DELETE( m_spawner );
  46.     };
  47.  
  48.     //-------------------------------------------------------------------------
  49.     // Returns the view setup details for the given frame.
  50.     //-------------------------------------------------------------------------
  51.     virtual void RequestViewer( ViewerSetup *viewer )
  52.     {
  53.         viewer->viewer = m_viewer;
  54.         viewer->viewClearFlags = D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER;
  55.     }
  56.  
  57.     //-------------------------------------------------------------------------
  58.     // Updates the test state.
  59.     //-------------------------------------------------------------------------
  60.     virtual void Update( float elapsed )
  61.     {
  62.         // Allow the camera (scene object) to be moved around.
  63.         if( g_engine->GetInput()->GetKeyPress( DIK_W, true ) == true )
  64.             m_viewer->Drive( 1000.0f * elapsed, false );
  65.         if( g_engine->GetInput()->GetKeyPress( DIK_S, true ) == true )
  66.             m_viewer->Drive( -1000.0f * elapsed, false );
  67.         if( g_engine->GetInput()->GetKeyPress( DIK_A, true ) == true )
  68.             m_viewer->Strafe( -1000.0f * elapsed, false );
  69.         if( g_engine->GetInput()->GetKeyPress( DIK_D, true ) == true )
  70.             m_viewer->Strafe( 1000.0f * elapsed, false );
  71.  
  72.         // Adjust the rotation of the camera based on the mouse movement.
  73.         m_viewer->AddRotation( (float)g_engine->GetInput()->GetDeltaY() * elapsed, (float)g_engine->GetInput()->GetDeltaX() * elapsed, 0.0f );
  74.  
  75.         m_character->Update( elapsed );
  76.         m_viewer->Update( elapsed );
  77.         m_spawner->Update( elapsed );
  78.     };
  79.  
  80.     //-------------------------------------------------------------------------
  81.     // Renders the test state.
  82.     //-------------------------------------------------------------------------
  83.     virtual void Render()
  84.     {
  85.         m_character->Render();
  86.         m_spawner->Render();
  87.     };
  88.  
  89. private:
  90.     AnimatedObject *m_character; // An animated character object.
  91.     SceneObject *m_viewer; // The viewer scene object is used as a camera.
  92.     SpawnerObject *m_spawner; // A test spawner object.
  93. };
  94.  
  95. //-----------------------------------------------------------------------------
  96. // Application specific state setup.
  97. //-----------------------------------------------------------------------------
  98. void StateSetup()
  99. {
  100.     g_engine->AddState( new TestState, true );
  101. }
  102.  
  103. //-----------------------------------------------------------------------------
  104. // Entry point for the application.
  105. //-----------------------------------------------------------------------------
  106. int WINAPI WinMain( HINSTANCE instance, HINSTANCE prev, LPSTR cmdLine, int cmdShow )
  107. {
  108.     // Create the engine setup structure.
  109.     EngineSetup setup;
  110.     setup.instance = instance;
  111.     setup.name = "Object Test";
  112.     setup.scale = 0.01f;
  113.     setup.StateSetup = StateSetup;
  114.  
  115.     // Create the engine (using the setup structure), then run it.
  116.     new Engine( &setup );
  117.     g_engine->Run();
  118.  
  119.     return true;
  120. }