6 changed files with 139 additions and 5 deletions
			
			
		| @ -0,0 +1,7 @@ | ||||
| { | ||||
| 	"window": { | ||||
| 		"height": 720, | ||||
| 		"title": "Inferno", | ||||
| 		"width": 1280 | ||||
| 	} | ||||
| } | ||||
| @ -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
 | ||||
					Loading…
					
					
				
		Reference in new issue