CmWin32Window.cpp 22 KB

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