-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller-state.cc
68 lines (48 loc) · 1.47 KB
/
controller-state.cc
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
// controller-state.cc
// Code for controller-state.h.
#include "controller-state.h" // this module
#include <windows.h> // GetTickCount
#include <xinput.h> // XInputGetState
#include <cstring> // std::{memset, memcpy}
ControllerState::ControllerState()
: m_inputState(),
m_hasInputState(false),
m_pollTimeMS(0)
{
// I'm not sure if the default ctor initializes this.
std::memset(&m_inputState, 0, sizeof(m_inputState));
}
ControllerState &ControllerState::operator=(ControllerState const &obj)
{
std::memcpy(&m_inputState,
&obj.m_inputState,
sizeof(m_inputState));
m_hasInputState = obj.m_hasInputState;
m_pollTimeMS = obj.m_pollTimeMS;
return *this;
}
void ControllerState::poll(int controllerID)
{
std::memset(&m_inputState, 0, sizeof(m_inputState));
DWORD res = XInputGetState(controllerID, &m_inputState);
m_hasInputState = (res == ERROR_SUCCESS);
m_pollTimeMS = GetTickCount();
}
bool ControllerState::isTriggerPressed(
AnalogThresholdConfig const &atConfig,
bool leftSide) const
{
if (m_hasInputState) {
BYTE trigger = (leftSide? m_inputState.Gamepad.bLeftTrigger :
m_inputState.Gamepad.bRightTrigger);
return trigger > atConfig.m_triggerDeadZone;
}
else {
return false;
}
}
bool ControllerState::isButtonPressed(WORD button) const
{
return (m_inputState.Gamepad.wButtons & button) != 0;
}
// EOF