From 9dd5936333082305b0b9680db2eac9abd128129b Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 8 Mar 2022 16:26:17 +0100 Subject: refactor: add game factory --- src/bootstrap.cpp | 8 ++++++++ src/engine/game_initializer.cpp | 8 +++++--- src/engine/game_initializer.hpp | 10 +++++----- src/interfaces/game.hpp | 4 ++++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index ddeba4d..610945f 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -24,6 +24,8 @@ #include "randomization/generator.hpp" #include "randomization/seed_generator.hpp" +#include "util/function.hpp" + #include #include #include @@ -40,6 +42,12 @@ Container bootstrap() container.bind().to(); container.bind().to(); + container.bind().to_factory(normalize_lambda( + [&container]() + { + return container.get(); + })); + container.bind().to_factory( [](const uint32_t &seed) { diff --git a/src/engine/game_initializer.cpp b/src/engine/game_initializer.cpp index 52e109a..13d9ba0 100644 --- a/src/engine/game_initializer.cpp +++ b/src/engine/game_initializer.cpp @@ -7,10 +7,10 @@ GameInitializer::GameInitializer(std::shared_ptr scene, std::shared_ptr input_handler, - std::shared_ptr game) + IGameFactory game_factory) : _scene(std::move(scene)), _input_handler(std::move(input_handler)), - _game(std::move(game)) + _game_factory(game_factory) { } @@ -26,5 +26,7 @@ void GameInitializer::initialize() _input_handler->leave_raw_mode(); })); - _game->run(*_scene, *_input_handler); + auto game = _game_factory(); + + game->run(*_scene, *_input_handler); } diff --git a/src/engine/game_initializer.hpp b/src/engine/game_initializer.hpp index 12b42de..2f63e0f 100644 --- a/src/engine/game_initializer.hpp +++ b/src/engine/game_initializer.hpp @@ -8,19 +8,19 @@ #include -class GameInitializer - : public IGameInitializer, - public AutoWirable +class GameInitializer : public IGameInitializer, + public AutoWirable { public: GameInitializer(std::shared_ptr scene, std::shared_ptr input_handler, - std::shared_ptr game); + IGameFactory game_factory); void initialize() override; private: std::shared_ptr _scene; std::shared_ptr _input_handler; - std::shared_ptr _game; + IGameFactory _game_factory; }; diff --git a/src/interfaces/game.hpp b/src/interfaces/game.hpp index eb13dc3..85c20e5 100644 --- a/src/interfaces/game.hpp +++ b/src/interfaces/game.hpp @@ -3,6 +3,8 @@ #include "interfaces/input.hpp" #include "interfaces/scene.hpp" +#include + class IGame { public: @@ -10,3 +12,5 @@ public: virtual void run(IScene &scene, IInputHandler &input_handler) = 0; }; + +using IGameFactory = std::shared_ptr (*)(); -- cgit v1.2.3-18-g5258