From c3cc85d1dae46f97f5ea74fd1ed6f0e2623f5bb7 Mon Sep 17 00:00:00 2001 From: TheUltimateOptimist <digitalConfidence@web.de> Date: Thu, 6 Feb 2025 18:06:18 +0100 Subject: [PATCH] implemented loading level from provided commandline arg --- src/game/Level.cpp | 2 +- src/game/Level.hpp | 2 +- src/game/main.cpp | 9 +++++++-- src/game/ui/Menu.cpp | 9 +++++---- src/game/ui/Menu.hpp | 4 +++- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 43362e1..27cb94c 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -57,7 +57,7 @@ Level::Level( m_selectedUnit = -1; }; -std::shared_ptr<Level> Level::loadLevel(std::string path, Engine& engine) +std::shared_ptr<Level> Level::loadLevel(const std::string& path, Engine& engine) { HighFive::File file(path, HighFive::File::ReadOnly); diff --git a/src/game/Level.hpp b/src/game/Level.hpp index 6e2aeea..9293018 100644 --- a/src/game/Level.hpp +++ b/src/game/Level.hpp @@ -80,7 +80,7 @@ class Level : public Scene std::vector<Building> buildings, std::vector<Unit> units, std::vector<Effect> effects, std::queue<Player> turnQ); - static std::shared_ptr<Level> loadLevel(std::string path, Engine& engine); + static std::shared_ptr<Level> loadLevel(const std::string& path, Engine& engine); void render(Engine& engine); diff --git a/src/game/main.cpp b/src/game/main.cpp index 955111a..be60fe7 100644 --- a/src/game/main.cpp +++ b/src/game/main.cpp @@ -11,8 +11,13 @@ using namespace advanced_wars; -int main() +int main(int argc, char* argv[]) { + if (argc <= 1) + { + std::cerr << "Please provide the path to the level that you want to play as a command line argument." << std::endl; + return 1; + } if (SDL_Init(SDL_INIT_VIDEO) < 0) { @@ -34,7 +39,7 @@ int main() engine.setSpritesheet(spritesheet); - std::shared_ptr<Menu> menu = std::make_shared<Menu>(0); + std::shared_ptr<Menu> menu = std::make_shared<Menu>(0, argv[1]); std::shared_ptr<ContextMenu> context_menu = std::make_shared<ContextMenu>(); context_menu->setOptions({"Move", "Info", "Wait"}); diff --git a/src/game/ui/Menu.cpp b/src/game/ui/Menu.cpp index 770a153..fdec910 100644 --- a/src/game/ui/Menu.cpp +++ b/src/game/ui/Menu.cpp @@ -1,7 +1,7 @@ #include "Menu.hpp" +#include "../Level.hpp" #include "../Building.hpp" #include "../Config.hpp" -#include "../Level.hpp" #include "../Spritesheet.hpp" #include "../Tile.hpp" #include "../Unit.hpp" @@ -14,8 +14,9 @@ namespace advanced_wars { -Menu::Menu(int selectedOption) - : m_selectedOption(selectedOption), m_options({"Start Game", "Options", "Exit"}), +Menu::Menu(int selectedOption, const std::string& level_filepath) + : m_selectedOption(selectedOption), m_level_filepath(level_filepath), + m_options({"Start Game", "Options", "Exit"}), m_backgroundTexture(nullptr) { } @@ -202,7 +203,7 @@ void Menu::handleEvent(Engine& engine, SDL_Event& event) // std::make_shared<Level>("Osnabrück", 20, 20, tiles, buildings, units, // effects, std::queue<Player>{}); - engine.pushScene(Level::loadLevel("../res/level.h5", engine)); + engine.pushScene(Level::loadLevel(m_level_filepath, engine)); } else if (m_options[m_selectedOption] == "Options") { diff --git a/src/game/ui/Menu.hpp b/src/game/ui/Menu.hpp index 507f3c2..6f48fef 100644 --- a/src/game/ui/Menu.hpp +++ b/src/game/ui/Menu.hpp @@ -23,6 +23,7 @@ class Menu : public Scene { private: size_t m_selectedOption; ///< Index of the currently selected menu option. + std::string m_level_filepath; ///< The path from which the level will be loaded. std::array<std::string, 3> m_options; ///< The available menu options. SDL_Texture* m_backgroundTexture; ///< Pointer to the background texture (if any). @@ -34,8 +35,9 @@ class Menu : public Scene * selected option based on the given index. * * @param selectedOption The index of the initially selected menu option. + * @param level_filepath The path from which the level will be loaded. */ - Menu(int selectedOption); + Menu(int selectedOption, const std::string& level_filepath); /** * @brief Renders the menu on the screen. -- GitLab