aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2019-09-25 16:50:02 -0400
committerClyne Sullivan <clyne@bitgloo.com>2019-09-25 16:50:02 -0400
commitff93f760dd50f078779bdabcd41626a45bea6548 (patch)
treef0d3e452ae0d09968113c2a5b6abd8b966b03489
parent5de39474dd9c17cc7f09015f291769fbe3fd5931 (diff)
created config class
-rw-r--r--src/config.cpp60
-rw-r--r--src/config.hpp64
-rw-r--r--src/main.cpp13
3 files changed, 137 insertions, 0 deletions
diff --git a/src/config.cpp b/src/config.cpp
new file mode 100644
index 0000000..4f5847c
--- /dev/null
+++ b/src/config.cpp
@@ -0,0 +1,60 @@
+/**
+ * @file config.cpp
+ * Manages name-value pairs for configuration options.
+ *
+ * Copyright (C) 2019 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.hpp"
+
+#include <cereal/cereal.hpp>
+#include <cereal/archives/json.hpp>
+#include <fstream>
+
+void Config::save(void) const
+{
+ if (std::ofstream file (fileName); file.good()) {
+ cereal::JSONOutputArchive archive (file);
+
+ for (auto& nvp : values) {
+ if (std::holds_alternative<int>(nvp.second)) {
+ archive(cereal::make_nvp(nvp.first, std::get<int>(nvp.second)));
+ } else if (std::holds_alternative<double>(nvp.second)) {
+ archive(cereal::make_nvp(nvp.first, std::get<double>(nvp.second)));
+ } else {
+ archive(cereal::make_nvp(nvp.first, std::get<std::string>(nvp.second)));
+ }
+ }
+ }
+}
+
+void Config::load(void)
+{
+ if (std::ifstream file (fileName); file.good()) {
+ cereal::JSONInputArchive archive (file);
+
+ for (auto& nvp : values) {
+ if (std::holds_alternative<int>(nvp.second)) {
+ archive(cereal::make_nvp(nvp.first, std::get<int>(nvp.second)));
+ } else if (std::holds_alternative<double>(nvp.second)) {
+ archive(cereal::make_nvp(nvp.first, std::get<double>(nvp.second)));
+ } else {
+ archive(cereal::make_nvp(nvp.first, std::get<std::string>(nvp.second)));
+ }
+ }
+ }
+}
+
diff --git a/src/config.hpp b/src/config.hpp
new file mode 100644
index 0000000..a57a5db
--- /dev/null
+++ b/src/config.hpp
@@ -0,0 +1,64 @@
+/**
+ * @file config.hpp
+ * Manages name-value pairs for configuration options.
+ *
+ * Copyright (C) 2019 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef CONFIG_HPP_
+#define CONFIG_HPP_
+
+#include <map>
+#include <optional>
+#include <string>
+#include <variant>
+
+class Config
+{
+private:
+ static constexpr const char *fileName = "settings.json";
+ std::map<std::string, std::variant<int, double, std::string>> values;
+
+public:
+ template<typename T>
+ std::optional<T> get(const std::string& name)
+ {
+ if (values.count(name) != 0) {
+ if (auto value = std::get_if<T>(&values[name]); value != nullptr)
+ return *value;
+ }
+
+ return {};
+ }
+
+ template<typename T>
+ void set(const std::string& name, T val)
+ {
+ values[name] = val;
+ }
+
+ template<typename T>
+ void add(const std::string& name)
+ {
+ values[name] = T();
+ }
+
+ void save(void) const;
+ void load(void);
+};
+
+#endif // CONFIG_HPP_
+
diff --git a/src/main.cpp b/src/main.cpp
index 5f39d95..814a2d4 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -21,6 +21,7 @@
#define GLEW_STATIC
#define SOL_ALL_SAFETIES_ON = 1
+#include "config.hpp"
#include "engine.hpp"
#include <SDL2/SDL.h>
@@ -42,9 +43,21 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[])
Engine engine;
engine.init();
+ //Config config;
+ //config.add<int>("screenWidth");
+ //config.add<int>("screenHeight");
+ //config.add<std::string>("title");
+ //config.load();
+ //std::cout << *config.get<int>("screenWidth") << std::endl;
+
// Go go go!
engine.run();
+ //config.set("screenWidth", 10000);
+ //config.set("screenHeight", 481);
+ //config.set("title", "gamEdevvv2");
+ //config.save();
+
return 0;
}