@@ -5,12 +5,24 @@ InputHandler* InputHandler::s_pInstance = 0; //IMPORTANT TO MAKE SINGLETON WORK
5
5
6
6
InputHandler::InputHandler ()
7
7
{
8
- // ctor
8
+ for (int i = 0 ; i < 3 ; i++)
9
+ {
10
+ m_mouseButtonStates.push_back (false ); // incialitze vector of buttons from mouse
11
+ }
12
+ m_mousePosition = new Vector2D (0 ,0 );
9
13
}
10
14
11
15
InputHandler::~InputHandler ()
12
16
{
17
+ // delete anything we created dynamically
18
+ delete m_keystates;
19
+ delete m_mousePosition;
13
20
21
+ // clear our arrays
22
+ m_joystickValues.clear ();
23
+ m_joysticks.clear ();
24
+ m_buttonStates.clear ();
25
+ m_mouseButtonStates.clear ();
14
26
}
15
27
16
28
void InputHandler::initialiseJoysticks ()
@@ -27,7 +39,17 @@ void InputHandler::initialiseJoysticks()
27
39
SDL_Joystick* joy = SDL_JoystickOpen (i);
28
40
if (SDL_JoystickOpen (i) != NULL ) // try to activate the joystick, otherwise show the error
29
41
{
30
- m_joysticks.push_back (joy);
42
+ m_joysticks.push_back (joy); // add to vector of joystick id
43
+ m_joystickValues.push_back (make_pair (new Vector2D (0 ,0 ), new Vector2D (0 ,0 ))); // inicialize values for joystick axis
44
+
45
+ vector<bool > tempButtons; // buttons of the joystick
46
+
47
+ for (int j = 0 ; j < SDL_JoystickNumButtons (joy); j++)
48
+ {
49
+ tempButtons.push_back (false );
50
+ }
51
+
52
+ m_buttonStates.push_back (tempButtons); // add buttons of the joystick to the vector of buttons of joystickS
31
53
}
32
54
else
33
55
{
@@ -54,7 +76,7 @@ void InputHandler::clean()
54
76
{
55
77
if (m_bJoysticksInitialised)
56
78
{
57
- for (int i= 0 ; i < SDL_NumJoysticks (); i++)
79
+ for (int i= 0 ; i < SDL_NumJoysticks (); i++) // clean joysticks opened
58
80
{
59
81
SDL_JoystickClose (m_joysticks[i]);
60
82
}
@@ -66,9 +88,248 @@ void InputHandler::update()
66
88
SDL_Event event;
67
89
while (SDL_PollEvent (&event))
68
90
{
69
- if (event.type == SDL_QUIT)
91
+ switch (event.type )
92
+ {
93
+ case SDL_QUIT:
94
+ TheGame::Instance ()->quit ();
95
+ break ;
96
+
97
+ case SDL_JOYAXISMOTION:
98
+ onJoystickAxisMove (event);
99
+ break ;
100
+
101
+ case SDL_JOYBUTTONDOWN:
102
+ onJoystickButtonDown (event);
103
+ break ;
104
+
105
+ case SDL_JOYBUTTONUP:
106
+ onJoystickButtonUp (event);
107
+ break ;
108
+
109
+ case SDL_MOUSEMOTION:
110
+ onMouseMove (event);
111
+ break ;
112
+
113
+ case SDL_MOUSEBUTTONDOWN:
114
+ onMouseButtonDown (event);
115
+ break ;
116
+
117
+ case SDL_MOUSEBUTTONUP:
118
+ onMouseButtonUp (event);
119
+ break ;
120
+
121
+ case SDL_KEYDOWN:
122
+ onKeyDown ();
123
+ break ;
124
+
125
+ case SDL_KEYUP:
126
+ onKeyUp ();
127
+ break ;
128
+
129
+ default :
130
+ break ;
131
+ }
132
+ }
133
+ }
134
+
135
+ void InputHandler::onKeyDown ()
136
+ {
137
+ m_keystates = SDL_GetKeyboardState (0 );
138
+ }
139
+
140
+ void InputHandler::onKeyUp ()
141
+ {
142
+ m_keystates = SDL_GetKeyboardState (0 );
143
+ }
144
+
145
+ void InputHandler::onMouseMove (SDL_Event &event)
146
+ {
147
+ m_mousePosition->setX (event.motion .x );
148
+ m_mousePosition->setY (event.motion .y );
149
+ }
150
+
151
+ void InputHandler::onMouseButtonDown (SDL_Event &event)
152
+ {
153
+ if (event.button .button == SDL_BUTTON_LEFT)
154
+ {
155
+ m_mouseButtonStates[LEFT] = true ;
156
+ }
157
+
158
+ if (event.button .button == SDL_BUTTON_MIDDLE)
159
+ {
160
+ m_mouseButtonStates[MIDDLE] = true ;
161
+ }
162
+
163
+ if (event.button .button == SDL_BUTTON_RIGHT)
164
+ {
165
+ m_mouseButtonStates[RIGHT] = true ;
166
+ }
167
+ }
168
+
169
+ void InputHandler::onMouseButtonUp (SDL_Event &event)
170
+ {
171
+ if (event.button .button == SDL_BUTTON_LEFT)
172
+ {
173
+ m_mouseButtonStates[LEFT] = false ;
174
+ }
175
+
176
+ if (event.button .button == SDL_BUTTON_MIDDLE)
177
+ {
178
+ m_mouseButtonStates[MIDDLE] = false ;
179
+ }
180
+
181
+ if (event.button .button == SDL_BUTTON_RIGHT)
182
+ {
183
+ m_mouseButtonStates[RIGHT] = false ;
184
+ }
185
+ }
186
+
187
+ void InputHandler::onJoystickAxisMove (SDL_Event &event)
188
+ {
189
+ int whichOne = event.jaxis .which ;
190
+
191
+ // left stick move left or right
192
+ if (event.jaxis .axis == 0 )
193
+ {
194
+ if (event.jaxis .value > m_joystickDeadZone)
195
+ {
196
+ m_joystickValues[whichOne].first ->setX (1 );
197
+ }
198
+ else if (event.jaxis .value < -m_joystickDeadZone)
199
+ {
200
+ m_joystickValues[whichOne].first ->setX (-1 );
201
+ }
202
+ else
203
+ {
204
+ m_joystickValues[whichOne].first ->setX (0 );
205
+ }
206
+ }
207
+
208
+ // left stick move up or down
209
+ if (event.jaxis .axis == 1 )
210
+ {
211
+ if (event.jaxis .value > m_joystickDeadZone)
212
+ {
213
+ m_joystickValues[whichOne].first ->setY (1 );
214
+ }
215
+ else if (event.jaxis .value < -m_joystickDeadZone)
216
+ {
217
+ m_joystickValues[whichOne].first ->setY (-1 );
218
+ }
219
+ else
220
+ {
221
+ m_joystickValues[whichOne].first ->setY (0 );
222
+ }
223
+ }
224
+
225
+ // right stick move left or right
226
+ if (event.jaxis .axis == 3 )
227
+ {
228
+ if (event.jaxis .value > m_joystickDeadZone)
229
+ {
230
+ m_joystickValues[whichOne].second ->setX (1 );
231
+ }
232
+ else if (event.jaxis .value < -m_joystickDeadZone)
233
+ {
234
+ m_joystickValues[whichOne].second ->setX (-1 );
235
+ }
236
+ else
237
+ {
238
+ m_joystickValues[whichOne].second ->setX (0 );
239
+ }
240
+ }
241
+
242
+ // right stick move up or down
243
+ if (event.jaxis .axis == 4 )
244
+ {
245
+ if (event.jaxis .value > m_joystickDeadZone)
246
+ {
247
+ m_joystickValues[whichOne].second ->setY (1 );
248
+ }
249
+ else if (event.jaxis .value < -m_joystickDeadZone)
250
+ {
251
+ m_joystickValues[whichOne].second ->setY (-1 );
252
+ }
253
+ else
254
+ {
255
+ m_joystickValues[whichOne].second ->setY (0 );
256
+ }
257
+ }
258
+ }
259
+
260
+ void InputHandler::onJoystickButtonDown (SDL_Event &event)
261
+ {
262
+ int whichOne = event.jaxis .which ;
263
+
264
+ m_buttonStates[whichOne][event.jbutton .button ] = true ;
265
+ }
266
+
267
+ void InputHandler::onJoystickButtonUp (SDL_Event &event)
268
+ {
269
+ int whichOne = event.jaxis .which ;
270
+
271
+ m_buttonStates[whichOne][event.jbutton .button ] = false ;
272
+ }
273
+
274
+ int InputHandler::xvalue (int joy, int stick)
275
+ {
276
+ if (m_joystickValues.size () > 0 )
277
+ {
278
+ if (stick == 1 )
279
+ {
280
+ return m_joystickValues[joy].first ->getX ();
281
+ }
282
+ else if (stick == 2 )
283
+ {
284
+ return m_joystickValues[joy].second ->getX ();
285
+ }
286
+ }
287
+ return 0 ;
288
+ }
289
+
290
+ int InputHandler::yvalue (int joy, int stick)
291
+ {
292
+ if (m_joystickValues.size () > 0 )
293
+ {
294
+ if (stick == 1 )
295
+ {
296
+ return m_joystickValues[joy].first ->getY ();
297
+ }
298
+ else if (stick == 2 )
299
+ {
300
+ return m_joystickValues[joy].second ->getY ();
301
+ }
302
+ }
303
+ return 0 ;
304
+ }
305
+
306
+ bool InputHandler::getJoyButtonState (int joy, int buttonNumber)
307
+ {
308
+ return m_buttonStates[joy][buttonNumber];
309
+ }
310
+
311
+ bool InputHandler::getMouseButtonState (int buttonNumber)
312
+ {
313
+ return m_mouseButtonStates[buttonNumber];
314
+ }
315
+
316
+ Vector2D* InputHandler::getMousePosition ()
317
+ {
318
+ return m_mousePosition;
319
+ }
320
+
321
+ bool InputHandler::isKeyDown (SDL_Scancode key)
322
+ {
323
+ if (m_keystates != 0 )
324
+ {
325
+ if (m_keystates[key] == 1 )
326
+ {
327
+ return true ;
328
+ }
329
+ else
70
330
{
71
- TheGame::Instance ()-> quit () ;
331
+ return false ;
72
332
}
73
333
}
334
+ return false ;
74
335
}
0 commit comments