BsD3D9RenderWindow.cpp 19 KB

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