From 859401ec24ebe8d273987c4a08481113a499007f Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Thu, 19 Dec 2019 02:50:56 +0100 Subject: [PATCH] Move fullscreen to separate function, vsync to Context --- assets/settings.json | 2 + inferno/src/inferno/render/context.cpp | 8 ++- inferno/src/inferno/window.cpp | 68 ++++++++++++++------------ inferno/src/inferno/window.h | 4 ++ 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/assets/settings.json b/assets/settings.json index 0dfc38c..6b3e32c 100644 --- a/assets/settings.json +++ b/assets/settings.json @@ -1,7 +1,9 @@ { "window": { + "fullscreen": "windowed", "height": 720, "title": "Inferno", + "vsync": true, "width": 1280 } } diff --git a/inferno/src/inferno/render/context.cpp b/inferno/src/inferno/render/context.cpp index 5fac876..41ed0e2 100644 --- a/inferno/src/inferno/render/context.cpp +++ b/inferno/src/inferno/render/context.cpp @@ -26,8 +26,14 @@ namespace Inferno { NF_CORE_INFO(" Renderer: %s", glGetString(GL_RENDERER)); NF_CORE_INFO(" Version: %s", glGetString(GL_VERSION)); - // Set viewport Window &w = *(Window*)glfwGetWindowUserPointer(m_window); + + // Disable vsync + if (!w.isVSync()) { + glfwSwapInterval(0); + } + + // Set viewport glViewport(0, 0, w.getWidth(), w.getHeight()); // Enable z-buffer / depth buffer diff --git a/inferno/src/inferno/window.cpp b/inferno/src/inferno/window.cpp index 82133e6..689385d 100644 --- a/inferno/src/inferno/window.cpp +++ b/inferno/src/inferno/window.cpp @@ -38,8 +38,6 @@ namespace Inferno { const char* title = m_windowProperties.title; unsigned int width = m_windowProperties.width; unsigned int height = m_windowProperties.height; - const char* fullscreen = m_windowProperties.fullscreen; - bool vsync = m_windowProperties.vsync; // Only init once if (s_windowCount == 0) { @@ -47,43 +45,19 @@ namespace Inferno { } // Set window properties - // ----------------------------------------- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); - - // Windowed - GLFWmonitor* monitor = nullptr; - // Fullscreen - if (strcmp(fullscreen, "fullscreen") == 0) { - monitor = glfwGetPrimaryMonitor(); - } - // Borderless fullscreen - if (strcmp(fullscreen, "borderless") == 0) { - monitor = glfwGetPrimaryMonitor(); - - const GLFWvidmode* mode = glfwGetVideoMode(monitor); - width = mode->width; - height = mode->height; - - glfwWindowHint(GLFW_RED_BITS, mode->redBits); - glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits); - glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits); - glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate); - } - - // Vsync - if (!vsync) { - glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE); - } - // ----------------------------------------- + // glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Create GLFW window - m_window = glfwCreateWindow(width, height, title, monitor, nullptr); + m_window = glfwCreateWindow(width, height, title, nullptr, nullptr); s_windowCount++; NF_CORE_ASSERT(m_window, "Failed to create GLFW window!"); + // Set windowed/fullscreen/borderless + this->setWindowMonitor(); + // Associate the wrapper to the window glfwSetWindowUserPointer(m_window, this); @@ -199,4 +173,36 @@ namespace Inferno { } } + void Window::setWindowMonitor() + { + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); + int xPos = 0; + int yPos = 0; + unsigned int width = m_windowProperties.width; + unsigned int height = m_windowProperties.height; + int refresh = GLFW_DONT_CARE; + + const char* fullscreen = m_windowProperties.fullscreen; + const GLFWvidmode* mode = glfwGetVideoMode(monitor); + + if (strcmp(fullscreen, "windowed") == 0) { + monitor = nullptr; + // Put window in the center of the monitor + xPos = (mode->width - width) / 2; + yPos = (mode->height - height) / 2; + } + + if (strcmp(fullscreen, "fullscreen") == 0) { + refresh = mode->refreshRate; + } + + if (strcmp(fullscreen, "borderless") == 0) { + width = mode->width; + height = mode->height; + refresh = mode->refreshRate; + } + + glfwSetWindowMonitor(m_window, monitor, xPos, yPos, width, height, refresh); + } + } diff --git a/inferno/src/inferno/window.h b/inferno/src/inferno/window.h index ab3d93e..5b57d55 100644 --- a/inferno/src/inferno/window.h +++ b/inferno/src/inferno/window.h @@ -30,8 +30,12 @@ namespace Inferno { // ----------------------------------------- + void setWindowMonitor(); + inline int getWidth() const { return m_windowProperties.width; } inline int getHeight() const { return m_windowProperties.height; } + inline bool isVSync() const { return m_windowProperties.vsync; } + inline GLFWwindow* getWindow() const { return m_window; } inline void setEventCallback(std::function callback) { m_eventCallback = callback; }