CmWin32Window.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. #ifndef _WIN32_WINNT
  25. #define _WIN32_WINNT 0x0500
  26. #endif
  27. #include "CmWin32Window.h"
  28. #include "CmRenderSystem.h"
  29. #include "CmException.h"
  30. #include "CmWin32GLSupport.h"
  31. #include "CmWin32Context.h"
  32. #include "CmWindowEventUtilities.h"
  33. #include "CmGLPixelFormat.h"
  34. namespace CamelotEngine {
  35. #define _MAX_CLASS_NAME_ 128
  36. Win32Window::Win32Window(Win32GLSupport &glsupport):
  37. mGLSupport(glsupport),
  38. mContext(0)
  39. {
  40. mIsFullScreen = false;
  41. mHWnd = 0;
  42. mGlrc = 0;
  43. mIsExternal = false;
  44. mIsExternalGLControl = false;
  45. mIsExternalGLContext = false;
  46. mSizing = false;
  47. mClosed = false;
  48. mDisplayFrequency = 0;
  49. mActive = false;
  50. mDeviceName = NULL;
  51. }
  52. Win32Window::~Win32Window()
  53. {
  54. destroy();
  55. }
  56. void Win32Window::initialize(const String& name, unsigned int width, unsigned int height,
  57. bool fullScreen, const NameValuePairList *miscParams)
  58. {
  59. // destroy current window, if any
  60. if (mHWnd)
  61. destroy();
  62. #ifdef CM_STATIC_LIB
  63. HINSTANCE hInst = GetModuleHandle( NULL );
  64. #else
  65. HINSTANCE hInst = GetModuleHandle("CamelotGLRenderSystem.dll");
  66. #endif
  67. mHWnd = 0;
  68. mName = name;
  69. mIsFullScreen = fullScreen;
  70. mClosed = false;
  71. mDisplayFrequency = 0;
  72. mColorDepth = mIsFullScreen? 32 : GetDeviceCaps(GetDC(0), BITSPIXEL);
  73. int left = -1; // Defaults to screen center
  74. int top = -1; // Defaults to screen center
  75. HWND parent = 0;
  76. String title = name;
  77. bool vsync = false;
  78. unsigned int vsyncInterval = 1;
  79. String border;
  80. bool outerSize = false;
  81. bool hwGamma = false;
  82. int monitorIndex = -1;
  83. HMONITOR hMonitor = NULL;
  84. if(miscParams)
  85. {
  86. // Get variable-length params
  87. NameValuePairList::const_iterator opt;
  88. NameValuePairList::const_iterator end = miscParams->end();
  89. if ((opt = miscParams->find("title")) != end)
  90. title = opt->second;
  91. if ((opt = miscParams->find("left")) != end)
  92. left = parseInt(opt->second);
  93. if ((opt = miscParams->find("top")) != end)
  94. top = parseInt(opt->second);
  95. if ((opt = miscParams->find("vsync")) != end)
  96. vsync = parseBool(opt->second);
  97. if ((opt = miscParams->find("vsyncInterval")) != end)
  98. vsyncInterval = parseUnsignedInt(opt->second);
  99. if ((opt = miscParams->find("FSAA")) != end)
  100. mFSAA = parseUnsignedInt(opt->second);
  101. if ((opt = miscParams->find("FSAAHint")) != end)
  102. mFSAAHint = opt->second;
  103. if ((opt = miscParams->find("gamma")) != end)
  104. hwGamma = parseBool(opt->second);
  105. if ((opt = miscParams->find("externalWindowHandle")) != end)
  106. {
  107. mHWnd = (HWND)parseUnsignedInt(opt->second);
  108. if (mHWnd)
  109. {
  110. mIsExternal = true;
  111. mIsFullScreen = false;
  112. }
  113. if ((opt = miscParams->find("externalGLControl")) != end) {
  114. mIsExternalGLControl = parseBool(opt->second);
  115. }
  116. }
  117. if ((opt = miscParams->find("externalGLContext")) != end)
  118. {
  119. mGlrc = (HGLRC)parseUnsignedLong(opt->second);
  120. if( mGlrc )
  121. mIsExternalGLContext = true;
  122. }
  123. // window border style
  124. opt = miscParams->find("border");
  125. if(opt != miscParams->end())
  126. border = opt->second;
  127. // set outer dimensions?
  128. opt = miscParams->find("outerDimensions");
  129. if(opt != miscParams->end())
  130. outerSize = parseBool(opt->second);
  131. // only available with fullscreen
  132. if ((opt = miscParams->find("displayFrequency")) != end)
  133. mDisplayFrequency = parseUnsignedInt(opt->second);
  134. if ((opt = miscParams->find("colourDepth")) != end)
  135. {
  136. mColorDepth = parseUnsignedInt(opt->second);
  137. if (!mIsFullScreen)
  138. {
  139. // make sure we don't exceed desktop colour depth
  140. if ((int)mColorDepth > GetDeviceCaps(GetDC(0), BITSPIXEL))
  141. mColorDepth = GetDeviceCaps(GetDC(0), BITSPIXEL);
  142. }
  143. }
  144. // incompatible with fullscreen
  145. if ((opt = miscParams->find("parentWindowHandle")) != end)
  146. parent = (HWND)parseUnsignedInt(opt->second);
  147. // monitor index
  148. if ((opt = miscParams->find("monitorIndex")) != end)
  149. monitorIndex = parseInt(opt->second);
  150. // monitor handle
  151. if ((opt = miscParams->find("monitorHandle")) != end)
  152. hMonitor = (HMONITOR)parseInt(opt->second);
  153. }
  154. if (!mIsExternal)
  155. {
  156. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  157. DWORD dwStyleEx = 0;
  158. MONITORINFOEX monitorInfoEx;
  159. RECT rc;
  160. // If we didn't specified the adapter index, or if it didn't find it
  161. if (hMonitor == NULL)
  162. {
  163. POINT windowAnchorPoint;
  164. // Fill in anchor point.
  165. windowAnchorPoint.x = left;
  166. windowAnchorPoint.y = top;
  167. // Get the nearest monitor to this window.
  168. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTONEAREST);
  169. }
  170. // Get the target monitor info
  171. memset(&monitorInfoEx, 0, sizeof(MONITORINFOEX));
  172. monitorInfoEx.cbSize = sizeof(MONITORINFOEX);
  173. GetMonitorInfo(hMonitor, &monitorInfoEx);
  174. size_t devNameLen = strlen(monitorInfoEx.szDevice);
  175. mDeviceName = new char[devNameLen + 1];
  176. strcpy_s(mDeviceName, devNameLen + 1, monitorInfoEx.szDevice);
  177. // No specified top left -> Center the window in the middle of the monitor
  178. if (left == -1 || top == -1)
  179. {
  180. int screenw = monitorInfoEx.rcWork.right - monitorInfoEx.rcWork.left;
  181. int screenh = monitorInfoEx.rcWork.bottom - monitorInfoEx.rcWork.top;
  182. unsigned int winWidth, winHeight;
  183. adjustWindow(width, height, &winWidth, &winHeight);
  184. // clamp window dimensions to screen size
  185. int outerw = (int(winWidth) < screenw)? int(winWidth) : screenw;
  186. int outerh = (int(winHeight) < screenh)? int(winHeight) : screenh;
  187. if (left == -1)
  188. left = monitorInfoEx.rcWork.left + (screenw - outerw) / 2;
  189. else if (monitorIndex != -1)
  190. left += monitorInfoEx.rcWork.left;
  191. if (top == -1)
  192. top = monitorInfoEx.rcWork.top + (screenh - outerh) / 2;
  193. else if (monitorIndex != -1)
  194. top += monitorInfoEx.rcWork.top;
  195. }
  196. else if (monitorIndex != -1)
  197. {
  198. left += monitorInfoEx.rcWork.left;
  199. top += monitorInfoEx.rcWork.top;
  200. }
  201. mWidth = width;
  202. mHeight = height;
  203. mTop = top;
  204. mLeft = left;
  205. if (mIsFullScreen)
  206. {
  207. dwStyle |= WS_POPUP;
  208. dwStyleEx |= WS_EX_TOPMOST;
  209. mTop = monitorInfoEx.rcMonitor.top;
  210. mLeft = monitorInfoEx.rcMonitor.left;
  211. }
  212. else
  213. {
  214. if (parent)
  215. {
  216. dwStyle |= WS_CHILD;
  217. }
  218. else
  219. {
  220. if (border == "none")
  221. dwStyle |= WS_POPUP;
  222. else if (border == "fixed")
  223. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  224. WS_SYSMENU | WS_MINIMIZEBOX;
  225. else
  226. dwStyle |= WS_OVERLAPPEDWINDOW;
  227. }
  228. int screenw = GetSystemMetrics(SM_CXSCREEN);
  229. int screenh = GetSystemMetrics(SM_CYSCREEN);
  230. if (!outerSize)
  231. {
  232. // Calculate window dimensions required
  233. // to get the requested client area
  234. SetRect(&rc, 0, 0, mWidth, mHeight);
  235. AdjustWindowRect(&rc, dwStyle, false);
  236. mWidth = rc.right - rc.left;
  237. mHeight = rc.bottom - rc.top;
  238. // Clamp window rect to the nearest display monitor.
  239. if (mLeft < monitorInfoEx.rcWork.left)
  240. mLeft = monitorInfoEx.rcWork.left;
  241. if (mTop < monitorInfoEx.rcWork.top)
  242. mTop = monitorInfoEx.rcWork.top;
  243. if ((int)mWidth > monitorInfoEx.rcWork.right - mLeft)
  244. mWidth = monitorInfoEx.rcWork.right - mLeft;
  245. if ((int)mHeight > monitorInfoEx.rcWork.bottom - mTop)
  246. mHeight = monitorInfoEx.rcWork.bottom - mTop;
  247. }
  248. }
  249. // register class and create window
  250. WNDCLASS wc = { CS_OWNDC, WindowEventUtilities::_WndProc, 0, 0, hInst,
  251. LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  252. (HBRUSH)GetStockObject(BLACK_BRUSH), NULL, "GLWindow" };
  253. RegisterClass(&wc);
  254. if (mIsFullScreen)
  255. {
  256. DEVMODE displayDeviceMode;
  257. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  258. displayDeviceMode.dmSize = sizeof(DEVMODE);
  259. displayDeviceMode.dmBitsPerPel = mColorDepth;
  260. displayDeviceMode.dmPelsWidth = mWidth;
  261. displayDeviceMode.dmPelsHeight = mHeight;
  262. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  263. if (mDisplayFrequency)
  264. {
  265. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  266. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  267. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  268. {
  269. // TODO LOG PORT - Log this somewhere
  270. //LogManager::getSingleton().logMessage(LML_NORMAL, "ChangeDisplaySettings with user display frequency failed");
  271. //displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  272. }
  273. }
  274. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  275. {
  276. // TODO LOG PORT - Log this somewhere
  277. //LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
  278. }
  279. }
  280. // Pass pointer to self as WM_CREATE parameter
  281. mHWnd = CreateWindowEx(dwStyleEx, "GLWindow", title.c_str(),
  282. dwStyle, mLeft, mTop, mWidth, mHeight, parent, 0, hInst, this);
  283. WindowEventUtilities::_addRenderWindow(this);
  284. }
  285. HDC old_hdc = wglGetCurrentDC();
  286. HGLRC old_context = wglGetCurrentContext();
  287. RECT rc;
  288. // top and left represent outer window position
  289. GetWindowRect(mHWnd, &rc);
  290. mTop = rc.top;
  291. mLeft = rc.left;
  292. // width and height represent drawable area only
  293. GetClientRect(mHWnd, &rc);
  294. mWidth = rc.right;
  295. mHeight = rc.bottom;
  296. mHDC = GetDC(mHWnd);
  297. if (!mIsExternalGLControl)
  298. {
  299. int testFsaa = mFSAA;
  300. bool testHwGamma = hwGamma;
  301. bool formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  302. if (!formatOk)
  303. {
  304. if (mFSAA > 0)
  305. {
  306. // try without FSAA
  307. testFsaa = 0;
  308. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  309. }
  310. if (!formatOk && hwGamma)
  311. {
  312. // try without sRGB
  313. testHwGamma = false;
  314. testFsaa = mFSAA;
  315. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  316. }
  317. if (!formatOk && hwGamma && (mFSAA > 0))
  318. {
  319. // try without both
  320. testHwGamma = false;
  321. testFsaa = 0;
  322. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  323. }
  324. if (!formatOk)
  325. CM_EXCEPT(RenderingAPIException, "selectPixelFormat failed");
  326. }
  327. // record what gamma option we used in the end
  328. // this will control enabling of sRGB state flags when used
  329. mHwGamma = testHwGamma;
  330. mFSAA = testFsaa;
  331. }
  332. if (!mIsExternalGLContext)
  333. {
  334. mGlrc = wglCreateContext(mHDC);
  335. if (!mGlrc)
  336. {
  337. CM_EXCEPT(RenderingAPIException,
  338. "wglCreateContext failed: " + translateWGLError());
  339. }
  340. }
  341. if (!wglMakeCurrent(mHDC, mGlrc))
  342. {
  343. CM_EXCEPT(RenderingAPIException, "wglMakeCurrent");
  344. }
  345. // Do not change vsync if the external window has the OpenGL control
  346. if (!mIsExternalGLControl) {
  347. // Don't use wglew as if this is the first window, we won't have initialised yet
  348. PFNWGLSWAPINTERVALEXTPROC _wglSwapIntervalEXT =
  349. (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  350. if (_wglSwapIntervalEXT)
  351. _wglSwapIntervalEXT(vsync? vsyncInterval : 0);
  352. }
  353. if (old_context && old_context != mGlrc)
  354. {
  355. // Restore old context
  356. if (!wglMakeCurrent(old_hdc, old_context))
  357. CM_EXCEPT(RenderingAPIException, "wglMakeCurrent() failed");
  358. // Share lists with old context
  359. if (!wglShareLists(old_context, mGlrc))
  360. CM_EXCEPT(RenderingAPIException, "wglShareLists() failed");
  361. }
  362. // Create RenderSystem context
  363. mContext = new Win32Context(mHDC, mGlrc);
  364. mActive = true;
  365. }
  366. void Win32Window::adjustWindow(unsigned int clientWidth, unsigned int clientHeight,
  367. unsigned int* winWidth, unsigned int* winHeight)
  368. {
  369. // NB only call this for non full screen
  370. RECT rc;
  371. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  372. AdjustWindowRect(&rc, WS_VISIBLE | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW, false);
  373. *winWidth = rc.right - rc.left;
  374. *winHeight = rc.bottom - rc.top;
  375. // adjust to monitor
  376. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  377. // Get monitor info
  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 Win32Window::setFullscreen(bool fullScreen, unsigned int width, unsigned int height)
  390. {
  391. if (mIsFullScreen != fullScreen || width != mWidth || height != mHeight)
  392. {
  393. mIsFullScreen = fullScreen;
  394. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  395. if (mIsFullScreen)
  396. {
  397. dwStyle |= WS_POPUP;
  398. DEVMODE displayDeviceMode;
  399. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  400. displayDeviceMode.dmSize = sizeof(DEVMODE);
  401. displayDeviceMode.dmBitsPerPel = mColorDepth;
  402. displayDeviceMode.dmPelsWidth = width;
  403. displayDeviceMode.dmPelsHeight = height;
  404. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  405. if (mDisplayFrequency)
  406. {
  407. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  408. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  409. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  410. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  411. {
  412. // TODO LOG PORT - Log this somewhere
  413. //LogManager::getSingleton().logMessage(LML_NORMAL, "ChangeDisplaySettings with user display frequency failed");
  414. displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  415. }
  416. }
  417. else
  418. {
  419. // try a few
  420. displayDeviceMode.dmDisplayFrequency = 100;
  421. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  422. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  423. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  424. {
  425. displayDeviceMode.dmDisplayFrequency = 75;
  426. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  427. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  428. {
  429. displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  430. }
  431. }
  432. }
  433. // move window to 0,0 before display switch
  434. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, mWidth, mHeight, SWP_NOACTIVATE);
  435. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  436. {
  437. // TODO LOG PORT - Log this somewhere
  438. //LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
  439. }
  440. // Get the nearest monitor to this window.
  441. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  442. // Get monitor info
  443. MONITORINFO monitorInfo;
  444. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  445. monitorInfo.cbSize = sizeof(MONITORINFO);
  446. GetMonitorInfo(hMonitor, &monitorInfo);
  447. mTop = monitorInfo.rcMonitor.top;
  448. mLeft = monitorInfo.rcMonitor.left;
  449. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  450. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height,
  451. SWP_NOACTIVATE);
  452. mWidth = width;
  453. mHeight = height;
  454. }
  455. else
  456. {
  457. dwStyle |= WS_OVERLAPPEDWINDOW;
  458. // drop out of fullscreen
  459. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  460. // calculate overall dimensions for requested client area
  461. unsigned int winWidth, winHeight;
  462. adjustWindow(width, height, &winWidth, &winHeight);
  463. // deal with centreing when switching down to smaller resolution
  464. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  465. MONITORINFO monitorInfo;
  466. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  467. monitorInfo.cbSize = sizeof(MONITORINFO);
  468. GetMonitorInfo(hMonitor, &monitorInfo);
  469. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  470. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  471. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  472. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  473. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  474. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  475. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  476. mWidth = width;
  477. mHeight = height;
  478. windowMovedOrResized();
  479. }
  480. }
  481. }
  482. void Win32Window::destroy(void)
  483. {
  484. if (!mHWnd)
  485. return;
  486. // Unregister and destroy OGRE GLContext
  487. delete mContext;
  488. if (!mIsExternalGLContext && mGlrc)
  489. {
  490. wglDeleteContext(mGlrc);
  491. mGlrc = 0;
  492. }
  493. if (!mIsExternal)
  494. {
  495. WindowEventUtilities::_removeRenderWindow(this);
  496. if (mIsFullScreen)
  497. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  498. DestroyWindow(mHWnd);
  499. }
  500. else
  501. {
  502. // just release the DC
  503. ReleaseDC(mHWnd, mHDC);
  504. }
  505. mActive = false;
  506. mClosed = true;
  507. mHDC = 0; // no release thanks to CS_OWNDC wndclass style
  508. mHWnd = 0;
  509. if (mDeviceName != NULL)
  510. {
  511. delete[] mDeviceName;
  512. mDeviceName = NULL;
  513. }
  514. }
  515. bool Win32Window::isActive(void) const
  516. {
  517. if (isFullScreen())
  518. return isVisible();
  519. return mActive && isVisible();
  520. }
  521. bool Win32Window::isVisible() const
  522. {
  523. return (mHWnd && !IsIconic(mHWnd));
  524. }
  525. bool Win32Window::isClosed() const
  526. {
  527. return mClosed;
  528. }
  529. void Win32Window::reposition(int left, int top)
  530. {
  531. if (mHWnd && !mIsFullScreen)
  532. {
  533. SetWindowPos(mHWnd, 0, left, top, 0, 0,
  534. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  535. }
  536. }
  537. void Win32Window::resize(unsigned int width, unsigned int height)
  538. {
  539. if (mHWnd && !mIsFullScreen)
  540. {
  541. RECT rc = { 0, 0, width, height };
  542. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  543. width = rc.right - rc.left;
  544. height = rc.bottom - rc.top;
  545. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  546. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  547. }
  548. }
  549. void Win32Window::windowMovedOrResized()
  550. {
  551. if (!mHWnd || IsIconic(mHWnd))
  552. return;
  553. RECT rc;
  554. // top and left represent outer window position
  555. GetWindowRect(mHWnd, &rc);
  556. mTop = rc.top;
  557. mLeft = rc.left;
  558. // width and height represent drawable area only
  559. GetClientRect(mHWnd, &rc);
  560. if (mWidth == rc.right && mHeight == rc.bottom)
  561. return;
  562. mWidth = rc.right - rc.left;
  563. mHeight = rc.bottom - rc.top;
  564. // TODO - Notify viewports of resize
  565. }
  566. void Win32Window::swapBuffers(bool waitForVSync)
  567. {
  568. if (!mIsExternalGLControl) {
  569. SwapBuffers(mHDC);
  570. }
  571. }
  572. void Win32Window::copyContentsToMemory(const PixelData &dst, FrameBuffer buffer)
  573. {
  574. if ((dst.left < 0) || (dst.right > mWidth) ||
  575. (dst.top < 0) || (dst.bottom > mHeight) ||
  576. (dst.front != 0) || (dst.back != 1))
  577. {
  578. CM_EXCEPT(InvalidParametersException, "Invalid box.");
  579. }
  580. if (buffer == FB_AUTO)
  581. {
  582. buffer = mIsFullScreen? FB_FRONT : FB_BACK;
  583. }
  584. GLenum format = CamelotEngine::GLPixelUtil::getGLOriginFormat(dst.format);
  585. GLenum type = CamelotEngine::GLPixelUtil::getGLOriginDataType(dst.format);
  586. if ((format == GL_NONE) || (type == 0))
  587. {
  588. CM_EXCEPT(InvalidParametersException, "Unsupported format.");
  589. }
  590. // Must change the packing to ensure no overruns!
  591. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  592. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  593. glReadPixels((GLint)dst.left, (GLint)dst.top,
  594. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  595. format, type, dst.data);
  596. // restore default alignment
  597. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  598. //vertical flip
  599. {
  600. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.format);
  601. size_t height = dst.getHeight();
  602. UINT8 *tmpData = new UINT8[rowSpan * height];
  603. UINT8 *srcRow = (UINT8 *)dst.data, *tmpRow = tmpData + (height - 1) * rowSpan;
  604. while (tmpRow >= tmpData)
  605. {
  606. memcpy(tmpRow, srcRow, rowSpan);
  607. srcRow += rowSpan;
  608. tmpRow -= rowSpan;
  609. }
  610. memcpy(dst.data, tmpData, rowSpan * height);
  611. delete [] tmpData;
  612. }
  613. }
  614. void Win32Window::getCustomAttribute( const String& name, void* pData )
  615. {
  616. if( name == "GLCONTEXT" ) {
  617. *static_cast<GLContext**>(pData) = mContext;
  618. return;
  619. } else if( name == "WINDOW" )
  620. {
  621. HWND *pHwnd = (HWND*)pData;
  622. *pHwnd = getWindowHandle();
  623. return;
  624. }
  625. }
  626. void Win32Window::setActive( bool state )
  627. {
  628. if (mDeviceName != NULL && state == false)
  629. {
  630. HWND hActiveWindow = GetActiveWindow();
  631. char classNameSrc[_MAX_CLASS_NAME_ + 1];
  632. char classNameDst[_MAX_CLASS_NAME_ + 1];
  633. GetClassName(mHWnd, classNameSrc, _MAX_CLASS_NAME_);
  634. GetClassName(hActiveWindow, classNameDst, _MAX_CLASS_NAME_);
  635. if (strcmp(classNameDst, classNameSrc) == 0)
  636. {
  637. state = true;
  638. }
  639. }
  640. mActive = state;
  641. if( mIsFullScreen )
  642. {
  643. if( state == false )
  644. { //Restore Desktop
  645. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  646. ShowWindow(mHWnd, SW_SHOWMINNOACTIVE);
  647. }
  648. else
  649. { //Restore App
  650. ShowWindow(mHWnd, SW_SHOWNORMAL);
  651. DEVMODE displayDeviceMode;
  652. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  653. displayDeviceMode.dmSize = sizeof(DEVMODE);
  654. displayDeviceMode.dmBitsPerPel = mColorDepth;
  655. displayDeviceMode.dmPelsWidth = mWidth;
  656. displayDeviceMode.dmPelsHeight = mHeight;
  657. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  658. if (mDisplayFrequency)
  659. {
  660. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  661. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  662. }
  663. ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL);
  664. }
  665. }
  666. }
  667. }