CmD3D9RenderWindow.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmD3D9RenderWindow.h"
  25. #include "CmViewport.h"
  26. #include "CmException.h"
  27. #include "CmD3D9RenderSystem.h"
  28. #include "CmRenderSystem.h"
  29. #include "CmBitwise.h"
  30. #include "CmWindowEventUtilities.h"
  31. #include "CmD3D9DeviceManager.h"
  32. namespace CamelotEngine
  33. {
  34. D3D9RenderWindow::D3D9RenderWindow(HINSTANCE instance)
  35. : mInstance(instance), mIsDepthBuffered(true)
  36. {
  37. mDevice = NULL;
  38. mIsFullScreen = false;
  39. mIsExternal = false;
  40. mHWnd = 0;
  41. mActive = false;
  42. mClosed = false;
  43. mSwitchingFullscreen = false;
  44. mDisplayFrequency = 0;
  45. mDeviceValid = false;
  46. mUseNVPerfHUD = false;
  47. }
  48. D3D9RenderWindow::~D3D9RenderWindow()
  49. {
  50. destroy();
  51. }
  52. void D3D9RenderWindow::initialize(const String& name, unsigned int width, unsigned int height,
  53. bool fullScreen, const NameValuePairList *miscParams)
  54. {
  55. HINSTANCE hInst = mInstance;
  56. HWND parentHWnd = 0;
  57. HWND externalHandle = 0;
  58. mFSAAType = D3DMULTISAMPLE_NONE;
  59. mFSAAQuality = 0;
  60. mFSAA = 0;
  61. mVSync = false;
  62. mVSyncInterval = 1;
  63. String title = name;
  64. unsigned int colourDepth = 32;
  65. int left = INT_MAX; // Defaults to screen center
  66. int top = INT_MAX; // Defaults to screen center
  67. bool depthBuffer = true;
  68. String border = "";
  69. bool outerSize = false;
  70. mUseNVPerfHUD = false;
  71. size_t fsaaSamples = 0;
  72. String fsaaHint;
  73. int monitorIndex = -1; //Default by detecting the adapter from left / top position
  74. if(miscParams)
  75. {
  76. // Get variable-length params
  77. NameValuePairList::const_iterator opt;
  78. // left (x)
  79. opt = miscParams->find("left");
  80. if(opt != miscParams->end())
  81. left = parseInt(opt->second);
  82. // top (y)
  83. opt = miscParams->find("top");
  84. if(opt != miscParams->end())
  85. top = parseInt(opt->second);
  86. // Window title
  87. opt = miscParams->find("title");
  88. if(opt != miscParams->end())
  89. title = opt->second;
  90. // parentWindowHandle -> parentHWnd
  91. opt = miscParams->find("parentWindowHandle");
  92. if(opt != miscParams->end())
  93. parentHWnd = (HWND)parseUnsignedInt(opt->second);
  94. // externalWindowHandle -> externalHandle
  95. opt = miscParams->find("externalWindowHandle");
  96. if(opt != miscParams->end())
  97. externalHandle = (HWND)parseUnsignedInt(opt->second);
  98. // vsync [parseBool]
  99. opt = miscParams->find("vsync");
  100. if(opt != miscParams->end())
  101. mVSync = parseBool(opt->second);
  102. // vsyncInterval [parseUnsignedInt]
  103. opt = miscParams->find("vsyncInterval");
  104. if(opt != miscParams->end())
  105. mVSyncInterval = parseUnsignedInt(opt->second);
  106. // displayFrequency
  107. opt = miscParams->find("displayFrequency");
  108. if(opt != miscParams->end())
  109. mDisplayFrequency = parseUnsignedInt(opt->second);
  110. // colourDepth
  111. opt = miscParams->find("colourDepth");
  112. if(opt != miscParams->end())
  113. colourDepth = parseUnsignedInt(opt->second);
  114. // depthBuffer [parseBool]
  115. opt = miscParams->find("depthBuffer");
  116. if(opt != miscParams->end())
  117. depthBuffer = parseBool(opt->second);
  118. // FSAA settings
  119. opt = miscParams->find("FSAA");
  120. if(opt != miscParams->end())
  121. {
  122. mFSAA = parseUnsignedInt(opt->second);
  123. }
  124. opt = miscParams->find("FSAAHint");
  125. if(opt != miscParams->end())
  126. {
  127. mFSAAHint = opt->second;
  128. }
  129. // window border style
  130. opt = miscParams->find("border");
  131. if(opt != miscParams->end())
  132. border = opt->second;
  133. // set outer dimensions?
  134. opt = miscParams->find("outerDimensions");
  135. if(opt != miscParams->end())
  136. outerSize = parseBool(opt->second);
  137. // NV perf HUD?
  138. opt = miscParams->find("useNVPerfHUD");
  139. if(opt != miscParams->end())
  140. mUseNVPerfHUD = parseBool(opt->second);
  141. // sRGB?
  142. opt = miscParams->find("gamma");
  143. if(opt != miscParams->end())
  144. mHwGamma = parseBool(opt->second);
  145. // monitor index
  146. opt = miscParams->find("monitorIndex");
  147. if(opt != miscParams->end())
  148. monitorIndex = parseInt(opt->second);
  149. }
  150. // Destroy current window if any
  151. if( mHWnd )
  152. destroy();
  153. if (!externalHandle)
  154. {
  155. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  156. DWORD dwStyleEx = 0;
  157. HMONITOR hMonitor = NULL;
  158. MONITORINFO monitorInfo;
  159. RECT rc;
  160. // If we specified which adapter we want to use - find it's monitor.
  161. if (monitorIndex != -1)
  162. {
  163. IDirect3D9* direct3D9 = D3D9RenderSystem::getDirect3D9();
  164. for (UINT32 i=0; i < direct3D9->GetAdapterCount(); ++i)
  165. {
  166. if (i == monitorIndex)
  167. {
  168. hMonitor = direct3D9->GetAdapterMonitor(i);
  169. break;
  170. }
  171. }
  172. }
  173. // If we didn't specified the adapter index, or if it didn't find it
  174. if (hMonitor == NULL)
  175. {
  176. POINT windowAnchorPoint;
  177. // Fill in anchor point.
  178. windowAnchorPoint.x = left;
  179. windowAnchorPoint.y = top;
  180. // Get the nearest monitor to this window.
  181. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTOPRIMARY);
  182. }
  183. // Get the target monitor info
  184. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  185. monitorInfo.cbSize = sizeof(MONITORINFO);
  186. GetMonitorInfo(hMonitor, &monitorInfo);
  187. unsigned int winWidth, winHeight;
  188. winWidth = width;
  189. winHeight = height;
  190. // No specified top left -> Center the window in the middle of the monitor
  191. if (left == INT_MAX || top == INT_MAX)
  192. {
  193. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  194. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  195. // clamp window dimensions to screen size
  196. int outerw = (int(winWidth) < screenw)? int(winWidth) : screenw;
  197. int outerh = (int(winHeight) < screenh)? int(winHeight) : screenh;
  198. if (left == INT_MAX)
  199. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  200. else if (monitorIndex != -1)
  201. left += monitorInfo.rcWork.left;
  202. if (top == INT_MAX)
  203. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  204. else if (monitorIndex != -1)
  205. top += monitorInfo.rcWork.top;
  206. }
  207. else if (monitorIndex != -1)
  208. {
  209. left += monitorInfo.rcWork.left;
  210. top += monitorInfo.rcWork.top;
  211. }
  212. mWidth = mDesiredWidth = width;
  213. mHeight = mDesiredHeight = height;
  214. mTop = top;
  215. mLeft = left;
  216. if (fullScreen)
  217. {
  218. dwStyleEx |= WS_EX_TOPMOST;
  219. dwStyle |= WS_POPUP;
  220. mTop = monitorInfo.rcMonitor.top;
  221. mLeft = monitorInfo.rcMonitor.left;
  222. }
  223. else
  224. {
  225. if (parentHWnd)
  226. {
  227. dwStyle |= WS_CHILD;
  228. }
  229. else
  230. {
  231. if (border == "none")
  232. dwStyle |= WS_POPUP;
  233. else if (border == "fixed")
  234. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  235. WS_SYSMENU | WS_MINIMIZEBOX;
  236. else
  237. dwStyle |= WS_OVERLAPPEDWINDOW;
  238. }
  239. adjustWindow(width, height, dwStyle, &winWidth, &winHeight);
  240. if (!outerSize)
  241. {
  242. // Calculate window dimensions required
  243. // to get the requested client area
  244. SetRect(&rc, 0, 0, mWidth, mHeight);
  245. AdjustWindowRect(&rc, dwStyle, false);
  246. mWidth = rc.right - rc.left;
  247. mHeight = rc.bottom - rc.top;
  248. // Clamp window rect to the nearest display monitor.
  249. if (mLeft < monitorInfo.rcWork.left)
  250. mLeft = monitorInfo.rcWork.left;
  251. if (mTop < monitorInfo.rcWork.top)
  252. mTop = monitorInfo.rcWork.top;
  253. if (static_cast<int>(winWidth) > monitorInfo.rcWork.right - mLeft)
  254. winWidth = monitorInfo.rcWork.right - mLeft;
  255. if (static_cast<int>(winHeight) > monitorInfo.rcWork.bottom - mTop)
  256. winHeight = monitorInfo.rcWork.bottom - mTop;
  257. }
  258. }
  259. // Register the window class
  260. // NB allow 4 bytes of window data for D3D9RenderWindow pointer
  261. WNDCLASS wc = { 0, WindowEventUtilities::_WndProc, 0, 0, hInst,
  262. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  263. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "D3D9Wnd" };
  264. RegisterClass(&wc);
  265. // Create our main window
  266. // Pass pointer to self
  267. mIsExternal = false;
  268. mHWnd = CreateWindowEx(dwStyleEx, "D3D9Wnd", title.c_str(), dwStyle,
  269. mLeft, mTop, winWidth, winHeight, parentHWnd, 0, hInst, this);
  270. mStyle = dwStyle;
  271. WindowEventUtilities::_addRenderWindow(this);
  272. }
  273. else
  274. {
  275. mHWnd = externalHandle;
  276. mIsExternal = true;
  277. }
  278. RECT rc;
  279. // top and left represent outer window coordinates
  280. GetWindowRect(mHWnd, &rc);
  281. mTop = rc.top;
  282. mLeft = rc.left;
  283. // width and height represent interior drawable area
  284. GetClientRect(mHWnd, &rc);
  285. mWidth = rc.right;
  286. mHeight = rc.bottom;
  287. mName = name;
  288. mIsDepthBuffered = depthBuffer;
  289. mIsFullScreen = fullScreen;
  290. mColorDepth = colourDepth;
  291. mActive = true;
  292. mClosed = false;
  293. }
  294. void D3D9RenderWindow::setFullscreen(bool fullScreen, unsigned int width, unsigned int height)
  295. {
  296. if (fullScreen != mIsFullScreen || width != mWidth || height != mHeight)
  297. {
  298. if (fullScreen != mIsFullScreen)
  299. mSwitchingFullscreen = true;
  300. mStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  301. bool oldFullscreen = mIsFullScreen;
  302. mIsFullScreen = fullScreen;
  303. mWidth = mDesiredWidth = width;
  304. mHeight = mDesiredHeight = height;
  305. if (fullScreen)
  306. {
  307. mStyle |= WS_POPUP;
  308. // Get the nearest monitor to this window.
  309. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  310. // Get monitor info
  311. MONITORINFO monitorInfo;
  312. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  313. monitorInfo.cbSize = sizeof(MONITORINFO);
  314. GetMonitorInfo(hMonitor, &monitorInfo);
  315. mTop = monitorInfo.rcMonitor.top;
  316. mLeft = monitorInfo.rcMonitor.left;
  317. // need different ordering here
  318. if (oldFullscreen)
  319. {
  320. // was previously fullscreen, just changing the resolution
  321. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height, SWP_NOACTIVATE);
  322. }
  323. else
  324. {
  325. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height, SWP_NOACTIVATE);
  326. //MoveWindow(mHWnd, mLeft, mTop, mWidth, mHeight, FALSE);
  327. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  328. SetWindowPos(mHWnd, 0, 0,0, 0,0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
  329. }
  330. }
  331. else
  332. {
  333. mStyle |= WS_OVERLAPPEDWINDOW;
  334. // Calculate window dimensions required
  335. // to get the requested client area
  336. unsigned int winWidth, winHeight;
  337. adjustWindow(mWidth, mHeight, mStyle, &winWidth, &winHeight);
  338. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  339. SetWindowPos(mHWnd, HWND_NOTOPMOST, 0, 0, winWidth, winHeight,
  340. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE);
  341. // Note that we also set the position in the restoreLostDevice method
  342. // via _finishSwitchingFullScreen
  343. }
  344. // Have to release & trigger device reset
  345. // NB don't use windowMovedOrResized since Win32 doesn't know
  346. // about the size change yet
  347. mDevice->invalidate(this);
  348. // TODO - Notify viewports of resize
  349. }
  350. }
  351. void D3D9RenderWindow::adjustWindow(unsigned int clientWidth, unsigned int clientHeight,
  352. DWORD style, unsigned int* winWidth, unsigned int* winHeight)
  353. {
  354. // NB only call this for non full screen
  355. RECT rc;
  356. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  357. AdjustWindowRect(&rc, style, false);
  358. *winWidth = rc.right - rc.left;
  359. *winHeight = rc.bottom - rc.top;
  360. // adjust to monitor
  361. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  362. // Get monitor info
  363. MONITORINFO monitorInfo;
  364. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  365. monitorInfo.cbSize = sizeof(MONITORINFO);
  366. GetMonitorInfo(hMonitor, &monitorInfo);
  367. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  368. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  369. if (*winWidth > (unsigned int)maxW)
  370. *winWidth = maxW;
  371. if (*winHeight > (unsigned int)maxH)
  372. *winHeight = maxH;
  373. }
  374. void D3D9RenderWindow::_finishSwitchingFullscreen()
  375. {
  376. if(mIsFullScreen)
  377. {
  378. // Need to reset the region on the window sometimes, when the
  379. // windowed mode was constrained by desktop
  380. HRGN hRgn = CreateRectRgn(0,0,mWidth, mHeight);
  381. SetWindowRgn(mHWnd, hRgn, FALSE);
  382. }
  383. else
  384. {
  385. // When switching back to windowed mode, need to reset window size
  386. // after device has been restored
  387. // We may have had a resize event which polluted our desired sizes
  388. unsigned int winWidth, winHeight;
  389. adjustWindow(mDesiredWidth, mDesiredHeight, mStyle, &winWidth, &winHeight);
  390. // deal with centreing when switching down to smaller resolution
  391. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  392. MONITORINFO monitorInfo;
  393. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  394. monitorInfo.cbSize = sizeof(MONITORINFO);
  395. GetMonitorInfo(hMonitor, &monitorInfo);
  396. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  397. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  398. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  399. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  400. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  401. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  402. if (mWidth != mDesiredWidth ||
  403. mHeight != mDesiredHeight)
  404. {
  405. mWidth = mDesiredWidth;
  406. mHeight = mDesiredHeight;
  407. // TODO - Notify viewports of resize
  408. }
  409. }
  410. mSwitchingFullscreen = false;
  411. }
  412. void D3D9RenderWindow::buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams)
  413. {
  414. // Set up the presentation parameters
  415. IDirect3D9* pD3D = D3D9RenderSystem::getDirect3D9();
  416. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  417. if (mDevice != NULL)
  418. devType = mDevice->getDeviceType();
  419. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  420. presentParams->Windowed = !mIsFullScreen;
  421. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  422. // triple buffer if VSync is on
  423. presentParams->BackBufferCount = mVSync ? 2 : 1;
  424. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  425. presentParams->hDeviceWindow = mHWnd;
  426. presentParams->BackBufferWidth = mWidth;
  427. presentParams->BackBufferHeight = mHeight;
  428. presentParams->FullScreen_RefreshRateInHz = mIsFullScreen ? mDisplayFrequency : 0;
  429. if (presentParams->BackBufferWidth == 0)
  430. presentParams->BackBufferWidth = 1;
  431. if (presentParams->BackBufferHeight == 0)
  432. presentParams->BackBufferHeight = 1;
  433. if (mVSync)
  434. {
  435. // D3D9 only seems to support 2-4 presentation intervals in fullscreen
  436. if (mIsFullScreen)
  437. {
  438. switch(mVSyncInterval)
  439. {
  440. case 1:
  441. default:
  442. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  443. break;
  444. case 2:
  445. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  446. break;
  447. case 3:
  448. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  449. break;
  450. case 4:
  451. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  452. break;
  453. };
  454. // check that the interval was supported, revert to 1 to be safe otherwise
  455. D3DCAPS9 caps;
  456. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  457. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  458. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  459. }
  460. else
  461. {
  462. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  463. }
  464. }
  465. else
  466. {
  467. // NB not using vsync in windowed mode in D3D9 can cause jerking at low
  468. // frame rates no matter what buffering modes are used (odd - perhaps a
  469. // timer issue in D3D9 since GL doesn't suffer from this)
  470. // low is < 200fps in this context
  471. if (!mIsFullScreen)
  472. {
  473. // TODO LOG PORT - Enable this warning later?
  474. //LogManager::getSingleton().logMessage("D3D9 : WARNING - "
  475. // "disabling VSync in windowed mode can cause timing issues at lower "
  476. // "frame rates, turn VSync on if you observe this problem.");
  477. }
  478. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  479. }
  480. presentParams->BackBufferFormat = D3DFMT_R5G6B5;
  481. if( mColorDepth > 16 )
  482. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  483. if (mColorDepth > 16 )
  484. {
  485. // Try to create a 32-bit depth, 8-bit stencil
  486. if( FAILED( pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  487. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  488. D3DRTYPE_SURFACE, D3DFMT_D24S8 )))
  489. {
  490. // Bugger, no 8-bit hardware stencil, just try 32-bit zbuffer
  491. if( FAILED( pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  492. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  493. D3DRTYPE_SURFACE, D3DFMT_D32 )))
  494. {
  495. // Jeez, what a naff card. Fall back on 16-bit depth buffering
  496. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  497. }
  498. else
  499. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  500. }
  501. else
  502. {
  503. // Woohoo!
  504. if( SUCCEEDED( pD3D->CheckDepthStencilMatch( mDevice->getAdapterNumber(), devType,
  505. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8 ) ) )
  506. {
  507. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  508. }
  509. else
  510. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  511. }
  512. }
  513. else
  514. // 16-bit depth, software stencil
  515. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  516. D3D9RenderSystem* rsys = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystem::instancePtr());
  517. rsys->determineFSAASettings(mDevice->getD3D9Device(),
  518. mFSAA, mFSAAHint, presentParams->BackBufferFormat, mIsFullScreen,
  519. &mFSAAType, &mFSAAQuality);
  520. presentParams->MultiSampleType = mFSAAType;
  521. presentParams->MultiSampleQuality = (mFSAAQuality == 0) ? 0 : mFSAAQuality;
  522. // Check sRGB
  523. if (mHwGamma)
  524. {
  525. /* hmm, this never succeeds even when device does support??
  526. if(FAILED(pD3D->CheckDeviceFormat(mDriver->getAdapterNumber(),
  527. devType, presentParams->BackBufferFormat, D3DUSAGE_QUERY_SRGBWRITE,
  528. D3DRTYPE_SURFACE, presentParams->BackBufferFormat )))
  529. {
  530. // disable - not supported
  531. mHwGamma = false;
  532. }
  533. */
  534. }
  535. }
  536. void D3D9RenderWindow::destroy()
  537. {
  538. if (mDevice != NULL)
  539. {
  540. mDevice->detachRenderWindow(this);
  541. mDevice = NULL;
  542. }
  543. if (mHWnd && !mIsExternal)
  544. {
  545. WindowEventUtilities::_removeRenderWindow(this);
  546. DestroyWindow(mHWnd);
  547. }
  548. mHWnd = 0;
  549. mActive = false;
  550. mClosed = true;
  551. }
  552. bool D3D9RenderWindow::isActive() const
  553. {
  554. if (isFullScreen())
  555. return isVisible();
  556. return mActive && isVisible();
  557. }
  558. bool D3D9RenderWindow::isVisible() const
  559. {
  560. return (mHWnd && !IsIconic(mHWnd));
  561. }
  562. void D3D9RenderWindow::reposition(int top, int left)
  563. {
  564. if (mHWnd && !mIsFullScreen)
  565. {
  566. SetWindowPos(mHWnd, 0, top, left, 0, 0,
  567. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  568. }
  569. }
  570. void D3D9RenderWindow::resize(unsigned int width, unsigned int height)
  571. {
  572. if (mHWnd && !mIsFullScreen)
  573. {
  574. unsigned int winWidth, winHeight;
  575. adjustWindow(width, height, mStyle, &winWidth, &winHeight);
  576. SetWindowPos(mHWnd, 0, 0, 0, winWidth, winHeight,
  577. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  578. }
  579. }
  580. void D3D9RenderWindow::windowMovedOrResized()
  581. {
  582. if (!mHWnd || IsIconic(mHWnd))
  583. return;
  584. updateWindowRect();
  585. }
  586. void D3D9RenderWindow::swapBuffers( bool waitForVSync )
  587. {
  588. if (mDeviceValid)
  589. mDevice->present(this);
  590. }
  591. void D3D9RenderWindow::getCustomAttribute_internal( const String& name, void* pData )
  592. {
  593. // Valid attributes and their equvalent native functions:
  594. // D3DDEVICE : getD3DDevice
  595. // WINDOW : getWindowHandle
  596. if( name == "D3DDEVICE" )
  597. {
  598. IDirect3DDevice9* *pDev = (IDirect3DDevice9**)pData;
  599. *pDev = getD3D9Device();
  600. return;
  601. }
  602. else if( name == "WINDOW" )
  603. {
  604. HWND *pHwnd = (HWND*)pData;
  605. *pHwnd = getWindowHandle();
  606. return;
  607. }
  608. else if( name == "isTexture" )
  609. {
  610. bool *b = reinterpret_cast< bool * >( pData );
  611. *b = false;
  612. return;
  613. }
  614. else if( name == "D3DZBUFFER" )
  615. {
  616. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  617. *pSurf = mDevice->getDepthBuffer(this);
  618. return;
  619. }
  620. else if( name == "DDBACKBUFFER" )
  621. {
  622. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  623. *pSurf = mDevice->getBackBuffer(this);
  624. return;
  625. }
  626. else if( name == "DDFRONTBUFFER" )
  627. {
  628. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  629. *pSurf = mDevice->getBackBuffer(this);
  630. return;
  631. }
  632. }
  633. void D3D9RenderWindow::copyContentsToMemory(const PixelData &dst, FrameBuffer buffer)
  634. {
  635. mDevice->copyContentsToMemory(this, dst, buffer);
  636. }
  637. //-----------------------------------------------------------------------------
  638. void D3D9RenderWindow::_beginUpdate()
  639. {
  640. // External windows should update per frame
  641. // since it dosen't get the window resize/move messages.
  642. if (mIsExternal)
  643. {
  644. updateWindowRect();
  645. }
  646. if (mWidth == 0 || mHeight == 0)
  647. {
  648. mDeviceValid = false;
  649. return;
  650. }
  651. D3D9RenderSystem::getDeviceManager()->setActiveRenderTargetDevice(mDevice);
  652. // Check that device can be used for rendering operations.
  653. mDeviceValid = mDevice->validate(this);
  654. if (mDeviceValid)
  655. {
  656. // Finish window / fullscreen mode switch.
  657. if (_getSwitchingFullscreen())
  658. {
  659. _finishSwitchingFullscreen();
  660. // have to re-validate since this may have altered dimensions
  661. mDeviceValid = mDevice->validate(this);
  662. }
  663. }
  664. RenderWindow::_beginUpdate();
  665. }
  666. //---------------------------------------------------------------------
  667. void D3D9RenderWindow::_endUpdate()
  668. {
  669. RenderWindow::_endUpdate();
  670. D3D9RenderSystem::getDeviceManager()->setActiveRenderTargetDevice(NULL);
  671. }
  672. //-----------------------------------------------------------------------------
  673. IDirect3DDevice9* D3D9RenderWindow::getD3D9Device()
  674. {
  675. return mDevice->getD3D9Device();
  676. }
  677. //-----------------------------------------------------------------------------
  678. IDirect3DSurface9* D3D9RenderWindow::getRenderSurface()
  679. {
  680. return mDevice->getBackBuffer(this);
  681. }
  682. //-----------------------------------------------------------------------------
  683. bool D3D9RenderWindow::_getSwitchingFullscreen() const
  684. {
  685. return mSwitchingFullscreen;
  686. }
  687. //-----------------------------------------------------------------------------
  688. D3D9Device* D3D9RenderWindow::getDevice()
  689. {
  690. return mDevice;
  691. }
  692. //-----------------------------------------------------------------------------
  693. void D3D9RenderWindow::setDevice(D3D9Device* device)
  694. {
  695. mDevice = device;
  696. mDeviceValid = false;
  697. }
  698. //-----------------------------------------------------------------------------
  699. bool D3D9RenderWindow::isDepthBuffered() const
  700. {
  701. return mIsDepthBuffered;
  702. }
  703. //-----------------------------------------------------------------------------
  704. void D3D9RenderWindow::updateWindowRect()
  705. {
  706. RECT rc;
  707. BOOL result;
  708. // Update top left parameters
  709. result = GetWindowRect(mHWnd, &rc);
  710. if (result == FALSE)
  711. {
  712. mTop = 0;
  713. mLeft = 0;
  714. mWidth = 0;
  715. mHeight = 0;
  716. return;
  717. }
  718. mTop = rc.top;
  719. mLeft = rc.left;
  720. // width and height represent drawable area only
  721. result = GetClientRect(mHWnd, &rc);
  722. if (result == FALSE)
  723. {
  724. mTop = 0;
  725. mLeft = 0;
  726. mWidth = 0;
  727. mHeight = 0;
  728. return;
  729. }
  730. unsigned int width = rc.right - rc.left;
  731. unsigned int height = rc.bottom - rc.top;
  732. // Case window resized.
  733. if (width != mWidth || height != mHeight)
  734. {
  735. mWidth = rc.right - rc.left;
  736. mHeight = rc.bottom - rc.top;
  737. // TODO - Notify viewports of resize
  738. }
  739. }
  740. //-----------------------------------------------------------------------------
  741. bool D3D9RenderWindow::isNvPerfHUDEnable() const
  742. {
  743. return mUseNVPerfHUD;
  744. }
  745. //---------------------------------------------------------------------
  746. bool D3D9RenderWindow::_validateDevice()
  747. {
  748. mDeviceValid = mDevice->validate(this);
  749. return mDeviceValid;
  750. }
  751. }