diff --git a/CMakeLists.txt b/CMakeLists.txt index ef8ac38..225e8d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ if(DEBUG) # -Wall = All warnings about contructions that are easily avoidable # -Wextra = Extra warning flags not covered by -Wall # -pg = Generate profile information for analysis with gprof - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og -Wall -Wextra -pg") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og -Wall -Wextra -g -pg") # gprof gmon.out > profile-data.txt else() # cmake -DDEBUG=off .. && make @@ -37,6 +37,7 @@ include_directories( "${ENGINE}/vendor/glad/include" "${ENGINE}/vendor/glfw/include" "${ENGINE}/vendor/glm" + "${ENGINE}/vendor/json/include" "${ENGINE}/src" ) diff --git a/assets/settings.json b/assets/settings.json new file mode 100644 index 0000000..0dfc38c --- /dev/null +++ b/assets/settings.json @@ -0,0 +1,7 @@ +{ + "window": { + "height": 720, + "title": "Inferno", + "width": 1280 + } +} diff --git a/inferno/src/inferno/application.cpp b/inferno/src/inferno/application.cpp index 4a1be78..913e261 100644 --- a/inferno/src/inferno/application.cpp +++ b/inferno/src/inferno/application.cpp @@ -8,6 +8,7 @@ #include "inferno/event/applicationevent.h" #include "inferno/event/event.h" #include "inferno/log.h" +#include "inferno/settings.h" #include "inferno/window.h" namespace Inferno { @@ -26,7 +27,10 @@ namespace Inferno { void Application::run() { - NF_CORE_LOG("Application startup!"); + NF_CORE_LOG("Application startup"); + + // Initialize Settings + new Settings(); m_window = std::make_unique(); m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent)); @@ -43,7 +47,7 @@ namespace Inferno { m_window->update(); } - NF_CORE_LOG("Application shutdown!"); + NF_CORE_LOG("Application shutdown"); } void Application::onEvent(Event &e) diff --git a/inferno/src/inferno/application.h b/inferno/src/inferno/application.h index 3093fe2..5647e6f 100644 --- a/inferno/src/inferno/application.h +++ b/inferno/src/inferno/application.h @@ -10,8 +10,7 @@ namespace Inferno { class WindowResizeEvent; class Window; - class Application - { + class Application { public: Application(); virtual ~Application(); diff --git a/inferno/src/inferno/settings.cpp b/inferno/src/inferno/settings.cpp new file mode 100644 index 0000000..0934919 --- /dev/null +++ b/inferno/src/inferno/settings.cpp @@ -0,0 +1,83 @@ +#include // std::ifstream, std::ofstream +#include // std::setfill, std::setw +#include // std::string + +#include "inferno/core.h" +#include "inferno/log.h" +#include "inferno/settings.h" + +namespace Inferno { + + Settings* Settings::s_instance = nullptr; + + 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(); + } + + void Settings::initialize() + { + nlohmann::json m_json = load(); + + try { + m_properties.title = m_json["window"]["title"].get().c_str(); + m_properties.width = m_json["window"]["width"].get(); + m_properties.height = m_json["window"]["height"].get(); + } + catch (...) { + NF_CORE_WARN("Settings syntax error: using default values"); + } + } + + void Settings::destroy() + { + } + + nlohmann::json Settings::load() const + { + nlohmann::json json; + + std::ifstream file(m_path); + NF_CORE_ASSERT(file.is_open(), "Could not open settings file!"); + + if (file.is_open()) { + // Read the JSON file + file >> json; + file.close(); + } + + NF_CORE_INFO("Settings loaded"); + + return json; + } + + bool Settings::save() + { + nlohmann::json json; + json["window"]["title"] = m_properties.title; + json["window"]["width"] = m_properties.width; + json["window"]["height"] = m_properties.height; + + std::ofstream file (m_path); + NF_CORE_ASSERT(file.is_open(), "Could not open settings file!"); + + if (file.is_open()) { + // Write the JSON file with single tabs, nicely formatted + file << std::setfill ('\t') << std::setw(1) << json << std::endl; + file.close(); + } + + NF_CORE_INFO("Settings saved"); + + return true; + } +} diff --git a/inferno/src/inferno/settings.h b/inferno/src/inferno/settings.h new file mode 100644 index 0000000..3c65fe2 --- /dev/null +++ b/inferno/src/inferno/settings.h @@ -0,0 +1,40 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include + +namespace Inferno { + + struct SettingsProperties { + const char* title = "Inferno default"; + int width = 1280; + int height = 720; + }; + + class Settings { + public: + Settings(const char* path = "assets/settings.json"); + virtual ~Settings(); + + void initialize(); + void destroy(); + + nlohmann::json load() const; + bool save(); + +// ----------------------------------------- + + static inline Settings &get() { return *s_instance; } + + inline SettingsProperties &properties() { return m_properties; } + + private: + const char* m_path; + SettingsProperties m_properties; + + static Settings* s_instance; + }; + +} + +#endif // SETTINGS_H