generated from ange-yaghi/delta-template
-
Notifications
You must be signed in to change notification settings - Fork 868
Expand file tree
/
Copy pathjoystick.cpp
More file actions
140 lines (104 loc) · 2.48 KB
/
joystick.cpp
File metadata and controls
140 lines (104 loc) · 2.48 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <windows.h>
#include "../include/joystick.h"
#pragma comment(lib, "Xinput.lib")
button_id j_Buttons;
Joystick::Joystick() {};
Joystick::Joystick(int j_iIndex) {
j_JoystickIndex = j_iIndex - 1;
for (int i = 0; i < button_count; i++)
{
bStates_prev[i] = false;
bStates[i] = false;
b_ButtonsDown[i] = false;
}
}
XINPUT_STATE Joystick::GetState() {
XINPUT_STATE js;
memset(&js, 0, sizeof(XINPUT_STATE));
XInputGetState(j_JoystickIndex, &js);
return js;
}
int Joystick::GetIndex() { return j_JoystickIndex; }
//Check if joystick is connected and working
bool Joystick::Alive() {
memset(&j_State, 0, sizeof(XINPUT_STATE));
DWORD r = XInputGetState(j_JoystickIndex, &j_State);
if (r == 0)
return true;
else
return false;
}
//updates state of controller
//needs to be called before using any functions of controller
void Joystick::Update() {
j_State = GetState();
for (int i = 0; i < button_count; i++)
{
// Set button state for current frame
bStates[i] = (j_State.Gamepad.wButtons &
XINPUT_Buttons[i]) == XINPUT_Buttons[i];
// Set 'DOWN' state for current frame
b_ButtonsDown[i] = !bStates_prev[i] &&
bStates[i];
}
}
//refreshes states of buttons. needs to be called at the end of use of controller
void Joystick::RefreshState()
{
memcpy(bStates_prev, bStates,
sizeof(bStates_prev));
}
//get left trigger pull
float Joystick::LT() {
BYTE T = j_State.Gamepad.bLeftTrigger;
if (T > XINPUT_GAMEPAD_TRIGGER_THRESHOLD) {
return T / 255.0f;
}
return 0.0;
}
//get right trigger pull
float Joystick::RT() {
BYTE T = j_State.Gamepad.bRightTrigger;
if (T > XINPUT_GAMEPAD_TRIGGER_THRESHOLD) {
return T / 255.0f;
}
return 0.0;
}
//vibrate controller, use 0,0 to cancel rumble
void Joystick::Rumble(float a_fLM, float a_fRM) {
XINPUT_VIBRATION VS;
memset(&VS, 0, sizeof(XINPUT_VIBRATION));
int LM = int(a_fLM * 65535.0f);
int RM = int(a_fRM * 65535.0f);
VS.wLeftMotorSpeed = LM;
VS.wRightMotorSpeed = RM;
XInputSetState(j_JoystickIndex, &VS);
}
//check if button is pressed. use this for single button checks
bool Joystick::buttonPressed(int j_iButton) {
if (j_State.Gamepad.wButtons & XINPUT_Buttons[j_iButton]) {
return true;
}
return false;
}
//check if button is down every frame
bool Joystick::buttonDown(int j_iButton)
{
return b_ButtonsDown[j_iButton];
}
button_id::button_id() {
A = 0;
B = 1;
X = 2;
Y = 3;
d_up = 4;
d_down = 5;
d_left = 6;
d_right = 7;
l_s = 8;
r_s = 9;
l_th = 10;
r_th = 11;
start = 12;
back = 13;
}