| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #ifndef _WIN32_WINNT
- #define _WIN32_WINNT 0x0500
- #endif
- #include "Win32/BsWin32RenderWindow.h"
- #include "Input/BsInput.h"
- #include "Renderapi/BsRenderAPI.h"
- #include "Corethread/BsCoreThread.h"
- #include "Error/BsException.h"
- #include "Win32/BsWin32GLSupport.h"
- #include "Win32/BsWin32Context.h"
- #include "Win32/BsWin32Platform.h"
- #include "Win32/BsWin32VideoModeInfo.h"
- #include "BsGLPixelFormat.h"
- #include "Managers/BsRenderWindowManager.h"
- #include "Win32/BsWin32Window.h"
- #include "Math/BsMath.h"
- GLenum GLEWAPIENTRY wglewContextInit(bs::ct::GLSupport* glSupport);
- namespace bs
- {
- #define _MAX_CLASS_NAME_ 128
- Win32RenderWindow::Win32RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, ct::Win32GLSupport &glsupport)
- :RenderWindow(desc, windowId), mGLSupport(glsupport), mProperties(desc)
- {
- }
- void Win32RenderWindow::getCustomAttribute(const String& name, void* pData) const
- {
- if (name == "WINDOW")
- {
- UINT64 *pHwnd = (UINT64*)pData;
- *pHwnd = (UINT64)getHWnd();
- return;
- }
- }
- Vector2I Win32RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
- {
- POINT pos;
- pos.x = screenPos.x;
- pos.y = screenPos.y;
- ScreenToClient(getHWnd(), &pos);
- return Vector2I(pos.x, pos.y);
- }
- Vector2I Win32RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
- {
- POINT pos;
- pos.x = windowPos.x;
- pos.y = windowPos.y;
- ClientToScreen(getHWnd(), &pos);
- return Vector2I(pos.x, pos.y);
- }
- SPtr<ct::Win32RenderWindow> Win32RenderWindow::getCore() const
- {
- return std::static_pointer_cast<ct::Win32RenderWindow>(mCoreSpecific);
- }
- void Win32RenderWindow::syncProperties()
- {
- ScopedSpinLock lock(getCore()->mLock);
- mProperties = getCore()->mSyncedProperties;
- }
- HWND Win32RenderWindow::getHWnd() const
- {
- blockUntilCoreInitialized();
- return getCore()->_getHWnd();
- }
- namespace ct
- {
- Win32RenderWindow::Win32RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, Win32GLSupport& glsupport)
- : RenderWindow(desc, windowId), mWindow(nullptr), mGLSupport(glsupport), mHDC(nullptr), mIsChild(false)
- , mDeviceName(nullptr), mDisplayFrequency(0), mShowOnSwap(false), mContext(nullptr), mProperties(desc)
- , mSyncedProperties(desc)
- { }
- Win32RenderWindow::~Win32RenderWindow()
- {
- RenderWindowProperties& props = mProperties;
- if (mWindow != nullptr)
- {
- ReleaseDC(mWindow->getHWnd(), mHDC);
- bs_delete(mWindow);
- mWindow = nullptr;
- }
- mHDC = nullptr;
- if (mDeviceName != nullptr)
- {
- bs_free(mDeviceName);
- mDeviceName = nullptr;
- }
- }
- void Win32RenderWindow::initialize()
- {
- RenderWindowProperties& props = mProperties;
- props.isFullScreen = mDesc.fullscreen;
- mIsChild = false;
- mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
- WINDOW_DESC windowDesc;
- windowDesc.showTitleBar = mDesc.showTitleBar;
- windowDesc.showBorder = mDesc.showBorder;
- windowDesc.allowResize = mDesc.allowResize;
- windowDesc.enableDoubleClick = true;
- windowDesc.fullscreen = mDesc.fullscreen;
- windowDesc.width = mDesc.videoMode.getWidth();
- windowDesc.height = mDesc.videoMode.getHeight();
- windowDesc.hidden = mDesc.hidden || mDesc.hideUntilSwap;
- windowDesc.left = mDesc.left;
- windowDesc.top = mDesc.top;
- windowDesc.outerDimensions = false;
- windowDesc.title = mDesc.title;
- windowDesc.toolWindow = mDesc.toolWindow;
- windowDesc.creationParams = this;
- windowDesc.modal = mDesc.modal;
- windowDesc.wndProc = &Win32Platform::_win32WndProc;
- #ifdef BS_STATIC_LIB
- windowDesc.module = GetModuleHandle(NULL);
- #else
- windowDesc.module = GetModuleHandle(MODULE_NAME);
- #endif
- NameValuePairList::const_iterator opt;
- opt = mDesc.platformSpecific.find("parentWindowHandle");
- if (opt != mDesc.platformSpecific.end())
- windowDesc.parent = (HWND)parseUINT64(opt->second);
- opt = mDesc.platformSpecific.find("externalWindowHandle");
- if (opt != mDesc.platformSpecific.end())
- windowDesc.external = (HWND)parseUINT64(opt->second);
-
- const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
- UINT32 numOutputs = videoModeInfo.getNumOutputs();
- if (numOutputs > 0)
- {
- UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
- const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
- windowDesc.monitor = outputInfo.getMonitorHandle();
- }
- mIsChild = windowDesc.parent != nullptr;
- props.isFullScreen = mDesc.fullscreen && !mIsChild;
- if (!windowDesc.external)
- {
- mShowOnSwap = mDesc.hideUntilSwap;
- props.isHidden = mDesc.hideUntilSwap || mDesc.hidden;
- }
- mWindow = bs_new<Win32Window>(windowDesc);
- props.width = mWindow->getWidth();
- props.height = mWindow->getHeight();
- props.top = mWindow->getTop();
- props.left = mWindow->getLeft();
-
- if (!windowDesc.external)
- {
- if (props.isFullScreen)
- {
- DEVMODE displayDeviceMode;
- memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
- displayDeviceMode.dmSize = sizeof(DEVMODE);
- displayDeviceMode.dmBitsPerPel = 32;
- displayDeviceMode.dmPelsWidth = props.width;
- displayDeviceMode.dmPelsHeight = props.height;
- displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
- if (mDisplayFrequency)
- {
- displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
- displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
- if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
- {
- BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings with user display frequency failed.");
- }
- }
- if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
- {
- BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed.");
- }
- }
- }
- mHDC = GetDC(mWindow->getHWnd());
- int testMultisample = props.multisampleCount;
- bool testHwGamma = mDesc.gamma;
- bool formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
- if (!formatOk)
- {
- if (props.multisampleCount > 0)
- {
- // Try without multisampling
- testMultisample = 0;
- formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
- }
- if (!formatOk && mDesc.gamma)
- {
- // Try without sRGB
- testHwGamma = false;
- testMultisample = props.multisampleCount;
- formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
- }
- if (!formatOk && mDesc.gamma && (props.multisampleCount > 0))
- {
- // Try without both
- testHwGamma = false;
- testMultisample = 0;
- formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
- }
- if (!formatOk)
- BS_EXCEPT(RenderingAPIException, "Failed selecting pixel format.");
- }
- // Record what gamma option we used in the end
- // this will control enabling of sRGB state flags when used
- props.hwGamma = testHwGamma;
- props.multisampleCount = testMultisample;
- mContext = mGLSupport.createContext(mHDC, nullptr);
- {
- ScopedSpinLock lock(mLock);
- mSyncedProperties = props;
- }
- bs::RenderWindowManager::instance().notifySyncDataDirty(this);
- RenderWindow::initialize();
- }
- void Win32RenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
- {
- THROW_IF_NOT_CORE_THREAD;
- if (mIsChild)
- return;
- const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
- UINT32 numOutputs = videoModeInfo.getNumOutputs();
- if (numOutputs == 0)
- return;
- RenderWindowProperties& props = mProperties;
- UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
- const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
- mDisplayFrequency = Math::roundToInt(refreshRate);
- props.isFullScreen = true;
- DEVMODE displayDeviceMode;
- memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
- displayDeviceMode.dmSize = sizeof(DEVMODE);
- displayDeviceMode.dmBitsPerPel = 32;
- displayDeviceMode.dmPelsWidth = width;
- displayDeviceMode.dmPelsHeight = height;
- displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
- displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
- HMONITOR hMonitor = outputInfo.getMonitorHandle();
- MONITORINFOEX monitorInfo;
- memset(&monitorInfo, 0, sizeof(MONITORINFOEX));
- monitorInfo.cbSize = sizeof(MONITORINFOEX);
- GetMonitorInfo(hMonitor, &monitorInfo);
- if (ChangeDisplaySettingsEx(monitorInfo.szDevice, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
- {
- BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed");
- }
- props.top = monitorInfo.rcMonitor.top;
- props.left = monitorInfo.rcMonitor.left;
- props.width = width;
- props.height = height;
- SetWindowLong(mWindow->getHWnd(), GWL_STYLE, WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
- SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, 0);
- SetWindowPos(mWindow->getHWnd(), HWND_TOP, props.left, props.top, width, height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
- _windowMovedOrResized();
- }
- void Win32RenderWindow::setFullscreen(const VideoMode& mode)
- {
- THROW_IF_NOT_CORE_THREAD;
- setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
- }
- void Win32RenderWindow::setWindowed(UINT32 width, UINT32 height)
- {
- THROW_IF_NOT_CORE_THREAD;
- RenderWindowProperties& props = mProperties;
- if (!props.isFullScreen)
- return;
- props.isFullScreen = false;
- props.width = width;
- props.height = height;
- // Drop out of fullscreen
- ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
- UINT32 winWidth = width;
- UINT32 winHeight = height;
- RECT rect;
- SetRect(&rect, 0, 0, winWidth, winHeight);
- AdjustWindowRect(&rect, mWindow->getStyle(), false);
- winWidth = rect.right - rect.left;
- winHeight = rect.bottom - rect.top;
- // Deal with centering when switching down to smaller resolution
- HMONITOR hMonitor = MonitorFromWindow(mWindow->getHWnd(), MONITOR_DEFAULTTONEAREST);
- MONITORINFO monitorInfo;
- memset(&monitorInfo, 0, sizeof(MONITORINFO));
- monitorInfo.cbSize = sizeof(MONITORINFO);
- GetMonitorInfo(hMonitor, &monitorInfo);
- LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
- LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
- INT32 left = screenw > INT32(winWidth) ? ((screenw - INT32(winWidth)) / 2) : 0;
- INT32 top = screenh > INT32(winHeight) ? ((screenh - INT32(winHeight)) / 2) : 0;
- SetWindowLong(mWindow->getHWnd(), GWL_STYLE, mWindow->getStyle() | WS_VISIBLE);
- SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, mWindow->getStyleEx());
- SetWindowPos(mWindow->getHWnd(), HWND_NOTOPMOST, left, top, winWidth, winHeight,
- SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
- {
- ScopedSpinLock lock(mLock);
- mSyncedProperties.width = props.width;
- mSyncedProperties.height = props.height;
- }
- bs::RenderWindowManager::instance().notifySyncDataDirty(this);
- _windowMovedOrResized();
- }
- void Win32RenderWindow::move(INT32 left, INT32 top)
- {
- THROW_IF_NOT_CORE_THREAD;
- RenderWindowProperties& props = mProperties;
- if (!props.isFullScreen)
- {
- mWindow->move(left, top);
- props.top = mWindow->getTop();
- props.left = mWindow->getLeft();
- {
- ScopedSpinLock lock(mLock);
- mSyncedProperties.top = props.top;
- mSyncedProperties.left = props.left;
- }
- bs::RenderWindowManager::instance().notifySyncDataDirty(this);
- }
- }
- void Win32RenderWindow::resize(UINT32 width, UINT32 height)
- {
- THROW_IF_NOT_CORE_THREAD;
- RenderWindowProperties& props = mProperties;
- if (!props.isFullScreen)
- {
- mWindow->resize(width, height);
- props.width = mWindow->getWidth();
- props.height = mWindow->getHeight();
-
- {
- ScopedSpinLock lock(mLock);
- mSyncedProperties.width = props.width;
- mSyncedProperties.height = props.height;
- }
- bs::RenderWindowManager::instance().notifySyncDataDirty(this);
- }
- }
- void Win32RenderWindow::minimize()
- {
- THROW_IF_NOT_CORE_THREAD;
- mWindow->minimize();
- }
- void Win32RenderWindow::maximize()
- {
- THROW_IF_NOT_CORE_THREAD;
- mWindow->maximize();
- }
- void Win32RenderWindow::restore()
- {
- THROW_IF_NOT_CORE_THREAD;
- mWindow->restore();
- }
- void Win32RenderWindow::swapBuffers(UINT32 syncMask)
- {
- THROW_IF_NOT_CORE_THREAD;
- if (mShowOnSwap)
- setHidden(false);
- SwapBuffers(mHDC);
- }
- void Win32RenderWindow::copyToMemory(PixelData &dst, FrameBuffer buffer)
- {
- THROW_IF_NOT_CORE_THREAD;
- if ((dst.getRight() > getProperties().width) ||
- (dst.getBottom() > getProperties().height) ||
- (dst.getFront() != 0) || (dst.getBack() != 1))
- {
- BS_EXCEPT(InvalidParametersException, "Invalid box.");
- }
- if (buffer == FB_AUTO)
- {
- buffer = mProperties.isFullScreen ? FB_FRONT : FB_BACK;
- }
- GLenum format = GLPixelUtil::getGLOriginFormat(dst.getFormat());
- GLenum type = GLPixelUtil::getGLOriginDataType(dst.getFormat());
- if ((format == GL_NONE) || (type == 0))
- {
- BS_EXCEPT(InvalidParametersException, "Unsupported format.");
- }
- // Must change the packing to ensure no overruns!
- glPixelStorei(GL_PACK_ALIGNMENT, 1);
- glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
- glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
- (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
- format, type, dst.getData());
- // restore default alignment
- glPixelStorei(GL_PACK_ALIGNMENT, 4);
- //vertical flip
- {
- size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
- size_t height = dst.getHeight();
- UINT8* tmpData = (UINT8*)bs_alloc((UINT32)(rowSpan * height));
- UINT8* srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
- while (tmpRow >= tmpData)
- {
- memcpy(tmpRow, srcRow, rowSpan);
- srcRow += rowSpan;
- tmpRow -= rowSpan;
- }
- memcpy(dst.getData(), tmpData, rowSpan * height);
- bs_free(tmpData);
- }
- }
- void Win32RenderWindow::getCustomAttribute(const String& name, void* pData) const
- {
- if(name == "GLCONTEXT")
- {
- SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(pData);
- *contextPtr = mContext;
- return;
- }
- else if(name == "WINDOW")
- {
- UINT64 *pHwnd = (UINT64*)pData;
- *pHwnd = (UINT64)_getHWnd();
- return;
- }
- }
- void Win32RenderWindow::setActive(bool state)
- {
- THROW_IF_NOT_CORE_THREAD;
- mWindow->setActive(state);
- RenderWindow::setActive(state);
- }
- void Win32RenderWindow::setHidden(bool hidden)
- {
- THROW_IF_NOT_CORE_THREAD;
- mShowOnSwap = false;
- mWindow->setHidden(hidden);
- RenderWindow::setHidden(hidden);
- }
- void Win32RenderWindow::_windowMovedOrResized()
- {
- if (!mWindow)
- return;
- mWindow->_windowMovedOrResized();
- RenderWindowProperties& props = mProperties;
- if (!props.isFullScreen) // Fullscreen is handled directly by this object
- {
- props.top = mWindow->getTop();
- props.left = mWindow->getLeft();
- props.width = mWindow->getWidth();
- props.height = mWindow->getHeight();
- }
- RenderWindow::_windowMovedOrResized();
- }
- HWND Win32RenderWindow::_getHWnd() const
- {
- return mWindow->getHWnd();
- }
- void Win32RenderWindow::syncProperties()
- {
- ScopedSpinLock lock(mLock);
- mProperties = mSyncedProperties;
- }
- }
- }
|