-
Notifications
You must be signed in to change notification settings - Fork 1
/
Input.cpp
101 lines (93 loc) · 3.19 KB
/
Input.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Input.h"
#include <SFML/Window/Keyboard.hpp>
#include <vector>
const std::vector<sf::Keyboard::Key> ALL_KEYS = {sf::Keyboard::Left,
sf::Keyboard::Right,
sf::Keyboard::Up,
sf::Keyboard::Down,
sf::Keyboard::A,
sf::Keyboard::S};
void KeyboardInput::setKey(sf::Keyboard::Key keycode, bool pressed) {
switch (keycode)
{
case sf::Keyboard::A:
A.keyIsDown = pressed;
break;
case sf::Keyboard::S:
B.keyIsDown = pressed;
break;
case sf::Keyboard::Right:
right.keyIsDown = pressed;
break;
case sf::Keyboard::Left:
left.keyIsDown = pressed;
break;
default:
break;
}
}
void KeyboardInput::updateWasDown(const KeyboardInput &previousInput) {
A.keyWasDown = previousInput.A.keyIsDown;
B.keyWasDown = previousInput.B.keyIsDown;
right.keyWasDown = previousInput.right.keyIsDown;
left.keyWasDown = previousInput.left.keyIsDown;
}
void updateInputState(KeyboardInputState& currentState,
const KeyboardInputState& previousState,
bool isPressed)
{
currentState.keyWasDown = previousState.keyIsDown;
currentState.keyIsDown = isPressed;
}
void updateKeyboardInputs(KeyboardInput& currentInput,
KeyboardInput& previousInput,
sf::Keyboard::Key knownKey,
bool isPressed)
{
switch (knownKey)
{
case sf::Keyboard::A:
updateInputState(currentInput.A, previousInput.A, isPressed);
break;
case sf::Keyboard::S:
updateInputState(currentInput.B, previousInput.B, isPressed);
break;
case sf::Keyboard::Right:
updateInputState(currentInput.right, previousInput.right, isPressed);
break;
case sf::Keyboard::Left:
updateInputState(currentInput.left, previousInput.left, isPressed);
break;
default:
break;
}
previousInput = currentInput;
}
std::vector<KeyboardInput> generateInputs(
const std::vector<std::vector<sf::Keyboard::Key>>& keyInputs)
{
std::vector<KeyboardInput> result;
KeyboardInput prevInput = {};
auto nullTerminatedKeyInputs(keyInputs);
nullTerminatedKeyInputs.emplace_back();
for (const auto& frameInputs : nullTerminatedKeyInputs)
{
KeyboardInput keyboardInput = {};
for (const auto& key : ALL_KEYS)
{
updateKeyboardInputs(keyboardInput,
prevInput,
key,
std::find(frameInputs.begin(),
frameInputs.end(),
key) != frameInputs.end());
}
prevInput = keyboardInput;
result.push_back(keyboardInput);
}
return result;
}
KeyboardInput nextInput(std::vector<KeyboardInput> keyboardInputs, size_t idx)
{
return idx < keyboardInputs.size() ? keyboardInputs[idx] : KeyboardInput{};
}