BsD3D9RenderWindow.cpp 19 KB

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