基於框架(三)的例程

 基於上一篇文章的D3D框架,一個簡單的例程。

按鍵功能:

W/S  向前/向後行走

A/D  向左/向右掃視

R/F   升/降

Up/Down  俯/仰

Left/Right  偏航

N/M          滾動

 

代碼清單:

  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. // 
  3. // File: cameraApp.cpp
  4. // 
  5. // by tianzhihen
  6. //
  7. // 2008.10.25
  8. //
  9. // VC++8.0 camera 例程
  10. //         
  11. //////////////////////////////////////////////////////////////////////////////////////////////////
  12. #include "d3dUtility.h"
  13. #include "camera.h"
  14. //
  15. // 全局變量
  16. //
  17. IDirect3DDevice9* Device = 0; 
  18. const int Width  = 640;
  19. const int Height = 480;
  20. Camera TheCamera(Camera::AIRCRAFT);
  21. //
  22. // 框架函數
  23. //
  24. bool Setup()
  25. {
  26.     //
  27.     // 建立一個基本場景
  28.     //
  29.     d3d::DrawBasicScene(Device, 0.0f); 
  30.     //
  31.     // 設置投影矩陣
  32.     //
  33.     D3DXMATRIX proj;
  34.     D3DXMatrixPerspectiveFovLH(
  35.             &proj,
  36.             D3DX_PI * 0.25f, // 45 - degree
  37.             (float)Width / (float)Height,
  38.             1.0f,
  39.             1000.0f);
  40.     Device->SetTransform(D3DTS_PROJECTION, &proj);
  41.     return true;
  42. }
  43. void Cleanup()
  44. {
  45.     // 第一個參數傳0
  46.     d3d::DrawBasicScene(0, 0.0f);
  47. }
  48. bool Display(float timeDelta)
  49. {
  50.     if( Device )
  51.     {
  52.         //
  53.         // 更新場景
  54.         //
  55.         if( ::GetAsyncKeyState('W') & 0x8000f )
  56.             TheCamera.walk(4.0f * timeDelta);
  57.         if( ::GetAsyncKeyState('S') & 0x8000f )
  58.             TheCamera.walk(-4.0f * timeDelta);
  59.         if( ::GetAsyncKeyState('A') & 0x8000f )
  60.             TheCamera.strafe(-4.0f * timeDelta);
  61.         if( ::GetAsyncKeyState('D') & 0x8000f )
  62.             TheCamera.strafe(4.0f * timeDelta);
  63.         if( ::GetAsyncKeyState('R') & 0x8000f )
  64.             TheCamera.fly(4.0f * timeDelta);
  65.         if( ::GetAsyncKeyState('F') & 0x8000f )
  66.             TheCamera.fly(-4.0f * timeDelta);
  67.         if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
  68.             TheCamera.pitch(1.0f * timeDelta);
  69.         if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
  70.             TheCamera.pitch(-1.0f * timeDelta);
  71.         if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
  72.             TheCamera.yaw(-1.0f * timeDelta);
  73.             
  74.         if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
  75.             TheCamera.yaw(1.0f * timeDelta);
  76.         if( ::GetAsyncKeyState('N') & 0x8000f )
  77.             TheCamera.roll(1.0f * timeDelta);
  78.         if( ::GetAsyncKeyState('M') & 0x8000f )
  79.             TheCamera.roll(-1.0f * timeDelta);
  80.         //
  81.         // 更新視點矩陣,由camera位置所得
  82.         // 
  83.         D3DXMATRIX V;
  84.         TheCamera.getViewMatrix(&V);
  85.         Device->SetTransform(D3DTS_VIEW, &V);
  86.         //
  87.         // 繪製
  88.         //
  89.         Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
  90.         Device->BeginScene();
  91.         d3d::DrawBasicScene(Device, 1.0f);
  92.         Device->EndScene();
  93.         Device->Present(0, 0, 0, 0);
  94.     }
  95.     return true;
  96. }
  97. //
  98. // WndProc
  99. //
  100. LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  101. {
  102.     switch( msg )
  103.     {
  104.     case WM_DESTROY:
  105.         ::PostQuitMessage(0);
  106.         break;
  107.         
  108.     case WM_KEYDOWN:
  109.         if( wParam == VK_ESCAPE )
  110.             ::DestroyWindow(hwnd);
  111.         break;
  112.     }
  113.     return ::DefWindowProc(hwnd, msg, wParam, lParam);
  114. }
  115. //
  116. // WinMain
  117. //
  118. int WINAPI WinMain(HINSTANCE hinstance,
  119.                    HINSTANCE prevInstance, 
  120.                    PSTR cmdLine,
  121.                    int showCmd)
  122. {
  123.     if(!d3d::InitD3D(hinstance,
  124.         Width, Height, true, D3DDEVTYPE_HAL, &Device))
  125.     {
  126.         ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
  127.         return 0;
  128.     }
  129.         
  130.     if(!Setup())
  131.     {
  132.         ::MessageBox(0, "Setup() - FAILED", 0, 0);
  133.         return 0;
  134.     }
  135.     d3d::EnterMsgLoop( Display );
  136.     Cleanup();
  137.     Device->Release();
  138.     return 0;
  139. }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章