CmD3D9RenderWindow.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. #include "CmD3D9RenderWindow.h"
  2. #include "CmInput.h"
  3. #include "CmCoreThread.h"
  4. #include "CmViewport.h"
  5. #include "CmException.h"
  6. #include "CmD3D9RenderSystem.h"
  7. #include "CmRenderSystem.h"
  8. #include "CmBitwise.h"
  9. #include "Win32/CmPlatformWndProc.h"
  10. #include "CmD3D9VideoModeInfo.h"
  11. #include "CmD3D9DeviceManager.h"
  12. namespace BansheeEngine
  13. {
  14. D3D9RenderWindow::D3D9RenderWindow(const RENDER_WINDOW_DESC& desc, HINSTANCE instance)
  15. : RenderWindow(desc), mInstance(instance), mIsDepthBuffered(true), mIsChild(false),
  16. mStyle(0), mWindowedStyle(0), mWindowedStyleEx(0)
  17. {
  18. mDevice = NULL;
  19. mIsFullScreen = false;
  20. mIsExternal = false;
  21. mHWnd = 0;
  22. mActive = false;
  23. mClosed = false;
  24. mDisplayFrequency = 0;
  25. mDeviceValid = false;
  26. }
  27. D3D9RenderWindow::~D3D9RenderWindow()
  28. { }
  29. void D3D9RenderWindow::initialize_internal()
  30. {
  31. HINSTANCE hInst = mInstance;
  32. mMultisampleType = D3DMULTISAMPLE_NONE;
  33. mMultisampleQuality = 0;
  34. mMultisampleCount = mDesc.multisampleCount;
  35. mVSync = mDesc.vsync;
  36. mVSyncInterval = mDesc.vsyncInterval;
  37. mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  38. HWND parentHWnd = 0;
  39. HWND externalHandle = 0;
  40. NameValuePairList::const_iterator opt;
  41. opt = mDesc.platformSpecific.find("parentWindowHandle");
  42. if(opt != mDesc.platformSpecific.end())
  43. parentHWnd = (HWND)parseUnsignedInt(opt->second);
  44. opt = mDesc.platformSpecific.find("externalWindowHandle");
  45. if(opt != mDesc.platformSpecific.end())
  46. externalHandle = (HWND)parseUnsignedInt(opt->second);
  47. mIsChild = parentHWnd != 0;
  48. mWindowedStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  49. mWindowedStyleEx = 0;
  50. if (!mDesc.fullscreen || mIsChild)
  51. {
  52. if (parentHWnd)
  53. {
  54. if (mDesc.toolWindow)
  55. mWindowedStyleEx = WS_EX_TOOLWINDOW;
  56. else
  57. mWindowedStyle |= WS_CHILD;
  58. }
  59. if (!parentHWnd || mDesc.toolWindow)
  60. {
  61. if (mDesc.border == WindowBorder::None)
  62. mWindowedStyle |= WS_POPUP;
  63. else if (mDesc.border == WindowBorder::Fixed)
  64. mWindowedStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  65. WS_SYSMENU | WS_MINIMIZEBOX;
  66. else
  67. mWindowedStyle |= WS_OVERLAPPEDWINDOW;
  68. }
  69. }
  70. if (!externalHandle)
  71. {
  72. MONITORINFO monitorInfo;
  73. RECT rc;
  74. HMONITOR hMonitor = NULL;
  75. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderSystem::instance().getVideoModeInfo());
  76. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  77. if (numOutputs > 0)
  78. {
  79. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  80. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  81. hMonitor = outputInfo.getMonitorHandle();
  82. }
  83. // If we didn't specified the adapter index, or if it didn't find it
  84. if (hMonitor == NULL)
  85. {
  86. POINT windowAnchorPoint;
  87. // Fill in anchor point.
  88. windowAnchorPoint.x = mDesc.left;
  89. windowAnchorPoint.y = mDesc.top;
  90. // Get the nearest monitor to this window.
  91. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTOPRIMARY);
  92. }
  93. // Get the target monitor info
  94. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  95. monitorInfo.cbSize = sizeof(MONITORINFO);
  96. GetMonitorInfo(hMonitor, &monitorInfo);
  97. unsigned int winWidth, winHeight;
  98. winWidth = mDesc.videoMode.getWidth();
  99. winHeight = mDesc.videoMode.getHeight();
  100. UINT32 left = mDesc.left;
  101. UINT32 top = mDesc.top;
  102. // No specified top left -> Center the window in the middle of the monitor
  103. if (left == -1 || top == -1)
  104. {
  105. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  106. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  107. // clamp window dimensions to screen size
  108. int outerw = (int(winWidth) < screenw)? int(winWidth) : screenw;
  109. int outerh = (int(winHeight) < screenh)? int(winHeight) : screenh;
  110. if (left == -1)
  111. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  112. else if (hMonitor != NULL)
  113. left += monitorInfo.rcWork.left;
  114. if (top == -1)
  115. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  116. else if (hMonitor != NULL)
  117. top += monitorInfo.rcWork.top;
  118. }
  119. else if (hMonitor != NULL)
  120. {
  121. left += monitorInfo.rcWork.left;
  122. top += monitorInfo.rcWork.top;
  123. }
  124. mWidth = mDesc.videoMode.getWidth();
  125. mHeight = mDesc.videoMode.getHeight();
  126. mTop = top;
  127. mLeft = left;
  128. DWORD dwStyle = 0;
  129. DWORD dwStyleEx = 0;
  130. if (mDesc.fullscreen && !mIsChild)
  131. {
  132. dwStyle = WS_VISIBLE | WS_CLIPCHILDREN | WS_POPUP;
  133. mTop = monitorInfo.rcMonitor.top;
  134. mLeft = monitorInfo.rcMonitor.left;
  135. }
  136. else
  137. {
  138. dwStyle = mWindowedStyle;
  139. dwStyleEx = mWindowedStyleEx;
  140. _adjustWindow(mDesc.videoMode.getWidth(), mDesc.videoMode.getHeight(), dwStyle, &winWidth, &winHeight);
  141. if (!mDesc.outerDimensions)
  142. {
  143. // Calculate window dimensions required
  144. // to get the requested client area
  145. SetRect(&rc, 0, 0, mWidth, mHeight);
  146. AdjustWindowRect(&rc, dwStyle, false);
  147. mWidth = rc.right - rc.left;
  148. mHeight = rc.bottom - rc.top;
  149. // Clamp window rect to the nearest display monitor.
  150. if (mLeft < monitorInfo.rcWork.left)
  151. mLeft = monitorInfo.rcWork.left;
  152. if (mTop < monitorInfo.rcWork.top)
  153. mTop = monitorInfo.rcWork.top;
  154. if (static_cast<int>(winWidth) > monitorInfo.rcWork.right - mLeft)
  155. winWidth = monitorInfo.rcWork.right - mLeft;
  156. if (static_cast<int>(winHeight) > monitorInfo.rcWork.bottom - mTop)
  157. winHeight = monitorInfo.rcWork.bottom - mTop;
  158. }
  159. }
  160. // Register the window class
  161. // NB allow 4 bytes of window data for D3D9RenderWindow pointer
  162. WNDCLASS wc = { 0, PlatformWndProc::_win32WndProc, 0, 0, hInst,
  163. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  164. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "D3D9Wnd" };
  165. RegisterClass(&wc);
  166. // Create our main window
  167. // Pass pointer to self
  168. mIsExternal = false;
  169. mHWnd = CreateWindowEx(dwStyleEx, "D3D9Wnd", mDesc.title.c_str(), dwStyle,
  170. mLeft, mTop, winWidth, winHeight, parentHWnd, 0, hInst, this);
  171. mStyle = dwStyle;
  172. }
  173. else
  174. {
  175. mHWnd = externalHandle;
  176. mIsExternal = true;
  177. }
  178. RECT rc;
  179. GetWindowRect(mHWnd, &rc);
  180. mTop = rc.top;
  181. mLeft = rc.left;
  182. GetClientRect(mHWnd, &rc);
  183. mWidth = rc.right;
  184. mHeight = rc.bottom;
  185. mName = mDesc.title;
  186. mIsDepthBuffered = mDesc.depthBuffer;
  187. mIsFullScreen = mDesc.fullscreen && !mIsChild;
  188. mColorDepth = 32;
  189. mActive = true;
  190. mClosed = false;
  191. D3D9RenderSystem* rs = static_cast<D3D9RenderSystem*>(RenderSystem::instancePtr());
  192. rs->registerWindow(*this);
  193. RenderWindow::initialize_internal();
  194. }
  195. void D3D9RenderWindow::destroy_internal()
  196. {
  197. if (mDevice != NULL)
  198. {
  199. mDevice->detachRenderWindow(this);
  200. mDevice = NULL;
  201. }
  202. if (mHWnd && !mIsExternal)
  203. {
  204. DestroyWindow(mHWnd);
  205. }
  206. mHWnd = 0;
  207. mActive = false;
  208. mClosed = true;
  209. RenderWindow::destroy_internal();
  210. }
  211. void D3D9RenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  212. {
  213. THROW_IF_NOT_CORE_THREAD;
  214. if (mIsChild)
  215. return;
  216. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderSystem::instance().getVideoModeInfo());
  217. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  218. if (numOutputs == 0)
  219. return;
  220. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  221. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  222. bool oldFullscreen = mIsFullScreen;
  223. mWidth = width;
  224. mHeight = height;
  225. mDisplayFrequency = Math::roundToInt(refreshRate);
  226. mIsFullScreen = true;
  227. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  228. MONITORINFO monitorInfo;
  229. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  230. monitorInfo.cbSize = sizeof(MONITORINFO);
  231. GetMonitorInfo(hMonitor, &monitorInfo);
  232. mTop = monitorInfo.rcMonitor.top;
  233. mLeft = monitorInfo.rcMonitor.left;
  234. // Invalidate device, which resets it
  235. mDevice->invalidate(this);
  236. mDevice->acquire();
  237. }
  238. void D3D9RenderWindow::setFullscreen(const VideoMode& mode)
  239. {
  240. THROW_IF_NOT_CORE_THREAD;
  241. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  242. }
  243. void D3D9RenderWindow::setWindowed()
  244. {
  245. THROW_IF_NOT_CORE_THREAD;
  246. if (!mIsFullScreen)
  247. return;
  248. mIsFullScreen = false;
  249. mStyle = mWindowedStyle;
  250. unsigned int winWidth, winHeight;
  251. _adjustWindow(mWidth, mHeight, mStyle, &winWidth, &winHeight);
  252. // Deal with centering when switching down to smaller resolution
  253. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  254. MONITORINFO monitorInfo;
  255. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  256. monitorInfo.cbSize = sizeof(MONITORINFO);
  257. GetMonitorInfo(hMonitor, &monitorInfo);
  258. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  259. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  260. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  261. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  262. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  263. SetWindowLong(mHWnd, GWL_EXSTYLE, mWindowedStyleEx);
  264. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  265. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  266. mDevice->invalidate(this);
  267. mDevice->acquire();
  268. }
  269. void D3D9RenderWindow::setHidden(bool hidden)
  270. {
  271. THROW_IF_NOT_CORE_THREAD;
  272. mHidden = hidden;
  273. if (!mIsExternal)
  274. {
  275. if (hidden)
  276. ShowWindow(mHWnd, SW_HIDE);
  277. else
  278. ShowWindow(mHWnd, SW_SHOWNORMAL);
  279. }
  280. }
  281. bool D3D9RenderWindow::isActive() const
  282. {
  283. if (isFullScreen())
  284. return isVisible();
  285. return mActive && isVisible();
  286. }
  287. bool D3D9RenderWindow::isVisible() const
  288. {
  289. return (mHWnd && !IsIconic(mHWnd));
  290. }
  291. void D3D9RenderWindow::move(INT32 top, INT32 left)
  292. {
  293. THROW_IF_NOT_CORE_THREAD;
  294. if (mHWnd && !mIsFullScreen)
  295. {
  296. mLeft = left;
  297. mTop = top;
  298. SetWindowPos(mHWnd, 0, top, left, 0, 0,
  299. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  300. }
  301. }
  302. void D3D9RenderWindow::resize(UINT32 width, UINT32 height)
  303. {
  304. THROW_IF_NOT_CORE_THREAD;
  305. if (mHWnd && !mIsFullScreen)
  306. {
  307. mWidth = width;
  308. mHeight = height;
  309. unsigned int winWidth, winHeight;
  310. _adjustWindow(width, height, mStyle, &winWidth, &winHeight);
  311. SetWindowPos(mHWnd, 0, 0, 0, winWidth, winHeight,
  312. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  313. }
  314. }
  315. void D3D9RenderWindow::getCustomAttribute( const String& name, void* pData ) const
  316. {
  317. // Valid attributes and their equivalent native functions:
  318. // D3DDEVICE : getD3DDevice
  319. // WINDOW : getWindowHandle
  320. if( name == "D3DDEVICE" )
  321. {
  322. IDirect3DDevice9* *pDev = (IDirect3DDevice9**)pData;
  323. *pDev = _getD3D9Device();
  324. return;
  325. }
  326. else if( name == "WINDOW" )
  327. {
  328. HWND *pHwnd = (HWND*)pData;
  329. *pHwnd = _getWindowHandle();
  330. return;
  331. }
  332. else if( name == "isTexture" )
  333. {
  334. bool *b = reinterpret_cast< bool * >( pData );
  335. *b = false;
  336. return;
  337. }
  338. else if( name == "D3DZBUFFER" )
  339. {
  340. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  341. *pSurf = mDevice->getDepthBuffer(this);
  342. return;
  343. }
  344. else if( name == "DDBACKBUFFER" )
  345. {
  346. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  347. *pSurf = mDevice->getBackBuffer(this);
  348. return;
  349. }
  350. else if( name == "DDFRONTBUFFER" )
  351. {
  352. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  353. *pSurf = mDevice->getBackBuffer(this);
  354. return;
  355. }
  356. }
  357. void D3D9RenderWindow::swapBuffers()
  358. {
  359. THROW_IF_NOT_CORE_THREAD;
  360. if (mDeviceValid)
  361. mDevice->present(this);
  362. }
  363. Vector2I D3D9RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  364. {
  365. POINT pos;
  366. pos.x = screenPos.x;
  367. pos.y = screenPos.y;
  368. ScreenToClient(mHWnd, &pos);
  369. return Vector2I(pos.x, pos.y);
  370. }
  371. Vector2I D3D9RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  372. {
  373. POINT pos;
  374. pos.x = windowPos.x;
  375. pos.y = windowPos.y;
  376. ClientToScreen(mHWnd, &pos);
  377. return Vector2I(pos.x, pos.y);
  378. }
  379. void D3D9RenderWindow::copyToMemory(const PixelData &dst, FrameBuffer buffer)
  380. {
  381. THROW_IF_NOT_CORE_THREAD;
  382. mDevice->copyContentsToMemory(this, dst, buffer);
  383. }
  384. void D3D9RenderWindow::_windowMovedOrResized()
  385. {
  386. THROW_IF_NOT_CORE_THREAD;
  387. if (!mHWnd || IsIconic(mHWnd))
  388. return;
  389. updateWindowRect();
  390. RenderWindow::_windowMovedOrResized();
  391. }
  392. /************************************************************************/
  393. /* D3D9 IMPLEMENTATION SPECIFIC */
  394. /************************************************************************/
  395. void D3D9RenderWindow::_adjustWindow(unsigned int clientWidth, unsigned int clientHeight,
  396. DWORD style, unsigned int* winWidth, unsigned int* winHeight)
  397. {
  398. // NB only call this for non full screen
  399. RECT rc;
  400. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  401. AdjustWindowRect(&rc, style, false);
  402. *winWidth = rc.right - rc.left;
  403. *winHeight = rc.bottom - rc.top;
  404. // adjust to monitor
  405. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  406. // Get monitor info
  407. MONITORINFO monitorInfo;
  408. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  409. monitorInfo.cbSize = sizeof(MONITORINFO);
  410. GetMonitorInfo(hMonitor, &monitorInfo);
  411. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  412. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  413. if (*winWidth > (unsigned int)maxW)
  414. *winWidth = maxW;
  415. if (*winHeight > (unsigned int)maxH)
  416. *winHeight = maxH;
  417. }
  418. void D3D9RenderWindow::_buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams) const
  419. {
  420. IDirect3D9* pD3D = D3D9RenderSystem::getDirect3D9();
  421. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  422. if (mDevice != NULL)
  423. devType = mDevice->getDeviceType();
  424. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  425. presentParams->Windowed = !mIsFullScreen;
  426. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  427. presentParams->BackBufferCount = 1;
  428. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  429. presentParams->hDeviceWindow = mHWnd;
  430. presentParams->BackBufferWidth = mWidth;
  431. presentParams->BackBufferHeight = mHeight;
  432. presentParams->FullScreen_RefreshRateInHz = mIsFullScreen ? mDisplayFrequency : 0;
  433. if (presentParams->BackBufferWidth == 0)
  434. presentParams->BackBufferWidth = 1;
  435. if (presentParams->BackBufferHeight == 0)
  436. presentParams->BackBufferHeight = 1;
  437. if (mVSync)
  438. {
  439. if (mIsFullScreen)
  440. {
  441. switch(mVSyncInterval)
  442. {
  443. case 1:
  444. default:
  445. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  446. break;
  447. case 2:
  448. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  449. break;
  450. case 3:
  451. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  452. break;
  453. case 4:
  454. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  455. break;
  456. };
  457. D3DCAPS9 caps;
  458. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  459. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  460. {
  461. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  462. }
  463. }
  464. else
  465. {
  466. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  467. }
  468. }
  469. else
  470. {
  471. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  472. }
  473. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  474. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  475. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  476. D3DRTYPE_SURFACE, D3DFMT_D24S8)))
  477. {
  478. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  479. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  480. D3DRTYPE_SURFACE, D3DFMT_D32)))
  481. {
  482. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  483. }
  484. else
  485. {
  486. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  487. }
  488. }
  489. else
  490. {
  491. if (SUCCEEDED(pD3D->CheckDepthStencilMatch(mDevice->getAdapterNumber(), devType,
  492. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8)))
  493. {
  494. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  495. }
  496. else
  497. {
  498. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  499. }
  500. }
  501. D3D9RenderSystem* rs = static_cast<D3D9RenderSystem*>(BansheeEngine::RenderSystem::instancePtr());
  502. D3DMULTISAMPLE_TYPE multisampleType;
  503. DWORD multisampleQuality;
  504. rs->determineMultisampleSettings(mDevice->getD3D9Device(), mMultisampleCount, mMultisampleHint,
  505. presentParams->BackBufferFormat, mIsFullScreen, &multisampleType, &multisampleQuality);
  506. presentParams->MultiSampleType = multisampleType;
  507. presentParams->MultiSampleQuality = (multisampleQuality == 0) ? 0 : multisampleQuality;
  508. }
  509. IDirect3DDevice9* D3D9RenderWindow::_getD3D9Device() const
  510. {
  511. return mDevice->getD3D9Device();
  512. }
  513. IDirect3DSurface9* D3D9RenderWindow::_getRenderSurface() const
  514. {
  515. return mDevice->getBackBuffer(this);
  516. }
  517. D3D9Device* D3D9RenderWindow::_getDevice() const
  518. {
  519. return mDevice;
  520. }
  521. void D3D9RenderWindow::_setDevice(D3D9Device* device)
  522. {
  523. mDevice = device;
  524. mDeviceValid = false;
  525. }
  526. bool D3D9RenderWindow::_isDepthBuffered() const
  527. {
  528. return mIsDepthBuffered;
  529. }
  530. void D3D9RenderWindow::updateWindowRect()
  531. {
  532. RECT rc;
  533. BOOL result;
  534. // Update top left parameters
  535. result = GetWindowRect(mHWnd, &rc);
  536. if (result == FALSE)
  537. {
  538. mTop = 0;
  539. mLeft = 0;
  540. mWidth = 0;
  541. mHeight = 0;
  542. return;
  543. }
  544. mTop = rc.top;
  545. mLeft = rc.left;
  546. // Width and height represent drawable area only
  547. result = GetClientRect(mHWnd, &rc);
  548. if (result == FALSE)
  549. {
  550. mTop = 0;
  551. mLeft = 0;
  552. mWidth = 0;
  553. mHeight = 0;
  554. return;
  555. }
  556. mWidth = rc.right - rc.left;
  557. mHeight = rc.bottom - rc.top;
  558. }
  559. bool D3D9RenderWindow::_validateDevice()
  560. {
  561. mDeviceValid = mDevice->validate(this);
  562. return mDeviceValid;
  563. }
  564. }