BsD3D9RenderWindow.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. #include "BsD3D9RenderWindow.h"
  2. #include "BsInput.h"
  3. #include "BsCoreThread.h"
  4. #include "BsViewport.h"
  5. #include "BsException.h"
  6. #include "BsD3D9RenderSystem.h"
  7. #include "BsRenderSystem.h"
  8. #include "BsBitwise.h"
  9. #include "Win32/BsPlatformWndProc.h"
  10. #include "BsD3D9VideoModeInfo.h"
  11. #include "BsD3D9DeviceManager.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. getAdjustedWindowSize(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. WNDCLASS wc = { 0, PlatformWndProc::_win32WndProc, 0, 0, hInst,
  162. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  163. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "D3D9Wnd" };
  164. RegisterClass(&wc);
  165. // Create our main window
  166. // Pass pointer to self
  167. mIsExternal = false;
  168. mHWnd = CreateWindowEx(dwStyleEx, "D3D9Wnd", mDesc.title.c_str(), dwStyle,
  169. mLeft, mTop, winWidth, winHeight, parentHWnd, 0, hInst, this);
  170. mStyle = dwStyle;
  171. }
  172. else
  173. {
  174. mHWnd = externalHandle;
  175. mIsExternal = true;
  176. }
  177. RECT rc;
  178. GetWindowRect(mHWnd, &rc);
  179. mTop = rc.top;
  180. mLeft = rc.left;
  181. GetClientRect(mHWnd, &rc);
  182. mWidth = rc.right;
  183. mHeight = rc.bottom;
  184. mName = mDesc.title;
  185. mIsDepthBuffered = mDesc.depthBuffer;
  186. mIsFullScreen = mDesc.fullscreen && !mIsChild;
  187. mColorDepth = 32;
  188. mActive = true;
  189. mClosed = false;
  190. D3D9RenderSystem* rs = static_cast<D3D9RenderSystem*>(RenderSystem::instancePtr());
  191. rs->registerWindow(*this);
  192. RenderWindow::initialize_internal();
  193. }
  194. void D3D9RenderWindow::destroy_internal()
  195. {
  196. if (mDevice != NULL)
  197. {
  198. mDevice->detachRenderWindow(this);
  199. mDevice = NULL;
  200. }
  201. if (mHWnd && !mIsExternal)
  202. {
  203. DestroyWindow(mHWnd);
  204. }
  205. mHWnd = 0;
  206. mActive = false;
  207. mClosed = true;
  208. RenderWindow::destroy_internal();
  209. }
  210. void D3D9RenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  211. {
  212. THROW_IF_NOT_CORE_THREAD;
  213. if (mIsChild)
  214. return;
  215. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderSystem::instance().getVideoModeInfo());
  216. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  217. if (numOutputs == 0)
  218. return;
  219. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  220. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  221. bool oldFullscreen = mIsFullScreen;
  222. mWidth = width;
  223. mHeight = height;
  224. mDisplayFrequency = Math::roundToInt(refreshRate);
  225. mIsFullScreen = true;
  226. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  227. MONITORINFO monitorInfo;
  228. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  229. monitorInfo.cbSize = sizeof(MONITORINFO);
  230. GetMonitorInfo(hMonitor, &monitorInfo);
  231. mTop = monitorInfo.rcMonitor.top;
  232. mLeft = monitorInfo.rcMonitor.left;
  233. // Invalidate device, which resets it
  234. mDevice->invalidate(this);
  235. mDevice->acquire();
  236. }
  237. void D3D9RenderWindow::setFullscreen(const VideoMode& mode)
  238. {
  239. THROW_IF_NOT_CORE_THREAD;
  240. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  241. }
  242. void D3D9RenderWindow::setWindowed(UINT32 width, UINT32 height)
  243. {
  244. THROW_IF_NOT_CORE_THREAD;
  245. if (!mIsFullScreen)
  246. return;
  247. mIsFullScreen = false;
  248. mStyle = mWindowedStyle;
  249. mWidth = width;
  250. mHeight = height;
  251. unsigned int winWidth, winHeight;
  252. getAdjustedWindowSize(mWidth, mHeight, mStyle, &winWidth, &winHeight);
  253. // Deal with centering when switching down to smaller resolution
  254. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  255. MONITORINFO monitorInfo;
  256. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  257. monitorInfo.cbSize = sizeof(MONITORINFO);
  258. GetMonitorInfo(hMonitor, &monitorInfo);
  259. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  260. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  261. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  262. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  263. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  264. SetWindowLong(mHWnd, GWL_EXSTYLE, mWindowedStyleEx);
  265. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  266. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  267. mDevice->invalidate(this);
  268. mDevice->acquire();
  269. }
  270. void D3D9RenderWindow::setHidden(bool hidden)
  271. {
  272. THROW_IF_NOT_CORE_THREAD;
  273. mHidden = hidden;
  274. if (!mIsExternal)
  275. {
  276. if (hidden)
  277. ShowWindow(mHWnd, SW_HIDE);
  278. else
  279. ShowWindow(mHWnd, SW_SHOWNORMAL);
  280. }
  281. }
  282. bool D3D9RenderWindow::isActive() const
  283. {
  284. if (isFullScreen())
  285. return isVisible();
  286. return mActive && isVisible();
  287. }
  288. bool D3D9RenderWindow::isVisible() const
  289. {
  290. return (mHWnd && !IsIconic(mHWnd));
  291. }
  292. void D3D9RenderWindow::move(INT32 top, INT32 left)
  293. {
  294. THROW_IF_NOT_CORE_THREAD;
  295. if (mHWnd && !mIsFullScreen)
  296. {
  297. mLeft = left;
  298. mTop = top;
  299. SetWindowPos(mHWnd, 0, top, left, 0, 0,
  300. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  301. }
  302. }
  303. void D3D9RenderWindow::resize(UINT32 width, UINT32 height)
  304. {
  305. THROW_IF_NOT_CORE_THREAD;
  306. if (mHWnd && !mIsFullScreen)
  307. {
  308. mWidth = width;
  309. mHeight = height;
  310. unsigned int winWidth, winHeight;
  311. getAdjustedWindowSize(width, height, mStyle, &winWidth, &winHeight);
  312. SetWindowPos(mHWnd, 0, 0, 0, winWidth, winHeight,
  313. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  314. }
  315. }
  316. void D3D9RenderWindow::getCustomAttribute( const String& name, void* pData ) const
  317. {
  318. // Valid attributes and their equivalent native functions:
  319. // D3DDEVICE : getD3DDevice
  320. // WINDOW : getWindowHandle
  321. if( name == "D3DDEVICE" )
  322. {
  323. IDirect3DDevice9* *pDev = (IDirect3DDevice9**)pData;
  324. *pDev = _getD3D9Device();
  325. return;
  326. }
  327. else if( name == "WINDOW" )
  328. {
  329. HWND *pHwnd = (HWND*)pData;
  330. *pHwnd = _getWindowHandle();
  331. return;
  332. }
  333. else if( name == "isTexture" )
  334. {
  335. bool *b = reinterpret_cast< bool * >( pData );
  336. *b = false;
  337. return;
  338. }
  339. else if( name == "D3DZBUFFER" )
  340. {
  341. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  342. *pSurf = mDevice->getDepthBuffer(this);
  343. return;
  344. }
  345. else if( name == "DDBACKBUFFER" )
  346. {
  347. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  348. *pSurf = mDevice->getBackBuffer(this);
  349. return;
  350. }
  351. else if( name == "DDFRONTBUFFER" )
  352. {
  353. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  354. *pSurf = mDevice->getBackBuffer(this);
  355. return;
  356. }
  357. }
  358. void D3D9RenderWindow::swapBuffers()
  359. {
  360. THROW_IF_NOT_CORE_THREAD;
  361. if (mDeviceValid)
  362. mDevice->present(this);
  363. }
  364. Vector2I D3D9RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  365. {
  366. POINT pos;
  367. pos.x = screenPos.x;
  368. pos.y = screenPos.y;
  369. ScreenToClient(mHWnd, &pos);
  370. return Vector2I(pos.x, pos.y);
  371. }
  372. Vector2I D3D9RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  373. {
  374. POINT pos;
  375. pos.x = windowPos.x;
  376. pos.y = windowPos.y;
  377. ClientToScreen(mHWnd, &pos);
  378. return Vector2I(pos.x, pos.y);
  379. }
  380. void D3D9RenderWindow::copyToMemory(PixelData &dst, FrameBuffer buffer)
  381. {
  382. THROW_IF_NOT_CORE_THREAD;
  383. mDevice->copyContentsToMemory(this, dst, buffer);
  384. }
  385. void D3D9RenderWindow::_windowMovedOrResized()
  386. {
  387. THROW_IF_NOT_CORE_THREAD;
  388. if (!mHWnd || IsIconic(mHWnd))
  389. return;
  390. updateWindowRect();
  391. RenderWindow::_windowMovedOrResized();
  392. }
  393. /************************************************************************/
  394. /* D3D9 IMPLEMENTATION SPECIFIC */
  395. /************************************************************************/
  396. void D3D9RenderWindow::getAdjustedWindowSize(UINT32 clientWidth, UINT32 clientHeight,
  397. DWORD style, UINT32* winWidth, UINT32* winHeight)
  398. {
  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. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  405. MONITORINFO monitorInfo;
  406. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  407. monitorInfo.cbSize = sizeof(MONITORINFO);
  408. GetMonitorInfo(hMonitor, &monitorInfo);
  409. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  410. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  411. if (*winWidth > (unsigned int)maxW)
  412. *winWidth = maxW;
  413. if (*winHeight > (unsigned int)maxH)
  414. *winHeight = maxH;
  415. }
  416. void D3D9RenderWindow::_buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams) const
  417. {
  418. IDirect3D9* pD3D = D3D9RenderSystem::getDirect3D9();
  419. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  420. if (mDevice != NULL)
  421. devType = mDevice->getDeviceType();
  422. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  423. presentParams->Windowed = !mIsFullScreen;
  424. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  425. presentParams->BackBufferCount = 1;
  426. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  427. presentParams->hDeviceWindow = mHWnd;
  428. presentParams->BackBufferWidth = mWidth;
  429. presentParams->BackBufferHeight = mHeight;
  430. presentParams->FullScreen_RefreshRateInHz = mIsFullScreen ? mDisplayFrequency : 0;
  431. if (presentParams->BackBufferWidth == 0)
  432. presentParams->BackBufferWidth = 1;
  433. if (presentParams->BackBufferHeight == 0)
  434. presentParams->BackBufferHeight = 1;
  435. if (mVSync)
  436. {
  437. if (mIsFullScreen)
  438. {
  439. switch(mVSyncInterval)
  440. {
  441. case 1:
  442. default:
  443. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  444. break;
  445. case 2:
  446. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  447. break;
  448. case 3:
  449. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  450. break;
  451. case 4:
  452. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  453. break;
  454. };
  455. D3DCAPS9 caps;
  456. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  457. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  458. {
  459. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  460. }
  461. }
  462. else
  463. {
  464. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  465. }
  466. }
  467. else
  468. {
  469. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  470. }
  471. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  472. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  473. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  474. D3DRTYPE_SURFACE, D3DFMT_D24S8)))
  475. {
  476. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  477. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  478. D3DRTYPE_SURFACE, D3DFMT_D32)))
  479. {
  480. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  481. }
  482. else
  483. {
  484. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  485. }
  486. }
  487. else
  488. {
  489. if (SUCCEEDED(pD3D->CheckDepthStencilMatch(mDevice->getAdapterNumber(), devType,
  490. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8)))
  491. {
  492. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  493. }
  494. else
  495. {
  496. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  497. }
  498. }
  499. D3D9RenderSystem* rs = static_cast<D3D9RenderSystem*>(BansheeEngine::RenderSystem::instancePtr());
  500. D3DMULTISAMPLE_TYPE multisampleType;
  501. DWORD multisampleQuality;
  502. rs->determineMultisampleSettings(mDevice->getD3D9Device(), mMultisampleCount, mMultisampleHint,
  503. presentParams->BackBufferFormat, mIsFullScreen, &multisampleType, &multisampleQuality);
  504. presentParams->MultiSampleType = multisampleType;
  505. presentParams->MultiSampleQuality = (multisampleQuality == 0) ? 0 : multisampleQuality;
  506. }
  507. IDirect3DDevice9* D3D9RenderWindow::_getD3D9Device() const
  508. {
  509. return mDevice->getD3D9Device();
  510. }
  511. IDirect3DSurface9* D3D9RenderWindow::_getRenderSurface() const
  512. {
  513. return mDevice->getBackBuffer(this);
  514. }
  515. D3D9Device* D3D9RenderWindow::_getDevice() const
  516. {
  517. return mDevice;
  518. }
  519. void D3D9RenderWindow::_setDevice(D3D9Device* device)
  520. {
  521. mDevice = device;
  522. mDeviceValid = false;
  523. }
  524. bool D3D9RenderWindow::_isDepthBuffered() const
  525. {
  526. return mIsDepthBuffered;
  527. }
  528. void D3D9RenderWindow::updateWindowRect()
  529. {
  530. RECT rc;
  531. BOOL result;
  532. // Update top left parameters
  533. result = GetWindowRect(mHWnd, &rc);
  534. if (result == FALSE)
  535. {
  536. mTop = 0;
  537. mLeft = 0;
  538. mWidth = 0;
  539. mHeight = 0;
  540. return;
  541. }
  542. mTop = rc.top;
  543. mLeft = rc.left;
  544. // Width and height represent drawable area only
  545. result = GetClientRect(mHWnd, &rc);
  546. if (result == FALSE)
  547. {
  548. mTop = 0;
  549. mLeft = 0;
  550. mWidth = 0;
  551. mHeight = 0;
  552. return;
  553. }
  554. mWidth = rc.right - rc.left;
  555. mHeight = rc.bottom - rc.top;
  556. }
  557. bool D3D9RenderWindow::_validateDevice()
  558. {
  559. mDeviceValid = mDevice->validate(this);
  560. return mDeviceValid;
  561. }
  562. }