git repos / saland

commit cd4434ec

sago007 · 2021-01-02 22:23
cd4434ecfd832e5fdeb14d931815fae04eed35cc patch · browse files
parent 52839cd7ef137db284ff3d44f42cd07a36aa8464

Now possible to change config variables with the console

Changed files

M src/common.cpp before
M src/common.h before
M src/saland.cpp before
M src/saland/Game.cpp before
M src/saland/GameConsoleCommand.cpp before
diff --git a/src/common.cpp b/src/common.cpp index 49a98d2..9358e5a 100644 --- a/src/common.cpp +++ b/src/common.cpp
@@ -247,3 +247,7 @@ double Config::getValue(const std::string& varName) {
return 0.0;
}
}
+
+const std::map<std::string, std::string>& Config::getConfigPairs() const {
+ return configMap;
+}
\ No newline at end of file
diff --git a/src/common.h b/src/common.h index dc201f1..e68405f 100644 --- a/src/common.h +++ b/src/common.h
@@ -199,6 +199,13 @@ public:
* @return
*/
long isShuttingDown() const;
+
+ /**
+ * Return the list of config. The reference is until next call to the function;
+ *
+ * @return const std::map<std::string, std::string>&
+ */
+ const std::map<std::string, std::string>& getConfigPairs() const;
};
#endif /* _COMMON_H */
diff --git a/src/saland.cpp b/src/saland.cpp index 4d21d7a..b77cfd3 100644 --- a/src/saland.cpp +++ b/src/saland.cpp
@@ -295,6 +295,8 @@ int main(int argc, char* argv[]) {
if (vm.count("no-fullscreen")) {
Config::getInstance()->setInt("fullscreen", 0);
}
+ Config::getInstance()->setDefault("fullscreen", "0");
+ Config::getInstance()->setDefault("draw_collision", "0");
runGame();
Config::getInstance()->save();
return 0;
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index e8c1fc7..a8c7957 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -305,6 +305,7 @@ static std::string GetLayerInfoForTile(const World& w, int x, int y) {
}
void Game::Draw(SDL_Renderer* target) {
+ data->drawCollision = Config::getInstance()->getInt("draw_collision");
double screen_width = globalData.xsize;
double screen_height = globalData.ysize;
int screen_boarder = 64;
diff --git a/src/saland/GameConsoleCommand.cpp b/src/saland/GameConsoleCommand.cpp index 8f12822..8360b5b 100644 --- a/src/saland/GameConsoleCommand.cpp +++ b/src/saland/GameConsoleCommand.cpp
@@ -23,6 +23,7 @@ https://github.com/sago007/saland
#include "../global.hpp"
#include "console/Console.hpp"
+#include "../common.h"
struct ConsoleCommandGiveItem : public ConsoleCommand {
virtual std::string getCommand() const override {
@@ -79,11 +80,56 @@ struct ConsoleCommandQuit : public ConsoleCommand {
}
};
+struct ConsoleCommandConfig : public ConsoleCommand {
+ virtual std::string getCommand() const override {
+ return "config";
+ }
+
+ virtual std::string run(const std::vector<std::string>& args) override {
+ if (args.size() < 2) {
+ throw std::runtime_error("See \"help config\" for calling instructions.");
+ }
+ if (args[1] == "list") {
+ std::string theList;
+ const std::map<std::string, std::string>& configs = Config::getInstance()->getConfigPairs();
+ for (const auto& item : configs) {
+ theList += item.first + "\n";
+ }
+ return theList;
+ }
+ else if (args[1] == "get") {
+ if (args.size() < 3) {
+ throw std::runtime_error("Must be called like \"config get VARIABLE_NAME\". Use \"config list\" to get the names.");
+ }
+ const std::string& key = args[2];
+ if (!Config::getInstance()->exists(key)) {
+ throw std::runtime_error(std::string("key \"")+key+"\" not found");
+ }
+ return Config::getInstance()->getString(key);
+ }
+ else if (args[1] == "set") {
+ if (args.size() < 4) {
+ throw std::runtime_error("Must be called like \"config set VARIABLE_NAME NEW_VALUE\". Use \"config list\" to set the value.");
+ }
+ const std::string& key = args[2];
+ const std::string& value = args[3];
+ Config::getInstance()->setString(key, value);
+ return "Value set";
+ }
+ throw std::runtime_error("Missing command");
+ }
+
+ virtual std::string helpMessage() const override {
+ return "Use \"config list\" to list the variables already set";
+ }
+};
+
static ConsoleCommandGiveItem cc_give_item;
static ConsoleCommandQuit cc_quit;
-
+static ConsoleCommandConfig cc_config;
void GameConsoleCommandRegister() {
RegisterCommand(&cc_give_item);
RegisterCommand(&cc_quit);
+ RegisterCommand(&cc_config);
}
\ No newline at end of file