BsD3D9RenderWindow.cpp 18 KB

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