Browse Source

Implement fullscreen/vsync into Window and Settings

master
Rick van Vonderen 5 years ago
parent
commit
18f4a028c1
  1. 17
      inferno/src/inferno/settings.cpp
  2. 2
      inferno/src/inferno/settings.h
  3. 31
      inferno/src/inferno/window.cpp
  4. 2
      inferno/src/inferno/window.h

17
inferno/src/inferno/settings.cpp

@ -29,9 +29,11 @@ namespace Inferno {
nlohmann::json m_json = this->load(); nlohmann::json m_json = this->load();
try { try {
m_properties.title = strdup(m_json["window"]["title"].get<std::string>().c_str()); m_properties.title = strdup(m_json["window"]["title"].get<std::string>().c_str());
m_properties.width = m_json["window"]["width"].get<int>(); m_properties.width = m_json["window"]["width"].get<int>();
m_properties.height = m_json["window"]["height"].get<int>(); m_properties.height = m_json["window"]["height"].get<int>();
m_properties.fullscreen = strdup(m_json["window"]["fullscreen"].get<std::string>().c_str());
m_properties.vsync = m_json["window"]["vsync"].get<bool>();
} }
catch (...) { catch (...) {
NF_CORE_WARN("Settings syntax error: using default values"); NF_CORE_WARN("Settings syntax error: using default values");
@ -42,6 +44,7 @@ namespace Inferno {
{ {
// Delete const char*s created by strdup() // Delete const char*s created by strdup()
delete m_properties.title; delete m_properties.title;
delete m_properties.fullscreen;
} }
nlohmann::json Settings::load() const nlohmann::json Settings::load() const
@ -65,9 +68,11 @@ namespace Inferno {
bool Settings::save() bool Settings::save()
{ {
nlohmann::json json; nlohmann::json json;
json["window"]["title"] = m_properties.title; json["window"]["title"] = m_properties.title;
json["window"]["width"] = m_properties.width; json["window"]["width"] = m_properties.width;
json["window"]["height"] = m_properties.height; json["window"]["height"] = m_properties.height;
json["window"]["fullscreen"] = m_properties.fullscreen;
json["window"]["vsync"] = m_properties.vsync;
std::ofstream file (m_path); std::ofstream file (m_path);
NF_CORE_ASSERT(file.is_open(), "Could not open settings file!"); NF_CORE_ASSERT(file.is_open(), "Could not open settings file!");

2
inferno/src/inferno/settings.h

@ -9,6 +9,8 @@ namespace Inferno {
const char* title = "Inferno default"; const char* title = "Inferno default";
unsigned int width = 1280; unsigned int width = 1280;
unsigned int height = 720; unsigned int height = 720;
const char* fullscreen = "windowed"; // windowed/fullscreen/borderless
bool vsync = true;
}; };
class Settings { class Settings {

31
inferno/src/inferno/window.cpp

@ -1,3 +1,5 @@
#include <cstring>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "inferno/core.h" #include "inferno/core.h"
@ -41,13 +43,40 @@ namespace Inferno {
} }
// Set window properties // Set window properties
// -----------------------------------------
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); 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 // Create GLFW window
m_window = glfwCreateWindow(width, height, title, NULL, NULL); m_window = glfwCreateWindow(width, height, title, monitor, nullptr);
s_windowCount++; s_windowCount++;
NF_CORE_ASSERT(m_window, "Failed to create GLFW window!"); NF_CORE_ASSERT(m_window, "Failed to create GLFW window!");

2
inferno/src/inferno/window.h

@ -14,6 +14,8 @@ namespace Inferno {
const char* title = "Inferno"; const char* title = "Inferno";
unsigned int width = 1280; unsigned int width = 1280;
unsigned int height = 720; unsigned int height = 720;
const char* fullscreen = "windowed"; // windowed/fullscreen/borderless
bool vsync = true;
}; };
class Window { class Window {

Loading…
Cancel
Save