BsD3D9RenderWindow.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. // 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(UINT32 width, UINT32 height)
  244. {
  245. THROW_IF_NOT_CORE_THREAD;
  246. if (!mIsFullScreen)
  247. return;
  248. mIsFullScreen = false;
  249. mStyle = mWindowedStyle;
  250. mWidth = width;
  251. mHeight = height;
  252. unsigned int winWidth, winHeight;
  253. getAdjustedWindowSize(mWidth, mHeight, mStyle, &winWidth, &winHeight);
  254. // Deal with centering when switching down to smaller resolution
  255. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  256. MONITORINFO monitorInfo;
  257. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  258. monitorInfo.cbSize = sizeof(MONITORINFO);
  259. GetMonitorInfo(hMonitor, &monitorInfo);
  260. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  261. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  262. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  263. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  264. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  265. SetWindowLong(mHWnd, GWL_EXSTYLE, mWindowedStyleEx);
  266. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  267. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  268. mDevice->invalidate(this);
  269. mDevice->acquire();
  270. }
  271. void D3D9RenderWindow::setHidden(bool hidden)
  272. {
  273. THROW_IF_NOT_CORE_THREAD;
  274. mHidden = hidden;
  275. if (!mIsExternal)
  276. {
  277. if (hidden)
  278. ShowWindow(mHWnd, SW_HIDE);
  279. else
  280. ShowWindow(mHWnd, SW_SHOWNORMAL);
  281. }
  282. }
  283. bool D3D9RenderWindow::isActive() const
  284. {
  285. if (isFullScreen())
  286. return isVisible();
  287. return mActive && isVisible();
  288. }
  289. bool D3D9RenderWindow::isVisible() const
  290. {
  291. return (mHWnd && !IsIconic(mHWnd));
  292. }
  293. void D3D9RenderWindow::move(INT32 top, INT32 left)
  294. {
  295. THROW_IF_NOT_CORE_THREAD;
  296. if (mHWnd && !mIsFullScreen)
  297. {
  298. mLeft = left;
  299. mTop = top;
  300. SetWindowPos(mHWnd, 0, top, left, 0, 0,
  301. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  302. }
  303. }
  304. void D3D9RenderWindow::resize(UINT32 width, UINT32 height)
  305. {
  306. THROW_IF_NOT_CORE_THREAD;
  307. if (mHWnd && !mIsFullScreen)
  308. {
  309. mWidth = width;
  310. mHeight = height;
  311. unsigned int winWidth, winHeight;
  312. getAdjustedWindowSize(width, height, mStyle, &winWidth, &winHeight);
  313. SetWindowPos(mHWnd, 0, 0, 0, winWidth, winHeight,
  314. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  315. }
  316. }
  317. void D3D9RenderWindow::getCustomAttribute( const String& name, void* pData ) const
  318. {
  319. // Valid attributes and their equivalent native functions:
  320. // D3DDEVICE : getD3DDevice
  321. // WINDOW : getWindowHandle
  322. if( name == "D3DDEVICE" )
  323. {
  324. IDirect3DDevice9* *pDev = (IDirect3DDevice9**)pData;
  325. *pDev = _getD3D9Device();
  326. return;
  327. }
  328. else if( name == "WINDOW" )
  329. {
  330. HWND *pHwnd = (HWND*)pData;
  331. *pHwnd = _getWindowHandle();
  332. return;
  333. }
  334. else if( name == "isTexture" )
  335. {
  336. bool *b = reinterpret_cast< bool * >( pData );
  337. *b = false;
  338. return;
  339. }
  340. else if( name == "D3DZBUFFER" )
  341. {
  342. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  343. *pSurf = mDevice->getDepthBuffer(this);
  344. return;
  345. }
  346. else if( name == "DDBACKBUFFER" )
  347. {
  348. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  349. *pSurf = mDevice->getBackBuffer(this);
  350. return;
  351. }
  352. else if( name == "DDFRONTBUFFER" )
  353. {
  354. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  355. *pSurf = mDevice->getBackBuffer(this);
  356. return;
  357. }
  358. }
  359. void D3D9RenderWindow::swapBuffers()
  360. {
  361. THROW_IF_NOT_CORE_THREAD;
  362. if (mDeviceValid)
  363. mDevice->present(this);
  364. }
  365. Vector2I D3D9RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  366. {
  367. POINT pos;
  368. pos.x = screenPos.x;
  369. pos.y = screenPos.y;
  370. ScreenToClient(mHWnd, &pos);
  371. return Vector2I(pos.x, pos.y);
  372. }
  373. Vector2I D3D9RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  374. {
  375. POINT pos;
  376. pos.x = windowPos.x;
  377. pos.y = windowPos.y;
  378. ClientToScreen(mHWnd, &pos);
  379. return Vector2I(pos.x, pos.y);
  380. }
  381. void D3D9RenderWindow::copyToMemory(const PixelData &dst, FrameBuffer buffer)
  382. {
  383. THROW_IF_NOT_CORE_THREAD;
  384. mDevice->copyContentsToMemory(this, dst, buffer);
  385. }
  386. void D3D9RenderWindow::_windowMovedOrResized()
  387. {
  388. THROW_IF_NOT_CORE_THREAD;
  389. if (!mHWnd || IsIconic(mHWnd))
  390. return;
  391. updateWindowRect();
  392. RenderWindow::_windowMovedOrResized();
  393. }
  394. /************************************************************************/
  395. /* D3D9 IMPLEMENTATION SPECIFIC */
  396. /************************************************************************/
  397. void D3D9RenderWindow::getAdjustedWindowSize(UINT32 clientWidth, UINT32 clientHeight,
  398. DWORD style, UINT32* winWidth, UINT32* winHeight)
  399. {
  400. RECT rc;
  401. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  402. AdjustWindowRect(&rc, style, false);
  403. *winWidth = rc.right - rc.left;
  404. *winHeight = rc.bottom - rc.top;
  405. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  406. MONITORINFO monitorInfo;
  407. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  408. monitorInfo.cbSize = sizeof(MONITORINFO);
  409. GetMonitorInfo(hMonitor, &monitorInfo);
  410. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  411. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  412. if (*winWidth > (unsigned int)maxW)
  413. *winWidth = maxW;
  414. if (*winHeight > (unsigned int)maxH)
  415. *winHeight = maxH;
  416. }
  417. void D3D9RenderWindow::_buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams) const
  418. {
  419. IDirect3D9* pD3D = D3D9RenderSystem::getDirect3D9();
  420. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  421. if (mDevice != NULL)
  422. devType = mDevice->getDeviceType();
  423. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  424. presentParams->Windowed = !mIsFullScreen;
  425. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  426. presentParams->BackBufferCount = 1;
  427. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  428. presentParams->hDeviceWindow = mHWnd;
  429. presentParams->BackBufferWidth = mWidth;
  430. presentParams->BackBufferHeight = mHeight;
  431. presentParams->FullScreen_RefreshRateInHz = mIsFullScreen ? mDisplayFrequency : 0;
  432. if (presentParams->BackBufferWidth == 0)
  433. presentParams->BackBufferWidth = 1;
  434. if (presentParams->BackBufferHeight == 0)
  435. presentParams->BackBufferHeight = 1;
  436. if (mVSync)
  437. {
  438. if (mIsFullScreen)
  439. {
  440. switch(mVSyncInterval)
  441. {
  442. case 1:
  443. default:
  444. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  445. break;
  446. case 2:
  447. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  448. break;
  449. case 3:
  450. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  451. break;
  452. case 4:
  453. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  454. break;
  455. };
  456. D3DCAPS9 caps;
  457. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  458. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  459. {
  460. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  461. }
  462. }
  463. else
  464. {
  465. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  466. }
  467. }
  468. else
  469. {
  470. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  471. }
  472. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  473. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  474. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  475. D3DRTYPE_SURFACE, D3DFMT_D24S8)))
  476. {
  477. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  478. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  479. D3DRTYPE_SURFACE, D3DFMT_D32)))
  480. {
  481. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  482. }
  483. else
  484. {
  485. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  486. }
  487. }
  488. else
  489. {
  490. if (SUCCEEDED(pD3D->CheckDepthStencilMatch(mDevice->getAdapterNumber(), devType,
  491. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8)))
  492. {
  493. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  494. }
  495. else
  496. {
  497. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  498. }
  499. }
  500. D3D9RenderSystem* rs = static_cast<D3D9RenderSystem*>(BansheeEngine::RenderSystem::instancePtr());
  501. D3DMULTISAMPLE_TYPE multisampleType;
  502. DWORD multisampleQuality;
  503. rs->determineMultisampleSettings(mDevice->getD3D9Device(), mMultisampleCount, mMultisampleHint,
  504. presentParams->BackBufferFormat, mIsFullScreen, &multisampleType, &multisampleQuality);
  505. presentParams->MultiSampleType = multisampleType;
  506. presentParams->MultiSampleQuality = (multisampleQuality == 0) ? 0 : multisampleQuality;
  507. }
  508. IDirect3DDevice9* D3D9RenderWindow::_getD3D9Device() const
  509. {
  510. return mDevice->getD3D9Device();
  511. }
  512. IDirect3DSurface9* D3D9RenderWindow::_getRenderSurface() const
  513. {
  514. return mDevice->getBackBuffer(this);
  515. }
  516. D3D9Device* D3D9RenderWindow::_getDevice() const
  517. {
  518. return mDevice;
  519. }
  520. void D3D9RenderWindow::_setDevice(D3D9Device* device)
  521. {
  522. mDevice = device;
  523. mDeviceValid = false;
  524. }
  525. bool D3D9RenderWindow::_isDepthBuffered() const
  526. {
  527. return mIsDepthBuffered;
  528. }
  529. void D3D9RenderWindow::updateWindowRect()
  530. {
  531. RECT rc;
  532. BOOL result;
  533. // Update top left parameters
  534. result = GetWindowRect(mHWnd, &rc);
  535. if (result == FALSE)
  536. {
  537. mTop = 0;
  538. mLeft = 0;
  539. mWidth = 0;
  540. mHeight = 0;
  541. return;
  542. }
  543. mTop = rc.top;
  544. mLeft = rc.left;
  545. // Width and height represent drawable area only
  546. result = GetClientRect(mHWnd, &rc);
  547. if (result == FALSE)
  548. {
  549. mTop = 0;
  550. mLeft = 0;
  551. mWidth = 0;
  552. mHeight = 0;
  553. return;
  554. }
  555. mWidth = rc.right - rc.left;
  556. mHeight = rc.bottom - rc.top;
  557. }
  558. bool D3D9RenderWindow::_validateDevice()
  559. {
  560. mDeviceValid = mDevice->validate(this);
  561. return mDeviceValid;
  562. }
  563. }