CmWin32Window.cpp 22 KB

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