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

其他书籍

开发平台:

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_pD3D = NULL;
  11. m_pD3DDevice = NULL;
  12. m_pDirectInput = NULL;
  13. m_pKeyboard = NULL;
  14. m_pMouse = NULL;
  15. m_dwFrames = 0;
  16. m_dwStartTime = 0;
  17. m_dwEndTime = 0;
  18. m_dwTotalPolygons = 0;
  19. m_fQuit = false;
  20. m_rRotate = 2.0f;
  21. m_rAngle = 0.0f;
  22. m_rScale = 1.0f;
  23. m_nMouseLeft = 0;
  24. m_nMouseRight = 0;
  25. m_nMouseX = 0;
  26. m_nMouseY = 0;
  27. m_pFont = NULL;
  28. m_pSphere = NULL;
  29. }
  30. CGame::~CGame()
  31. {
  32. //Game finished, so destroy game objects
  33. LogInfo("<br>Finish Game:");
  34. //Clean up our objects and interfaces
  35. CleanUpGame();
  36. CleanUpDirectInput();
  37. CleanUpDirect3D();
  38. //Game finished, so save statistics to log
  39. DWORD dwDuration = (m_dwEndTime - m_dwStartTime) / 1000;
  40. if((dwDuration != 0)&&(m_dwFrames != 0))
  41. {
  42. //Log stats
  43. LogInfo("<br>Statistics:");
  44. LogInfo("<li>Start Time (ms): %d", m_dwStartTime);
  45. LogInfo("<li>End Time (ms): %d", m_dwEndTime);
  46. LogInfo("<li>Duration (s): %d", dwDuration);
  47. LogInfo("<li>Total Frame Count: %d", m_dwFrames);
  48. LogInfo("<li>Average FPS: %d", (m_dwFrames / dwDuration));
  49. LogInfo("<li>Total Polygons: %d", m_dwTotalPolygons);
  50. LogInfo("<li>Average Polygons per Frame: %d", (m_dwTotalPolygons / m_dwFrames));
  51. }
  52. else
  53. {
  54. LogInfo("<br>No statistics to report");
  55. }
  56. StopLogging();
  57. }
  58. void CGame::CleanUpGame()
  59. {
  60. SafeDelete(m_pFont);
  61. SafeDelete(m_pSphere);
  62. }
  63. void CGame::CleanUpDirectInput()
  64. {
  65. if(m_pKeyboard)
  66. {
  67. m_pKeyboard->Unacquire(); 
  68. }
  69. if(m_pMouse)
  70. {
  71. m_pMouse->Unacquire();
  72. }
  73. SafeRelease(m_pMouse);
  74. SafeRelease(m_pKeyboard);
  75. SafeRelease(m_pDirectInput);
  76. }
  77. void CGame::CleanUpDirect3D()
  78. {
  79. SafeRelease(m_pD3DDevice);
  80. SafeRelease(m_pD3D);
  81. }
  82. bool CGame::Initialise(HWND hWnd, HINSTANCE hInst, UINT nWidth, UINT nHeight)
  83. {
  84. //Initialise Direct3D
  85. if(!InitialiseDirect3D(hWnd, nWidth, nHeight))
  86. {
  87. return false;
  88. }
  89. //Initialise DirectInput
  90. if(!InitialiseDirectInput(hWnd, hInst))
  91. {
  92. return false;
  93. }
  94. //Initialise Lighting
  95. if(!InitialiseLights()) 
  96. {
  97. return false;
  98. }
  99. //Initialise Game Objects
  100. if(!InitialiseGame())
  101. {
  102. return false;
  103. }
  104. return true;
  105. }
  106. bool CGame::InitialiseGame()
  107. {
  108. LogInfo("<br>Initialise Game:");
  109. //Setup games objects here
  110. m_pSphere = new CSphere(m_pD3DDevice); 
  111. m_pSphere->SetTexture("Earth.bmp");
  112. //Setup fonts here
  113. m_pFont = new CFont(m_pD3DDevice, "Verdana", 12, false, false, false);
  114. //Setup panels for 2D
  115. return true;
  116. }
  117. D3DFORMAT CGame::CheckDisplayMode(UINT nWidth, UINT nHeight, UINT nDepth)
  118. {
  119. UINT x;
  120. D3DDISPLAYMODE d3ddm;
  121. for(x = 0; x < m_pD3D->GetAdapterModeCount(0); x++)
  122. {
  123. m_pD3D->EnumAdapterModes(0, x, &d3ddm);
  124. if(d3ddm.Width == nWidth)
  125. {
  126. if(d3ddm.Height == nHeight)
  127. {
  128. if((d3ddm.Format == D3DFMT_R5G6B5) || (d3ddm.Format == D3DFMT_X1R5G5B5) || (d3ddm.Format == D3DFMT_X4R4G4B4))
  129. {
  130. if(nDepth == 16)
  131. {
  132. return d3ddm.Format;
  133. }
  134. }
  135. else if((d3ddm.Format == D3DFMT_R8G8B8) || (d3ddm.Format == D3DFMT_X8R8G8B8))
  136. {
  137. if(nDepth == 32)
  138. {
  139. return d3ddm.Format;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. return D3DFMT_UNKNOWN;
  146. }
  147. bool CGame::InitialiseDirectInput(HWND hWnd, HINSTANCE hInst)
  148. {
  149. LogInfo("<br>Initialise DirectInput:");
  150. //Create the DirectInput object
  151. if(FAILED(DirectInput8Create(hInst, DIRECTINPUT_VERSION, 
  152.   IID_IDirectInput8, (void**)&m_pDirectInput, NULL))) 
  153. LogError("<li>Unable to create DirectInput interface.");
  154. return false;
  155. }
  156. else
  157. {
  158. LogInfo("<li>DirectInput interface created OK");
  159. }
  160. //KEYBOARD =======================================================================
  161. //Create the keyboard device object
  162. if(FAILED(m_pDirectInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL))) 
  163. CleanUpDirectInput();
  164. LogError("<li>Unable to create DirectInput keyboard device interface.");
  165. return false; 
  166. }
  167. else
  168. {
  169. LogInfo("<li>DirectInput keyboard device interface created OK.");
  170. }
  171. //Set the data format for the keyboard
  172. if(FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
  173. CleanUpDirectInput();
  174. LogError("<li>Unable to set the keyboard data format.");
  175. return false; 
  176. }
  177. else
  178. {
  179. LogInfo("<li>Set the keyboard data format OK.");
  180. }
  181. //Set the cooperative level for the keyboard
  182. if(FAILED(m_pKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
  183. CleanUpDirectInput();
  184. LogError("<li>Unable to set the keyboard cooperative level.");
  185. return false;
  186. }
  187. else
  188. {
  189. LogInfo("<li>Set the keyboard cooperative level OK.");
  190. }
  191. //Acquire the keyboard
  192. if(m_pKeyboard)
  193. {
  194. m_pKeyboard->Acquire(); 
  195. }
  196. //MOUSE =======================================================================
  197. //Create the mouse device object
  198. if(FAILED(m_pDirectInput->CreateDevice(GUID_SysMouse, &m_pMouse, NULL)))
  199. CleanUpDirectInput();
  200. LogError("<li>Unable to create DirectInput mouse device interface.");
  201. return false; 
  202. }
  203. else
  204. {
  205. LogInfo("<li>DirectInput mouse device interface created OK.");
  206. }
  207. //Set the data format for the mouse
  208. if(FAILED(m_pMouse->SetDataFormat(&c_dfDIMouse)))
  209. CleanUpDirectInput();
  210. LogError("<li>Unable to set the mouse data format.");
  211. return false; 
  212. }
  213. else
  214. {
  215. LogInfo("<li>Set the mouse data format OK.");
  216. }
  217. //Set the cooperative level for the mouse
  218. if(FAILED(m_pMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
  219. CleanUpDirectInput();
  220. LogError("<li>Unable to set the mouse cooperative level.");
  221. return false;
  222. }
  223. else
  224. {
  225. LogInfo("<li>Set the mouse cooperative level OK.");
  226. }
  227. //Acquire the mouse
  228. if(m_pMouse)
  229. {
  230. m_pMouse->Acquire(); 
  231. }
  232. return true;
  233. }
  234. bool CGame::InitialiseDirect3D(HWND hWnd, UINT nWidth, UINT nHeight)
  235. {
  236. LogInfo("<br>Initialise Direct3D:");
  237.     //First of all, create the main D3D object. If it is created successfully we 
  238.     //should get a pointer to an IDirect3D8 interface.
  239.     m_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
  240.     if(m_pD3D == NULL)
  241.     {
  242. LogError("<li>Unable to create DirectX8 interface.");
  243.         return false;
  244.     }
  245.     //Get the current display mode
  246.     D3DDISPLAYMODE d3ddm;
  247. d3ddm.Format = CheckDisplayMode(nWidth, nHeight, 32);
  248. if(d3ddm.Format != D3DFMT_UNKNOWN)
  249. {
  250. //Width x Height x 32bit has been selected
  251. d3ddm.Width = nWidth;
  252. d3ddm.Height = nHeight;
  253. LogInfo("<li>%d x %d x 32bit back buffer format selected. Format = %d.", nWidth, nHeight, d3ddm.Format);
  254. }
  255. else
  256. {
  257. d3ddm.Format = CheckDisplayMode(nWidth, nHeight, 16);
  258. if(d3ddm.Format != D3DFMT_UNKNOWN)
  259. {
  260.             //Width x Height x 16bit has been selected
  261. d3ddm.Width = nWidth;
  262. d3ddm.Height = nHeight;
  263. LogInfo("<li>%d x %d x 16bit back buffer format selected. Format = %d.", nWidth, nHeight, d3ddm.Format);
  264. }
  265.         else
  266. {
  267. LogError("<li>Unable to select back buffer format for %d x %d.", nWidth, nHeight);
  268.             return false;
  269.         }
  270. }
  271.     //Create a structure to hold the settings for our device
  272.     D3DPRESENT_PARAMETERS d3dpp; 
  273.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  274. d3dpp.Windowed = FALSE;
  275.     d3dpp.BackBufferCount = 1;
  276.     d3dpp.BackBufferFormat = d3ddm.Format;
  277.     d3dpp.BackBufferWidth = d3ddm.Width;
  278.     d3dpp.BackBufferHeight = d3ddm.Height;
  279.     d3dpp.hDeviceWindow = hWnd;
  280.     d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
  281. d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  282.     d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  283. m_nScreenWidth = d3ddm.Width;
  284. m_nScreenHeight = d3ddm.Height;
  285. //Select the best depth buffer, select 32, 24 or 16 bit
  286.     if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D32) == D3D_OK)
  287. {
  288.         d3dpp.AutoDepthStencilFormat = D3DFMT_D32;
  289.         d3dpp.EnableAutoDepthStencil = TRUE;
  290. LogInfo("<li>32bit depth buffer selected");
  291.     }
  292.     else if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24X8) == D3D_OK)
  293.     {
  294. d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;
  295.         d3dpp.EnableAutoDepthStencil = TRUE;
  296. LogInfo("<li>24bit depth buffer selected");
  297. }
  298.     else if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16) == D3D_OK)
  299.     {
  300. d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  301.         d3dpp.EnableAutoDepthStencil = TRUE;
  302. LogInfo("<li>16bit depth buffer selected");
  303. }
  304.     else
  305. {
  306.         d3dpp.EnableAutoDepthStencil = FALSE;
  307. LogError("<li>Unable to select depth buffer.");
  308. }
  309.     //Create a Direct3D device.
  310.     if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
  311.                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice)))
  312.     {
  313. LogError("<li>Unable to create device.");
  314.         return false;
  315.     }
  316.     
  317. //Turn on back face culling. This is becuase we want to hide the back of our polygons
  318. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW)))
  319. {
  320. LogError("<li>SetRenderState: D3DRS_CULLMODE Failed");
  321. return false;
  322. }
  323. else
  324. {
  325. LogInfo("<li>SetRenderState: D3DRS_CULLMODE OK");
  326. }
  327. //Turn on Depth Buffering
  328. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE)))
  329. {
  330. LogError("<li>SetRenderState: D3DRS_ZENABLE Failed");
  331. return false;
  332. }
  333. else
  334. {
  335. LogInfo("<li>SetRenderState: D3DRS_ZENABLE OK");
  336. }
  337. //Set fill state. Possible values: D3DFILL_POINT, D3DFILL_WIREFRAME, D3DFILL_SOLID
  338. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID)))
  339. {
  340. LogError("<li>SetRenderState: D3DRS_FILLMODE Failed");
  341. return false;
  342. }
  343. else
  344. {
  345. LogInfo("<li>SetRenderState: D3DRS_FILLMODE OK");
  346. }
  347. //Set the D3DRS_NORMALIZENORMALS render state to fix the problem when scaling the objects get darker
  348. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE)))
  349. {
  350. LogError("<li>SetRenderState: D3DRS_NORMALIZENORMALS Failed");
  351. return false;
  352. }
  353. else
  354. {
  355. LogInfo("<li>SetRenderState: D3DRS_NORMALIZENORMALS OK");
  356. }
  357. //Enable alpha blending so we can use transparent textures
  358. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,  TRUE)))
  359. {
  360. LogError("<li>SetRenderState: D3DRS_ALPHABLENDENABLE Failed");
  361. return false;
  362. }
  363. else
  364. {
  365. //Set how the texture should be blended (use alpha)
  366. m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  367. m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  368. LogInfo("<li>SetRenderState: D3DRS_ALPHABLENDENABLE OK");
  369. }
  370.     return true;
  371. }
  372. bool CGame::InitialiseLights()
  373. {
  374. LogInfo("<br>Initialise Lights:");
  375. D3DLIGHT8 d3dLight;
  376. //Initialize the light structure.
  377. ZeroMemory(&d3dLight, sizeof(D3DLIGHT8));
  378. d3dLight.Type = D3DLIGHT_POINT;
  379. d3dLight.Position.x = -30.0f;
  380. d3dLight.Position.y = 0.0f;
  381. d3dLight.Position.z = -15.0f;
  382. d3dLight.Attenuation0 = 1.0f; 
  383. d3dLight.Attenuation1 = 0.0f; 
  384. d3dLight.Attenuation2 = 0.0f; 
  385. d3dLight.Range = 1000.0f;
  386. d3dLight.Diffuse.r = 1.0f;
  387. d3dLight.Diffuse.g = 1.0f;
  388. d3dLight.Diffuse.b = 1.0f;
  389. d3dLight.Ambient.r = 0.0f;
  390. d3dLight.Ambient.g = 0.0f;
  391. d3dLight.Ambient.b = 0.0f;
  392. d3dLight.Specular.r = 0.0f;
  393. d3dLight.Specular.g = 0.0f;
  394. d3dLight.Specular.b = 0.0f;
  395. //Assign the point light to our device in poisition (index) 0
  396. if(FAILED(m_pD3DDevice->SetLight(0, &d3dLight)))
  397. {
  398. LogError("<li>SetLight Failed");
  399. return false;
  400. }
  401. else
  402. {
  403. LogInfo("<li>SetLight OK");
  404. }
  405. //Enable our point light in position (index) 0
  406. if(FAILED(m_pD3DDevice->LightEnable(0, TRUE)))
  407. {
  408. LogError("<li>LightEnable Failed");
  409. return false;
  410. }
  411. else
  412. {
  413. LogInfo("<li>LightEnable OK");
  414. }
  415. //Turn on lighting
  416.     if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE)))
  417. {
  418. LogError("<li>SetRenderState: D3DRS_LIGHTING Failed");
  419. return false;
  420. }
  421. else
  422. {
  423. LogInfo("<li>SetRenderState: D3DRS_LIGHTING OK");
  424. }
  425. //Set ambient light level
  426. if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(100, 100, 100))))
  427. {
  428. LogError("<li>SetRenderState: D3DRS_AMBIENT Failed");
  429. return false;
  430. }
  431. else
  432. {
  433. LogInfo("<li>SetRenderState: D3DRS_AMBIENT OK");
  434. }
  435. return true;
  436. }
  437. LPDIRECT3DDEVICE8 CGame::GetDevice()
  438. {
  439. return m_pD3DDevice;
  440. }
  441. void CGame::GameLoop()
  442. {
  443.     //Enter the game loop
  444.     MSG msg; 
  445.     BOOL fMessage;
  446.     PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
  447. //Game started, so record time
  448. m_dwStartTime = timeGetTime();
  449.     while((msg.message != WM_QUIT) && (!m_fQuit))
  450.     {
  451.         fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
  452.         if(fMessage)
  453.         {
  454.             //Process message
  455.             TranslateMessage(&msg);
  456.             DispatchMessage(&msg);
  457.         }
  458.         else
  459.         {
  460.             //No message to process, so render the current scene
  461.             Render();
  462.         }
  463.     }
  464. //Game finished, so record time
  465. m_dwEndTime = timeGetTime();
  466. }
  467. void CGame::Render()
  468. {
  469. if(m_pD3DDevice == NULL)
  470.     {
  471.         return;
  472.     }
  473. //Process keyboard and mouse user input
  474. ProcessKeyboard();
  475. ProcessMouse();
  476. if(!m_fQuit)
  477. {
  478. //Clear the back buffer and depth buffer
  479. m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  480.     
  481. //Begin the scene
  482. m_pD3DDevice->BeginScene();
  483.     
  484. //Setup the camera ready for 3D elements
  485. Setup3DCamera();
  486. //Now that the 3D camera is setup, render the 3D objects
  487. Render3D();
  488. //Now render the text
  489. RenderText();
  490. //End the scene
  491. m_pD3DDevice->EndScene();
  492.     
  493. //Filp the back and front buffers so that whatever has been rendered on the back buffer
  494. //will now be visible on screen (front buffer).
  495. m_pD3DDevice->Present(NULL, NULL, NULL, NULL);
  496. //Count Frames
  497. m_dwFrames++;
  498. }
  499. }
  500. void CGame::Setup2DCamera()
  501. {
  502. D3DXMATRIX matOrtho;
  503. D3DXMATRIX matIdentity;
  504. //Setup the orthogonal projection matrix and the default world/view matrix
  505. D3DXMatrixOrthoLH(&matOrtho, (float)m_nScreenWidth, (float)m_nScreenHeight, 0.0f, 1.0f);
  506. D3DXMatrixIdentity(&matIdentity);
  507. m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matOrtho);
  508. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matIdentity);
  509. m_pD3DDevice->SetTransform(D3DTS_VIEW, &matIdentity);
  510. //Make sure that the z-buffer and lighting are disabled
  511. m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
  512. m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
  513. }
  514. void CGame::Setup3DCamera()
  515. {
  516. //Here we will setup the camera.
  517. //The camera has three settings: "Camera Position", "Look at Position" and "Up Direction"
  518. //We have set the following:
  519. //Camera Position: (0, 15, -50)
  520. //Look at Position: (0, 0, 0)
  521. //Up direction: Y-Axis.
  522.     D3DXMATRIX matView;
  523.     D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 15.0f, -50.0f), //Camera Position
  524.                                  &D3DXVECTOR3(0.0f, 0.0f, 0.0f), //Look At Position
  525.                                  &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction
  526.     m_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
  527. //Here we specify the field of view, aspect ration and near and far clipping planes.
  528.     D3DXMATRIX matProj;
  529.     D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.25f, 1.0f, 2000.0f);
  530.     m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
  531. //Make sure that the z-buffer and lighting are enabled
  532. m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
  533. m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
  534. }
  535. void CGame::Render2D()
  536. {
  537. //Render our 2D objects
  538. }
  539. void CGame::Render3D()
  540. {
  541. //Render our 3D objects
  542. D3DXMATRIX matEarth, matScale, matRotate, matEarthRoll, matEarthMove;
  543. float rMouseSpeed = 20.0f;
  544. //If left mouse button is down, stop the earth from rotating
  545. if(m_nMouseLeft == 1)
  546. {
  547. m_rRotate = 0.0f;
  548. }
  549. //If right mouse button is down, start the earth rotating at default speed
  550. if(m_nMouseRight == 1)
  551. {
  552. m_rRotate = 2.0f;
  553. }
  554. m_rAngle += m_rRotate;
  555. //Create the transformation matrices
  556. D3DXMatrixRotationY(&matRotate, D3DXToRadian(m_rAngle));
  557. D3DXMatrixScaling(&matScale, m_rScale, m_rScale, m_rScale);
  558. D3DXMatrixRotationYawPitchRoll(&matEarthRoll, 0.0f, 0.0f, D3DXToRadian(23.44f));
  559. D3DXMatrixTranslation(&matEarthMove, (m_nMouseX / rMouseSpeed), -(m_nMouseY / rMouseSpeed), 0.0f);
  560. D3DXMatrixMultiply(&matEarth, &matScale, &matRotate);
  561. D3DXMatrixMultiply(&matEarth, &matEarth, &matEarthRoll);
  562. D3DXMatrixMultiply(&matEarth, &matEarth, &matEarthMove);
  563. //Render our objects
  564. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matEarth);
  565. m_dwTotalPolygons += m_pSphere->Render();
  566. }
  567. void CGame::RenderText()
  568. {
  569. //Draw some text at the top of the screen showing stats
  570. char buffer[255];
  571. DWORD dwDuration = (timeGetTime() - m_dwStartTime) / 1000;
  572. if(dwDuration > 0)
  573. {
  574. sprintf(buffer, "Duration: %d seconds. Frames: %d. FPS: %d. Rotation Rate: %f. Angle: %f. Scale: %f. Left: %d. Right: %d. X = %d. Y = %d.", dwDuration, m_dwFrames, (m_dwFrames / dwDuration), m_rRotate, m_rAngle, m_rScale, m_nMouseLeft, m_nMouseRight, m_nMouseX, m_nMouseY);
  575. }
  576. else
  577. {
  578. sprintf(buffer, "Calculating...");
  579. }
  580. m_pFont->DrawText(buffer, 0, 0, D3DCOLOR_XRGB(255, 255, 255));
  581. }
  582. void CGame::ProcessKeyboard()
  583. {
  584.     char KeyboardState[256]; 
  585.      
  586.     if(FAILED(m_pKeyboard->GetDeviceState(sizeof(KeyboardState),(LPVOID)&KeyboardState)))
  587.     { 
  588. return; 
  589.     } 
  590.  
  591. if(KEYDOWN(KeyboardState, DIK_ESCAPE))
  592. {
  593.         //Escape key pressed. Quit game.
  594. m_fQuit = true;
  595. }
  596. //Rotate Earth
  597.     if(KEYDOWN(KeyboardState, DIK_RIGHT))
  598. {
  599. m_rRotate -= 0.5f;
  600. }
  601.     else if(KEYDOWN(KeyboardState, DIK_LEFT))
  602. {
  603. m_rRotate += 0.5f;
  604. }
  605. //Set an upper and lower limit for the rotation factor
  606. if(m_rRotate < -20.0f)
  607. {
  608. m_rRotate = -20.0f;
  609. }
  610. else if(m_rRotate > 20.0f)
  611. {
  612. m_rRotate = 20.0f;
  613. }
  614. //Scale Earth
  615.     if(KEYDOWN(KeyboardState, DIK_UP))
  616. {
  617. m_rScale += 0.5f;
  618. }
  619.     else if (KEYDOWN(KeyboardState, DIK_DOWN))
  620. {
  621. m_rScale -= 0.5f;
  622. }
  623. //Set an upper and lower limit for the scale factor
  624. if(m_rScale < 1.0f)
  625. {
  626. m_rScale = 1.0f;
  627. }
  628. else if(m_rScale > 20.0f)
  629. {
  630. m_rScale = 20.0f;
  631. }
  632. }
  633. void CGame::ProcessMouse()
  634. {
  635. DIMOUSESTATE MouseState;
  636. if(FAILED(m_pMouse->GetDeviceState(sizeof(MouseState),(LPVOID)&MouseState)))
  637.     { 
  638. return; 
  639.     } 
  640. //Is the left mouse button down?
  641. if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_LEFT]))
  642. {
  643. m_nMouseLeft = 1;
  644. }
  645. else
  646. {
  647. m_nMouseLeft = 0;
  648. }
  649. //Is the right mouse button down?
  650. if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_RIGHT]))
  651. {
  652. m_nMouseRight = 1;
  653. }
  654. else
  655. {
  656. m_nMouseRight = 0;
  657. }
  658.     
  659. m_nMouseX += MouseState.lX;
  660. m_nMouseY += MouseState.lY;
  661. }