Add Settings class
This commit is contained in:
+2
-1
@@ -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 <GAME> 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"
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"window": {
|
||||
"height": 720,
|
||||
"title": "Inferno",
|
||||
"width": 1280
|
||||
}
|
||||
}
|
||||
@@ -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<Window>();
|
||||
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)
|
||||
|
||||
@@ -10,8 +10,7 @@ namespace Inferno {
|
||||
class WindowResizeEvent;
|
||||
class Window;
|
||||
|
||||
class Application
|
||||
{
|
||||
class Application {
|
||||
public:
|
||||
Application();
|
||||
virtual ~Application();
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <fstream> // std::ifstream, std::ofstream
|
||||
#include <iomanip> // std::setfill, std::setw
|
||||
#include <string> // 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<std::string>().c_str();
|
||||
m_properties.width = m_json["window"]["width"].get<int>();
|
||||
m_properties.height = m_json["window"]["height"].get<int>();
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user