BsWin32Window.cpp 22 KB

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