Merge pull request #134 from halx99/fullscreen-mode

Refactor full screen support
This commit is contained in:
HALX99 2020-08-02 20:03:07 -07:00 committed by GitHub
commit 5f96ce8981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 52 deletions

View File

@ -545,7 +545,7 @@ void GLViewImpl::pollEvents()
}
void GLViewImpl::enableRetina(bool enabled)
{
{ // official v4 comment follow sources
// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
// _isRetinaEnabled = enabled;
// if (_isRetinaEnabled)
@ -643,36 +643,51 @@ bool GLViewImpl::isFullscreen() const {
return (_monitor != nullptr);
}
void GLViewImpl::setFullscreen() {
if (this->isFullscreen()) {
return;
}
_monitor = glfwGetPrimaryMonitor();
if (nullptr == _monitor) {
return;
}
const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
this->setFullscreen(*videoMode, _monitor);
void GLViewImpl::setFullscreen()
{
setFullscreen(-1, -1, -1);
}
void GLViewImpl::setFullscreen(int monitorIndex) {
// set fullscreen on specific monitor
void GLViewImpl::setFullscreen(int w, int h, int refreshRate) {
auto monitor = glfwGetPrimaryMonitor();
if (nullptr == monitor || monitor == _monitor) {
return;
}
this->setFullscreen(monitor, w, h, refreshRate);
}
void GLViewImpl::setFullscreen(int monitorIndex)
{
setFullscreen(monitorIndex, -1, -1, -1);
}
void GLViewImpl::setFullscreen(int monitorIndex, int w, int h, int refreshRate) {
int count = 0;
GLFWmonitor** monitors = glfwGetMonitors(&count);
if (monitorIndex < 0 || monitorIndex >= count) {
return;
}
GLFWmonitor* monitor = monitors[monitorIndex];
if (nullptr == monitor) {
if (nullptr == monitor || _monitor == monitor) {
return;
}
const GLFWvidmode* videoMode = glfwGetVideoMode(monitor);
this->setFullscreen(*videoMode, monitor);
this->setFullscreen(monitor, w, h, refreshRate);
}
void GLViewImpl::setFullscreen(const GLFWvidmode &videoMode, GLFWmonitor *monitor) {
void GLViewImpl::setFullscreen(GLFWmonitor *monitor, int w, int h, int refreshRate) {
_monitor = monitor;
glfwSetWindowMonitor(_mainWindow, _monitor, 0, 0, videoMode.width, videoMode.height, videoMode.refreshRate);
const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
if (w == -1)
w = videoMode->width;
if (h == -1)
h = videoMode->height;
if (refreshRate == -1)
refreshRate = videoMode->refreshRate;
glfwSetWindowMonitor(_mainWindow, _monitor, 0, 0, w, h, refreshRate);
updateWindowSize();
}
void GLViewImpl::setWindowed(int width, int height) {
@ -690,9 +705,22 @@ void GLViewImpl::setWindowed(int width, int height) {
// on mac window will sometimes lose title when windowed
glfwSetWindowTitle(_mainWindow, _viewName.c_str());
#endif
updateWindowSize();
}
}
void GLViewImpl::updateWindowSize()
{
int w = 0, h = 0;
glfwGetFramebufferSize(_mainWindow, &w, &h);
int frameWidth = w / _frameZoomFactor;
int frameHeight = h / _frameZoomFactor;
setFrameSize(frameWidth, frameHeight);
updateDesignResolutionSize();
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_RESIZED, nullptr);
}
int GLViewImpl::getMonitorCount() const {
int count = 0;
glfwGetMonitors(&count);
@ -726,21 +754,21 @@ void GLViewImpl::updateFrameSize()
int frameBufferW = 0, frameBufferH = 0;
glfwGetFramebufferSize(_mainWindow, &frameBufferW, &frameBufferH);
// if (frameBufferW == 2 * w && frameBufferH == 2 * h)
// {
// if (_isRetinaEnabled)
// {
// _retinaFactor = 1;
// }
// else
// {
// _retinaFactor = 2;
// }
// glfwSetWindowSize(_mainWindow, _screenSize.width/2 * _retinaFactor * _frameZoomFactor, _screenSize.height/2 * _retinaFactor * _frameZoomFactor);
if (frameBufferW == 2 * w && frameBufferH == 2 * h)
{
if (_isRetinaEnabled)
{
_retinaFactor = 1;
}
else
{
_retinaFactor = 2;
}
glfwSetWindowSize(_mainWindow, _screenSize.width/2 * _retinaFactor * _frameZoomFactor, _screenSize.height/2 * _retinaFactor * _frameZoomFactor);
// _isInRetinaMonitor = true;
// }
// else
_isInRetinaMonitor = true;
}
else
{
if (_isInRetinaMonitor)
{
@ -966,7 +994,7 @@ void GLViewImpl::onGLFWWindowPosCallback(GLFWwindow* /*window*/, int /*x*/, int
}
void GLViewImpl::onGLFWFramebufferSizeCallback(GLFWwindow* window, int w, int h)
{ // win32 glfw never invoke this callback
{ // win32 glfw same with onGLFWWindowSizeCallback
#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32
float frameSizeW = _screenSize.width;
float frameSizeH = _screenSize.height;
@ -1000,14 +1028,19 @@ void GLViewImpl::onGLFWWindowSizeCallback(GLFWwindow* /*window*/, int w, int h)
{
if (w && h && _resolutionPolicy != ResolutionPolicy::UNKNOWN)
{
Size baseDesignSize = _designResolutionSize;
ResolutionPolicy baseResolutionPolicy = _resolutionPolicy;
int frameWidth = w / _frameZoomFactor;
int frameHeight = h / _frameZoomFactor;
setFrameSize(frameWidth, frameHeight);
setDesignResolutionSize(baseDesignSize.width, baseDesignSize.height, baseResolutionPolicy);
/*
x-studio spec, fix view size incorrect when window size changed.
The original code behavior:
1. first time enter full screen: w,h=1920,1080
2. second or later enter full screen: will trigger 2 times WindowSizeCallback
1). w,h=976,679
2). w,h=1024,768
@remark: we should use glfwSetWindowMonitor to control the window size in full screen mode
@see also: updateWindowSize (call after enter/exit full screen mode)
*/
updateDesignResolutionSize();
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_RESIZED, nullptr);
}
}

View File

@ -82,10 +82,27 @@ public:
GLFWwindow* getWindow() const { return _mainWindow; }
bool isFullscreen() const;
/* Sets primary monitor full screen with default w*h(refresh rate) */
void setFullscreen();
/* Sets primary monitor full screen with w*h(refresh rate) */
void setFullscreen(int w, int h, int refreshRate);
/* Sets monitor full screen with default w*h(refresh rate) */
void setFullscreen(int monitorIndex);
void setFullscreen(const GLFWvidmode &videoMode, GLFWmonitor *monitor);
/// <summary>
/// Sets monitor full screen with w*h(refresh rate)
/// </summary>
/// <param name="monitorIndex">the 0 based index of monitor</param>
/// <param name="w">the width of hardware resolution in full screen, -1 use default value</param>
/// <param name="h">the height of hardware resolution in full screen, -1 use default value</param>
/// <param name="refreshRate">the display refresh rate, usually 60, -1 use default value</param>
void setFullscreen(int monitorIndex, int w, int h, int refreshRate);
/* for internal use */
void setFullscreen(GLFWmonitor *monitor, int w, int h, int refreshRate);
void setWindowed(int width, int height);
int getMonitorCount() const;
Size getMonitorSize() const;
@ -139,6 +156,9 @@ protected:
bool loadGL();
/* update frame layout when enter/exit full screen mode */
void updateWindowSize();
void updateFrameSize();
// GLFW callbacks

View File

@ -59,30 +59,33 @@ std::string WindowTestWindowed2::subtitle() const {
return "Windowed 960x640";
}
void WindowTestFullscreen1::onEnter() {
WindowTest::onEnter();
bool WindowTestFullscreen1::init()
{
// @remark: Set full screen before layout renderer elements to ensure VisibleRect is
// correct with full screen window size
GLViewImpl* view = (GLViewImpl*)Director::getInstance()->getOpenGLView();
view->setFullscreen();
return WindowTest::init();
}
std::string WindowTestFullscreen1::subtitle() const {
return "Fullscreen";
}
void WindowTestFullscreen2::onEnter() {
WindowTest::onEnter();
bool WindowTestFullscreen2::init() {
GLViewImpl* view = (GLViewImpl*)Director::getInstance()->getOpenGLView();
view->setFullscreen(1);
return WindowTest::init();
}
std::string WindowTestFullscreen2::subtitle() const {
return "Fullscreen Monitor 2 (if present)";
}
void WindowTestFullscreen3::onEnter() {
WindowTest::onEnter();
bool WindowTestFullscreen3::init() {
GLViewImpl* view = (GLViewImpl*)Director::getInstance()->getOpenGLView();
view->setFullscreen(2);
view->setFullscreen(3);
return WindowTest::init();
}
std::string WindowTestFullscreen3::subtitle() const {

View File

@ -56,7 +56,7 @@ class WindowTestFullscreen1 : public WindowTest
{
public:
CREATE_FUNC(WindowTestFullscreen1);
virtual void onEnter() override;
bool init() override;
virtual std::string subtitle() const override;
};
@ -64,14 +64,14 @@ class WindowTestFullscreen2 : public WindowTest
{
public:
CREATE_FUNC(WindowTestFullscreen2);
virtual void onEnter() override;
bool init() override;
virtual std::string subtitle() const override;
};
class WindowTestFullscreen3 : public WindowTest
{
public:
CREATE_FUNC(WindowTestFullscreen3);
virtual void onEnter() override;
bool init() override;
virtual std::string subtitle() const override;
};