From d27f1a89266e141a944f88e9080bdeca95c49da3 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 3 Mar 2022 20:40:29 +0100 Subject: refactor: add game initializer --- src/game/game.cpp | 82 ++++++++++++++++++++++++------------------------------- src/game/game.hpp | 11 ++------ 2 files changed, 38 insertions(+), 55 deletions(-) (limited to 'src/game') diff --git a/src/game/game.cpp b/src/game/game.cpp index ee3a2b8..559f9a0 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -2,58 +2,46 @@ #include -Game::Game(std::shared_ptr scene, std::shared_ptr input_handler, - std::shared_ptr cursor_controller) - : _scene(std::move(scene)), - _input_handler(std::move(input_handler)), - _cursor_controller(std::move(cursor_controller)) +Game::Game(std::shared_ptr cursor_controller) + : _cursor_controller(std::move(cursor_controller)) { } -void Game::run() +void Game::run(IScene &scene, IInputHandler &input_handler) { - _scene->enter(); - - _input_handler->enter_raw_mode(); - - auto scene = _scene; - auto input_handler = _input_handler; - - _input_handler->attach('q', - [&input_handler, &scene]() - { - input_handler->leave_raw_mode(); - scene->leave(); - exit(EXIT_SUCCESS); - }); + input_handler.attach('q', + [&input_handler, &scene]() + { + input_handler.leave_raw_mode(); + scene.leave(); + exit(EXIT_SUCCESS); + }); auto cursor_controller = _cursor_controller; - _input_handler->attach('k', - [&cursor_controller]() - { - cursor_controller->move(1U); - }); - - _input_handler->attach('j', - [&cursor_controller]() - { - cursor_controller->move(1U); - }); - - _input_handler->attach('h', - [&cursor_controller]() - { - cursor_controller->move(1U); - }); - - _input_handler->attach('l', - [&cursor_controller]() - { - cursor_controller->move(1U); - }); - - _input_handler->listen(); - - _scene->leave(); + input_handler.attach('k', + [&cursor_controller]() + { + cursor_controller->move(1U); + }); + + input_handler.attach('j', + [&cursor_controller]() + { + cursor_controller->move(1U); + }); + + input_handler.attach('h', + [&cursor_controller]() + { + cursor_controller->move(1U); + }); + + input_handler.attach('l', + [&cursor_controller]() + { + cursor_controller->move(1U); + }); + + input_handler.listen(); } diff --git a/src/game/game.hpp b/src/game/game.hpp index dd0d3c8..c52399d 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -9,18 +9,13 @@ #include -class Game : public IGame, - public AutoWirable +class Game : public IGame, public AutoWirable { public: - explicit Game(std::shared_ptr scene, - std::shared_ptr input_handler, - std::shared_ptr cursor_controller); + explicit Game(std::shared_ptr cursor_controller); - void run() override; + void run(IScene &scene, IInputHandler &input_handler) override; private: - std::shared_ptr _scene; - std::shared_ptr _input_handler; std::shared_ptr _cursor_controller; }; -- cgit v1.2.3-18-g5258