From 5933ed96f3c9353f5577f21aaf6f2f2042365c45 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 25 Dec 2019 00:06:50 +0100 Subject: [PATCH] Convert Settings to pure static --- inferno/src/inferno/application.cpp | 2 +- inferno/src/inferno/input.h | 2 ++ inferno/src/inferno/settings.cpp | 56 ++++++++++++----------------- inferno/src/inferno/settings.h | 26 +++++--------- inferno/src/inferno/window.cpp | 10 +++--- 5 files changed, 39 insertions(+), 57 deletions(-) diff --git a/inferno/src/inferno/application.cpp b/inferno/src/inferno/application.cpp index 03c0fad..58a8677 100644 --- a/inferno/src/inferno/application.cpp +++ b/inferno/src/inferno/application.cpp @@ -23,7 +23,7 @@ namespace Inferno { s_instance = this; // Initialize Settings - new Settings(); + Settings::initialize(); m_window = std::make_unique(); m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent)); diff --git a/inferno/src/inferno/input.h b/inferno/src/inferno/input.h index bd5fb41..366f497 100644 --- a/inferno/src/inferno/input.h +++ b/inferno/src/inferno/input.h @@ -19,6 +19,8 @@ namespace Inferno { static float getMouseX(); static float getMouseY(); +// ----------------------------------------- + static inline float getXOffset() { return m_xOffset; } static inline float getYOffset() { return m_yOffset; } diff --git a/inferno/src/inferno/settings.cpp b/inferno/src/inferno/settings.cpp index 963517e..f39787b 100644 --- a/inferno/src/inferno/settings.cpp +++ b/inferno/src/inferno/settings.cpp @@ -1,47 +1,37 @@ -#include // std::ifstream, std::ofstream -#include // std::setfill, std::setw #include // std::string -#include "inferno/core.h" #include "inferno/file.h" #include "inferno/log.h" #include "inferno/settings.h" namespace Inferno { - Settings* Settings::s_instance = nullptr; + bool Settings::m_initialized = false; - Settings::Settings(const char* path) : - m_path(path) - { - NF_CORE_ASSERT(!s_instance, "Settings already exists!"); - s_instance = this; - - this->initialize(); - } - - Settings::~Settings() - { - this->destroy(); - } - -// ----------------------------------------- + const char* Settings::m_path = "assets/settings.json"; + SettingsProperties Settings::m_properties = {}; void Settings::initialize() { - this->update(); + Settings::update(); + + m_initialized = true; } void Settings::update() { - nlohmann::json json = this->load(); + if (m_initialized) { + Settings::destroy(); + } + + nlohmann::json json = Settings::load(); try { - m_properties.window.title = strdup(json["window"]["title"].get().c_str()); - m_properties.window.width = json["window"]["width"].get(); - m_properties.window.height = json["window"]["height"].get(); - m_properties.window.fullscreen = strdup(json["window"]["fullscreen"].get().c_str()); - m_properties.window.vsync = json["window"]["vsync"].get(); + m_properties.window.title = strdup(json["window"]["title"].get().c_str()); + m_properties.window.width = json["window"]["width"].get(); + m_properties.window.height = json["window"]["height"].get(); + m_properties.window.fullscreen = strdup(json["window"]["fullscreen"].get().c_str()) ; + m_properties.window.vsync = json["window"]["vsync"].get(); } catch (...) { NF_CORE_WARN("Settings syntax error: using default values"); @@ -55,7 +45,7 @@ namespace Inferno { delete m_properties.window.fullscreen; } - nlohmann::json Settings::load() const + nlohmann::json Settings::load() { nlohmann::json json; @@ -68,11 +58,11 @@ namespace Inferno { bool Settings::save() { nlohmann::json json; - 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; + 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; File::ioWrite(json, m_path); NF_CORE_INFO("Settings saved"); @@ -80,6 +70,4 @@ namespace Inferno { return true; } -// ----------------------------------------- - } diff --git a/inferno/src/inferno/settings.h b/inferno/src/inferno/settings.h index d2fd6a7..a628138 100644 --- a/inferno/src/inferno/settings.h +++ b/inferno/src/inferno/settings.h @@ -13,30 +13,22 @@ namespace Inferno { class Settings { public: - Settings(const char* path = "assets/settings.json"); - virtual ~Settings(); + static void initialize(); + static void update(); + static void destroy(); -// ----------------------------------------- - - void initialize(); - void update(); - // void render(); - void destroy(); - - nlohmann::json load() const; - bool save(); + static nlohmann::json load(); + static bool save(); // ----------------------------------------- - static inline Settings &get() { return *s_instance; } - - inline SettingsProperties &properties() { return m_properties; } + static inline SettingsProperties &get() { return m_properties; } private: - const char* m_path; - SettingsProperties m_properties; + static bool m_initialized; - static Settings* s_instance; + static const char* m_path; + static SettingsProperties m_properties; }; } diff --git a/inferno/src/inferno/window.cpp b/inferno/src/inferno/window.cpp index c3527be..c41822e 100644 --- a/inferno/src/inferno/window.cpp +++ b/inferno/src/inferno/window.cpp @@ -18,11 +18,11 @@ namespace Inferno { Window::Window() { m_windowProperties = { - 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, + Settings::get().window.title, + Settings::get().window.width, + Settings::get().window.height, + Settings::get().window.fullscreen, + Settings::get().window.vsync, }; this->initialize();