aboutsummaryrefslogtreecommitdiff
path: root/src/engine/engine.cpp
blob: ed81ed32ac70f1f10b3e17894690752f9fdcab87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "engine.hpp"

#include "util/function.hpp"

#include <utility>

CLIGameEngine::CLIGameEngine(IGameFactory game_factory, ISceneFactory scene_factory,
							 std::shared_ptr<IInputHandler> input_handler,
							 std::shared_ptr<ICursorController> cursor_controller,
							 std::shared_ptr<IWindow> 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))
{
}

void CLIGameEngine::start() noexcept
{
	auto scene = _scene_factory(_cursor_controller, _window);

	scene->enter();
	_input_handler->enter_raw_mode();

	auto game = _game_factory(_window, scene, _cursor_controller);

	game->on_start();

	std::atexit(normalize_lambda(
		[this, scene, game]()
		{
			scene->leave();
			_input_handler->leave_raw_mode();

			game->on_exit();
		}));

	_configure_input(game->get_input_config());

	_input_handler->listen();
}

void CLIGameEngine::_configure_input(
	const std::unordered_map<char, std::shared_ptr<ICommand>> &input_config) noexcept
{
	for (const auto &config_pair : input_config)
	{
		auto key = config_pair.first;
		auto command = config_pair.second;

		_input_handler->attach(key, command);
	}
}