/
input.h
110 lines (99 loc) · 3.06 KB
/
input.h
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
102
103
104
105
106
107
108
109
110
#ifndef INPUT_H__
#define INPUT_H__
#include <map>
namespace Ocho {
//////////////////////////////////////////////////////////////////////////////
// Class: Input
//
// Emulates Chip8 keyboard input
class Input {
public:
Input()
: lastKey_(NUM_KEYS)
{
memset(keys_, false, sizeof(bool) * NUM_KEYS);
}
////////////////////////////////////////////////////////////////////////////
// Function: mapKey
//
// Maps the given keyboard key to the given Chip8 key
void mapKey(const Byte& chipKey, const int& keyboardKey) {
assert(chipKey < NUM_KEYS);
keyMap_[chipKey] = keyboardKey;
}
////////////////////////////////////////////////////////////////////////////
// Function: setKey
//
// Activates a key press for the given key
void setKey(const int& key) {
if (_isMapped(key)) {
lastKey_ = _chipKey(key);
keys_[lastKey_] = true;
}
}
////////////////////////////////////////////////////////////////////////////
// Function: clearKey
//
// Deactivates a key press for the given key
void clearKey(const int& key) {
if (_isMapped(key)) {
keys_[_chipKey(key)] = false;
}
}
////////////////////////////////////////////////////////////////////////////
// Function: isPressed
//
// Indicates whether or not the given Chip8 key is pressed
bool isPressed(const Byte& chipKey) {
assert(chipKey < NUM_KEYS);
return keys_[chipKey];
}
////////////////////////////////////////////////////////////////////////////
// Function: getLastKey
//
// Gets the key with highest priority in the queue
Byte getLastKey() {
Byte tmp = lastKey_;
lastKey_ = NUM_KEYS;
return tmp;
}
////////////////////////////////////////////////////////////////////////////
// Function: hasKey
//
// Returns true if there is pending keyboard input, false otherwise
bool hasKey() const {
return (lastKey_ != NUM_KEYS);
}
private:
////////////////////////////////////////////////////////////////////////////
// Function: _isMapped
//
// Returns true if the given key is mapped, false otherwise
bool _isMapped(const int& key) {
for (std::map<Byte, int>::iterator it = keyMap_.begin();
it != keyMap_.end(); it++) {
if (it->second == key) {
return true;
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////
// Function: _chipKey
//
// Returns the Chip8 key corresponding to the given keyboard key
Byte _chipKey(const int& key) {
for (std::map<Byte, int>::iterator it = keyMap_.begin();
it != keyMap_.end(); it++) {
if (it->second == key) {
return it->first;
}
}
return 0x10;
}
std::map<Byte, int> keyMap_; // Maps Chip8 keys to keyboard keys
bool keys_[NUM_KEYS]; // Boolean map indicating which keys are pressed
Byte lastKey_; // The most recent key pressed;
};
}
#endif // INPUT_H__