aboutsummaryrefslogtreecommitdiff
path: root/src/engine/user/input.cpp
blob: cbba143b5d503bd6b6f6d7c61cad5e67871c4435 (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
#include "input.hpp"

#include <iostream>
#include <unistd.h>

#include <iostream>

void UserInputObserver::listen() noexcept
{
	while (true)
	{
		char character = 0;

		if (std::cin.read(&character, 1).fail())
		{
			continue;
		}

		_currently_pressed_mutex.lock();

		if (character != _currently_pressed)
		{
			_currently_pressed = character;
		}

		_currently_pressed_mutex.unlock();
	}
}

bool UserInputObserver::is_key_pressed(Key key) noexcept
{
	_currently_pressed_mutex.lock();

	const auto is_key_pressed = key == _currently_pressed;

	_currently_pressed_mutex.unlock();

	return is_key_pressed;
}

auto UserInputObserver::get_currently_pressed_key() const noexcept -> Key
{
	return _currently_pressed;
}

void UserInputObserver::clear_currently_pressed() noexcept
{
	_currently_pressed_mutex.lock();

	_currently_pressed = 0;

	_currently_pressed_mutex.unlock();
}