]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
created config class
authorClyne Sullivan <clyne@bitgloo.com>
Wed, 25 Sep 2019 20:50:02 +0000 (16:50 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Wed, 25 Sep 2019 20:50:02 +0000 (16:50 -0400)
src/config.cpp [new file with mode: 0644]
src/config.hpp [new file with mode: 0644]
src/main.cpp

diff --git a/src/config.cpp b/src/config.cpp
new file mode 100644 (file)
index 0000000..4f5847c
--- /dev/null
@@ -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 (file)
index 0000000..a57a5db
--- /dev/null
@@ -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_
+
index 5f39d952cfb9538cb40bd7977676e2eb03ec6f78..814a2d49f651435719015af7d056e4809ae5aace 100644 (file)
@@ -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;
 }