Include Window properties inside Settings

This commit is contained in:
Rick van Vonderen
2019-12-19 01:39:21 +01:00
parent 18f4a028c1
commit e769a1d03f
3 changed files with 28 additions and 26 deletions
+12 -12
View File
@@ -29,11 +29,11 @@ namespace Inferno {
nlohmann::json m_json = this->load();
try {
m_properties.title = strdup(m_json["window"]["title"].get<std::string>().c_str());
m_properties.width = m_json["window"]["width"].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>();
m_properties.window.title = strdup(m_json["window"]["title"].get<std::string>().c_str());
m_properties.window.width = m_json["window"]["width"].get<int>();
m_properties.window.height = m_json["window"]["height"].get<int>();
m_properties.window.fullscreen = strdup(m_json["window"]["fullscreen"].get<std::string>().c_str());
m_properties.window.vsync = m_json["window"]["vsync"].get<bool>();
}
catch (...) {
NF_CORE_WARN("Settings syntax error: using default values");
@@ -43,8 +43,8 @@ namespace Inferno {
void Settings::destroy()
{
// Delete const char*s created by strdup()
delete m_properties.title;
delete m_properties.fullscreen;
delete m_properties.window.title;
delete m_properties.window.fullscreen;
}
nlohmann::json Settings::load() const
@@ -68,11 +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"]["fullscreen"] = m_properties.fullscreen;
json["window"]["vsync"] = m_properties.vsync;
json["window"]["title"] = m_properties.window.title;
json["window"]["width"] = m_properties.window.width;
json["window"]["height"] = m_properties.window.height;
json["window"]["fullscreen"] = m_properties.window.fullscreen;
json["window"]["vsync"] = m_properties.window.vsync;
std::ofstream file (m_path);
NF_CORE_ASSERT(file.is_open(), "Could not open settings file!");
+3 -5
View File
@@ -3,14 +3,12 @@
#include <nlohmann/json.hpp>
#include "inferno/window.h"
namespace Inferno {
struct SettingsProperties {
const char* title = "Inferno default";
unsigned int width = 1280;
unsigned int height = 720;
const char* fullscreen = "windowed"; // windowed/fullscreen/borderless
bool vsync = true;
WindowProperties window;
};
class Settings {
+13 -9
View File
@@ -18,9 +18,11 @@ namespace Inferno {
Window::Window()
{
m_windowProperties = {
Settings::get().properties().title,
Settings::get().properties().width,
Settings::get().properties().height,
Settings::get().properties().window.title,
Settings::get().properties().window.width,
Settings::get().properties().window.height,
Settings::get().properties().window.fullscreen,
Settings::get().properties().window.vsync,
};
this->initialize();
@@ -33,9 +35,11 @@ namespace Inferno {
void Window::initialize()
{
const char* title = m_windowProperties.title;
unsigned int width = m_windowProperties.width;
unsigned int height = m_windowProperties.height;
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) {
@@ -52,11 +56,11 @@ namespace Inferno {
// Windowed
GLFWmonitor* monitor = nullptr;
// Fullscreen
if (strcmp(Settings::get().properties().fullscreen, "fullscreen") == 0) {
if (strcmp(fullscreen, "fullscreen") == 0) {
monitor = glfwGetPrimaryMonitor();
}
// Borderless fullscreen
if (strcmp(Settings::get().properties().fullscreen, "borderless") == 0) {
if (strcmp(fullscreen, "borderless") == 0) {
monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
@@ -70,7 +74,7 @@ namespace Inferno {
}
// Vsync
if (!Settings::get().properties().vsync) {
if (!vsync) {
glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE);
}
// -----------------------------------------