CmWin32Window.cpp 24 KB

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