Browse Source

Include Window properties inside Settings

master
Rick van Vonderen 5 years ago
parent
commit
e769a1d03f
  1. 24
      inferno/src/inferno/settings.cpp
  2. 8
      inferno/src/inferno/settings.h
  3. 16
      inferno/src/inferno/window.cpp

24
inferno/src/inferno/settings.cpp

@ -29,11 +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.window.title = strdup(m_json["window"]["title"].get<std::string>().c_str());
m_properties.width = m_json["window"]["width"].get<int>(); m_properties.window.width = m_json["window"]["width"].get<int>();
m_properties.height = m_json["window"]["height"].get<int>(); m_properties.window.height = m_json["window"]["height"].get<int>();
m_properties.fullscreen = strdup(m_json["window"]["fullscreen"].get<std::string>().c_str()); m_properties.window.fullscreen = strdup(m_json["window"]["fullscreen"].get<std::string>().c_str());
m_properties.vsync = m_json["window"]["vsync"].get<bool>(); m_properties.window.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");
@ -43,8 +43,8 @@ namespace Inferno {
void Settings::destroy() void Settings::destroy()
{ {
// Delete const char*s created by strdup() // Delete const char*s created by strdup()
delete m_properties.title; delete m_properties.window.title;
delete m_properties.fullscreen; delete m_properties.window.fullscreen;
} }
nlohmann::json Settings::load() const nlohmann::json Settings::load() const
@ -68,11 +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.window.title;
json["window"]["width"] = m_properties.width; json["window"]["width"] = m_properties.window.width;
json["window"]["height"] = m_properties.height; json["window"]["height"] = m_properties.window.height;
json["window"]["fullscreen"] = m_properties.fullscreen; json["window"]["fullscreen"] = m_properties.window.fullscreen;
json["window"]["vsync"] = m_properties.vsync; json["window"]["vsync"] = m_properties.window.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!");

8
inferno/src/inferno/settings.h

@ -3,14 +3,12 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "inferno/window.h"
namespace Inferno { namespace Inferno {
struct SettingsProperties { struct SettingsProperties {
const char* title = "Inferno default"; WindowProperties window;
unsigned int width = 1280;
unsigned int height = 720;
const char* fullscreen = "windowed"; // windowed/fullscreen/borderless
bool vsync = true;
}; };
class Settings { class Settings {

16
inferno/src/inferno/window.cpp

@ -18,9 +18,11 @@ namespace Inferno {
Window::Window() Window::Window()
{ {
m_windowProperties = { m_windowProperties = {
Settings::get().properties().title, Settings::get().properties().window.title,
Settings::get().properties().width, Settings::get().properties().window.width,
Settings::get().properties().height, Settings::get().properties().window.height,
Settings::get().properties().window.fullscreen,
Settings::get().properties().window.vsync,
}; };
this->initialize(); this->initialize();
@ -36,6 +38,8 @@ namespace Inferno {
const char* title = m_windowProperties.title; const char* title = m_windowProperties.title;
unsigned int width = m_windowProperties.width; unsigned int width = m_windowProperties.width;
unsigned int height = m_windowProperties.height; unsigned int height = m_windowProperties.height;
const char* fullscreen = m_windowProperties.fullscreen;
bool vsync = m_windowProperties.vsync;
// Only init once // Only init once
if (s_windowCount == 0) { if (s_windowCount == 0) {
@ -52,11 +56,11 @@ namespace Inferno {
// Windowed // Windowed
GLFWmonitor* monitor = nullptr; GLFWmonitor* monitor = nullptr;
// Fullscreen // Fullscreen
if (strcmp(Settings::get().properties().fullscreen, "fullscreen") == 0) { if (strcmp(fullscreen, "fullscreen") == 0) {
monitor = glfwGetPrimaryMonitor(); monitor = glfwGetPrimaryMonitor();
} }
// Borderless fullscreen // Borderless fullscreen
if (strcmp(Settings::get().properties().fullscreen, "borderless") == 0) { if (strcmp(fullscreen, "borderless") == 0) {
monitor = glfwGetPrimaryMonitor(); monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor); const GLFWvidmode* mode = glfwGetVideoMode(monitor);
@ -70,7 +74,7 @@ namespace Inferno {
} }
// Vsync // Vsync
if (!Settings::get().properties().vsync) { if (!vsync) {
glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE); glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE);
} }
// ----------------------------------------- // -----------------------------------------

Loading…
Cancel
Save