diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-08-06 17:04:50 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-08-06 17:04:50 +0200 |
commit | e9904604ce0199d696c4941dff4f5ad84d22355f (patch) | |
tree | 1380d5b4f882a61aa1b5e9ee08d4aa4d72c00f48 | |
parent | Separate lighting for each block face (diff) | |
download | AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.gz AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.bz2 AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.lz AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.xz AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.zst AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Render.cpp | 25 | ||||
-rw-r--r-- | src/Render.hpp | 3 | ||||
-rw-r--r-- | src/Settings.cpp | 58 | ||||
-rw-r--r-- | src/Settings.hpp | 13 |
4 files changed, 91 insertions, 8 deletions
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<std::string> 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 <fstream> +#include <map> + +#include <nlohmann/json.hpp> +#include <easylogging++.h> + + +using json = nlohmann::json; + +std::map<std::string, std::string> 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<std::string>())); + } + 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 <string> + +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 |