BsWin32Window.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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/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. Win32RenderWindowProperties::Win32RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  19. :RenderWindowProperties(desc)
  20. { }
  21. Win32WindowCore::Win32WindowCore(const RENDER_WINDOW_DESC& desc, Win32GLSupport& glsupport)
  22. : RenderWindowCore(desc), mProperties(desc), mGLSupport(glsupport), mContext(0), mWindowedStyle(0), mWindowedStyleEx(0), mIsExternal(false),
  23. mIsExternalGLControl(false), mDisplayFrequency(0), mDeviceName(nullptr), mHWnd(0)
  24. { }
  25. Win32WindowCore::~Win32WindowCore()
  26. {
  27. Win32RenderWindowProperties& props = mProperties;
  28. if (!mHWnd)
  29. return;
  30. if (!mIsExternal)
  31. {
  32. if (props.mIsFullScreen)
  33. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  34. DestroyWindow(mHWnd);
  35. }
  36. else
  37. {
  38. // just release the DC
  39. ReleaseDC(mHWnd, mHDC);
  40. }
  41. props.mActive = false;
  42. mHDC = 0; // no release thanks to CS_OWNDC wndclass style
  43. mHWnd = 0;
  44. if (mDeviceName != NULL)
  45. {
  46. bs_free<ScratchAlloc>(mDeviceName);
  47. mDeviceName = NULL;
  48. }
  49. markCoreDirty();
  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, PlatformWndProc::_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. markCoreDirty();
  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. markCoreDirty();
  362. }
  363. void Win32WindowCore::move(INT32 left, INT32 top)
  364. {
  365. THROW_IF_NOT_CORE_THREAD;
  366. Win32RenderWindowProperties& props = mProperties;
  367. if (mHWnd && !props.mIsFullScreen)
  368. {
  369. props.mLeft = left;
  370. props.mTop = top;
  371. SetWindowPos(mHWnd, 0, left, top, 0, 0,
  372. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  373. markCoreDirty();
  374. }
  375. }
  376. void Win32WindowCore::resize(UINT32 width, UINT32 height)
  377. {
  378. THROW_IF_NOT_CORE_THREAD;
  379. Win32RenderWindowProperties& props = mProperties;
  380. if (mHWnd && !props.mIsFullScreen)
  381. {
  382. props.mWidth = width;
  383. props.mHeight = height;
  384. RECT rc = { 0, 0, width, height };
  385. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  386. width = rc.right - rc.left;
  387. height = rc.bottom - rc.top;
  388. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  389. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  390. markCoreDirty();
  391. }
  392. }
  393. void Win32WindowCore::swapBuffers()
  394. {
  395. THROW_IF_NOT_CORE_THREAD;
  396. if (!mIsExternalGLControl) {
  397. SwapBuffers(mHDC);
  398. }
  399. }
  400. void Win32WindowCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  401. {
  402. THROW_IF_NOT_CORE_THREAD;
  403. if ((dst.getLeft() < 0) || (dst.getRight() > getProperties().getWidth()) ||
  404. (dst.getTop() < 0) || (dst.getBottom() > getProperties().getHeight()) ||
  405. (dst.getFront() != 0) || (dst.getBack() != 1))
  406. {
  407. BS_EXCEPT(InvalidParametersException, "Invalid box.");
  408. }
  409. if (buffer == FB_AUTO)
  410. {
  411. buffer = mProperties.isFullScreen() ? FB_FRONT : FB_BACK;
  412. }
  413. GLenum format = BansheeEngine::GLPixelUtil::getGLOriginFormat(dst.getFormat());
  414. GLenum type = BansheeEngine::GLPixelUtil::getGLOriginDataType(dst.getFormat());
  415. if ((format == GL_NONE) || (type == 0))
  416. {
  417. BS_EXCEPT(InvalidParametersException, "Unsupported format.");
  418. }
  419. // Must change the packing to ensure no overruns!
  420. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  421. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  422. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  423. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  424. format, type, dst.getData());
  425. // restore default alignment
  426. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  427. //vertical flip
  428. {
  429. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  430. size_t height = dst.getHeight();
  431. UINT8 *tmpData = (UINT8*)bs_alloc<ScratchAlloc>((UINT32)(rowSpan * height));
  432. UINT8 *srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  433. while (tmpRow >= tmpData)
  434. {
  435. memcpy(tmpRow, srcRow, rowSpan);
  436. srcRow += rowSpan;
  437. tmpRow -= rowSpan;
  438. }
  439. memcpy(dst.getData(), tmpData, rowSpan * height);
  440. bs_free<ScratchAlloc>(tmpData);
  441. }
  442. }
  443. void Win32WindowCore::getCustomAttribute(const String& name, void* pData) const
  444. {
  445. if(name == "GLCONTEXT")
  446. {
  447. SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(pData);
  448. *contextPtr = mContext;
  449. return;
  450. }
  451. else if(name == "WINDOW")
  452. {
  453. HWND *pHwnd = (HWND*)pData;
  454. *pHwnd = mHWnd;
  455. return;
  456. }
  457. }
  458. void Win32WindowCore::setActive(bool state)
  459. {
  460. THROW_IF_NOT_CORE_THREAD;
  461. Win32RenderWindowProperties& props = mProperties;
  462. if (mDeviceName != NULL && state == false)
  463. {
  464. HWND hActiveWindow = GetActiveWindow();
  465. char classNameSrc[_MAX_CLASS_NAME_ + 1];
  466. char classNameDst[_MAX_CLASS_NAME_ + 1];
  467. GetClassName(mHWnd, classNameSrc, _MAX_CLASS_NAME_);
  468. GetClassName(hActiveWindow, classNameDst, _MAX_CLASS_NAME_);
  469. if (strcmp(classNameDst, classNameSrc) == 0)
  470. {
  471. state = true;
  472. }
  473. }
  474. props.mActive = state;
  475. if(props.mIsFullScreen)
  476. {
  477. if( state == false )
  478. { //Restore Desktop
  479. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  480. ShowWindow(mHWnd, SW_SHOWMINNOACTIVE);
  481. }
  482. else
  483. { //Restore App
  484. ShowWindow(mHWnd, SW_SHOWNORMAL);
  485. DEVMODE displayDeviceMode;
  486. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  487. displayDeviceMode.dmSize = sizeof(DEVMODE);
  488. displayDeviceMode.dmBitsPerPel = props.mColorDepth;
  489. displayDeviceMode.dmPelsWidth = props.mWidth;
  490. displayDeviceMode.dmPelsHeight = props.mHeight;
  491. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  492. if (mDisplayFrequency)
  493. {
  494. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  495. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  496. }
  497. ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL);
  498. }
  499. }
  500. markCoreDirty();
  501. }
  502. void Win32WindowCore::setHidden(bool hidden)
  503. {
  504. THROW_IF_NOT_CORE_THREAD;
  505. Win32RenderWindowProperties& props = mProperties;
  506. props.mHidden = hidden;
  507. if (!mIsExternal)
  508. {
  509. if (hidden)
  510. ShowWindow(mHWnd, SW_HIDE);
  511. else
  512. ShowWindow(mHWnd, SW_SHOWNORMAL);
  513. }
  514. markCoreDirty();
  515. }
  516. void Win32WindowCore::_windowMovedOrResized()
  517. {
  518. Win32RenderWindowProperties& props = mProperties;
  519. if (!mHWnd || IsIconic(mHWnd))
  520. return;
  521. RECT rc;
  522. GetWindowRect(mHWnd, &rc);
  523. props.mTop = rc.top;
  524. props.mLeft = rc.left;
  525. GetClientRect(mHWnd, &rc);
  526. props.mWidth = rc.right - rc.left;
  527. props.mHeight = rc.bottom - rc.top;
  528. markCoreDirty();
  529. RenderWindowCore::_windowMovedOrResized();
  530. }
  531. void Win32WindowCore::getAdjustedWindowSize(UINT32 clientWidth, UINT32 clientHeight, UINT32* winWidth, UINT32* winHeight)
  532. {
  533. Win32RenderWindowProperties& props = mProperties;
  534. RECT rc;
  535. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  536. AdjustWindowRectEx(&rc, mWindowedStyle, false, mWindowedStyleEx);
  537. *winWidth = rc.right - rc.left;
  538. *winHeight = rc.bottom - rc.top;
  539. // Adjust to monitor
  540. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  541. // Get monitor info
  542. MONITORINFO monitorInfo;
  543. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  544. monitorInfo.cbSize = sizeof(MONITORINFO);
  545. GetMonitorInfo(hMonitor, &monitorInfo);
  546. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  547. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  548. if (*winWidth > (UINT32)maxW)
  549. *winWidth = maxW;
  550. if (*winHeight > (UINT32)maxH)
  551. *winHeight = maxH;
  552. }
  553. Win32Window::Win32Window(const RENDER_WINDOW_DESC& desc, Win32GLSupport &glsupport)
  554. :RenderWindow(desc), mGLSupport(glsupport), mProperties(desc)
  555. {
  556. }
  557. void Win32Window::getCustomAttribute(const String& name, void* pData) const
  558. {
  559. if (name == "WINDOW")
  560. {
  561. HWND *pHwnd = (HWND*)pData;
  562. *pHwnd = getHWnd();
  563. return;
  564. }
  565. }
  566. Vector2I Win32Window::screenToWindowPos(const Vector2I& screenPos) const
  567. {
  568. POINT pos;
  569. pos.x = screenPos.x;
  570. pos.y = screenPos.y;
  571. ScreenToClient(getHWnd(), &pos);
  572. return Vector2I(pos.x, pos.y);
  573. }
  574. Vector2I Win32Window::windowToScreenPos(const Vector2I& windowPos) const
  575. {
  576. POINT pos;
  577. pos.x = windowPos.x;
  578. pos.y = windowPos.y;
  579. ClientToScreen(getHWnd(), &pos);
  580. return Vector2I(pos.x, pos.y);
  581. }
  582. SPtr<Win32WindowCore> Win32Window::getCore() const
  583. {
  584. return std::static_pointer_cast<Win32WindowCore>(mCoreSpecific);
  585. }
  586. HWND Win32Window::getHWnd() const
  587. {
  588. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  589. // could be returned here if requested too soon after initialization.
  590. return getCore()->_getHWnd();
  591. }
  592. }