-
Notifications
You must be signed in to change notification settings - Fork 9
/
InputHandler.cpp
364 lines (314 loc) · 6.94 KB
/
InputHandler.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "InputHandler.h"
#include <iostream>
#include "Game.h"
//definition for static instance variable
InputHandler* InputHandler::s_pInstance = 0;
//constructor initialises the mouse buttons to false
InputHandler::InputHandler()
{
for (int i = 0; i < 3; ++i)
{
m_mouseButtonStates.push_back(false);
}
m_mousePosition = new Vector2D(0, 0);
}
InputHandler::~InputHandler()
{
//delete what we created with new
delete m_mousePosition;
delete m_keystates;
//clear the vectors
m_joystickValues.clear();
m_joysticks.clear();
m_buttonStates.clear();
m_mouseButtonStates.clear();
}
void InputHandler::initialiseJoysticks()
{
//check if joystick was initialised(will return non zero)
if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
{
//initialise the joystick
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
}
//find the number of joysticks and push them in vector
if (SDL_NumJoysticks() > 0)
{
for (int i = 0; i < SDL_NumJoysticks(); ++i)
{
SDL_Joystick* joy = SDL_JoystickOpen(i); //opens a joystick for use
//check if NULL was not returned and joystick opened successfully
if (SDL_JoystickOpen(i) != NULL) //original was == 1
{
m_joysticks.push_back(joy);
//add our pair of analog sticks for that joystick
m_joystickValues.push_back(std::make_pair(new Vector2D(0, 0), new Vector2D(0, 0)));
//initialize all the buttons for this joystick to false state
std::vector <bool> tempButtons;
for (int j = 0; j < SDL_JoystickNumButtons(joy); ++j)
{
tempButtons.push_back(false);
}
//push this joystick in the outer container
m_buttonStates.push_back(tempButtons);
}
else
{
//error opening joystick
std::cout << SDL_GetError();
}
}
//listen for joystick events
SDL_JoystickEventState(SDL_ENABLE);
m_bJoysticksInitialised = true;
std::cout << "Initialised " << m_joysticks.size() << " joystick(s)";
}
else
{
m_bJoysticksInitialised = false;
}
}
bool InputHandler::joysticksInitialised()
{
return m_bJoysticksInitialised;
}
int InputHandler::xvalue(int joy, int stick)
{
if (m_joystickValues.size() > 0)
{
if (stick == 1) //left stick
{
return (int)m_joystickValues[joy].first->getX();
}
else //right stick
{
return (int)m_joystickValues[joy].second->getX();
}
}
return 0;
}
int InputHandler::yvalue(int joy, int stick)
{
if (m_joystickValues.size() > 0)
{
if (stick == 1) //left stick
{
return (int)m_joystickValues[joy].first->getY();
}
else //right stick
{
return (int)m_joystickValues[joy].second->getY();
}
}
return 0;
}
bool InputHandler::getButtonState(int joy, int buttonNumber)
{
return m_buttonStates[joy][buttonNumber];
}
bool InputHandler::getMouseButtonState(int buttonNumber)
{
return m_mouseButtonStates[buttonNumber];
}
Vector2D* InputHandler::getMousePosition()
{
return m_mousePosition;
}
//Function to check if specific key was pressed by taking SDL_Scancode as param
//Refer to this link for SDL_Scancode
//https://wiki.libsdl.org/SDL_Scancode?highlight=%28%5CbCategoryEnum%5Cb%29%7C%28SDLEnumTemplate%29
//SDL_Scancode for entire all the keys
bool InputHandler::isKeyDown(SDL_Scancode key)
{
if (m_keystates != NULL)
{
if (m_keystates[key] == 1)
{
return true;
}
else
{
return false;
}
}
return false;
}
void InputHandler::update()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
TheGame::Instance()->quit();
break;
case SDL_JOYAXISMOTION:
onJoystickAxisMove(event);
break;
case SDL_JOYBUTTONDOWN:
onJoystickButtonDown(event);
break;
case SDL_JOYBUTTONUP:
onJoystickButtonUp(event);
break;
case SDL_MOUSEMOTION:
onMouseMove(event);
break;
case SDL_MOUSEBUTTONDOWN:
onMouseButtonDown(event);
break;
case SDL_MOUSEBUTTONUP:
onMouseButtonUp(event);
break;
case SDL_KEYDOWN:
onKeyDown();
break;
case SDL_KEYUP:
onKeyUp();
break;
default:
break;
}
}
}
//keyboard event functions
void InputHandler::onKeyDown()
{
m_keystates = SDL_GetKeyboardState(NULL);
}
void InputHandler::onKeyUp()
{
m_keystates = SDL_GetKeyboardState(NULL);
}
//mouse event functions
void InputHandler::onMouseMove(SDL_Event &event)
{
m_mousePosition->setX((float)event.motion.x);
m_mousePosition->setY((float)event.motion.y);
}
void InputHandler::onMouseButtonDown(SDL_Event &event)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
m_mouseButtonStates[LEFT] = true;
}
if (event.button.button == SDL_BUTTON_MIDDLE)
{
m_mouseButtonStates[MIDDLE] = true;
}
if (event.button.button == SDL_BUTTON_RIGHT)
{
m_mouseButtonStates[RIGHT] = true;
}
}
void InputHandler::onMouseButtonUp(SDL_Event &event)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
m_mouseButtonStates[LEFT] = false;
}
if (event.button.button == SDL_BUTTON_MIDDLE)
{
m_mouseButtonStates[MIDDLE] = false;
}
if (event.button.button == SDL_BUTTON_RIGHT)
{
m_mouseButtonStates[RIGHT] = false;
}
}
//joystick event functions
void InputHandler::onJoystickAxisMove(SDL_Event &event)
{
int whichOne = event.jaxis.which;
// left stick move left or right
if (event.jaxis.axis == 0)
{
if (event.jaxis.value > m_joystickDeadZone)
{
m_joystickValues[whichOne].first->setX(1);
}
else if (event.jaxis.value < -m_joystickDeadZone)
{
m_joystickValues[whichOne].first->setX(-1);
}
else
{
m_joystickValues[whichOne].first->setX(0);
}
}
// left stick move up or down
if (event.jaxis.axis == 1)
{
if (event.jaxis.value > m_joystickDeadZone)
{
m_joystickValues[whichOne].first->setY(1);
}
else if (event.jaxis.value < -m_joystickDeadZone)
{
m_joystickValues[whichOne].first->setY(-1);
}
else
{
m_joystickValues[whichOne].first->setY(0);
}
}
// right stick move left or right
if (event.jaxis.axis == 3)
{
if (event.jaxis.value > m_joystickDeadZone)
{
m_joystickValues[whichOne].second->setX(1);
}
else if (event.jaxis.value < -m_joystickDeadZone)
{
m_joystickValues[whichOne].second->setX(-1);
}
else
{
m_joystickValues[whichOne].second->setX(0);
}
}
// right stick move up or down
if (event.jaxis.axis == 4)
{
if (event.jaxis.value > m_joystickDeadZone)
{
m_joystickValues[whichOne].second->setY(1);
}
else if (event.jaxis.value < -m_joystickDeadZone)
{
m_joystickValues[whichOne].second->setY(-1);
}
else
{
m_joystickValues[whichOne].second->setY(0);
}
}
}
void InputHandler::onJoystickButtonDown(SDL_Event &event)
{
int whichOne = event.jaxis.which;
m_buttonStates[whichOne][event.jbutton.button] = true;
}
void InputHandler::onJoystickButtonUp(SDL_Event &event)
{
int whichOne = event.jaxis.which;
m_buttonStates[whichOne][event.jbutton.button] = false;
}
void InputHandler::clean()
{
if (m_bJoysticksInitialised)
{
for (unsigned int i = 0; i < m_joysticks.size(); ++i)
{
SDL_JoystickClose(m_joysticks[i]);
}
}
}
void InputHandler::reset()
{
m_mouseButtonStates[LEFT] = false;
m_mouseButtonStates[MIDDLE] = false;
m_mouseButtonStates[RIGHT] = false;
}