CmWin32Window.cpp 23 KB

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