topical media & game development

talk show tell print

graphic-directx-game-17-Camera-Demo-CameraDemo.cpp / cpp



  //=============================================================================
  // CameraDemo.cpp by Frank Luna (C) 2005 All Rights Reserved.
  //
  // Demonstrates how to create a more flexible camera.
  //
  // Controls: Use mouse to look and 'W', 'S', 'A', and 'D' keys to move.
  //=============================================================================
  
  include <d3dApp.h>
  include <DirectInput.h>
  include <crtdbg.h>
  include <GfxStats.h>
  include <list>
  include <Terrain.h>
  include <Camera.h>
  
  class CameraDemo : public D3DApp
  {
  public:
          CameraDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);
          ~CameraDemo();
  
          bool checkDeviceCaps();
          void onLostDevice();
          void onResetDevice();
          void updateScene(float dt);
          void drawScene();
  
  private:
          GfxStats* mGfxStats;
          Terrain*  mTerrain;
  };
  
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
                                     PSTR cmdLine, int showCmd)
  {
          // Enable run-time memory check for debug builds.
          #if defined(DEBUG) | defined(_DEBUG)
                  _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
          #endif
  
          // Construct camera before application, since the application uses the camera.
          Camera camera;
          gCamera = &camera;
  
          CameraDemo app(hInstance, "Camera Demo", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
          gd3dApp = &app;
  
          DirectInput di(DISCL_NONEXCLUSIVE|DISCL_FOREGROUND, DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
          gDInput = &di;
  
      return gd3dApp->run();
  }
  
  CameraDemo::CameraDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
  : D3DApp(hInstance, winCaption, devType, requestedVP)
  {
          if(!checkDeviceCaps())
          {
                  MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
                  PostQuitMessage(0);
          }
  
          InitAllVertexDeclarations();
  
          mGfxStats = new GfxStats();
          
          mTerrain = new Terrain(257, 257, 10.0f, 10.0f, 
                  "heightmap17_257.raw",  
                  "grass.dds",
                  "dirt.dds",
                  "stone.dds",
                  "blend_hm17.dds",
                  3.0f, 0.0f);
  
          mTerrain->setDirToSunW(D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  
          // Initialize camera.
          gCamera->pos() = D3DXVECTOR3(-200.0f, 300.0f, 0.0f);
  
          mGfxStats->addVertices(mTerrain->getNumVertices());
          mGfxStats->addTriangles(mTerrain->getNumTriangles());
  
          onResetDevice();
  }
  
  CameraDemo::~CameraDemo()
  {
          delete mGfxStats;
          delete mTerrain;
  
          DestroyAllVertexDeclarations();
  }
  
  bool CameraDemo::checkDeviceCaps()
  {
          D3DCAPS9 caps;
          HR(gd3dDevice->GetDeviceCaps(&caps));
  
          // Check for vertex shader version 2.0 support.
          if( caps.VertexShaderVersion < D3DVS_VERSION(2, 0) )
                  return false;
  
          // Check for pixel shader version 2.0 support.
          if( caps.PixelShaderVersion < D3DPS_VERSION(2, 0) )
                  return false;
  
          return true;
  }
  
  void CameraDemo::onLostDevice()
  {
          mGfxStats->onLostDevice();
          mTerrain->onLostDevice();
  }
  
  void CameraDemo::onResetDevice()
  {
          mGfxStats->onResetDevice();
          mTerrain->onResetDevice();
  
          // The aspect ratio depends on the backbuffer dimensions, which can 
          // possibly change after a reset.  So rebuild the projection matrix.
          float w = (float)md3dPP.BackBufferWidth;
          float h = (float)md3dPP.BackBufferHeight;
          gCamera->setLens(D3DX_PI * 0.25f, w/h, 1.0f, 5000.0f);
  }
  
  void CameraDemo::updateScene(float dt)
  {
          mGfxStats->update(dt);
  
          gDInput->poll();
  
          gCamera->update(dt);
  }
  
  void CameraDemo::drawScene()
  {
          // Clear the backbuffer and depth buffer.
          HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0));
  
          HR(gd3dDevice->BeginScene());
  
          mTerrain->draw();
          
          mGfxStats->display();
  
          HR(gd3dDevice->EndScene());
  
          // Present the backbuffer.
          HR(gd3dDevice->Present(0, 0, 0, 0));
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.