From 18f4a028c1bb891720dec76e231f57e50a61129d Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Thu, 19 Dec 2019 01:17:53 +0100 Subject: [PATCH] Implement fullscreen/vsync into Window and Settings --- inferno/src/inferno/settings.cpp | 17 +++++++++++------ inferno/src/inferno/settings.h | 2 ++ inferno/src/inferno/window.cpp | 31 ++++++++++++++++++++++++++++++- inferno/src/inferno/window.h | 2 ++ 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/inferno/src/inferno/settings.cpp b/inferno/src/inferno/settings.cpp index 3d1d730..5c16711 100644 --- a/inferno/src/inferno/settings.cpp +++ b/inferno/src/inferno/settings.cpp @@ -29,9 +29,11 @@ namespace Inferno { nlohmann::json m_json = this->load(); try { - m_properties.title = strdup(m_json["window"]["title"].get().c_str()); - m_properties.width = m_json["window"]["width"].get(); - m_properties.height = m_json["window"]["height"].get(); + m_properties.title = strdup(m_json["window"]["title"].get().c_str()); + m_properties.width = m_json["window"]["width"].get(); + m_properties.height = m_json["window"]["height"].get(); + m_properties.fullscreen = strdup(m_json["window"]["fullscreen"].get().c_str()); + m_properties.vsync = m_json["window"]["vsync"].get(); } catch (...) { NF_CORE_WARN("Settings syntax error: using default values"); @@ -42,6 +44,7 @@ namespace Inferno { { // Delete const char*s created by strdup() delete m_properties.title; + delete m_properties.fullscreen; } nlohmann::json Settings::load() const @@ -65,9 +68,11 @@ namespace Inferno { bool Settings::save() { nlohmann::json json; - json["window"]["title"] = m_properties.title; - json["window"]["width"] = m_properties.width; - json["window"]["height"] = m_properties.height; + json["window"]["title"] = m_properties.title; + json["window"]["width"] = m_properties.width; + json["window"]["height"] = m_properties.height; + json["window"]["fullscreen"] = m_properties.fullscreen; + json["window"]["vsync"] = m_properties.vsync; std::ofstream file (m_path); NF_CORE_ASSERT(file.is_open(), "Could not open settings file!"); diff --git a/inferno/src/inferno/settings.h b/inferno/src/inferno/settings.h index 38f7c0d..768f4bf 100644 --- a/inferno/src/inferno/settings.h +++ b/inferno/src/inferno/settings.h @@ -9,6 +9,8 @@ namespace Inferno { const char* title = "Inferno default"; unsigned int width = 1280; unsigned int height = 720; + const char* fullscreen = "windowed"; // windowed/fullscreen/borderless + bool vsync = true; }; class Settings { diff --git a/inferno/src/inferno/window.cpp b/inferno/src/inferno/window.cpp index 5450b80..4e291ce 100644 --- a/inferno/src/inferno/window.cpp +++ b/inferno/src/inferno/window.cpp @@ -1,3 +1,5 @@ +#include + #include #include "inferno/core.h" @@ -41,13 +43,40 @@ 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(Settings::get().properties().fullscreen, "fullscreen") == 0) { + monitor = glfwGetPrimaryMonitor(); + } + // Borderless fullscreen + if (strcmp(Settings::get().properties().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 (!Settings::get().properties().vsync) { + glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE); + } + // ----------------------------------------- + // Create GLFW window - m_window = glfwCreateWindow(width, height, title, NULL, NULL); + m_window = glfwCreateWindow(width, height, title, monitor, nullptr); s_windowCount++; NF_CORE_ASSERT(m_window, "Failed to create GLFW window!"); diff --git a/inferno/src/inferno/window.h b/inferno/src/inferno/window.h index 456f204..ab3d93e 100644 --- a/inferno/src/inferno/window.h +++ b/inferno/src/inferno/window.h @@ -14,6 +14,8 @@ namespace Inferno { const char* title = "Inferno"; unsigned int width = 1280; unsigned int height = 720; + const char* fullscreen = "windowed"; // windowed/fullscreen/borderless + bool vsync = true; }; class Window {