BsWin32Window.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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), mGLSupport(glsupport), mContext(0), mWindowedStyle(0), mWindowedStyleEx(0), mIsExternal(false),
  24. 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. void Win32WindowCore::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  289. {
  290. THROW_IF_NOT_CORE_THREAD;
  291. if (mIsChild)
  292. return;
  293. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  294. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  295. if (numOutputs == 0)
  296. return;
  297. Win32RenderWindowProperties& props = mProperties;
  298. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  299. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  300. bool oldFullscreen = props.mIsFullScreen;
  301. mDisplayFrequency = Math::roundToInt(refreshRate);
  302. props.mIsFullScreen = true;
  303. DEVMODE displayDeviceMode;
  304. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  305. displayDeviceMode.dmSize = sizeof(DEVMODE);
  306. displayDeviceMode.dmBitsPerPel = props.mColorDepth;
  307. displayDeviceMode.dmPelsWidth = width;
  308. displayDeviceMode.dmPelsHeight = height;
  309. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  310. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  311. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  312. MONITORINFOEX monitorInfo;
  313. memset(&monitorInfo, 0, sizeof(MONITORINFOEX));
  314. monitorInfo.cbSize = sizeof(MONITORINFOEX);
  315. GetMonitorInfo(hMonitor, &monitorInfo);
  316. if (ChangeDisplaySettingsEx(monitorInfo.szDevice, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  317. {
  318. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed");
  319. }
  320. props.mTop = monitorInfo.rcMonitor.top;
  321. props.mLeft = monitorInfo.rcMonitor.left;
  322. props.mWidth = width;
  323. props.mHeight = height;
  324. SetWindowPos(mHWnd, HWND_TOP, props.mLeft, props.mTop, width, height, SWP_NOACTIVATE);
  325. RenderWindowManager::instance().notifyMovedOrResized(this);
  326. }
  327. void Win32WindowCore::setFullscreen(const VideoMode& mode)
  328. {
  329. THROW_IF_NOT_CORE_THREAD;
  330. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  331. }
  332. void Win32WindowCore::setWindowed(UINT32 width, UINT32 height)
  333. {
  334. THROW_IF_NOT_CORE_THREAD;
  335. Win32RenderWindowProperties& props = mProperties;
  336. if (!props.mIsFullScreen)
  337. return;
  338. props.mIsFullScreen = false;
  339. props.mWidth = width;
  340. props.mHeight = height;
  341. // Drop out of fullscreen
  342. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  343. // Calculate overall dimensions for requested client area
  344. UINT32 winWidth, winHeight;
  345. getAdjustedWindowSize(props.mWidth, props.mHeight, &winWidth, &winHeight);
  346. // Deal with centering when switching down to smaller resolution
  347. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  348. MONITORINFO monitorInfo;
  349. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  350. monitorInfo.cbSize = sizeof(MONITORINFO);
  351. GetMonitorInfo(hMonitor, &monitorInfo);
  352. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  353. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  354. INT32 left = screenw > INT32(winWidth) ? ((screenw - INT32(winWidth)) / 2) : 0;
  355. INT32 top = screenh > INT32(winHeight) ? ((screenh - INT32(winHeight)) / 2) : 0;
  356. SetWindowLong(mHWnd, GWL_STYLE, mWindowedStyle);
  357. SetWindowLong(mHWnd, GWL_EXSTYLE, mWindowedStyleEx);
  358. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  359. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  360. _windowMovedOrResized();
  361. }
  362. void Win32WindowCore::move(INT32 left, INT32 top)
  363. {
  364. THROW_IF_NOT_CORE_THREAD;
  365. Win32RenderWindowProperties& props = mProperties;
  366. if (mHWnd && !props.mIsFullScreen)
  367. {
  368. props.mLeft = left;
  369. props.mTop = top;
  370. SetWindowPos(mHWnd, 0, left, top, 0, 0,
  371. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  372. }
  373. }
  374. void Win32WindowCore::resize(UINT32 width, UINT32 height)
  375. {
  376. THROW_IF_NOT_CORE_THREAD;
  377. Win32RenderWindowProperties& props = mProperties;
  378. if (mHWnd && !props.mIsFullScreen)
  379. {
  380. props.mWidth = width;
  381. props.mHeight = height;
  382. RECT rc = { 0, 0, width, height };
  383. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  384. width = rc.right - rc.left;
  385. height = rc.bottom - rc.top;
  386. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  387. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  388. }
  389. }
  390. void Win32WindowCore::minimize()
  391. {
  392. THROW_IF_NOT_CORE_THREAD;
  393. if (mHWnd)
  394. SendMessage(mHWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  395. }
  396. void Win32WindowCore::maximize()
  397. {
  398. THROW_IF_NOT_CORE_THREAD;
  399. if (mHWnd)
  400. SendMessage(mHWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  401. }
  402. void Win32WindowCore::restore()
  403. {
  404. THROW_IF_NOT_CORE_THREAD;
  405. if (mHWnd)
  406. SendMessage(mHWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
  407. }
  408. void Win32WindowCore::swapBuffers()
  409. {
  410. THROW_IF_NOT_CORE_THREAD;
  411. if (!mIsExternalGLControl) {
  412. SwapBuffers(mHDC);
  413. }
  414. }
  415. void Win32WindowCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  416. {
  417. THROW_IF_NOT_CORE_THREAD;
  418. if ((dst.getLeft() < 0) || (dst.getRight() > getProperties().getWidth()) ||
  419. (dst.getTop() < 0) || (dst.getBottom() > getProperties().getHeight()) ||
  420. (dst.getFront() != 0) || (dst.getBack() != 1))
  421. {
  422. BS_EXCEPT(InvalidParametersException, "Invalid box.");
  423. }
  424. if (buffer == FB_AUTO)
  425. {
  426. buffer = mProperties.isFullScreen() ? FB_FRONT : FB_BACK;
  427. }
  428. GLenum format = BansheeEngine::GLPixelUtil::getGLOriginFormat(dst.getFormat());
  429. GLenum type = BansheeEngine::GLPixelUtil::getGLOriginDataType(dst.getFormat());
  430. if ((format == GL_NONE) || (type == 0))
  431. {
  432. BS_EXCEPT(InvalidParametersException, "Unsupported format.");
  433. }
  434. // Must change the packing to ensure no overruns!
  435. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  436. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  437. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  438. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  439. format, type, dst.getData());
  440. // restore default alignment
  441. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  442. //vertical flip
  443. {
  444. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  445. size_t height = dst.getHeight();
  446. UINT8 *tmpData = (UINT8*)bs_alloc<ScratchAlloc>((UINT32)(rowSpan * height));
  447. UINT8 *srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  448. while (tmpRow >= tmpData)
  449. {
  450. memcpy(tmpRow, srcRow, rowSpan);
  451. srcRow += rowSpan;
  452. tmpRow -= rowSpan;
  453. }
  454. memcpy(dst.getData(), tmpData, rowSpan * height);
  455. bs_free<ScratchAlloc>(tmpData);
  456. }
  457. }
  458. void Win32WindowCore::getCustomAttribute(const String& name, void* pData) const
  459. {
  460. if(name == "GLCONTEXT")
  461. {
  462. SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(pData);
  463. *contextPtr = mContext;
  464. return;
  465. }
  466. else if(name == "WINDOW")
  467. {
  468. UINT64 *pHwnd = (UINT64*)pData;
  469. *pHwnd = (UINT64)mHWnd;
  470. return;
  471. }
  472. }
  473. void Win32WindowCore::setActive(bool state)
  474. {
  475. THROW_IF_NOT_CORE_THREAD;
  476. Win32RenderWindowProperties& props = mProperties;
  477. if (mDeviceName != NULL && state == false)
  478. {
  479. HWND hActiveWindow = GetActiveWindow();
  480. char classNameSrc[_MAX_CLASS_NAME_ + 1];
  481. char classNameDst[_MAX_CLASS_NAME_ + 1];
  482. GetClassName(mHWnd, classNameSrc, _MAX_CLASS_NAME_);
  483. GetClassName(hActiveWindow, classNameDst, _MAX_CLASS_NAME_);
  484. if (strcmp(classNameDst, classNameSrc) == 0)
  485. {
  486. state = true;
  487. }
  488. }
  489. props.mActive = state;
  490. if(props.mIsFullScreen)
  491. {
  492. if( state == false )
  493. { //Restore Desktop
  494. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  495. ShowWindow(mHWnd, SW_SHOWMINNOACTIVE);
  496. }
  497. else
  498. { //Restore App
  499. ShowWindow(mHWnd, SW_SHOWNORMAL);
  500. DEVMODE displayDeviceMode;
  501. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  502. displayDeviceMode.dmSize = sizeof(DEVMODE);
  503. displayDeviceMode.dmBitsPerPel = props.mColorDepth;
  504. displayDeviceMode.dmPelsWidth = props.mWidth;
  505. displayDeviceMode.dmPelsHeight = props.mHeight;
  506. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  507. if (mDisplayFrequency)
  508. {
  509. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  510. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  511. }
  512. ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL);
  513. }
  514. }
  515. RenderWindowManager::instance().notifyPropertiesDirty(this);
  516. }
  517. void Win32WindowCore::setHidden(bool hidden)
  518. {
  519. THROW_IF_NOT_CORE_THREAD;
  520. Win32RenderWindowProperties& props = mProperties;
  521. props.mHidden = hidden;
  522. if (!mIsExternal)
  523. {
  524. if (hidden)
  525. ShowWindow(mHWnd, SW_HIDE);
  526. else
  527. ShowWindow(mHWnd, SW_SHOWNORMAL);
  528. }
  529. RenderWindowManager::instance().notifyPropertiesDirty(this);
  530. }
  531. void Win32WindowCore::_windowMovedOrResized()
  532. {
  533. Win32RenderWindowProperties& props = mProperties;
  534. if (!mHWnd || IsIconic(mHWnd))
  535. return;
  536. RECT rc;
  537. GetWindowRect(mHWnd, &rc);
  538. props.mTop = rc.top;
  539. props.mLeft = rc.left;
  540. GetClientRect(mHWnd, &rc);
  541. props.mWidth = rc.right - rc.left;
  542. props.mHeight = rc.bottom - rc.top;
  543. RenderWindowCore::_windowMovedOrResized();
  544. }
  545. void Win32WindowCore::getAdjustedWindowSize(UINT32 clientWidth, UINT32 clientHeight, UINT32* winWidth, UINT32* winHeight)
  546. {
  547. Win32RenderWindowProperties& props = mProperties;
  548. RECT rc;
  549. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  550. AdjustWindowRectEx(&rc, mWindowedStyle, false, mWindowedStyleEx);
  551. *winWidth = rc.right - rc.left;
  552. *winHeight = rc.bottom - rc.top;
  553. // Adjust to monitor
  554. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  555. // Get monitor info
  556. MONITORINFO monitorInfo;
  557. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  558. monitorInfo.cbSize = sizeof(MONITORINFO);
  559. GetMonitorInfo(hMonitor, &monitorInfo);
  560. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  561. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  562. if (*winWidth > (UINT32)maxW)
  563. *winWidth = maxW;
  564. if (*winHeight > (UINT32)maxH)
  565. *winHeight = maxH;
  566. }
  567. UINT32 Win32WindowCore::getSyncData(UINT8* buffer)
  568. {
  569. UINT32 size = sizeof(mProperties);
  570. if (buffer != nullptr)
  571. memcpy(buffer, &mProperties, size);
  572. return size;
  573. }
  574. Win32Window::Win32Window(const RENDER_WINDOW_DESC& desc, Win32GLSupport &glsupport)
  575. :RenderWindow(desc), mGLSupport(glsupport), mProperties(desc)
  576. {
  577. }
  578. void Win32Window::getCustomAttribute(const String& name, void* pData) const
  579. {
  580. if (name == "WINDOW")
  581. {
  582. UINT64 *pHwnd = (UINT64*)pData;
  583. *pHwnd = (UINT64)getHWnd();
  584. return;
  585. }
  586. }
  587. Vector2I Win32Window::screenToWindowPos(const Vector2I& screenPos) const
  588. {
  589. POINT pos;
  590. pos.x = screenPos.x;
  591. pos.y = screenPos.y;
  592. ScreenToClient(getHWnd(), &pos);
  593. return Vector2I(pos.x, pos.y);
  594. }
  595. Vector2I Win32Window::windowToScreenPos(const Vector2I& windowPos) const
  596. {
  597. POINT pos;
  598. pos.x = windowPos.x;
  599. pos.y = windowPos.y;
  600. ClientToScreen(getHWnd(), &pos);
  601. return Vector2I(pos.x, pos.y);
  602. }
  603. SPtr<Win32WindowCore> Win32Window::getCore() const
  604. {
  605. return std::static_pointer_cast<Win32WindowCore>(mCoreSpecific);
  606. }
  607. void Win32Window::setSyncData(UINT8* buffer, UINT32 size)
  608. {
  609. assert(size == sizeof(mProperties));
  610. memcpy(&mProperties, buffer, size);
  611. }
  612. HWND Win32Window::getHWnd() const
  613. {
  614. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  615. // could be returned here if requested too soon after initialization.
  616. return getCore()->_getHWnd();
  617. }
  618. }