-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
60 lines (47 loc) · 1.51 KB
/
input.cpp
File metadata and controls
60 lines (47 loc) · 1.51 KB
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
55
56
57
58
59
#include "input.h"
Input::Input()
{
for (int key = sf::Keyboard::A; key < sf::Keyboard::KeyCount; ++key) {
this->keysPressed[static_cast<sf::Keyboard::Key>(key)] = false;
this->keysJustPressed[static_cast<sf::Keyboard::Key>(key)] = false;
}
for (int button = sf::Mouse::Left; button < sf::Mouse::Middle; ++button) {
this->buttonsPressed[static_cast<sf::Mouse::Button>(button)] = false;
this->buttonsJustPressed[static_cast<sf::Mouse::Button>(button)] = false;
}
}
void Input::update()
{
for (const auto& pair : this->keysPressed)
{
sf::Keyboard::Key key = pair.first;
bool state = pair.second;
bool pressed = sf::Keyboard::isKeyPressed(key);
this->keysJustPressed[key] = !state && pressed;
this->keysPressed[key] = pressed;
}
for (const auto& pair : this->buttonsPressed)
{
sf::Mouse::Button button = pair.first;
bool state = pair.second;
bool pressed = sf::Mouse::isButtonPressed(button);
this->buttonsJustPressed[button] = !state && pressed;
this->buttonsPressed[button] = pressed;
}
}
bool Input::isKeyPressed(sf::Keyboard::Key key)
{
return this->keysPressed[key];
}
bool Input::isKeyJustPressed(sf::Keyboard::Key key)
{
return this->keysJustPressed[key];
}
bool Input::isButtonPressed(sf::Mouse::Button button)
{
return this->buttonsPressed[button];
}
bool Input::isButtonJustPressed(sf::Mouse::Button button)
{
return this->buttonsJustPressed[button];
}