CmWin32Window.cpp 23 KB

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