-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cpp
55 lines (47 loc) · 1.19 KB
/
Controller.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
#include "Controller.h"
#include "GB.h"
#include "Serializer.h"
Controller::Controller(GB *gb) : gb(gb) {
Reset();
}
void Controller::Reset() {
dpad = 0;
buttons = 0;
dpadSelected = false;
buttonsSelected = false;
}
void Controller::Serialize(Serializer & s) {
s.Serialize<byte>(dpad);
s.Serialize<byte>(buttons);
s.Serialize<bool>(dpadSelected);
s.Serialize<bool>(buttonsSelected);
}
void Controller::Deserialize(Serializer & s) {
dpad = s.Deserialize<byte>();
buttons = s.Deserialize<byte>();
dpadSelected = s.Deserialize<bool>();
buttonsSelected = s.Deserialize<bool>();
}
bool Controller::OnNewInput(const byte & newDpad, const byte & newButtons) {
// Check if input has changed
bool inputChanged = (dpadSelected && (dpad ^ newDpad)) || (buttonsSelected && (buttons ^ newButtons));
dpad = newDpad;
buttons = newButtons;
return inputChanged;
}
void Controller::SelectInput(const byte & val) {
dpadSelected = !!((~val) & 0x10);
buttonsSelected = !!((~val) & 0x20);
}
byte Controller::GetInput() {
byte input = 0;
if (dpadSelected) {
input |= 1 << 4;
input |= dpad;
}
if (buttonsSelected) {
input |= 1 << 5;
input |= buttons;
}
return ~input;
}