diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b44666..b0764f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,13 +24,11 @@ add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL) add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL) -include_directories(extern/nlohmann_json/include) +add_subdirectory(extern/nlohmann_json EXCLUDE_FROM_ALL) file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp) -add_library(${PROJECT_NAME} ${SOURCES} - include/ConfigLoader.h - src/ConfigLoader.cpp) +add_library(${PROJECT_NAME} ${SOURCES}) target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR}) @@ -39,6 +37,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL fu SDL3_image::SDL3_image-static SDL3_mixer::SDL3_mixer-static SDL3_ttf::SDL3_ttf-static + nlohmann_json::nlohmann_json tmxlite ) diff --git a/config.json b/config.json index 0e0eb80..e4ee3bf 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,7 @@ { "fullscreen": false, "title": "VGG (Very Good Game)", - "height": 100, - "width": 100, + "screen_height": 100, + "screen_width": 100, "icon": "./engine/internalAssets/iconImage.bmp" } \ No newline at end of file diff --git a/include/ConfigLoader.h b/include/ConfigLoader.h index 8ba9c55..d92977e 100644 --- a/include/ConfigLoader.h +++ b/include/ConfigLoader.h @@ -17,8 +17,8 @@ public: json getFinalConfig(); private: - json baseConfig; - std::optional customConfig; + + std::optional customConfigPath; json finalConfig; json loadConfigFromJSON(const std::string& path); diff --git a/include/Game.h b/include/Game.h index 75f6713..84e3b8e 100644 --- a/include/Game.h +++ b/include/Game.h @@ -9,7 +9,7 @@ public: virtual void init() = 0; virtual void update(uint_fast16_t diffTime) = 0; - virtual std::optional getConfigFilePath() {return std::nullopt;} + virtual std::optional setConfigFilePath() {return std::nullopt;} GameInternal* gameInternal; //!< \deprecated }; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 553617a..75e78ec 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -77,8 +77,8 @@ Vector2D AssetManager::calculateSpawnPosition() { SDL_Rect spawnRect; spawnRect.h = spawnRect.w = 32; - spawnRect.x = rand() % (VEGO_Game().config->getFinalConfig().at("width").get() - spawnRect.w); - spawnRect.y = rand() % (VEGO_Game().config->getFinalConfig().at("height").get() - spawnRect.h); + spawnRect.x = rand() % (VEGO_Game().config->getFinalConfig().at("screen_width").get() - spawnRect.w); + spawnRect.y = rand() % (VEGO_Game().config->getFinalConfig().at("screen_height").get() - spawnRect.h); conflict = false; for (auto cc : this->man->getGame()->collisionHandler->getColliders({ Entity::GroupLabel::MAPTILES })) { diff --git a/src/CollisionHandler.cpp b/src/CollisionHandler.cpp index 0177728..d153d0c 100644 --- a/src/CollisionHandler.cpp +++ b/src/CollisionHandler.cpp @@ -67,20 +67,20 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V // all 4 directions and both sides to allow checking for fully out of bounds if (collider->x + posMod.x < 0 || - collider->x + posMod.x > VEGO_Game().config->getFinalConfig().at("width")) { + collider->x + posMod.x > VEGO_Game().config->getFinalConfig().at("screen_width")) { intersections.set((size_t) Direction::LEFT); } if (collider->x + collider->w + posMod.x < 0 || - collider->x + collider->w + posMod.x > VEGO_Game().config->getFinalConfig().at("width")) + collider->x + collider->w + posMod.x > VEGO_Game().config->getFinalConfig().at("screen_width")) intersections.set((size_t) Direction::RIGHT); if (collider->y + posMod.y < 0 || - collider->y + posMod.y > VEGO_Game().config->getFinalConfig().at("height")) + collider->y + posMod.y > VEGO_Game().config->getFinalConfig().at("screen_height")) intersections.set((size_t) Direction::UP); if (collider->y + collider->h + posMod.y < 0 || - collider->y + collider->h + posMod.y > VEGO_Game().config->getFinalConfig().at("height")) + collider->y + collider->h + posMod.y > VEGO_Game().config->getFinalConfig().at("screen_height")) intersections.set((size_t) Direction::DOWN); return intersections; diff --git a/src/ConfigLoader.cpp b/src/ConfigLoader.cpp index 65381f5..7b244e4 100644 --- a/src/ConfigLoader.cpp +++ b/src/ConfigLoader.cpp @@ -2,20 +2,21 @@ #include -ConfigLoader::ConfigLoader() { - //TODO: look into adaptive paths for better handling as this requires the implemented game - // to have ./engine in the root folder - baseConfig = loadConfigFromJSON("./engine/config.json"); -} +ConfigLoader::ConfigLoader() {} ConfigLoader::~ConfigLoader() {} void ConfigLoader::init() { - if (!customConfig.has_value()) { + //TODO: look into adaptive paths for better handling as this requires the implemented game + // to have ./engine in the root folder + const json baseConfig = loadConfigFromJSON("./engine/config.json"); + + if (!customConfigPath.has_value()) { finalConfig = baseConfig; return; } - finalConfig = mergeConfigs(baseConfig, customConfig.value()); + + finalConfig = mergeConfigs(baseConfig, loadConfigFromJSON(customConfigPath.value())); } json ConfigLoader::loadConfigFromJSON(const std::string& path) { @@ -32,9 +33,7 @@ json ConfigLoader::loadConfigFromJSON(const std::string& path) { void ConfigLoader::setCustomConfig(const std::optional& path) { - if (path.has_value()) { - customConfig = loadConfigFromJSON(path.value()); - } + customConfigPath = path; } json ConfigLoader::mergeConfigs(json baseConfig, json customConfig) { diff --git a/src/GameInternal.cpp b/src/GameInternal.cpp index 3c55148..77becb6 100644 --- a/src/GameInternal.cpp +++ b/src/GameInternal.cpp @@ -31,10 +31,10 @@ GameInternal::~GameInternal() = default; SDL_AppResult GameInternal::init() { - config = new ConfigLoader; + config = new ConfigLoader(); this->gameInstance = GameFactory::instance().create(this); - config->setCustomConfig(this->gameInstance->getConfigFilePath()); + config->setCustomConfig(this->gameInstance->setConfigFilePath()); config->init(); json finalConfig = config->getFinalConfig(); @@ -62,7 +62,9 @@ SDL_AppResult GameInternal::init() return SDL_APP_FAILURE; } - window = SDL_CreateWindow(finalConfig.at("title").get().c_str(), finalConfig.at("width"), finalConfig.at("height"), flags); + window = SDL_CreateWindow(finalConfig.at("title").get().c_str(), + finalConfig.at("screen_width"), finalConfig.at("screen_height"), flags); + if (!window) { std::cout << "ERROR: Window couldnt be created! " << SDL_GetError() << std::endl;