2
0

CmWin32Window.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 CamelotFramework {
  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 = (char*)cm_alloc<ScratchAlloc>((UINT32)(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. if(mDesc.toolWindow)
  170. dwStyleEx = WS_EX_TOOLWINDOW;
  171. else
  172. dwStyle |= WS_CHILD;
  173. }
  174. if (!parent || mDesc.toolWindow)
  175. {
  176. if (mDesc.border == WindowBorder::None)
  177. dwStyle |= WS_POPUP;
  178. else if (mDesc.border == WindowBorder::Fixed)
  179. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  180. WS_SYSMENU | WS_MINIMIZEBOX;
  181. else
  182. dwStyle |= WS_OVERLAPPEDWINDOW;
  183. }
  184. int screenw = GetSystemMetrics(SM_CXSCREEN);
  185. int screenh = GetSystemMetrics(SM_CYSCREEN);
  186. if (!mDesc.outerDimensions)
  187. {
  188. // Calculate window dimensions required
  189. // to get the requested client area
  190. SetRect(&rc, 0, 0, mWidth, mHeight);
  191. AdjustWindowRect(&rc, dwStyle, false);
  192. mWidth = rc.right - rc.left;
  193. mHeight = rc.bottom - rc.top;
  194. // Clamp window rect to the nearest display monitor.
  195. if (mLeft < monitorInfoEx.rcWork.left)
  196. mLeft = monitorInfoEx.rcWork.left;
  197. if (mTop < monitorInfoEx.rcWork.top)
  198. mTop = monitorInfoEx.rcWork.top;
  199. if ((int)mWidth > monitorInfoEx.rcWork.right - mLeft)
  200. mWidth = monitorInfoEx.rcWork.right - mLeft;
  201. if ((int)mHeight > monitorInfoEx.rcWork.bottom - mTop)
  202. mHeight = monitorInfoEx.rcWork.bottom - mTop;
  203. }
  204. }
  205. // register class and create window
  206. WNDCLASS wc = { CS_OWNDC, WindowEventUtilities::_WndProc, 0, 0, hInst,
  207. LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  208. (HBRUSH)GetStockObject(BLACK_BRUSH), NULL, "GLWindow" };
  209. RegisterClass(&wc);
  210. if (mIsFullScreen)
  211. {
  212. DEVMODE displayDeviceMode;
  213. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  214. displayDeviceMode.dmSize = sizeof(DEVMODE);
  215. displayDeviceMode.dmBitsPerPel = mColorDepth;
  216. displayDeviceMode.dmPelsWidth = mWidth;
  217. displayDeviceMode.dmPelsHeight = mHeight;
  218. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  219. if (mDisplayFrequency)
  220. {
  221. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  222. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  223. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  224. {
  225. // TODO LOG PORT - Log this somewhere
  226. //LogManager::getSingleton().logMessage(LML_NORMAL, "ChangeDisplaySettings with user display frequency failed");
  227. //displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  228. }
  229. }
  230. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  231. {
  232. // TODO LOG PORT - Log this somewhere
  233. //LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
  234. }
  235. }
  236. // Pass pointer to self as WM_CREATE parameter
  237. mHWnd = CreateWindowEx(dwStyleEx, "GLWindow", mDesc.title.c_str(),
  238. dwStyle, mLeft, mTop, mWidth, mHeight, parent, 0, hInst, this);
  239. WindowEventUtilities::_addRenderWindow(this);
  240. }
  241. RECT rc;
  242. // top and left represent outer window position
  243. GetWindowRect(mHWnd, &rc);
  244. mTop = rc.top;
  245. mLeft = rc.left;
  246. // width and height represent drawable area only
  247. GetClientRect(mHWnd, &rc);
  248. mWidth = rc.right;
  249. mHeight = rc.bottom;
  250. mHDC = GetDC(mHWnd);
  251. if (!mIsExternalGLControl)
  252. {
  253. int testFsaa = mFSAA;
  254. bool testHwGamma = mDesc.gamma;
  255. bool formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  256. if (!formatOk)
  257. {
  258. if (mFSAA > 0)
  259. {
  260. // try without FSAA
  261. testFsaa = 0;
  262. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  263. }
  264. if (!formatOk && mDesc.gamma)
  265. {
  266. // try without sRGB
  267. testHwGamma = false;
  268. testFsaa = mFSAA;
  269. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  270. }
  271. if (!formatOk && mDesc.gamma && (mFSAA > 0))
  272. {
  273. // try without both
  274. testHwGamma = false;
  275. testFsaa = 0;
  276. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  277. }
  278. if (!formatOk)
  279. CM_EXCEPT(RenderingAPIException, "selectPixelFormat failed");
  280. }
  281. // record what gamma option we used in the end
  282. // this will control enabling of sRGB state flags when used
  283. mHwGamma = testHwGamma;
  284. mFSAA = testFsaa;
  285. }
  286. mActive = true;
  287. GLRenderSystem* rs = static_cast<GLRenderSystem*>(RenderSystem::instancePtr());
  288. // If RenderSystem has initialized a context use that, otherwise we create our own
  289. if(!rs->isContextInitialized())
  290. {
  291. if (!mIsExternalGLContext)
  292. {
  293. mGlrc = wglCreateContext(mHDC);
  294. if (!mGlrc)
  295. {
  296. CM_EXCEPT(RenderingAPIException,
  297. "wglCreateContext failed: " + translateWGLError());
  298. }
  299. }
  300. }
  301. else
  302. {
  303. rs->getMainContext()->setCurrent();
  304. mGlrc = wglGetCurrentContext();
  305. }
  306. // Create RenderSystem context
  307. mContext = cm_new<Win32Context>(mHDC, mGlrc);
  308. RenderWindow::initialize_internal();
  309. }
  310. void Win32Window::destroy_internal()
  311. {
  312. if (!mHWnd)
  313. return;
  314. // Unregister and destroy GLContext
  315. cm_delete(mContext);
  316. if (!mIsExternalGLContext && mGlrc)
  317. {
  318. wglDeleteContext(mGlrc);
  319. mGlrc = 0;
  320. }
  321. if (!mIsExternal)
  322. {
  323. WindowEventUtilities::_removeRenderWindow(this);
  324. if (mIsFullScreen)
  325. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  326. DestroyWindow(mHWnd);
  327. }
  328. else
  329. {
  330. // just release the DC
  331. ReleaseDC(mHWnd, mHDC);
  332. }
  333. mActive = false;
  334. mClosed = true;
  335. mHDC = 0; // no release thanks to CS_OWNDC wndclass style
  336. mHWnd = 0;
  337. if (mDeviceName != NULL)
  338. {
  339. cm_free<ScratchAlloc>(mDeviceName);
  340. mDeviceName = NULL;
  341. }
  342. RenderWindow::destroy_internal();
  343. }
  344. void Win32Window::adjustWindow(unsigned int clientWidth, unsigned int clientHeight,
  345. unsigned int* winWidth, unsigned int* winHeight)
  346. {
  347. // NB only call this for non full screen
  348. RECT rc;
  349. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  350. AdjustWindowRect(&rc, WS_VISIBLE | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW, false);
  351. *winWidth = rc.right - rc.left;
  352. *winHeight = rc.bottom - rc.top;
  353. // adjust to monitor
  354. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  355. // Get monitor info
  356. MONITORINFO monitorInfo;
  357. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  358. monitorInfo.cbSize = sizeof(MONITORINFO);
  359. GetMonitorInfo(hMonitor, &monitorInfo);
  360. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  361. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  362. if (*winWidth > (unsigned int)maxW)
  363. *winWidth = maxW;
  364. if (*winHeight > (unsigned int)maxH)
  365. *winHeight = maxH;
  366. }
  367. void Win32Window::setFullscreen(bool fullScreen, unsigned int width, unsigned int height)
  368. {
  369. if (mIsFullScreen != fullScreen || width != mWidth || height != mHeight)
  370. {
  371. mIsFullScreen = fullScreen;
  372. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  373. if (mIsFullScreen)
  374. {
  375. dwStyle |= WS_POPUP;
  376. DEVMODE displayDeviceMode;
  377. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  378. displayDeviceMode.dmSize = sizeof(DEVMODE);
  379. displayDeviceMode.dmBitsPerPel = mColorDepth;
  380. displayDeviceMode.dmPelsWidth = width;
  381. displayDeviceMode.dmPelsHeight = height;
  382. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  383. if (mDisplayFrequency)
  384. {
  385. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  386. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  387. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  388. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  389. {
  390. // TODO LOG PORT - Log this somewhere
  391. //LogManager::getSingleton().logMessage(LML_NORMAL, "ChangeDisplaySettings with user display frequency failed");
  392. displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  393. }
  394. }
  395. else
  396. {
  397. // try a few
  398. displayDeviceMode.dmDisplayFrequency = 100;
  399. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  400. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  401. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  402. {
  403. displayDeviceMode.dmDisplayFrequency = 75;
  404. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  405. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  406. {
  407. displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  408. }
  409. }
  410. }
  411. // move window to 0,0 before display switch
  412. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, mWidth, mHeight, SWP_NOACTIVATE);
  413. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  414. {
  415. // TODO LOG PORT - Log this somewhere
  416. //LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
  417. }
  418. // Get the nearest monitor to this window.
  419. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  420. // Get monitor info
  421. MONITORINFO monitorInfo;
  422. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  423. monitorInfo.cbSize = sizeof(MONITORINFO);
  424. GetMonitorInfo(hMonitor, &monitorInfo);
  425. mTop = monitorInfo.rcMonitor.top;
  426. mLeft = monitorInfo.rcMonitor.left;
  427. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  428. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height,
  429. SWP_NOACTIVATE);
  430. mWidth = width;
  431. mHeight = height;
  432. }
  433. else
  434. {
  435. dwStyle |= WS_OVERLAPPEDWINDOW;
  436. // drop out of fullscreen
  437. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  438. // calculate overall dimensions for requested client area
  439. unsigned int winWidth, winHeight;
  440. adjustWindow(width, height, &winWidth, &winHeight);
  441. // deal with centreing when switching down to smaller resolution
  442. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  443. MONITORINFO monitorInfo;
  444. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  445. monitorInfo.cbSize = sizeof(MONITORINFO);
  446. GetMonitorInfo(hMonitor, &monitorInfo);
  447. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  448. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  449. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  450. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  451. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  452. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  453. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  454. mWidth = width;
  455. mHeight = height;
  456. windowMovedOrResized();
  457. }
  458. }
  459. }
  460. bool Win32Window::isActive(void) const
  461. {
  462. if (isFullScreen())
  463. return isVisible();
  464. return mActive && isVisible();
  465. }
  466. bool Win32Window::isVisible() const
  467. {
  468. return (mHWnd && !IsIconic(mHWnd));
  469. }
  470. bool Win32Window::isClosed() const
  471. {
  472. return mClosed;
  473. }
  474. void Win32Window::reposition(int left, int top)
  475. {
  476. if (mHWnd && !mIsFullScreen)
  477. {
  478. SetWindowPos(mHWnd, 0, left, top, 0, 0,
  479. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  480. }
  481. }
  482. void Win32Window::resize(unsigned int width, unsigned int height)
  483. {
  484. if (mHWnd && !mIsFullScreen)
  485. {
  486. RECT rc = { 0, 0, width, height };
  487. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  488. width = rc.right - rc.left;
  489. height = rc.bottom - rc.top;
  490. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  491. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  492. }
  493. }
  494. void Win32Window::windowMovedOrResized()
  495. {
  496. if (!mHWnd || IsIconic(mHWnd))
  497. return;
  498. RECT rc;
  499. // top and left represent outer window position
  500. GetWindowRect(mHWnd, &rc);
  501. mTop = rc.top;
  502. mLeft = rc.left;
  503. // width and height represent drawable area only
  504. GetClientRect(mHWnd, &rc);
  505. if (mWidth == rc.right && mHeight == rc.bottom)
  506. return;
  507. mWidth = rc.right - rc.left;
  508. mHeight = rc.bottom - rc.top;
  509. RenderWindow::windowMovedOrResized();
  510. }
  511. void Win32Window::swapBuffers()
  512. {
  513. if (!mIsExternalGLControl) {
  514. SwapBuffers(mHDC);
  515. }
  516. }
  517. void Win32Window::copyContentsToMemory(const PixelData &dst, FrameBuffer buffer)
  518. {
  519. if ((dst.getLeft() < 0) || (dst.getRight() > mWidth) ||
  520. (dst.getTop() < 0) || (dst.getBottom() > mHeight) ||
  521. (dst.getFront() != 0) || (dst.getBack() != 1))
  522. {
  523. CM_EXCEPT(InvalidParametersException, "Invalid box.");
  524. }
  525. if (buffer == FB_AUTO)
  526. {
  527. buffer = mIsFullScreen? FB_FRONT : FB_BACK;
  528. }
  529. GLenum format = CamelotFramework::GLPixelUtil::getGLOriginFormat(dst.getFormat());
  530. GLenum type = CamelotFramework::GLPixelUtil::getGLOriginDataType(dst.getFormat());
  531. if ((format == GL_NONE) || (type == 0))
  532. {
  533. CM_EXCEPT(InvalidParametersException, "Unsupported format.");
  534. }
  535. // Must change the packing to ensure no overruns!
  536. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  537. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  538. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  539. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  540. format, type, dst.getData());
  541. // restore default alignment
  542. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  543. //vertical flip
  544. {
  545. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  546. size_t height = dst.getHeight();
  547. UINT8 *tmpData = (UINT8*)cm_alloc<ScratchAlloc>((UINT32)(rowSpan * height));
  548. UINT8 *srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  549. while (tmpRow >= tmpData)
  550. {
  551. memcpy(tmpRow, srcRow, rowSpan);
  552. srcRow += rowSpan;
  553. tmpRow -= rowSpan;
  554. }
  555. memcpy(dst.getData(), tmpData, rowSpan * height);
  556. cm_free<ScratchAlloc>(tmpData);
  557. }
  558. }
  559. Int2 Win32Window::screenToWindowPos(const Int2& screenPos) const
  560. {
  561. POINT pos;
  562. // Convert client coordinates to screen coordinates
  563. pos.x = screenPos.x;
  564. pos.y = screenPos.y;
  565. ClientToScreen(mHWnd, &pos);
  566. return Int2(pos.x, pos.y);
  567. }
  568. void Win32Window::getCustomAttribute( const String& name, void* pData ) const
  569. {
  570. if( name == "GLCONTEXT" ) {
  571. *static_cast<GLContext**>(pData) = mContext;
  572. return;
  573. } else if( name == "WINDOW" )
  574. {
  575. HWND *pHwnd = (HWND*)pData;
  576. *pHwnd = getWindowHandle();
  577. return;
  578. }
  579. }
  580. void Win32Window::setActive( bool state )
  581. {
  582. if (mDeviceName != NULL && state == false)
  583. {
  584. HWND hActiveWindow = GetActiveWindow();
  585. char classNameSrc[_MAX_CLASS_NAME_ + 1];
  586. char classNameDst[_MAX_CLASS_NAME_ + 1];
  587. GetClassName(mHWnd, classNameSrc, _MAX_CLASS_NAME_);
  588. GetClassName(hActiveWindow, classNameDst, _MAX_CLASS_NAME_);
  589. if (strcmp(classNameDst, classNameSrc) == 0)
  590. {
  591. state = true;
  592. }
  593. }
  594. mActive = state;
  595. if( mIsFullScreen )
  596. {
  597. if( state == false )
  598. { //Restore Desktop
  599. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  600. ShowWindow(mHWnd, SW_SHOWMINNOACTIVE);
  601. }
  602. else
  603. { //Restore App
  604. ShowWindow(mHWnd, SW_SHOWNORMAL);
  605. DEVMODE displayDeviceMode;
  606. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  607. displayDeviceMode.dmSize = sizeof(DEVMODE);
  608. displayDeviceMode.dmBitsPerPel = mColorDepth;
  609. displayDeviceMode.dmPelsWidth = mWidth;
  610. displayDeviceMode.dmPelsHeight = mHeight;
  611. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  612. if (mDisplayFrequency)
  613. {
  614. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  615. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  616. }
  617. ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL);
  618. }
  619. }
  620. }
  621. }