From 5e48f4e34ff4fd963880c08fda5f4540d96b4fdb Mon Sep 17 00:00:00 2001 From: freezarite Date: Fri, 13 Dec 2024 14:36:43 +0100 Subject: [PATCH] Config now gets read by the GameInternal.cpp Made the process of adding a custom Config file work via a Virtual function within Game.h --- include/ConfigLoader.h | 4 +++- include/Game.h | 1 + src/ConfigLoader.cpp | 11 +++++++++-- src/GameInternal.cpp | 11 +++++++++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/ConfigLoader.h b/include/ConfigLoader.h index d0115a4..8ba9c55 100644 --- a/include/ConfigLoader.h +++ b/include/ConfigLoader.h @@ -12,7 +12,9 @@ public: void init(); - void setCustomConfig(std::string path); + void setCustomConfig(const std::optional& path); + + json getFinalConfig(); private: json baseConfig; diff --git a/include/Game.h b/include/Game.h index 3eb990d..75f6713 100644 --- a/include/Game.h +++ b/include/Game.h @@ -9,6 +9,7 @@ public: virtual void init() = 0; virtual void update(uint_fast16_t diffTime) = 0; + virtual std::optional getConfigFilePath() {return std::nullopt;} GameInternal* gameInternal; //!< \deprecated }; diff --git a/src/ConfigLoader.cpp b/src/ConfigLoader.cpp index 4c8cb04..4620c56 100644 --- a/src/ConfigLoader.cpp +++ b/src/ConfigLoader.cpp @@ -6,6 +6,8 @@ ConfigLoader::ConfigLoader() { baseConfig = loadConfigFromJSON("config.json"); } +ConfigLoader::~ConfigLoader() {} + void ConfigLoader::init() { if (!customConfig.has_value()) { finalConfig = baseConfig; @@ -27,8 +29,8 @@ json ConfigLoader::loadConfigFromJSON(const std::string& path) { } -void ConfigLoader::setCustomConfig(std::string path) { - customConfig.emplace(loadConfigFromJSON(path)); +void ConfigLoader::setCustomConfig(const std::optional& path) { + customConfig = path; } json ConfigLoader::mergeConfigs(json baseConfig, json customConfig) { @@ -38,4 +40,9 @@ json ConfigLoader::mergeConfigs(json baseConfig, json customConfig) { return baseConfig; } +json ConfigLoader::getFinalConfig() { + return finalConfig; +} + + diff --git a/src/GameInternal.cpp b/src/GameInternal.cpp index 1e4eac3..c1df0be 100644 --- a/src/GameInternal.cpp +++ b/src/GameInternal.cpp @@ -15,6 +15,8 @@ #include +#include "ConfigLoader.h" + GameInternal::GameInternal() : manager(this), renderManager(), @@ -29,13 +31,19 @@ GameInternal::~GameInternal() = default; SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) { + this->gameInstance = GameFactory::instance().create(this); + ConfigLoader().setCustomConfig(this->gameInstance->getConfigFilePath()); + ConfigLoader().init(); + + json config = ConfigLoader().getFinalConfig(); + GameInternal::assets = new AssetManager(&manager); GameInternal::textureManager = new TextureManager(&manager); GameInternal::soundManager = new SoundManager(); GameInternal::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer? int flags = 0; - if (fullscreen) + if (config.at("fullscreen")) { flags = SDL_WINDOW_FULLSCREEN; } @@ -95,7 +103,6 @@ SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int widt // loading music // assets->addMusic("background_music", "assets/sound/background_music.mp3"); - this->gameInstance = GameFactory::instance().create(this); this->gameInstance->init(); return SDL_APP_CONTINUE;