aboutsummaryrefslogtreecommitdiff
path: root/src/engine/user/input.cpp
blob: ac6d660428e97673c238b42fdf2ac29debe0292b (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
#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;
}

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

	_currently_pressed = 0;

	_currently_pressed_mutex.unlock();
}