BsD3D9RenderWindow.cpp 19 KB

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