This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
joystick.cpp
132 lines (114 loc) · 3.3 KB
/
joystick.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
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
#include "joystick.h"
#include <SDL.h>
#include <assert.h>
SDL_Joystick* s_sdlJoy = 0;
Joystick s_joy;
void JOYSTICK_Init()
{
// Only Xbox360Controller is supported
if (SDL_NumJoysticks() > 0)
{
s_sdlJoy = SDL_JoystickOpen(0);
printf("Found joystick \"%s\"\n", SDL_JoystickName(0));
printf(" numAxes = %d\n", SDL_JoystickNumAxes(s_sdlJoy));
printf(" numButtons = %d\n", SDL_JoystickNumButtons(s_sdlJoy));
printf(" numBalls = %d\n", SDL_JoystickNumBalls(s_sdlJoy));
printf(" numHats = %d\n", SDL_JoystickNumHats(s_sdlJoy));
}
else
printf("Joystick not found.\n");
}
void JOYSTICK_Shutdown()
{
if (s_sdlJoy)
SDL_JoystickClose(s_sdlJoy);
}
void JOYSTICK_ClearFlags()
{
s_joy.buttonPressFlags = 0;
s_joy.buttonReleaseFlags = 0;
}
// TODO: do a real joystick config.
// maps event to axes
#ifdef DARWIN
static int s_axisMap[] = {
Joystick::LeftStickX,
Joystick::LeftStickY,
Joystick::LeftTrigger,
Joystick::RightStickX,
Joystick::RightStickY,
Joystick::RightTrigger
};
#else
static int s_axisMap[] = {
Joystick::LeftStickX,
Joystick::LeftStickY,
Joystick::LeftTrigger,
Joystick::RightStickY,
Joystick::RightStickX,
Joystick::RightTrigger
};
#endif
void JOYSTICK_UpdateMotion(const SDL_JoyAxisEvent* event)
{
if (event->which != 0)
return;
const float kDeadSpot = 0.2f;
int i = s_axisMap[event->axis];
switch (i)
{
case Joystick::LeftStickX:
case Joystick::RightStickX:
s_joy.axes[i] = event->value / 32768.0f;
if (fabs(s_joy.axes[i]) < kDeadSpot)
s_joy.axes[i] = 0.0f;
break;
// flip y axis, not sure if this a driver thing or an SDL thing, but I want my +y to mean up.
case Joystick::LeftStickY:
case Joystick::RightStickY:
s_joy.axes[i] = event->value / -32768.0f;
if (fabs(s_joy.axes[i]) < kDeadSpot)
s_joy.axes[i] = 0.0f;
break;
// I want to map the triggers from 0 to 1
case Joystick::LeftTrigger:
case Joystick::RightTrigger:
s_joy.axes[i] = ((event->value / 32768.0f) * 0.5f) + 0.5f;
if (s_joy.axes[i] < kDeadSpot)
s_joy.axes[i] = 0.0f;
break;
}
}
unsigned int UpdateButtonFlag(unsigned int orig, unsigned int mask, bool newState)
{
if (newState)
return orig | mask; // set flag
else
return orig & ~mask; // clear flag
}
// TODO: do a real joystick config.
#ifdef DARWIN
static int s_buttonMap[] = {Joystick::DPadDown, Joystick::DPadUp, Joystick::DPadLeft, Joystick::DPadRight,
Joystick::Start, Joystick::Back, Joystick::LeftStick, Joystick::RightStick,
Joystick::LeftBumper, Joystick::RightBumper, Joystick::Xbox, Joystick::A,
Joystick::B, Joystick::X, Joystick::Y};
#else
static int s_buttonMap[] = {Joystick::A, Joystick::B, Joystick::X, Joystick::Y,
Joystick::LeftBumper, Joystick::RightBumper, Joystick::Back, Joystick::Start,
Joystick::LeftStick, Joystick::RightStick};
#endif
void JOYSTICK_UpdateButton(const SDL_JoyButtonEvent* event)
{
if (event->which != 0)
return;
int i = s_buttonMap[event->button];
s_joy.buttonStateFlags = UpdateButtonFlag(s_joy.buttonStateFlags, 1 << i, event->state);
if (event->state)
s_joy.buttonPressFlags = UpdateButtonFlag(s_joy.buttonPressFlags, 1 << i, event->state);
else
s_joy.buttonReleaseFlags = UpdateButtonFlag(s_joy.buttonReleaseFlags, 1 << i, event->state);
}
Joystick* JOYSTICK_GetJoystick()
{
return &s_joy;
}