CmWin32Window.cpp 22 KB

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