Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

785 lines
24 KiB

  1. /*****************************************************************************\
  2. FILE: main.cpp
  3. DESCRIPTION:
  4. Here we can subclass CDXScreenSaver if we want to override the behavior.
  5. BryanSt 12/24/2000
  6. Copyright (C) Microsoft Corp 2000-2001. All rights reserved.
  7. \*****************************************************************************/
  8. #include "stdafx.h"
  9. #include <shlobj.h>
  10. #include "main.h"
  11. #include "..\\D3DSaver\\D3DSaver.h"
  12. CRITICAL_SECTION g_csDll = {{0},0, 0, NULL, NULL, 0 };
  13. CMSLogoDXScreenSaver * g_pScreenSaver = NULL; // Replace with CMyDXScreenSaver if you want to override.
  14. DWORD g_dwBaseTime = 0;
  15. HINSTANCE g_hMainInstance = NULL;
  16. IDirect3D8 * g_pD3D = NULL;
  17. DWORD g_dwWidth = 0;
  18. DWORD g_dwHeight = 0;
  19. BOOL g_fFirstFrame = TRUE; // On our first frame, we don't want to render the images because then the screensaver will remain black for a long time while they load
  20. //-----------------------------------------------------------------------------
  21. // Name: WinMain()
  22. // Desc: Entry point to the program. Initializes everything, and goes into a
  23. // message-processing loop. Idle time is used to render the scene.
  24. //-----------------------------------------------------------------------------
  25. INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
  26. {
  27. HRESULT hr = E_OUTOFMEMORY;
  28. HRESULT hrOle = CoInitialize(0);
  29. g_pScreenSaver = new CMSLogoDXScreenSaver();
  30. g_hMainInstance = hInst;
  31. if (g_pScreenSaver)
  32. {
  33. hr = g_pScreenSaver->Create(hInst);
  34. if (SUCCEEDED(hr))
  35. {
  36. hr = g_pScreenSaver->Run();
  37. }
  38. if (FAILED(hr))
  39. {
  40. g_pScreenSaver->DisplayErrorMsg(hr);
  41. }
  42. SAFE_DELETE(g_pScreenSaver);
  43. }
  44. if (SUCCEEDED(hrOle))
  45. {
  46. CoUninitialize();
  47. }
  48. return hr;
  49. }
  50. CMSLogoDXScreenSaver::CMSLogoDXScreenSaver()
  51. {
  52. InitializeCriticalSection(&g_csDll);
  53. time_t nTime = time(NULL);
  54. UINT uSeed = (UINT) nTime;
  55. InitCommonControls(); // To enable fusion.
  56. srand(uSeed);
  57. m_ptheCamera = NULL;
  58. m_pCurrentRoom = NULL;
  59. m_fFrontToBack = FALSE;
  60. m_bUseDepthBuffer = TRUE;
  61. m_pDeviceObjects = 0;
  62. m_nCurrentDevice = 0;
  63. m_fShowFrameInfo = FALSE;
  64. m_fUseSmallImages = TRUE;
  65. m_pDeviceObjects = NULL;
  66. ZeroMemory(m_DeviceObjects, sizeof(m_DeviceObjects));
  67. #ifdef MANUAL_CAMERA
  68. ZeroMemory( &m_camera, sizeof(m_camera) );
  69. m_camera.m_vPosition = D3DXVECTOR3( 60.16f, 20.0f, 196.10f );
  70. m_camera.m_fYaw = -81.41f;
  71. ZeroMemory( m_bKey, 256 );
  72. #endif
  73. for (int nIndex = 0; nIndex < ARRAYSIZE(m_pTextures); nIndex++)
  74. {
  75. m_pTextures[nIndex] = NULL;
  76. }
  77. if (!g_pConfig)
  78. {
  79. g_pConfig = new CConfig(this);
  80. }
  81. }
  82. extern int g_nLeakCheck;
  83. CMSLogoDXScreenSaver::~CMSLogoDXScreenSaver()
  84. {
  85. DWORD dwTemp = 0;
  86. for (int nIndex = 0; nIndex < ARRAYSIZE(m_pTextures); nIndex++)
  87. {
  88. SAFE_RELEASE(m_pTextures[nIndex]);
  89. }
  90. IUnknown_Set((IUnknown **) &g_pD3D, NULL);
  91. SAFE_DELETE(g_pConfig);
  92. if (m_pCurrentRoom)
  93. {
  94. m_pCurrentRoom->FinalCleanup();
  95. }
  96. SAFE_RELEASE(m_pCurrentRoom);
  97. // AssertMsg((0 == g_nLeakCheck), TEXT("We have a bug in the CTheRoom ref-counting that caused %d rooms to be leaked."), g_nLeakCheck);
  98. if (g_nLeakCheck)
  99. {
  100. dwTemp = g_nLeakCheck;
  101. }
  102. DeleteCriticalSection(&g_csDll);
  103. }
  104. IDirect3DDevice8 * CMSLogoDXScreenSaver::GetD3DDevice(void)
  105. {
  106. return m_pd3dDevice;
  107. }
  108. void CMSLogoDXScreenSaver::SetDevice(UINT iDevice)
  109. {
  110. m_nCurrentDevice = iDevice;
  111. m_pDeviceObjects = &m_DeviceObjects[m_nCurrentDevice];
  112. }
  113. void CMSLogoDXScreenSaver::ReadSettings()
  114. {
  115. g_pConfig->LoadStatePublic();
  116. }
  117. HRESULT CMSLogoDXScreenSaver::GetCurrentScreenSize(int * pnWidth, int * pnHeight)
  118. {
  119. int nAdapter = m_RenderUnits[m_nCurrentDevice].iAdapter;
  120. D3DAdapterInfo * pAdapterInfo = m_Adapters[nAdapter];
  121. D3DDeviceInfo * pDeviceInfo = &(pAdapterInfo->devices[pAdapterInfo->dwCurrentDevice]);
  122. D3DModeInfo * pModeInfo = &(pDeviceInfo->modes[pDeviceInfo->dwCurrentMode]);
  123. *pnWidth = pModeInfo->Width;
  124. *pnHeight = pModeInfo->Height;
  125. return S_OK;
  126. }
  127. CTexture * CMSLogoDXScreenSaver::GetGlobalTexture(DWORD dwItem, float * pfScale)
  128. {
  129. CTexture * pTexture = NULL;
  130. if (dwItem >= ARRAYSIZE(m_pTextures))
  131. {
  132. return NULL;
  133. }
  134. *pfScale = 1.0f;
  135. pTexture = m_pTextures[dwItem];
  136. if (!pTexture && m_pd3dDevice && g_pConfig)
  137. {
  138. TCHAR szPath[MAX_PATH];
  139. DWORD dwScale;
  140. if (SUCCEEDED(g_pConfig->GetTexturePath(dwItem, &dwScale, szPath, ARRAYSIZE(szPath))) &&
  141. PathFileExists(szPath))
  142. {
  143. *pfScale = (1.0f / (((float) dwScale) / 100.0f));
  144. }
  145. else
  146. {
  147. StrCpyN(szPath, c_pszGlobalTextures[dwItem], ARRAYSIZE(szPath));
  148. }
  149. // This will give people a chance to customize the images.
  150. pTexture = new CTexture(this, szPath, c_pszGlobalTextures[dwItem], *pfScale);
  151. m_pTextures[dwItem] = pTexture;
  152. }
  153. *pfScale = (pTexture ? pTexture->GetScale() : 1.0f);
  154. return pTexture;
  155. }
  156. HRESULT CMSLogoDXScreenSaver::RegisterSoftwareDevice(void)
  157. {
  158. if (m_pD3D)
  159. {
  160. m_pD3D->RegisterSoftwareDevice(D3D8RGBRasterizer);
  161. }
  162. return S_OK;
  163. }
  164. void _stdcall ModuleEntry(void)
  165. {
  166. int nReturn = WinMain(GetModuleHandle(NULL), NULL, NULL, 0);
  167. ExitProcess(nReturn);
  168. }
  169. HRESULT CMSLogoDXScreenSaver::_SetTestCameraPosition(void)
  170. {
  171. D3DXVECTOR3 vSourceLoc(1.0f, 20.f, 199.0f);
  172. D3DXVECTOR3 vSourceTangent(5.0f, 0.0f, 0.0f);
  173. HRESULT hr = m_ptheCamera->Init(vSourceLoc, vSourceTangent, D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  174. if (SUCCEEDED(hr))
  175. {
  176. D3DXVECTOR3 vDestLoc(1.0f, 20.f, 10.0f);
  177. D3DXVECTOR3 vDestTangent(5.0, 0.0f, 0.0f);
  178. hr = m_ptheCamera->CreateNextMove(vSourceLoc, vSourceTangent, vDestLoc, vDestTangent);
  179. hr = m_ptheCamera->CreateNextWait(0, 0, 300.0f);
  180. }
  181. return hr;
  182. }
  183. HRESULT CMSLogoDXScreenSaver::_CheckMachinePerf(void)
  184. {
  185. // Initialize member variables
  186. int nWidth;
  187. int nHeight;
  188. // We will only consider using large images if the screen is larger
  189. // than 1248x1024
  190. if (SUCCEEDED(GetCurrentScreenSize(&nWidth, &nHeight)) && g_pConfig &&
  191. (nWidth > 1200) && (nHeight >= 1024))
  192. {
  193. // Now, the user can force this on by using a high "Quality" setting
  194. if ((MAX_QUALITY - 2) <= g_pConfig->GetDWORDSetting(CONFIG_DWORD_QUALITY_SLIDER))
  195. {
  196. m_fUseSmallImages = FALSE;
  197. }
  198. else
  199. {
  200. // Otherwise, we need to check the machines capabilities.
  201. MEMORYSTATUS ms;
  202. GlobalMemoryStatus(&ms);
  203. SIZE_T nMegabytes = (ms.dwTotalPhys / (1024 * 1024));
  204. // Only use large images if there is more than 170MB of RAM or we can
  205. // thrash.
  206. if ((nMegabytes > 170) && (2 < g_pConfig->GetDWORDSetting(CONFIG_DWORD_QUALITY_SLIDER)))
  207. {
  208. // We should only use 60% of the video memory. So at this resolution, find
  209. // out how many images that will most likely be.
  210. // TODO: nNumberOfImages = floor((VideoMemory * 0.60) / AveBytesPerImage);
  211. m_fUseSmallImages = FALSE;
  212. }
  213. }
  214. }
  215. return S_OK;
  216. }
  217. extern float g_fRoomWidthX;
  218. extern float g_fRoomDepthZ;
  219. extern float g_fRoomHeightY;
  220. HRESULT CMSLogoDXScreenSaver::_Init(void)
  221. {
  222. // Initialize member variables
  223. HRESULT hr = S_OK;
  224. if (g_pConfig)
  225. {
  226. m_fShowFrameInfo = g_pConfig->GetBoolSetting(IDC_CHECK_SHOWSTATS);
  227. }
  228. _CheckMachinePerf();
  229. if (!g_pPictureMgr)
  230. {
  231. g_pPictureMgr = new CPictureManager(this);
  232. if (!g_pPictureMgr)
  233. {
  234. hr = E_OUTOFMEMORY;
  235. }
  236. }
  237. IUnknown_Set((IUnknown **) &g_pD3D, (IUnknown *)m_pD3D);
  238. return hr;
  239. }
  240. void CMSLogoDXScreenSaver::DoConfig(void)
  241. {
  242. HRESULT hr = E_UNEXPECTED;
  243. if (g_pConfig)
  244. {
  245. hr = g_pConfig->DisplayConfigDialog(NULL);
  246. }
  247. }
  248. HRESULT CMSLogoDXScreenSaver::SetViewParams(IDirect3DDevice8 * pD3DDevice, D3DXVECTOR3 * pvecEyePt, D3DXVECTOR3 * pvecLookatPt, D3DXVECTOR3 * pvecUpVec, float nNumber)
  249. {
  250. HRESULT hr = E_UNEXPECTED;
  251. if (pD3DDevice)
  252. {
  253. D3DXMATRIX matView;
  254. D3DXMatrixLookAtLH(&matView, pvecEyePt, pvecLookatPt, pvecUpVec);
  255. hr = pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
  256. }
  257. return hr;
  258. }
  259. //-----------------------------------------------------------------------------
  260. // Name: OneTimeSceneInit()
  261. // Desc: Called during initial app startup, this function performs all the
  262. // permanent initialization.
  263. //-----------------------------------------------------------------------------
  264. HRESULT CMSLogoDXScreenSaver::_OneTimeSceneInit(void)
  265. {
  266. HRESULT hr = _Init();
  267. if (SUCCEEDED(hr))
  268. {
  269. m_pCurrentRoom = new CTheRoom(FALSE, this, NULL, 0);
  270. if (m_pCurrentRoom)
  271. {
  272. m_pCurrentRoom->SetCurrent(TRUE);
  273. hr = m_pCurrentRoom->OneTimeSceneInit(2, FALSE);
  274. if (SUCCEEDED(hr) && !m_ptheCamera)
  275. {
  276. m_ptheCamera = new CCameraMove();
  277. if (m_ptheCamera && g_pPictureMgr)
  278. {
  279. // return _SetTestCameraPosition();
  280. hr = m_pCurrentRoom->LoadCameraMoves(m_ptheCamera);
  281. }
  282. }
  283. }
  284. else
  285. {
  286. hr = E_OUTOFMEMORY;
  287. }
  288. }
  289. return hr;
  290. }
  291. //-----------------------------------------------------------------------------
  292. // Name: FinalCleanup()
  293. // Desc: Called before the app exits, this function gives the app the chance
  294. // to cleanup after itself.
  295. //-----------------------------------------------------------------------------
  296. HRESULT CMSLogoDXScreenSaver::FinalCleanup(void)
  297. {
  298. HRESULT hr = S_OK;
  299. if (m_pCurrentRoom)
  300. {
  301. m_pCurrentRoom->FinalCleanup();
  302. }
  303. return hr;
  304. }
  305. //-----------------------------------------------------------------------------
  306. // Name: InitDeviceObjects()
  307. // Desc: Initialize scene objects.
  308. //-----------------------------------------------------------------------------
  309. HRESULT CMSLogoDXScreenSaver::InitDeviceObjects(void)
  310. {
  311. HRESULT hr = E_FAIL;
  312. if (m_pd3dDevice && g_pConfig)
  313. {
  314. DWORD dwAmbient = 0xD0D0D0D0; // 0x33333333, 0x0a0a0a0a, 0x11111111
  315. // Set up the lights
  316. hr = m_pd3dDevice->SetRenderState(D3DRS_AMBIENT, dwAmbient);
  317. // TODO: GetViewport() is not compatible with pure-devices, which we want because it gives us
  318. // Big perf win.
  319. D3DMATERIAL8 mtrl = {0};
  320. mtrl.Ambient.r = mtrl.Specular.r = mtrl.Diffuse.r = 1.0f;
  321. mtrl.Ambient.g = mtrl.Specular.g = mtrl.Diffuse.g = 1.0f;
  322. mtrl.Ambient.b = mtrl.Specular.b = mtrl.Diffuse.b = 1.0f;
  323. mtrl.Ambient.a = mtrl.Specular.a = mtrl.Diffuse.a = 1.0f;
  324. m_pd3dDevice->SetMaterial(&mtrl);
  325. // Setup texture states
  326. hr = m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
  327. hr = m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
  328. m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  329. m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  330. m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  331. m_pd3dDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
  332. m_pd3dDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
  333. m_pd3dDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
  334. // Set default render states
  335. hr = m_pd3dDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
  336. hr = m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
  337. hr = m_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, D3DZB_TRUE);
  338. hr = m_pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
  339. hr = m_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, FALSE);
  340. switch (g_pConfig->GetDWORDSetting(CONFIG_DWORD_RENDERQUALITY))
  341. {
  342. case 0:
  343. hr = m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
  344. break;
  345. case 1:
  346. hr = m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
  347. break;
  348. case 2:
  349. hr = m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_PHONG);
  350. break;
  351. }
  352. if (SUCCEEDED(hr))
  353. {
  354. hr = _OneTimeSceneInit();
  355. }
  356. }
  357. return hr;
  358. }
  359. //-----------------------------------------------------------------------------
  360. // Name: RestoreDeviceObjects()
  361. // Desc: Initialize scene objects.
  362. //-----------------------------------------------------------------------------
  363. HRESULT CMSLogoDXScreenSaver::RestoreDeviceObjects(void)
  364. {
  365. m_pDeviceObjects->m_pStatsFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  366. m_pDeviceObjects->m_pStatsFont->InitDeviceObjects( m_pd3dDevice );
  367. m_pDeviceObjects->m_pStatsFont->RestoreDeviceObjects();
  368. return S_OK;
  369. }
  370. //-----------------------------------------------------------------------------
  371. // Name: InvalidateDeviceObjects()
  372. // Desc:
  373. //-----------------------------------------------------------------------------
  374. HRESULT CMSLogoDXScreenSaver::InvalidateDeviceObjects()
  375. {
  376. m_pDeviceObjects->m_pStatsFont->InvalidateDeviceObjects();
  377. m_pDeviceObjects->m_pStatsFont->DeleteDeviceObjects();
  378. SAFE_DELETE( m_pDeviceObjects->m_pStatsFont );
  379. return S_OK;
  380. }
  381. /*****************************************************************************\
  382. DESCRIPTION:
  383. This function is used to check the computers capabilities. By default
  384. we try to use the user's current resolution to render. If we are happy with
  385. the computer's capabilities, we return FALSE, and the current resolution is used.
  386. Otherwise, we return FALSE and recommend a resolution to use. Most often
  387. 640x480 or 800x600 is recommended.
  388. \*****************************************************************************/
  389. BOOL CMSLogoDXScreenSaver::UseLowResolution(int * pRecommendX, int * pRecommendY)
  390. {
  391. BOOL fUseLowRes = FALSE;
  392. MEMORYSTATUS ms;
  393. /*
  394. DDCAPS ddCaps = {0};
  395. ddCaps.dwSize = sizeof(ddCaps);
  396. HRESULT hr = pDDraw7->GetCaps(&ddCaps, NULL);
  397. */
  398. GlobalMemoryStatus(&ms);
  399. // Our frame rate will hurt if any of these are true...
  400. if ( (ms.dwTotalPhys < (125 * 1024 * 1024)) // If the computer has less than 128 MB of physical RAM, then it could trash.
  401. /* TODO
  402. ||
  403. FAILED(hr) ||
  404. (ddCaps.dwCaps & DDCAPS_3D) || ) // Our frame rate will hurt without these abilities
  405. (ddCaps.dwVidMemTotal < (15 * 1024 * 1024)) || ) // We want video cards with 16 MB of RAM. Less indicates old hardware.
  406. (ddCaps.dwCaps & DDCAPS_3D)) // We want a real 3D card
  407. (ddCaps.dwCaps.ddsCaps & DDSCAPS_TEXTURE)) // We want video cards with at least this support...
  408. */
  409. )
  410. {
  411. fUseLowRes = TRUE;
  412. *pRecommendX = 640;
  413. *pRecommendY = 480;
  414. }
  415. return fUseLowRes;
  416. }
  417. //-----------------------------------------------------------------------------
  418. // Name: DeleteDeviceObjects()
  419. // Desc: Called when the app is exitting, or the device is being changed,
  420. // this function deletes any device dependant objects.
  421. //-----------------------------------------------------------------------------
  422. HRESULT CMSLogoDXScreenSaver::DeleteDeviceObjects(void)
  423. {
  424. HRESULT hr = S_OK;
  425. if (m_pCurrentRoom)
  426. {
  427. m_pCurrentRoom->DeleteDeviceObjects();
  428. }
  429. return hr;
  430. }
  431. //-----------------------------------------------------------------------------
  432. // Name: Render()
  433. // Desc: Called once per frame, the call is the entry point for 3d
  434. // rendering. This function sets up render states, clears the
  435. // viewport, and renders the scene.
  436. //-----------------------------------------------------------------------------
  437. HRESULT CMSLogoDXScreenSaver::Render(void)
  438. {
  439. HRESULT hr = E_INVALIDARG;
  440. g_nTexturesRenderedInThisFrame = 0;
  441. g_nTrianglesRenderedInThisFrame = 0;
  442. SetProjectionMatrix( 1.0f, 1000.0f );
  443. if (m_pd3dDevice)
  444. {
  445. // Clear the viewport
  446. hr = m_pd3dDevice->Clear(0, NULL, (D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER), 0x00000000 /*WATER_COLOR*/, 1.0f, 0L);
  447. // Watch the number of textures we have open at one time.
  448. // TCHAR szOut[MAX_PATH];
  449. // wsprintf(szOut, TEXT("Number of open textures: %d"), D3DTextr_GetTextureCount());
  450. // TraceOutput(szOut);
  451. // Begin the scene
  452. if (m_ptheCamera && SUCCEEDED(m_pd3dDevice->BeginScene()))
  453. {
  454. D3DXMATRIX matIdentity;
  455. D3DXMatrixIdentity(&matIdentity);
  456. #ifdef MANUAL_CAMERA
  457. UpdateCamera(&m_camera);
  458. hr = m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_camera.m_matView );
  459. #else
  460. hr = m_ptheCamera->SetCamera(m_pd3dDevice, m_fTimeKeyIn);
  461. if (FAILED(hr))
  462. {
  463. DXUtil_Trace(TEXT("ERROR: m_ptheCamera->SetCamera failed."));
  464. }
  465. #endif
  466. if ((S_FALSE == hr) && m_pCurrentRoom)
  467. {
  468. CTheRoom * pNextRoom = NULL;
  469. m_pCurrentRoom->FreePictures();
  470. hr = m_pCurrentRoom->GetNextRoom(&pNextRoom);
  471. if (SUCCEEDED(hr))
  472. {
  473. m_pCurrentRoom->SetCurrent(FALSE);
  474. pNextRoom->SetCurrent(TRUE);
  475. SAFE_RELEASE(m_pCurrentRoom);
  476. m_pCurrentRoom = pNextRoom;
  477. hr = m_ptheCamera->DeleteAllMovements(m_fTimeKeyIn); // Purge all the previous movements.
  478. hr = m_pCurrentRoom->LoadCameraMoves(m_ptheCamera);
  479. if (SUCCEEDED(hr))
  480. {
  481. // We need to set the camera at the start of the new room.
  482. hr = m_ptheCamera->SetCamera(m_pd3dDevice, m_fTimeKeyIn);
  483. }
  484. }
  485. }
  486. // Update cull info for this frame
  487. // TODO: cache these matrices ourselves rather than using GetTransform
  488. D3DXMATRIX matView;
  489. D3DXMATRIX matProj;
  490. m_pd3dDevice->GetTransform( D3DTS_VIEW, &matView );
  491. m_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );
  492. UpdateCullInfo( &m_cullInfo, &matView, &matProj );
  493. ////////////////////////////
  494. // Render the objects in the room
  495. ////////////////////////////
  496. // Room
  497. m_pd3dDevice->SetRenderState(D3DRS_ZBIAS, 0);
  498. m_pd3dDevice->SetTransform(D3DTS_WORLDMATRIX(0), &matIdentity);
  499. if (m_pCurrentRoom)
  500. {
  501. int nMaxPhases = m_pCurrentRoom->GetMaxRenderPhases();
  502. m_fFrontToBack = !m_fFrontToBack;
  503. // An object will break down it's rendering by the textures it uses. It will then
  504. // render one phase for each texture. We reverse the phase render order to try
  505. // to keep the textures in memory when transitioning between one rendering cycle and the next.
  506. // For now, reversing the rendering order has been removed. DX should take care of this for us
  507. // and it causes us to control z-order.
  508. for (int nCurrentPhase = 0; nCurrentPhase < nMaxPhases; nCurrentPhase++)
  509. {
  510. hr = m_pCurrentRoom->Render(m_pd3dDevice, nCurrentPhase, TRUE /*m_fFrontToBack*/);
  511. }
  512. }
  513. if( m_fShowFrameInfo )
  514. {
  515. m_pDeviceObjects->m_pStatsFont->DrawText( 3, 1, D3DCOLOR_ARGB(255,0,0,0), m_strFrameStats );
  516. m_pDeviceObjects->m_pStatsFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  517. m_pDeviceObjects->m_pStatsFont->DrawText( 3, 21, D3DCOLOR_ARGB(255,0,0,0), m_strDeviceStats );
  518. m_pDeviceObjects->m_pStatsFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  519. }
  520. // End the scene.
  521. m_pd3dDevice->EndScene();
  522. }
  523. else
  524. {
  525. DXUtil_Trace(TEXT("ERROR: m_ptheCamera is NULL or ::BeginScene() failed."));
  526. }
  527. }
  528. g_nTexturesRenderedInThisFrame++; // This is the picture frame.
  529. g_fFirstFrame = FALSE;
  530. // Display stats for this frame.
  531. DXUtil_Trace(TEXT("RENDER FRAME: Textures: %d (Rendered %d) Triangles Rendered: %d Room: %d\n"),
  532. g_nTotalTexturesLoaded, g_nTexturesRenderedInThisFrame, g_nTrianglesRenderedInThisFrame, m_pCurrentRoom->m_nBatch);
  533. return hr;
  534. }
  535. #ifdef MANUAL_CAMERA
  536. //-----------------------------------------------------------------------------
  537. // Name: UpdateCamera()
  538. // Desc:
  539. //-----------------------------------------------------------------------------
  540. VOID CMSLogoDXScreenSaver::UpdateCamera(Camera* pCamera)
  541. {
  542. FLOAT fElapsedTime;
  543. if( m_fElapsedTime > 0.0f )
  544. fElapsedTime = m_fElapsedTime;
  545. else
  546. fElapsedTime = 0.05f;
  547. FLOAT fSpeed = 5.0f*fElapsedTime;
  548. FLOAT fAngularSpeed = 2.0f*fElapsedTime;
  549. // De-accelerate the camera movement (for smooth motion)
  550. pCamera->m_vVelocity *= 0.75f;
  551. pCamera->m_fYawVelocity *= 0.75f;
  552. pCamera->m_fPitchVelocity *= 0.75f;
  553. // Process keyboard input
  554. if( m_bKey[VK_RIGHT] ) pCamera->m_vVelocity.x += fSpeed; // Slide Right
  555. if( m_bKey[VK_LEFT] ) pCamera->m_vVelocity.x -= fSpeed; // Slide Left
  556. if( m_bKey[VK_UP] ) pCamera->m_vVelocity.y += fSpeed; // Slide Up
  557. if( m_bKey[VK_DOWN] ) pCamera->m_vVelocity.y -= fSpeed; // Slide Down
  558. if( m_bKey['W'] ) pCamera->m_vVelocity.z += fSpeed; // Move Forward
  559. if( m_bKey['S'] ) pCamera->m_vVelocity.z -= fSpeed; // Move Backward
  560. if( m_bKey['E'] ) pCamera->m_fYawVelocity += fSpeed; // Turn Right
  561. if( m_bKey['Q'] ) pCamera->m_fYawVelocity -= fSpeed; // Turn Left
  562. if( m_bKey['Z'] ) pCamera->m_fPitchVelocity += fSpeed; // Turn Down
  563. if( m_bKey['A'] ) pCamera->m_fPitchVelocity -= fSpeed; // Turn Up
  564. // Update the position vector
  565. D3DXVECTOR3 vT = pCamera->m_vVelocity * fSpeed;
  566. D3DXVec3TransformNormal( &vT, &vT, &pCamera->m_matOrientation );
  567. pCamera->m_vPosition += vT;
  568. // Update the yaw-pitch-rotation vector
  569. pCamera->m_fYaw += fAngularSpeed * pCamera->m_fYawVelocity;
  570. pCamera->m_fPitch += fAngularSpeed * pCamera->m_fPitchVelocity;
  571. if( pCamera->m_fPitch < -D3DX_PI/2 )
  572. pCamera->m_fPitch = -D3DX_PI/2;
  573. if( pCamera->m_fPitch > D3DX_PI/2 )
  574. pCamera->m_fPitch = D3DX_PI/2;
  575. // Set the view matrix
  576. D3DXQUATERNION qR;
  577. D3DXQuaternionRotationYawPitchRoll( &qR, pCamera->m_fYaw, pCamera->m_fPitch, 0.0f );
  578. D3DXMatrixAffineTransformation( &pCamera->m_matOrientation, 1.25f, NULL, &qR, &pCamera->m_vPosition );
  579. D3DXMatrixInverse( &pCamera->m_matView, NULL, &pCamera->m_matOrientation );
  580. }
  581. LRESULT CMSLogoDXScreenSaver::SaverProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  582. {
  583. if( WM_KEYDOWN == uMsg )
  584. {
  585. m_bKey[wParam] = 1;
  586. }
  587. // Perform commands when keys are released
  588. if( WM_KEYUP == uMsg )
  589. {
  590. m_bKey[wParam] = 0;
  591. }
  592. return CD3DScreensaver::SaverProc( hWnd, uMsg, wParam, lParam );
  593. }
  594. #endif
  595. //-----------------------------------------------------------------------------
  596. // Name: FrameMove()
  597. // Desc: Called once per frame, the call is the entry point for animating
  598. // the scene.
  599. //-----------------------------------------------------------------------------
  600. HRESULT CMSLogoDXScreenSaver::FrameMove(void)
  601. {
  602. if (sm_preview == m_SaverMode)
  603. {
  604. Sleep(50); // We can render plenty of frames in preview mode.
  605. }
  606. if (0 == g_dwBaseTime)
  607. {
  608. g_dwBaseTime = timeGetTime();
  609. }
  610. m_fTimeKeyIn = (timeGetTime() - g_dwBaseTime) * 0.001f;
  611. return S_OK;
  612. }
  613. HRESULT CMSLogoDXScreenSaver::ConfirmDevice(D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT fmtBackBuffer)
  614. {
  615. // TODO: In the future, we would like to use PURE-Devices because it's
  616. // a big perf win. However, if we do, then we need to stop using GetViewport and
  617. // GetTransform.
  618. if (dwBehavior & D3DCREATE_PUREDEVICE)
  619. return E_FAIL;
  620. return S_OK;
  621. }