-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
51 lines (40 loc) · 1.63 KB
/
Input.cpp
File metadata and controls
51 lines (40 loc) · 1.63 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
#include "Input.hpp"
void Input::Update()
{
lastFrameKeys.SetMask(thisFrameKeys); // 1
thisFrameKeys.SetBit((int)Key::Left,
(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::A))); // 2
thisFrameKeys.SetBit((int)Key::Right,
(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::D)));
thisFrameKeys.SetBit((int)Key::Up,
(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::W)));
thisFrameKeys.SetBit((int)Key::Down,
(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::S)));
thisFrameKeys.SetBit((int)Key::Esc),
sf::Keyboard::isKeyPressed(sf::Keyboard::Escape);
}
// Return true if the specified key is currently being pressed.
bool Input::IsKeyPressed(Key keycode)
{
return thisFrameKeys.GetBit((int)keycode);
}
// Returns true if the key was just pressed
// (i.e. registered as pressed this frame but not the previous).
bool Input::IsKeyDown(Key keycode)
{
bool lastFrame = lastFrameKeys.GetBit((int)keycode);
bool thisFrame = thisFrameKeys.GetBit((int)keycode);
return thisFrame && !lastFrame;
}
// Returns true if the key was just released (i.e. registered as
// pressed last frame but not the current frame).
bool Input::IsKeyUp(Key keycode)
{
bool lastFrame = lastFrameKeys.GetBit((int)keycode);
bool thisFrame = thisFrameKeys.GetBit((int)keycode);
return !thisFrame && lastFrame;
}