Game.cpp
上传用户:whgydz
上传日期:2007-01-12
资源大小:2259k
文件大小:12k
源码类别:

其他书籍

开发平台:

HTML/CSS

  1. // Game.cpp: implementation of the CGame class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "Game.h"
  5. //////////////////////////////////////////////////////////////////////
  6. // Construction/Destruction
  7. //////////////////////////////////////////////////////////////////////
  8. CGame::CGame()
  9. {
  10. m_fEnableLogging = false;
  11. m_pD3D = NULL;
  12. m_pD3DDevice = NULL;
  13. m_dwFrames = 0;
  14. m_dwStartTime = 0;
  15. m_dwEndTime = 0;
  16. m_pCube1 = NULL;
  17. m_pCube2 = NULL;
  18. m_pCube3 = NULL;
  19. m_pCube4 = NULL;
  20. m_pCube5 = NULL;
  21. }
  22. CGame::~CGame()
  23. {
  24. //Game finished, so record time
  25. m_dwEndTime = timeGetTime();
  26. DWORD dwDuration = (m_dwEndTime - m_dwStartTime) / 1000;
  27. //Log stats
  28. WriteToLog("Statistics:");
  29. WriteToLog("tStart Time (ms): %d", m_dwStartTime);
  30. WriteToLog("tEnd Time (ms): %d", m_dwEndTime);
  31. WriteToLog("tDuration (s): %d", dwDuration);
  32. WriteToLog("tTotal Frame Count: %d", m_dwFrames);
  33. WriteToLog("tAverage FPS: %d", (m_dwFrames / dwDuration));
  34. //Clean up objects/interfaces
  35. SafeDelete(m_pCube1);
  36. SafeDelete(m_pCube2);
  37. SafeDelete(m_pCube3);
  38. SafeDelete(m_pCube4);
  39. SafeDelete(m_pCube5);
  40. SafeRelease(m_pD3DDevice);
  41. SafeRelease(m_pD3D);
  42. }
  43. bool CGame::Initialise(HWND hWnd, UINT nWidth, UINT nHeight)
  44. {
  45. if(SUCCEEDED(InitialiseD3D(hWnd, nWidth, nHeight)))
  46. {
  47. return InitialiseGame();
  48. }
  49. else
  50. {
  51. return false;
  52. }
  53. return true;
  54. }
  55. bool CGame::InitialiseGame()
  56. {
  57. //Setup games objects here
  58. m_pCube1 = new CCuboid(m_pD3DDevice);
  59. m_pCube1->SetPosition(-27.0, 0.0, 0.0);
  60. m_pCube2 = new CCuboid(m_pD3DDevice);
  61. m_pCube2->SetPosition(-9.0, 0.0, 0.0);
  62. m_pCube3 = new CCuboid(m_pD3DDevice);
  63. m_pCube3->SetPosition(9.0, 0.0, 0.0);
  64. m_pCube4 = new CCuboid(m_pD3DDevice);
  65. m_pCube4->SetPosition(27.0, 0.0, 0.0);
  66. m_pCube5 = new CCuboid(m_pD3DDevice);
  67. m_pCube5->SetPosition(0.0, 15.0, 0.0);
  68. return true;
  69. }
  70. D3DFORMAT CGame::CheckDisplayMode(UINT nWidth, UINT nHeight, UINT nDepth)
  71. {
  72. UINT x;
  73. D3DDISPLAYMODE d3ddm;
  74. for(x = 0; x < m_pD3D->GetAdapterModeCount(0); x++)
  75. {
  76. m_pD3D->EnumAdapterModes(0, x, &d3ddm);
  77. if(d3ddm.Width == nWidth)
  78. {
  79. if(d3ddm.Height == nHeight)
  80. {
  81. if((d3ddm.Format == D3DFMT_R5G6B5) || (d3ddm.Format == D3DFMT_X1R5G5B5) || (d3ddm.Format == D3DFMT_X4R4G4B4))
  82. {
  83. if(nDepth == 16)
  84. {
  85. return d3ddm.Format;
  86. }
  87. }
  88. else if((d3ddm.Format == D3DFMT_R8G8B8) || (d3ddm.Format == D3DFMT_X8R8G8B8))
  89. {
  90. if(nDepth == 32)
  91. {
  92. return d3ddm.Format;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. return D3DFMT_UNKNOWN;
  99. }
  100. HRESULT CGame::InitialiseD3D(HWND hWnd, UINT nWidth, UINT nHeight)
  101. {
  102. WriteToLog("InitialiseD3D Started...");
  103.     //First of all, create the main D3D object. If it is created successfully we 
  104.     //should get a pointer to an IDirect3D8 interface.
  105.     m_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
  106.     if(m_pD3D == NULL)
  107.     {
  108. WriteToLog("tUnable to create DirectX8 interface.");
  109.         return E_FAIL;
  110.     }
  111.     //Get the current display mode
  112.     D3DDISPLAYMODE d3ddm;
  113. d3ddm.Format = CheckDisplayMode(nWidth, nHeight, 32);
  114. if(d3ddm.Format != D3DFMT_UNKNOWN)
  115. {
  116. //Width x Height x 32bit has been selected
  117. d3ddm.Width = nWidth;
  118. d3ddm.Height = nHeight;
  119. WriteToLog("t%d x %d x 32bit back buffer format selected. Format = %d.", nWidth, nHeight, d3ddm.Format);
  120. }
  121. else
  122. {
  123. d3ddm.Format = CheckDisplayMode(nWidth, nHeight, 16);
  124. if(d3ddm.Format != D3DFMT_UNKNOWN)
  125. {
  126.             //Width x Height x 16bit has been selected
  127. d3ddm.Width = nWidth;
  128. d3ddm.Height = nHeight;
  129. WriteToLog("t%d x %d x 16bit back buffer format selected. Format = %d.", nWidth, nHeight, d3ddm.Format);
  130. }
  131.         else
  132. {
  133. WriteToLog("tUnable to select back buffer format for %d x %d.", nWidth, nHeight);
  134.             return E_FAIL;
  135.         }
  136. }
  137.     //Create a structure to hold the settings for our device
  138.     D3DPRESENT_PARAMETERS d3dpp; 
  139.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  140. d3dpp.Windowed = FALSE;
  141.     d3dpp.BackBufferCount = 1;
  142.     d3dpp.BackBufferFormat = d3ddm.Format;
  143.     d3dpp.BackBufferWidth = d3ddm.Width;
  144.     d3dpp.BackBufferHeight = d3ddm.Height;
  145.     d3dpp.hDeviceWindow = hWnd;
  146.     d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
  147. d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  148.     d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  149. //Select the best depth buffer, select 32, 24 or 16 bit
  150.     if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D32) == D3D_OK)
  151. {
  152.         d3dpp.AutoDepthStencilFormat = D3DFMT_D32;
  153.         d3dpp.EnableAutoDepthStencil = TRUE;
  154. WriteToLog("t32bit depth buffer selected");
  155.     }
  156.     else if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24X8) == D3D_OK)
  157.     {
  158. d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;
  159.         d3dpp.EnableAutoDepthStencil = TRUE;
  160. WriteToLog("t24bit depth buffer selected");
  161. }
  162.     else if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16) == D3D_OK)
  163.     {
  164. d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  165.         d3dpp.EnableAutoDepthStencil = TRUE;
  166. WriteToLog("t16bit depth buffer selected");
  167. }
  168.     else
  169. {
  170.         d3dpp.EnableAutoDepthStencil = FALSE;
  171. WriteToLog("tUnable to select depth buffer.");
  172. }
  173.     //Create a Direct3D device.
  174.     if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
  175.                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice)))
  176.     {
  177. WriteToLog("tUnable to create device.");
  178.         return E_FAIL;
  179.     }
  180.     
  181. //Turn on back face culling. This is becuase we want to hide the back of our polygons
  182.     m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
  183. //Turn off lighting becuase we are specifying that our vertices have colour
  184.     m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
  185. //Turn on Depth Buffering
  186.     m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
  187. WriteToLog("InitialiseD3D Finished OK");
  188.     return S_OK;
  189. }
  190. LPDIRECT3DDEVICE8 CGame::GetDevice()
  191. {
  192. return m_pD3DDevice;
  193. }
  194. void CGame::GameLoop()
  195. {
  196.     //Enter the game loop
  197.     MSG msg; 
  198.     BOOL fMessage;
  199.     PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
  200. //Game started, so record time
  201. m_dwStartTime = timeGetTime();
  202.     while(msg.message != WM_QUIT)
  203.     {
  204.         fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
  205.         if(fMessage)
  206.         {
  207.             //Process message
  208.             TranslateMessage(&msg);
  209.             DispatchMessage(&msg);
  210.         }
  211.         else
  212.         {
  213.             //No message to process, so render the current scene
  214.             Render();
  215.         }
  216.     }
  217. }
  218. void CGame::Render()
  219. {
  220.     D3DXMATRIX matRotationX, matRotationY, matRotationZ, matRotationUser1;
  221. D3DXMATRIX matMoveRight27, matMoveLeft27, matMoveRight9, matMoveLeft9, matMoveDown15, matMoveUp15;
  222. D3DXMATRIX matTransformation2, matTransformation3, matTransformation4, matTransformation5;
  223.     D3DXMATRIX matScaleUp1p5;
  224. if(m_pD3DDevice == NULL)
  225.     {
  226.         return;
  227.     }
  228.     //Clear the back buffer and depth buffer
  229.     m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  230.     
  231.     //Begin the scene
  232.     m_pD3DDevice->BeginScene();
  233.     
  234. //Setup camera and perspective
  235. SetupCamera();
  236.     
  237. //Create the rotation transformation matrices around the x, y and z axis
  238. D3DXMatrixRotationX(&matRotationX, timeGetTime()/400.0f);
  239. D3DXMatrixRotationY(&matRotationY, timeGetTime()/400.0f);
  240. D3DXMatrixRotationZ(&matRotationZ, timeGetTime()/400.0f);
  241. //Create the rotation transformation matrices around our user defined axis
  242. D3DXMatrixRotationAxis(&matRotationUser1, &D3DXVECTOR3(1.0f, 1.0f, 0.0f), timeGetTime()/400.0f);
  243. //Create the translation (move) matrices
  244. D3DXMatrixTranslation(&matMoveRight27, 27.0, 0.0, 0.0);
  245. D3DXMatrixTranslation(&matMoveLeft27, -27.0, 0.0, 0.0);
  246. D3DXMatrixTranslation(&matMoveRight9, 9.0, 0.0, 0.0);
  247. D3DXMatrixTranslation(&matMoveLeft9, -9.0, 0.0, 0.0);
  248. D3DXMatrixTranslation(&matMoveDown15, 0.0, -15.0, 0.0);
  249. D3DXMatrixTranslation(&matMoveUp15, 0.0, 15.0, 0.0);
  250. //Create a scale transformation
  251. D3DXMatrixScaling(&matScaleUp1p5, 1.5, 1.5, 1.5);
  252. //Combine the matrices to form 4 transformation matrices
  253. D3DXMatrixMultiply(&matTransformation2, &matMoveRight9, &matRotationY);
  254. D3DXMatrixMultiply(&matTransformation2, &matTransformation2, &matMoveLeft9);
  255. D3DXMatrixMultiply(&matTransformation3, &matMoveLeft9, &matRotationZ);
  256. D3DXMatrixMultiply(&matTransformation3, &matTransformation3, &matMoveRight9);
  257. D3DXMatrixMultiply(&matTransformation4, &matMoveLeft27, &matRotationUser1);
  258. D3DXMatrixMultiply(&matTransformation4, &matTransformation4, &matMoveRight27);
  259. D3DXMatrixMultiply(&matTransformation5, &matMoveDown15, &matRotationY);
  260. D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matRotationX);
  261. D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matRotationZ);
  262. D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matMoveUp15);
  263. D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matScaleUp1p5);
  264.     //Apply the transformations and render our objects
  265. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matRotationX);
  266.     m_pCube1->Render();
  267. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation2);
  268. m_pCube2->Render();
  269. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation3);
  270. m_pCube3->Render();
  271. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation4);
  272. m_pCube4->Render();
  273. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation5);
  274. m_pCube5->Render();
  275.     //End the scene
  276.     m_pD3DDevice->EndScene();
  277.     
  278.     //Filp the back and front buffers so that whatever has been rendered on the back buffer
  279.     //will now be visible on screen (front buffer).
  280.     m_pD3DDevice->Present(NULL, NULL, NULL, NULL);
  281. //Count Frames
  282. m_dwFrames++;
  283. }
  284. void CGame::SetupCamera()
  285. {
  286. //Here we will setup the camera.
  287. //The camera has three settings: "Camera Position", "Look at Position" and "Up Direction"
  288. //We have set the following:
  289. //Camera Position: (0, 0, -100)
  290. //Look at Position: (0, 0, 0)
  291. //Up direction: Y-Axis.
  292.     D3DXMATRIX matView;
  293.     D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f,-100.0f), //Camera Position
  294.                                  &D3DXVECTOR3(0.0f, 0.0f, 0.0f), //Look At Position
  295.                                  &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction
  296.     m_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
  297. //Here we specify the field of view, aspect ration and near and far clipping planes.
  298.     D3DXMATRIX matProj;
  299.     D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
  300.     m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
  301. }
  302. void CGame::WriteToLog(char *lpszText, ...)
  303. {
  304. if(m_fEnableLogging)
  305. {
  306. va_list argList;
  307. FILE *pFile;
  308. //Initialize variable argument list
  309. va_start(argList, lpszText);
  310. //Open the log file for appending
  311. pFile = fopen("log.txt", "a+");
  312. //Write the text and a newline
  313. vfprintf(pFile, lpszText, argList);
  314. putc('n', pFile);
  315. //Close the file
  316. fclose(pFile);
  317. va_end(argList);
  318. }
  319. }
  320. void CGame::EnableLogging()
  321. {
  322. m_fEnableLogging = true;
  323. FILE* pFile;
  324. //Clear the file contents
  325. pFile = fopen("log.txt", "wb");
  326. //Close it up and return success
  327. fclose(pFile);
  328. }