From a9852bd2c5a601f9f9c58b1dff60e9130587657b Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 20 Mar 2022 14:09:48 +0100 Subject: refactor: move on start & on exit details to the game class --- src/bootstrap.cpp | 7 +++--- src/engine/engine.cpp | 41 +++++++------------------------- src/game/game.cpp | 63 +++++++++++++++++++++++++++++++++++++++---------- src/game/game.hpp | 20 +++++++++------- src/interfaces/game.hpp | 12 ++++++---- 5 files changed, 81 insertions(+), 62 deletions(-) (limited to 'src') diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 0ef0584..713991d 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -36,16 +36,17 @@ Container bootstrap() noexcept auto container = Container(); container.bind().to(); - container.bind().to(); container.bind().to(); container.bind().to(); container.bind().to(); container.bind().to(); container.bind().to_factory(normalize_lambda( - [&container]() + [](const std::shared_ptr &window, const std::shared_ptr &scene, + const std::shared_ptr &cursor_controller) { - return container.get(); + return std::dynamic_pointer_cast( + std::make_shared(window, scene, cursor_controller)); })); container.bind().to_factory( diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index b2add9c..db3ffb7 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1,10 +1,7 @@ #include "engine.hpp" -#include "strings.hpp" #include "util/function.hpp" -#include -#include #include CLIGameEngine::CLIGameEngine(IGameFactory game_factory, ISceneFactory scene_factory, @@ -13,9 +10,9 @@ CLIGameEngine::CLIGameEngine(IGameFactory game_factory, ISceneFactory scene_fact std::shared_ptr window) noexcept : _game_factory(game_factory), _scene_factory(scene_factory), - _input_handler(std::move(input_handler)), - _cursor_controller(std::move(cursor_controller)), - _window(std::move(window)) + _input_handler(std::move(std::move(input_handler))), + _cursor_controller(std::move(std::move(cursor_controller))), + _window(std::move(std::move(window))) { } @@ -26,44 +23,22 @@ void CLIGameEngine::start() noexcept scene->enter(); _input_handler->enter_raw_mode(); - const auto window_size = _window->size(); + auto game = _game_factory(_window, scene, _cursor_controller); - const auto center_position = - Vector2({.x = static_cast(window_size.get_width()) / 2, - .y = static_cast(window_size.get_height()) / 2}); - - _cursor_controller->move_to(center_position); - - scene->write_status(fmt::format(STATUS_BAR_COORDINATES, - fmt::arg("x", center_position.get_x()), - fmt::arg("y", center_position.get_y()))); + game->on_start(); std::atexit(normalize_lambda( - [this, scene]() + [this, scene, game]() { scene->leave(); _input_handler->leave_raw_mode(); - for (auto row : *scene->get_matrix()) - { - for (auto &col : row) - { - fmt::print("{}", col); - } - - fmt::print("\n"); - } - - std::cout.flush(); + game->on_exit(); })); - auto game = _game_factory(); - - _configure_input(game->get_input_config(_window, scene, _cursor_controller)); + _configure_input(game->get_input_config()); _input_handler->listen(); - - game->run(); } void CLIGameEngine::_configure_input( diff --git a/src/game/game.cpp b/src/game/game.cpp index 45e6a79..5df3935 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -3,21 +3,58 @@ #include "commands/insert_cell.hpp" #include "commands/move_cursor.hpp" #include "commands/quit.hpp" +#include "strings.hpp" -void Game::run() noexcept {} +#include +#include -std::unordered_map> Game::get_input_config( - const std::shared_ptr &window, const std::shared_ptr &scene, - const std::shared_ptr &cursor_controller) const noexcept +Game::Game(const std::shared_ptr &window, const std::shared_ptr &scene, + const std::shared_ptr &cursor_controller) noexcept + : _window(window), _scene(scene), _cursor_controller(cursor_controller) +{ +} + +void Game::on_start() noexcept +{ + const auto window_size = _window->size(); + + const auto center_position = + Vector2({.x = static_cast(window_size.get_width()) / 2, + .y = static_cast(window_size.get_height()) / 2}); + + _cursor_controller->move_to(center_position); + + _scene->write_status(fmt::format(STATUS_BAR_COORDINATES, + fmt::arg("x", center_position.get_x()), + fmt::arg("y", center_position.get_y()))); +} + +void Game::on_exit() const noexcept +{ + for (auto row : *_scene->get_matrix()) + { + for (auto &col : row) + { + fmt::print("{}", col); + } + + fmt::print("\n"); + } + + std::cout.flush(); +} + +std::unordered_map> +Game::get_input_config() const noexcept { return {{'q', std::make_shared()}, - {'i', std::make_shared(cursor_controller, scene)}, - {'k', std::make_shared(Vector2::up(), cursor_controller, - scene, window)}, - {'j', std::make_shared(Vector2::down(), cursor_controller, - scene, window)}, - {'h', std::make_shared(Vector2::left(), cursor_controller, - scene, window)}, - {'l', std::make_shared(Vector2::right(), cursor_controller, - scene, window)}}; + {'i', std::make_shared(_cursor_controller, _scene)}, + {'k', std::make_shared(Vector2::up(), _cursor_controller, + _scene, _window)}, + {'j', std::make_shared(Vector2::down(), _cursor_controller, + _scene, _window)}, + {'h', std::make_shared(Vector2::left(), _cursor_controller, + _scene, _window)}, + {'l', std::make_shared( + Vector2::right(), _cursor_controller, _scene, _window)}}; } diff --git a/src/game/game.hpp b/src/game/game.hpp index 9ec5b41..2493a42 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -1,6 +1,5 @@ #pragma once -#include "DI/auto_wirable.hpp" #include "interfaces/cursor.hpp" #include "interfaces/game.hpp" #include "interfaces/scene.hpp" @@ -8,16 +7,21 @@ #include -class Game : public IGame, public AutoWirable +class Game : public IGame { public: - Game() noexcept = default; + Game(const std::shared_ptr &window, const std::shared_ptr &scene, + const std::shared_ptr &cursor_controller) noexcept; - void run() noexcept override; + void on_start() noexcept override; + + void on_exit() const noexcept override; [[nodiscard]] std::unordered_map> - get_input_config(const std::shared_ptr &window, - const std::shared_ptr &scene, - const std::shared_ptr &cursor_controller) - const noexcept override; + get_input_config() const noexcept override; + +private: + const std::shared_ptr &_window; + const std::shared_ptr &_scene; + const std::shared_ptr &_cursor_controller; }; diff --git a/src/interfaces/game.hpp b/src/interfaces/game.hpp index cfff8c5..c99c01f 100644 --- a/src/interfaces/game.hpp +++ b/src/interfaces/game.hpp @@ -13,12 +13,14 @@ class IGame public: virtual ~IGame() = default; - virtual void run() = 0; + virtual void on_start() = 0; + + virtual void on_exit() const noexcept = 0; [[nodiscard]] virtual std::unordered_map> - get_input_config( - const std::shared_ptr &window, const std::shared_ptr &scene, - const std::shared_ptr &cursor_controller) const noexcept = 0; + get_input_config() const noexcept = 0; }; -using IGameFactory = std::shared_ptr (*)(); +using IGameFactory = std::shared_ptr (*)( + const std::shared_ptr &window, const std::shared_ptr &scene, + const std::shared_ptr &cursor_controller); -- cgit v1.2.3-18-g5258