From e9904604ce0199d696c4941dff4f5ad84d22355f Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Mon, 6 Aug 2018 20:04:50 +0500 Subject: Implemented Settings Manager --- src/Render.cpp | 25 ++++++++++++++++-------- src/Render.hpp | 3 +++ src/Settings.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Settings.hpp | 13 +++++++++++++ 4 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 src/Settings.cpp create mode 100644 src/Settings.hpp diff --git a/src/Render.cpp b/src/Render.cpp index c44d2b4..d5a4b6a 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -12,12 +12,15 @@ #include "World.hpp" #include "GameState.hpp" #include "RendererWorld.hpp" +#include "Settings.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(16)) { InitEvents(); + Settings::Load(); + InitSdl(windowWidth, windowHeight, windowTitle); glCheckError(); InitGlew(); @@ -27,10 +30,19 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight, PrepareToRendering(); glCheckError(); + strcpy(fieldUsername, Settings::Read("username", "HelloOne").c_str()); + strcpy(fieldServerAddr, Settings::Read("serverAddr", "127.0.0.1").c_str()); + fieldServerPort = std::stoi(Settings::Read("serverPort", "25565")); + LOG(INFO) << "Supported threads: " << std::thread::hardware_concurrency(); } Render::~Render() { + Settings::Write("username", fieldUsername); + Settings::Write("serverAddr", fieldServerAddr); + Settings::Write("serverPort", std::to_string(fieldServerPort)); + Settings::Save(); + ImGui_ImplSdlGL3_Shutdown(); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(window); @@ -383,16 +395,13 @@ void Render::RenderGui() { case State::MainMenu: { ImGui::SetNextWindowPosCenter(); ImGui::Begin("Menu", 0, windowFlags); - static char buff[512] = "127.0.0.1"; - static int port = 25565; - static char buffName[512] = "HelloOne"; if (ImGui::Button("Connect")) { - PUSH_EVENT("ConnectToServer", std::make_tuple(std::string(buff), - (unsigned short) port, std::string(buffName))); + PUSH_EVENT("ConnectToServer", std::make_tuple(std::string(fieldServerAddr), + (unsigned short) fieldServerPort, std::string(fieldUsername))); } - ImGui::InputText("Username", buffName, 512); - ImGui::InputText("Address", buff, 512); - ImGui::InputInt("Port", &port); + ImGui::InputText("Username", fieldUsername, 512); + ImGui::InputText("Address", fieldServerAddr, 512); + ImGui::InputInt("Port", &fieldServerPort); ImGui::Separator(); if (ImGui::Button("Exit")) PUSH_EVENT("Exit",0); diff --git a/src/Render.hpp b/src/Render.hpp index 3367100..f4523a1 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -32,6 +32,9 @@ class Render { std::vector chatMessages; EventListener listener; std::string stateString; + char fieldUsername[512]; + char fieldServerAddr[512]; + int fieldServerPort; void SetMouseCapture(bool IsCaptured); diff --git a/src/Settings.cpp b/src/Settings.cpp new file mode 100644 index 0000000..a585752 --- /dev/null +++ b/src/Settings.cpp @@ -0,0 +1,58 @@ +#include "Settings.hpp" + +#include +#include + +#include +#include + + +using json = nlohmann::json; + +std::map values; + +const std::string saveFileName = "./settings.json"; + +void Settings::Load() { + std::ifstream stream(saveFileName); + if (!stream) { + LOG(ERROR) << "Loading settings failed: Can't open ifstream for " << saveFileName << ": " << strerror(errno); + return; + } + json j; + stream >> j; + + for (json::iterator it = j.begin(); it != j.end(); ++it) { + values.insert(std::make_pair(it.key(), it->get())); + } + LOG(INFO) << "Loaded " << values.size() << " settings"; +} + +void Settings::Save() { + json j; + for (auto &it : values) { + j[it.first] = it.second; + } + std::string text = j.dump(4); + + std::ofstream stream(saveFileName); + if (!stream) { + LOG(ERROR) << "Saving settings failed: Can't open ofstream for " << saveFileName << ": "<< strerror(errno); + return; + } + stream << text; + LOG(INFO) << "Saved " << values.size() << " settings"; +} + +std::string Settings::Read(const std::string &key, const std::string &defaultValue) { + auto it = values.find(key); + if (it == values.end()) { + values.insert(std::make_pair(key, defaultValue)); + it = values.find(key); + } + return it->second; +} + +void Settings::Write(const std::string &key, const std::string &value) { + values[key] = value; +} diff --git a/src/Settings.hpp b/src/Settings.hpp new file mode 100644 index 0000000..194e736 --- /dev/null +++ b/src/Settings.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace Settings { + void Load(); + + void Save(); + + std::string Read(const std::string &key, const std::string &defaultValue); + + void Write(const std::string &key, const std::string &value); +} \ No newline at end of file -- cgit v1.2.3