2
0

CmWin32Window.cpp 24 KB

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