BsWin32Window.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. #ifndef _WIN32_WINNT
  2. #define _WIN32_WINNT 0x0500
  3. #endif
  4. #include "BsWin32Window.h"
  5. #include "BsInput.h"
  6. #include "BsRenderAPI.h"
  7. #include "BsCoreThread.h"
  8. #include "BsException.h"
  9. #include "BsWin32GLSupport.h"
  10. #include "BsWin32Context.h"
  11. #include "Win32/BsWin32Platform.h"
  12. #include "BsWin32VideoModeInfo.h"
  13. #include "BsGLPixelFormat.h"
  14. #include "BsRenderWindowManager.h"
  15. GLenum GLEWAPIENTRY wglewContextInit(BansheeEngine::GLSupport *glSupport);
  16. namespace BansheeEngine
  17. {
  18. #define _MAX_CLASS_NAME_ 128
  19. Win32RenderWindowProperties::Win32RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  20. :RenderWindowProperties(desc)
  21. { }
  22. Win32WindowCore::Win32WindowCore(const RENDER_WINDOW_DESC& desc, Win32GLSupport& glsupport)
  23. : RenderWindowCore(desc), mProperties(desc), mSyncedProperties(desc), mGLSupport(glsupport), mContext(0),
  24. mWindowedStyle(0), mWindowedStyleEx(0), mIsExternal(false), mIsExternalGLControl(false), mDisplayFrequency(0), mDeviceName(nullptr), mHWnd(0)
  25. { }
  26. Win32WindowCore::~Win32WindowCore()
  27. {
  28. Win32RenderWindowProperties& props = mProperties;
  29. if (!mHWnd)
  30. return;
  31. if (!mIsExternal)
  32. {
  33. if (props.mIsFullScreen)
  34. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  35. DestroyWindow(mHWnd);
  36. }
  37. else
  38. {
  39. // just release the DC
  40. ReleaseDC(mHWnd, mHDC);
  41. }
  42. props.mActive = false;
  43. mHDC = 0; // no release thanks to CS_OWNDC wndclass style
  44. mHWnd = 0;
  45. if (mDeviceName != NULL)
  46. {
  47. bs_free<ScratchAlloc>(mDeviceName);
  48. mDeviceName = NULL;
  49. }
  50. }
  51. void Win32WindowCore::initialize()
  52. {
  53. RenderWindowCore::initialize();
  54. #ifdef BS_STATIC_LIB
  55. HINSTANCE hInst = GetModuleHandle(NULL);
  56. #else
  57. HINSTANCE hInst = GetModuleHandle(MODULE_NAME.c_str());
  58. #endif
  59. Win32RenderWindowProperties& props = mProperties;
  60. props.mIsFullScreen = mDesc.fullscreen;
  61. mIsChild = false;
  62. mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  63. props.mColorDepth = 32;
  64. HWND parent = 0;
  65. NameValuePairList::const_iterator opt;
  66. NameValuePairList::const_iterator end = mDesc.platformSpecific.end();
  67. if ((opt = mDesc.platformSpecific.find("externalWindowHandle")) != end)
  68. {
  69. mHWnd = (HWND)parseUnsignedInt(opt->second);
  70. if (mHWnd)
  71. {
  72. mIsExternal = true;
  73. }
  74. if ((opt = mDesc.platformSpecific.find("externalGLControl")) != end) {
  75. mIsExternalGLControl = parseBool(opt->second);
  76. }
  77. }
  78. HGLRC glrc = 0;
  79. if ((opt = mDesc.platformSpecific.find("externalGLContext")) != end)
  80. {
  81. glrc = (HGLRC)parseUnsignedLong(opt->second);
  82. }
  83. if ((opt = mDesc.platformSpecific.find("parentWindowHandle")) != end)
  84. {
  85. parent = (HWND)parseUnsignedInt(opt->second);
  86. mIsChild = true;
  87. props.mIsFullScreen = false;
  88. }
  89. HMONITOR hMonitor = NULL;
  90. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  91. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  92. if (numOutputs > 0)
  93. {
  94. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  95. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  96. hMonitor = outputInfo.getMonitorHandle();
  97. }
  98. if (!props.mIsFullScreen)
  99. {
  100. // Make sure we don't exceed desktop color depth
  101. if ((int)props.mColorDepth > GetDeviceCaps(GetDC(0), BITSPIXEL))
  102. props.mColorDepth = GetDeviceCaps(GetDC(0), BITSPIXEL);
  103. }
  104. mWindowedStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  105. mWindowedStyleEx = 0;
  106. if (!props.mIsFullScreen)
  107. {
  108. if (parent)
  109. {
  110. if (mDesc.toolWindow)
  111. mWindowedStyleEx = WS_EX_TOOLWINDOW;
  112. else
  113. mWindowedStyle |= WS_CHILD;
  114. }
  115. if (!parent || mDesc.toolWindow)
  116. {
  117. if (mDesc.border == WindowBorder::None)
  118. mWindowedStyle |= WS_POPUP;
  119. else if (mDesc.border == WindowBorder::Fixed)
  120. mWindowedStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  121. WS_SYSMENU | WS_MINIMIZEBOX;
  122. else
  123. mWindowedStyle |= WS_OVERLAPPEDWINDOW;
  124. }
  125. }
  126. if (!mIsExternal)
  127. {
  128. RECT rc;
  129. // If we didn't specified the adapter index, or if it didn't find it
  130. if (hMonitor == NULL)
  131. {
  132. POINT windowAnchorPoint;
  133. // Fill in anchor point.
  134. windowAnchorPoint.x = mDesc.left;
  135. windowAnchorPoint.y = mDesc.top;
  136. // Get the nearest monitor to this window.
  137. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTONEAREST);
  138. }
  139. // Get the target monitor info
  140. MONITORINFOEX monitorInfoEx;
  141. memset(&monitorInfoEx, 0, sizeof(MONITORINFOEX));
  142. monitorInfoEx.cbSize = sizeof(MONITORINFOEX);
  143. GetMonitorInfo(hMonitor, &monitorInfoEx);
  144. size_t devNameLen = strlen(monitorInfoEx.szDevice);
  145. mDeviceName = (char*)bs_alloc<ScratchAlloc>((UINT32)(devNameLen + 1));
  146. strcpy_s(mDeviceName, devNameLen + 1, monitorInfoEx.szDevice);
  147. UINT32 left = mDesc.left;
  148. UINT32 top = mDesc.top;
  149. // No specified top left -> Center the window in the middle of the monitor
  150. if (left == -1 || top == -1)
  151. {
  152. int screenw = monitorInfoEx.rcWork.right - monitorInfoEx.rcWork.left;
  153. int screenh = monitorInfoEx.rcWork.bottom - monitorInfoEx.rcWork.top;
  154. unsigned int winWidth, winHeight;
  155. getAdjustedWindowSize(mDesc.videoMode.getWidth(), mDesc.videoMode.getHeight(), &winWidth, &winHeight);
  156. // clamp window dimensions to screen size
  157. int outerw = (int(winWidth) < screenw) ? int(winWidth) : screenw;
  158. int outerh = (int(winHeight) < screenh) ? int(winHeight) : screenh;
  159. if (left == -1)
  160. left = monitorInfoEx.rcWork.left + (screenw - outerw) / 2;
  161. else if (hMonitor != NULL)
  162. left += monitorInfoEx.rcWork.left;
  163. if (top == -1)
  164. top = monitorInfoEx.rcWork.top + (screenh - outerh) / 2;
  165. else if (hMonitor != NULL)
  166. top += monitorInfoEx.rcWork.top;
  167. }
  168. else if (hMonitor != NULL)
  169. {
  170. left += monitorInfoEx.rcWork.left;
  171. top += monitorInfoEx.rcWork.top;
  172. }
  173. props.mWidth = mDesc.videoMode.getWidth();
  174. props.mHeight = mDesc.videoMode.getHeight();
  175. props.mTop = top;
  176. props.mLeft = left;
  177. DWORD dwStyle = 0;
  178. DWORD dwStyleEx = 0;
  179. if (props.mIsFullScreen)
  180. {
  181. dwStyle = WS_VISIBLE | WS_CLIPCHILDREN | WS_POPUP;
  182. props.mTop = monitorInfoEx.rcMonitor.top;
  183. props.mLeft = monitorInfoEx.rcMonitor.left;
  184. }
  185. else
  186. {
  187. dwStyle = mWindowedStyle;
  188. dwStyleEx = mWindowedStyleEx;
  189. int screenw = GetSystemMetrics(SM_CXSCREEN);
  190. int screenh = GetSystemMetrics(SM_CYSCREEN);
  191. if (!mDesc.outerDimensions)
  192. {
  193. // Calculate window dimensions required
  194. // to get the requested client area
  195. SetRect(&rc, 0, 0, props.mWidth, props.mHeight);
  196. AdjustWindowRect(&rc, dwStyle, false);
  197. props.mWidth = rc.right - rc.left;
  198. props.mHeight = rc.bottom - rc.top;
  199. // Clamp window rect to the nearest display monitor.
  200. if (props.mLeft < monitorInfoEx.rcWork.left)
  201. props.mLeft = monitorInfoEx.rcWork.left;
  202. if (props.mTop < monitorInfoEx.rcWork.top)
  203. props.mTop = monitorInfoEx.rcWork.top;
  204. if ((int)props.mWidth > monitorInfoEx.rcWork.right - props.mLeft)
  205. props.mWidth = monitorInfoEx.rcWork.right - props.mLeft;
  206. if ((int)props.mHeight > monitorInfoEx.rcWork.bottom - props.mTop)
  207. props.mHeight = monitorInfoEx.rcWork.bottom - props.mTop;
  208. }
  209. }
  210. // register class and create window
  211. WNDCLASS wc = { CS_OWNDC, Win32Platform::_win32WndProc, 0, 0, hInst,
  212. LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  213. (HBRUSH)GetStockObject(BLACK_BRUSH), NULL, "GLWindow" };
  214. RegisterClass(&wc);
  215. if (props.mIsFullScreen)
  216. {
  217. DEVMODE displayDeviceMode;
  218. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  219. displayDeviceMode.dmSize = sizeof(DEVMODE);
  220. displayDeviceMode.dmBitsPerPel = props.mColorDepth;
  221. displayDeviceMode.dmPelsWidth = props.mWidth;
  222. displayDeviceMode.dmPelsHeight = props.mHeight;
  223. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  224. if (mDisplayFrequency)
  225. {
  226. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  227. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  228. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  229. {
  230. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings with user display frequency failed.");
  231. }
  232. }
  233. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  234. {
  235. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed.");
  236. }
  237. }
  238. // Pass pointer to self as WM_CREATE parameter
  239. mHWnd = CreateWindowEx(dwStyleEx, "GLWindow", mDesc.title.c_str(),
  240. dwStyle, props.mLeft, props.mTop, props.mWidth, props.mHeight, parent, 0, hInst, this);
  241. }
  242. RECT rc;
  243. GetWindowRect(mHWnd, &rc);
  244. props.mTop = rc.top;
  245. props.mLeft = rc.left;
  246. GetClientRect(mHWnd, &rc);
  247. props.mWidth = rc.right;
  248. props.mHeight = rc.bottom;
  249. mHDC = GetDC(mHWnd);
  250. if (!mIsExternalGLControl)
  251. {
  252. int testMultisample = props.mMultisampleCount;
  253. bool testHwGamma = mDesc.gamma;
  254. bool formatOk = mGLSupport.selectPixelFormat(mHDC, props.mColorDepth, testMultisample, testHwGamma);
  255. if (!formatOk)
  256. {
  257. if (props.mMultisampleCount > 0)
  258. {
  259. // Try without multisampling
  260. testMultisample = 0;
  261. formatOk = mGLSupport.selectPixelFormat(mHDC, props.mColorDepth, testMultisample, testHwGamma);
  262. }
  263. if (!formatOk && mDesc.gamma)
  264. {
  265. // Try without sRGB
  266. testHwGamma = false;
  267. testMultisample = props.mMultisampleCount;
  268. formatOk = mGLSupport.selectPixelFormat(mHDC, props.mColorDepth, testMultisample, testHwGamma);
  269. }
  270. if (!formatOk && mDesc.gamma && (props.mMultisampleCount > 0))
  271. {
  272. // Try without both
  273. testHwGamma = false;
  274. testMultisample = 0;
  275. formatOk = mGLSupport.selectPixelFormat(mHDC, props.mColorDepth, testMultisample, testHwGamma);
  276. }
  277. if (!formatOk)
  278. BS_EXCEPT(RenderingAPIException, "Failed selecting pixel format.");
  279. }
  280. // Record what gamma option we used in the end
  281. // this will control enabling of sRGB state flags when used
  282. props.mHwGamma = testHwGamma;
  283. props.mMultisampleCount = testMultisample;
  284. }
  285. props.mActive = true;
  286. mContext = mGLSupport.createContext(mHDC, glrc);
  287. {
  288. ScopedSpinLock lock(mLock);
  289. mSyncedProperties = props;
  290. }
  291. RenderWindowManager::instance().notifySyncDataDirty(this);
  292. }
  293. void Win32WindowCore::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  294. {
  295. THROW_IF_NOT_CORE_THREAD;
  296. if (mIsChild)
  297. return;
  298. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  299. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  300. if (numOutputs == 0)
  301. return;
  302. Win32RenderWindowProperties& props = mProperties;
  303. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  304. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  305. bool oldFullscreen = props.mIsFullScreen;
  306. mDisplayFrequency = Math::roundToInt(refreshRate);
  307. props.mIsFullScreen = true;
  308. DEVMODE displayDeviceMode;
  309. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  310. displayDeviceMode.dmSize = sizeof(DEVMODE);
  311. displayDeviceMode.dmBitsPerPel = props.mColorDepth;
  312. displayDeviceMode.dmPelsWidth = width;
  313. displayDeviceMode.dmPelsHeight = height;
  314. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  315. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  316. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  317. MONITORINFOEX monitorInfo;
  318. memset(&monitorInfo, 0, sizeof(MONITORINFOEX));
  319. monitorInfo.cbSize = sizeof(MONITORINFOEX);
  320. GetMonitorInfo(hMonitor, &monitorInfo);
  321. if (ChangeDisplaySettingsEx(monitorInfo.szDevice, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  322. {
  323. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed");
  324. }
  325. props.mTop = monitorInfo.rcMonitor.top;
  326. props.mLeft = monitorInfo.rcMonitor.left;
  327. props.mWidth = width;
  328. props.mHeight = height;
  329. SetWindowPos(mHWnd, HWND_TOP, props.mLeft, props.mTop, width, height, SWP_NOACTIVATE);
  330. {
  331. ScopedSpinLock lock(mLock);
  332. mSyncedProperties.mTop = props.mTop;
  333. mSyncedProperties.mLeft = props.mLeft;
  334. mSyncedProperties.mWidth = props.mWidth;
  335. mSyncedProperties.mHeight = props.mHeight;
  336. }
  337. RenderWindowManager::instance().notifySyncDataDirty(this);
  338. RenderWindowManager::instance().notifyMovedOrResized(this);
  339. }
  340. void Win32WindowCore::setFullscreen(const VideoMode& mode)
  341. {
  342. THROW_IF_NOT_CORE_THREAD;
  343. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  344. }
  345. void Win32WindowCore::setWindowed(UINT32 width, UINT32 height)
  346. {
  347. THROW_IF_NOT_CORE_THREAD;
  348. Win32RenderWindowProperties& props = mProperties;
  349. if (!props.mIsFullScreen)
  350. return;
  351. props.mIsFullScreen = false;
  352. props.mWidth = width;
  353. props.mHeight = height;
  354. // Drop out of fullscreen
  355. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  356. // Calculate overall dimensions for requested client area
  357. UINT32 winWidth, winHeight;
  358. getAdjustedWindowSize(props.mWidth, props.mHeight, &winWidth, &winHeight);
  359. // Deal with centering when switching down to smaller resolution
  360. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  361. MONITORINFO monitorInfo;
  362. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  363. monitorInfo.cbSize = sizeof(MONITORINFO);
  364. GetMonitorInfo(hMonitor, &monitorInfo);
  365. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  366. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  367. INT32 left = screenw > INT32(winWidth) ? ((screenw - INT32(winWidth)) / 2) : 0;
  368. INT32 top = screenh > INT32(winHeight) ? ((screenh - INT32(winHeight)) / 2) : 0;
  369. SetWindowLong(mHWnd, GWL_STYLE, mWindowedStyle);
  370. SetWindowLong(mHWnd, GWL_EXSTYLE, mWindowedStyleEx);
  371. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  372. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  373. {
  374. ScopedSpinLock lock(mLock);
  375. mSyncedProperties.mWidth = props.mWidth;
  376. mSyncedProperties.mHeight = props.mHeight;
  377. }
  378. RenderWindowManager::instance().notifySyncDataDirty(this);
  379. _windowMovedOrResized();
  380. }
  381. void Win32WindowCore::move(INT32 left, INT32 top)
  382. {
  383. THROW_IF_NOT_CORE_THREAD;
  384. Win32RenderWindowProperties& props = mProperties;
  385. if (mHWnd && !props.mIsFullScreen)
  386. {
  387. props.mLeft = left;
  388. props.mTop = top;
  389. SetWindowPos(mHWnd, 0, left, top, 0, 0,
  390. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  391. {
  392. ScopedSpinLock lock(mLock);
  393. mSyncedProperties.mTop = props.mTop;
  394. mSyncedProperties.mLeft = props.mLeft;
  395. }
  396. RenderWindowManager::instance().notifySyncDataDirty(this);
  397. }
  398. }
  399. void Win32WindowCore::resize(UINT32 width, UINT32 height)
  400. {
  401. THROW_IF_NOT_CORE_THREAD;
  402. Win32RenderWindowProperties& props = mProperties;
  403. if (mHWnd && !props.mIsFullScreen)
  404. {
  405. props.mWidth = width;
  406. props.mHeight = height;
  407. RECT rc = { 0, 0, width, height };
  408. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  409. width = rc.right - rc.left;
  410. height = rc.bottom - rc.top;
  411. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  412. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  413. {
  414. ScopedSpinLock lock(mLock);
  415. mSyncedProperties.mWidth = props.mWidth;
  416. mSyncedProperties.mHeight = props.mHeight;
  417. }
  418. RenderWindowManager::instance().notifySyncDataDirty(this);
  419. }
  420. }
  421. void Win32WindowCore::minimize()
  422. {
  423. THROW_IF_NOT_CORE_THREAD;
  424. if (mHWnd)
  425. SendMessage(mHWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  426. }
  427. void Win32WindowCore::maximize()
  428. {
  429. THROW_IF_NOT_CORE_THREAD;
  430. if (mHWnd)
  431. SendMessage(mHWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  432. }
  433. void Win32WindowCore::restore()
  434. {
  435. THROW_IF_NOT_CORE_THREAD;
  436. if (mHWnd)
  437. SendMessage(mHWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
  438. }
  439. void Win32WindowCore::swapBuffers()
  440. {
  441. THROW_IF_NOT_CORE_THREAD;
  442. if (!mIsExternalGLControl) {
  443. SwapBuffers(mHDC);
  444. }
  445. }
  446. void Win32WindowCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  447. {
  448. THROW_IF_NOT_CORE_THREAD;
  449. if ((dst.getLeft() < 0) || (dst.getRight() > getProperties().getWidth()) ||
  450. (dst.getTop() < 0) || (dst.getBottom() > getProperties().getHeight()) ||
  451. (dst.getFront() != 0) || (dst.getBack() != 1))
  452. {
  453. BS_EXCEPT(InvalidParametersException, "Invalid box.");
  454. }
  455. if (buffer == FB_AUTO)
  456. {
  457. buffer = mProperties.isFullScreen() ? FB_FRONT : FB_BACK;
  458. }
  459. GLenum format = BansheeEngine::GLPixelUtil::getGLOriginFormat(dst.getFormat());
  460. GLenum type = BansheeEngine::GLPixelUtil::getGLOriginDataType(dst.getFormat());
  461. if ((format == GL_NONE) || (type == 0))
  462. {
  463. BS_EXCEPT(InvalidParametersException, "Unsupported format.");
  464. }
  465. // Must change the packing to ensure no overruns!
  466. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  467. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  468. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  469. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  470. format, type, dst.getData());
  471. // restore default alignment
  472. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  473. //vertical flip
  474. {
  475. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  476. size_t height = dst.getHeight();
  477. UINT8 *tmpData = (UINT8*)bs_alloc<ScratchAlloc>((UINT32)(rowSpan * height));
  478. UINT8 *srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  479. while (tmpRow >= tmpData)
  480. {
  481. memcpy(tmpRow, srcRow, rowSpan);
  482. srcRow += rowSpan;
  483. tmpRow -= rowSpan;
  484. }
  485. memcpy(dst.getData(), tmpData, rowSpan * height);
  486. bs_free<ScratchAlloc>(tmpData);
  487. }
  488. }
  489. void Win32WindowCore::getCustomAttribute(const String& name, void* pData) const
  490. {
  491. if(name == "GLCONTEXT")
  492. {
  493. SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(pData);
  494. *contextPtr = mContext;
  495. return;
  496. }
  497. else if(name == "WINDOW")
  498. {
  499. UINT64 *pHwnd = (UINT64*)pData;
  500. *pHwnd = (UINT64)mHWnd;
  501. return;
  502. }
  503. }
  504. void Win32WindowCore::setActive(bool state)
  505. {
  506. THROW_IF_NOT_CORE_THREAD;
  507. Win32RenderWindowProperties& props = mProperties;
  508. if (mDeviceName != NULL && state == false)
  509. {
  510. HWND hActiveWindow = GetActiveWindow();
  511. char classNameSrc[_MAX_CLASS_NAME_ + 1];
  512. char classNameDst[_MAX_CLASS_NAME_ + 1];
  513. GetClassName(mHWnd, classNameSrc, _MAX_CLASS_NAME_);
  514. GetClassName(hActiveWindow, classNameDst, _MAX_CLASS_NAME_);
  515. if (strcmp(classNameDst, classNameSrc) == 0)
  516. {
  517. state = true;
  518. }
  519. }
  520. props.mActive = state;
  521. if(props.mIsFullScreen)
  522. {
  523. if( state == false )
  524. { //Restore Desktop
  525. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  526. ShowWindow(mHWnd, SW_SHOWMINNOACTIVE);
  527. }
  528. else
  529. { //Restore App
  530. ShowWindow(mHWnd, SW_SHOWNORMAL);
  531. DEVMODE displayDeviceMode;
  532. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  533. displayDeviceMode.dmSize = sizeof(DEVMODE);
  534. displayDeviceMode.dmBitsPerPel = props.mColorDepth;
  535. displayDeviceMode.dmPelsWidth = props.mWidth;
  536. displayDeviceMode.dmPelsHeight = props.mHeight;
  537. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  538. if (mDisplayFrequency)
  539. {
  540. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  541. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  542. }
  543. ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL);
  544. }
  545. }
  546. RenderWindowCore::setActive(state);
  547. }
  548. void Win32WindowCore::setHidden(bool hidden)
  549. {
  550. THROW_IF_NOT_CORE_THREAD;
  551. Win32RenderWindowProperties& props = mProperties;
  552. props.mHidden = hidden;
  553. if (!mIsExternal)
  554. {
  555. if (hidden)
  556. ShowWindow(mHWnd, SW_HIDE);
  557. else
  558. ShowWindow(mHWnd, SW_SHOWNORMAL);
  559. }
  560. RenderWindowCore::setHidden(hidden);
  561. }
  562. void Win32WindowCore::_windowMovedOrResized()
  563. {
  564. Win32RenderWindowProperties& props = mProperties;
  565. if (!mHWnd || IsIconic(mHWnd))
  566. return;
  567. RECT rc;
  568. GetWindowRect(mHWnd, &rc);
  569. props.mTop = rc.top;
  570. props.mLeft = rc.left;
  571. GetClientRect(mHWnd, &rc);
  572. props.mWidth = rc.right - rc.left;
  573. props.mHeight = rc.bottom - rc.top;
  574. RenderWindowCore::_windowMovedOrResized();
  575. }
  576. void Win32WindowCore::getAdjustedWindowSize(UINT32 clientWidth, UINT32 clientHeight, UINT32* winWidth, UINT32* winHeight)
  577. {
  578. Win32RenderWindowProperties& props = mProperties;
  579. RECT rc;
  580. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  581. AdjustWindowRectEx(&rc, mWindowedStyle, false, mWindowedStyleEx);
  582. *winWidth = rc.right - rc.left;
  583. *winHeight = rc.bottom - rc.top;
  584. // Adjust to monitor
  585. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  586. // Get monitor info
  587. MONITORINFO monitorInfo;
  588. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  589. monitorInfo.cbSize = sizeof(MONITORINFO);
  590. GetMonitorInfo(hMonitor, &monitorInfo);
  591. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  592. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  593. if (*winWidth > (UINT32)maxW)
  594. *winWidth = maxW;
  595. if (*winHeight > (UINT32)maxH)
  596. *winHeight = maxH;
  597. }
  598. void Win32WindowCore::syncProperties()
  599. {
  600. ScopedSpinLock lock(mLock);
  601. mProperties = mSyncedProperties;
  602. }
  603. Win32Window::Win32Window(const RENDER_WINDOW_DESC& desc, Win32GLSupport &glsupport)
  604. :RenderWindow(desc), mGLSupport(glsupport), mProperties(desc)
  605. {
  606. }
  607. void Win32Window::getCustomAttribute(const String& name, void* pData) const
  608. {
  609. if (name == "WINDOW")
  610. {
  611. UINT64 *pHwnd = (UINT64*)pData;
  612. *pHwnd = (UINT64)getHWnd();
  613. return;
  614. }
  615. }
  616. Vector2I Win32Window::screenToWindowPos(const Vector2I& screenPos) const
  617. {
  618. POINT pos;
  619. pos.x = screenPos.x;
  620. pos.y = screenPos.y;
  621. ScreenToClient(getHWnd(), &pos);
  622. return Vector2I(pos.x, pos.y);
  623. }
  624. Vector2I Win32Window::windowToScreenPos(const Vector2I& windowPos) const
  625. {
  626. POINT pos;
  627. pos.x = windowPos.x;
  628. pos.y = windowPos.y;
  629. ClientToScreen(getHWnd(), &pos);
  630. return Vector2I(pos.x, pos.y);
  631. }
  632. SPtr<Win32WindowCore> Win32Window::getCore() const
  633. {
  634. return std::static_pointer_cast<Win32WindowCore>(mCoreSpecific);
  635. }
  636. void Win32Window::syncProperties()
  637. {
  638. ScopedSpinLock lock(getCore()->mLock);
  639. mProperties = getCore()->mSyncedProperties;
  640. }
  641. HWND Win32Window::getHWnd() const
  642. {
  643. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  644. // could be returned here if requested too soon after initialization.
  645. return getCore()->_getHWnd();
  646. }
  647. }