-
Notifications
You must be signed in to change notification settings - Fork 1
/
Input.cpp
187 lines (154 loc) · 4.08 KB
/
Input.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "Input.h"
Input::Input() :
m_mouseX(0), m_mouseY(0), m_mouseRelX(0), m_mouseRelY(0),
m_numJoysticks(0),
m_terminate(false)
{
// Keyboard & mouse
for(int i(0); i<SDL_NUM_SCANCODES; i++)
m_keys[i] = false;
for(int i(0); i<8; i++)
m_mouseButtons[i] = false;
// Joysticks
for(int i(0); i<4; i++)
{
for(int j(0); j<13; j++)
m_joystickButtons[i][j] = false;
for(int j(0); j<6; j++)
m_joystickAxes[i][j] = 0;
m_joystickHat[i] = SDL_HAT_CENTERED;
}
}
Input::~Input()
{}
void Input::openJoysticks()
{
m_numJoysticks = SDL_NumJoysticks();
if(m_numJoysticks > 4)
m_numJoysticks = 4; // Sets maximum joysticks to 4
for(int i(0); i<m_numJoysticks; i++)
{
m_joysticks[i] = SDL_JoystickOpen(i); // Open Joysticks
std::cout << "Joystick #" << i << " OK " << std::endl;
}
}
void Input::closeJoysticks()
{
for(int i(0); i<m_numJoysticks; i++)
SDL_JoystickClose(m_joysticks[i]); // Close Joysticks
/*
/!\ There might be an error if a device is removed during game session /!\
TODO : - close joysticks if SDL_JOYDEVICEREMOVED in events
- update m_numJoystick
- re-open m_joysticks
*/
}
void Input::updateEvents()
{
m_mouseRelX = 0;
m_mouseRelY = 0;
while(SDL_PollEvent(&m_events))
{
switch(m_events.type)
{
// Keyboard events
case SDL_KEYDOWN:
m_keys[m_events.key.keysym.scancode] = true;
break;
case SDL_KEYUP:
m_keys[m_events.key.keysym.scancode] = false;
break;
// Mouse events
case SDL_MOUSEBUTTONDOWN:
m_mouseButtons[m_events.button.button] = true;
break;
case SDL_MOUSEBUTTONUP:
m_mouseButtons[m_events.button.button] = false;
break;
case SDL_MOUSEMOTION:
m_mouseX = m_events.motion.x;
m_mouseY = m_events.motion.y;
m_mouseRelX = m_events.motion.xrel;
m_mouseRelY = m_events.motion.yrel;
break;
// Joystick events
case SDL_JOYBUTTONDOWN:
m_joystickButtons[m_events.jbutton.which][m_events.jbutton.button] = true;
break;
case SDL_JOYBUTTONUP:
m_joystickButtons[m_events.jbutton.which][m_events.jbutton.button] = false;
break;
case SDL_JOYAXISMOTION:
m_joystickAxes[m_events.jaxis.which][m_events.jaxis.axis] = m_events.jaxis.value;
break;
case SDL_JOYHATMOTION:
m_joystickHat[m_events.jhat.which] = m_events.jhat.value;
break;
// Window events
case SDL_WINDOWEVENT:
if(m_events.window.event == SDL_WINDOWEVENT_CLOSE)
m_terminate = true;
break;
default:
break;
}
}
}
bool Input::terminate() const
{
return m_terminate;
}
bool Input::getKey(const SDL_Scancode key) const
{
return m_keys[key];
}
bool Input::getMouseButton(const Uint8 button) const
{
return m_mouseButtons[button];
}
bool Input::mouseMotion() const
{
return m_mouseRelX || m_mouseRelY;
}
int Input::getMouseX() const
{
return m_mouseX;
}
int Input::getMouseY() const
{
return m_mouseY;
}
int Input::getMouseRelX() const
{
return m_mouseRelX;
}
int Input::getMouseRelY() const
{
return m_mouseRelY;
}
bool Input::getJoystickButton(const int joystickNumber, const Uint8 button) const
{
return m_joystickButtons[joystickNumber][button];
}
int Input::getJoystickAxes(const int joystickNumber, const Uint8 axis) const
{
return m_joystickAxes[joystickNumber][axis];
}
Uint8 Input::getJoystickHat(const int joystickNumber) const
{
return m_joystickHat[joystickNumber];
}
void Input::afficherPtr(bool response) const
{
if(response)
SDL_ShowCursor(SDL_ENABLE);
else
SDL_ShowCursor(SDL_DISABLE);
}
void Input::capturePtr(bool response) const
{
if(response)
SDL_SetRelativeMouseMode(SDL_TRUE);
else
SDL_SetRelativeMouseMode(SDL_FALSE);
}