From 6b8d5dd090bd09c1f56c50e808e0b534724ff14b Mon Sep 17 00:00:00 2001 From: Nick Barrios Date: Tue, 8 Apr 2014 13:33:24 -0400 Subject: [PATCH] CCGLView: Add support for specifying video mode and monitor for fullscreen window creation. --- cocos/2d/platform/desktop/CCGLView.cpp | 43 +++++++++++++++++++++----- cocos/2d/platform/desktop/CCGLView.h | 4 ++- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/cocos/2d/platform/desktop/CCGLView.cpp b/cocos/2d/platform/desktop/CCGLView.cpp index 4f29628566..06d6170887 100644 --- a/cocos/2d/platform/desktop/CCGLView.cpp +++ b/cocos/2d/platform/desktop/CCGLView.cpp @@ -260,7 +260,7 @@ GLView::GLView() , _retinaFactor(1) , _frameZoomFactor(1.0f) , _mainWindow(nullptr) -, _primaryMonitor(nullptr) +, _monitor(nullptr) , _mouseX(0.0f) , _mouseY(0.0f) { @@ -328,6 +328,17 @@ GLView* GLView::createWithFullScreen(const std::string& viewName, Size size) return nullptr; } +GLView* GLView::createWithFullScreen(const std::string& viewName, const GLFWvidmode &videoMode, GLFWmonitor *monitor) +{ + auto ret = new GLView(); + if(ret && ret->initWithFullscreen(viewName, videoMode, monitor)) { + ret->autorelease(); + return ret; + } + + return nullptr; +} + bool GLView::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor) { @@ -340,7 +351,7 @@ bool GLView::initWithRect(const std::string& viewName, Rect rect, float frameZoo _mainWindow = glfwCreateWindow(rect.size.width * _frameZoomFactor, rect.size.height * _frameZoomFactor, _viewName.c_str(), - _primaryMonitor, + _monitor, nullptr); glfwMakeContextCurrent(_mainWindow); @@ -378,23 +389,41 @@ bool GLView::initWithRect(const std::string& viewName, Rect rect, float frameZoo bool GLView::initWithFullScreen(const std::string& viewName) { - _primaryMonitor = glfwGetPrimaryMonitor(); - if (nullptr == _primaryMonitor) + //Create fullscreen window on primary monitor at its current video mode. + _monitor = glfwGetPrimaryMonitor(); + if (nullptr == _monitor) return false; - const GLFWvidmode* videoMode = glfwGetVideoMode(_primaryMonitor); + const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor); return initWithRect(viewName, Rect(0, 0, videoMode->width, videoMode->height), 1.0f); } bool GLView::initWithFullScreen(const std::string &viewName, cocos2d::Size size) { - _primaryMonitor = glfwGetPrimaryMonitor(); - if (nullptr == _primaryMonitor) + //Create fullscreen window on primary monitor with the video mode closest to the specified size (refresh rate and bit depth will be at GLFW defaults). + _monitor = glfwGetPrimaryMonitor(); + if (nullptr == _monitor) return false; return initWithRect(viewName, Rect(0, 0, size.width, size.height), 1.0f); } +bool GLView::initWithFullscreen(const std::string &viewname, const GLFWvidmode &videoMode, GLFWmonitor *monitor) +{ + //Create fullscreen on specified monitor at the specified video mode. + _monitor = monitor; + if (nullptr == _monitor) + return false; + + //These are soft contraints. If the video mode is retrieved at runtime, the resulting window and context should match these exactly. If invalid attribs are passed (eg. from an outdated cache), window creation will NOT fail but the actual window/context may differ. + glfwWindowHint(GLFW_REFRESH_RATE, videoMode.refreshRate); + glfwWindowHint(GLFW_RED_BITS, videoMode.redBits); + glfwWindowHint(GLFW_BLUE_BITS, videoMode.blueBits); + glfwWindowHint(GLFW_GREEN_BITS, videoMode.greenBits); + + return initWithRect(viewname, Rect(0, 0, videoMode.width, videoMode.height), 1.0f); +} + bool GLView::isOpenGLReady() { return nullptr != _mainWindow; diff --git a/cocos/2d/platform/desktop/CCGLView.h b/cocos/2d/platform/desktop/CCGLView.h index 92902c4262..89031ed1c7 100644 --- a/cocos/2d/platform/desktop/CCGLView.h +++ b/cocos/2d/platform/desktop/CCGLView.h @@ -40,6 +40,7 @@ public: static GLView* createWithRect(const std::string& viewName, Rect size, float frameZoomFactor = 1.0f); static GLView* createWithFullScreen(const std::string& viewName); static GLView* createWithFullScreen(const std::string& viewName, Size size); + static GLView* createWithFullScreen(const std::string& viewName, const GLFWvidmode &videoMode, GLFWmonitor *monitor); /* *frameZoomFactor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop. @@ -84,6 +85,7 @@ protected: bool initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor); bool initWithFullScreen(const std::string& viewName); bool initWithFullScreen(const std::string& viewName, Size rect); + bool initWithFullscreen(const std::string& viewname, const GLFWvidmode &videoMode, GLFWmonitor *monitor); bool initGlew(); @@ -109,7 +111,7 @@ protected: float _frameZoomFactor; GLFWwindow* _mainWindow; - GLFWmonitor* _primaryMonitor; + GLFWmonitor* _monitor; float _mouseX; float _mouseY;