CmWin32Window.cpp 22 KB

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