-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputHandler.cpp
368 lines (318 loc) · 7.49 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
365
366
367
368
//
// InputHandler.cpp
// SDL Game Programming Book
//
// Created by shaun mitchell on 24/01/2013.
// Copyright (c) 2013 shaun mitchell. All rights reserved.
//
#include "InputHandler.h"
#include "Game.h"
#include <utils/log.h>
#include <utils/init.h>
#include <iostream>
InputHandler* InputHandler::s_pInstance = 0;
InputHandler::InputHandler() : m_keystates(0),
m_bJoysticksInitialised(false),
m_mousePosition(0,0),
m_logMouseClicks(false)
{
m_logMouseClicks = dani::init::hasArg("--logMouseClicks");
// create button states for the mouse
for(int i = 0; i < 3; i++)
{
m_mouseButtonStates.push_back(false);
}
}
InputHandler::~InputHandler()
{
// delete anything we created dynamically
delete m_keystates;
// clear our arrays
m_joystickValues.clear();
m_joysticks.clear();
m_buttonStates.clear();
m_mouseButtonStates.clear();
}
void InputHandler::clean()
{
// we need to clean up after ourselves and close the joysticks we opened
if(m_bJoysticksInitialised)
{
for(unsigned int i = 0; i < SDL_NumJoysticks(); i++)
{
SDL_JoystickClose(m_joysticks[i]);
}
}
}
void InputHandler::initialiseJoysticks()
{
// if we haven't already initialised the joystick subystem, we will do it here
if(SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
{
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
}
// get the number of joysticks, skip init if there aren't any
if(SDL_NumJoysticks() > 0)
{
for(int i = 0; i < SDL_NumJoysticks(); i++)
{
// create a new joystick
SDL_Joystick* joy = SDL_JoystickOpen(i);
// if the joystick opened correctly we need to populate our arrays
if(SDL_JoystickOpen(i))
{
// push back into the array to be closed later
m_joysticks.push_back(joy);
// create a pair of values for the axes, a vector for each stick
m_joystickValues.push_back(std::make_pair(new Vector2D(0,0),new Vector2D(0,0)));
// create an array to hold the button values
std::vector<bool> tempButtons;
// fill the array with a false value for each button
for(int j = 0; j < SDL_JoystickNumButtons(joy); j++)
{
tempButtons.push_back(false);
}
// push the button array into the button state array
m_buttonStates.push_back(tempButtons);
}
else
{
// if there was an error initialising a joystick we want to know about it
std::cout << SDL_GetError();
}
}
// enable joystick events
SDL_JoystickEventState(SDL_ENABLE);
m_bJoysticksInitialised = true;
std::cout << "Initialised " << m_joysticks.size() << " joystick(s)";
}
else
{
m_bJoysticksInitialised = false;
}
}
void InputHandler::reset()
{
m_mouseButtonStates[LEFT] = false;
m_mouseButtonStates[RIGHT] = false;
m_mouseButtonStates[MIDDLE] = false;
}
bool InputHandler::isKeyDown(SDL_Scancode key) const
{
if(m_keystates != 0)
{
if(m_keystates[key] == 1)
{
return true;
}
else
{
return false;
}
}
return false;
}
int InputHandler::getAxisX(int joy, int stick) const
{
if(m_joystickValues.size() > 0)
{
if(stick == 1)
{
return m_joystickValues[joy].first->getX();
}
else if(stick == 2)
{
return m_joystickValues[joy].second->getX();
}
}
return 0;
}
int InputHandler::getAxisY(int joy, int stick) const
{
if(m_joystickValues.size() > 0)
{
if(stick == 1)
{
return m_joystickValues[joy].first->getY();
}
else if(stick == 2)
{
return m_joystickValues[joy].second->getY();
}
}
return 0;
}
bool InputHandler::getButtonState(int joy, int buttonNumber) const
{
return m_buttonStates[joy][buttonNumber];
}
bool InputHandler::getMouseButtonState(int buttonNumber) const
{
return m_mouseButtonStates[buttonNumber];
}
const Vector2D &InputHandler::getMousePosition() const
{
return m_mousePosition;
}
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;
}
}
}
void InputHandler::onKeyDown()
{
m_keystates = SDL_GetKeyboardState(0);
}
void InputHandler::onKeyUp()
{
m_keystates = SDL_GetKeyboardState(0);
}
void InputHandler::onMouseMove(SDL_Event &event)
{
m_mousePosition.setX(event.motion.x);
m_mousePosition.setY(event.motion.y);
}
void InputHandler::onMouseButtonDown(SDL_Event &event)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
m_mouseButtonStates[LEFT] = true;
if (m_logMouseClicks)
LOG_ERROR("{ " << event.button.x << ", " << event.button.y << "}, ");
}
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;
}
}
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;
}