Skip to content

Commit 3e517a8

Browse files
committed
InputHandler class v.2, now controlls mouse-events, joystick-events and keyboard-events, clean code
1 parent 27f6065 commit 3e517a8

File tree

4 files changed

+369
-13
lines changed

4 files changed

+369
-13
lines changed

InputHandler.cpp

Lines changed: 266 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@ InputHandler* InputHandler::s_pInstance = 0; //IMPORTANT TO MAKE SINGLETON WORK
55

66
InputHandler::InputHandler()
77
{
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);
913
}
1014

1115
InputHandler::~InputHandler()
1216
{
17+
// delete anything we created dynamically
18+
delete m_keystates;
19+
delete m_mousePosition;
1320

21+
// clear our arrays
22+
m_joystickValues.clear();
23+
m_joysticks.clear();
24+
m_buttonStates.clear();
25+
m_mouseButtonStates.clear();
1426
}
1527

1628
void InputHandler::initialiseJoysticks()
@@ -27,7 +39,17 @@ void InputHandler::initialiseJoysticks()
2739
SDL_Joystick* joy = SDL_JoystickOpen(i);
2840
if (SDL_JoystickOpen(i) != NULL) // try to activate the joystick, otherwise show the error
2941
{
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
3153
}
3254
else
3355
{
@@ -54,7 +76,7 @@ void InputHandler::clean()
5476
{
5577
if(m_bJoysticksInitialised)
5678
{
57-
for(int i= 0; i < SDL_NumJoysticks(); i++)
79+
for(int i= 0; i < SDL_NumJoysticks(); i++) //clean joysticks opened
5880
{
5981
SDL_JoystickClose(m_joysticks[i]);
6082
}
@@ -66,9 +88,248 @@ void InputHandler::update()
6688
SDL_Event event;
6789
while(SDL_PollEvent(&event))
6890
{
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
70330
{
71-
TheGame::Instance()->quit();
331+
return false;
72332
}
73333
}
334+
return false;
74335
}

InputHandler.h

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
#include "SDL2/SDL.h"
55
#include <iostream>
66
#include <vector>
7+
#include "Vector2D.h"
78

89
using namespace std;
10+
11+
enum mouse_buttons
12+
{
13+
LEFT = 0,
14+
MIDDLE = 1,
15+
RIGHT = 2
16+
};
917

1018
class InputHandler
1119
{
@@ -26,20 +34,55 @@ class InputHandler
2634

2735
void initialiseJoysticks(); //pre: -- //post: initialise joystick handling
2836
bool joysticksInitialised(); //pre: -- //post: return if joysticks are initialised
29-
37+
38+
int xvalue(int joy, int stick); //pre: joy is the joystick ID, stick 1 is left, stick 2 is right //post: return x value
39+
int yvalue(int joy, int stick); //pre: joy is the joystick ID, stick 1 is left, stick 2 is right //post: return y value
40+
41+
//GET METHODS
42+
bool getJoyButtonState(int joy, int buttonNumber); //get the state of the buttonNumber from joy.
43+
bool getMouseButtonState(int buttonNumber); //get the state of the buttonNumber from mouse
44+
Vector2D* getMousePosition(); //get the actual Vector2D coords of the mouse in the window
45+
bool isKeyDown(SDL_Scancode key); //check if a keyboard key is down
46+
3047
private:
3148

3249
//Atribute members
3350
vector<SDL_Joystick* > m_joysticks; //all the connected joysticks
3451
bool m_bJoysticksInitialised; //are joystick initialisated?
52+
vector<pair<Vector2D*, Vector2D* > > m_joystickValues; //values of axis joystic
53+
vector< vector<bool> > m_buttonStates; //matrix with buttons input
54+
vector<bool> m_mouseButtonStates; //vector with the state of mouse buttons
55+
Vector2D* m_mousePosition; //vector with the actual position in the window of the cursor(mouse)
56+
const Uint8* m_keystates;
57+
58+
//SINGLETON
59+
static InputHandler* s_pInstance; //the own instance
60+
61+
//Constants
62+
const int m_joystickDeadZone = 10000; //Value has to change accordingly to the controller type
63+
3564

3665
//Private methods
3766
InputHandler(); //SINGLETON private constructor
3867
~InputHandler(); //SINGLETON private destructor
3968

40-
//SINGLETON
41-
static InputHandler* s_pInstance; //the own instance
42-
};
69+
//Private functions to handle different event types
70+
71+
//handle keyboard events
72+
void onKeyDown();
73+
void onKeyUp();
74+
75+
//handle mpuse events
76+
void onMouseMove(SDL_Event& event);
77+
void onMouseButtonDown(SDL_Event& event);
78+
void onMouseButtonUp(SDL_Event& event);
79+
80+
//handle joysticks events
81+
void onJoystickAxisMove(SDL_Event& event);
82+
void onJoystickButtonDown(SDL_Event& event);
83+
void onJoystickButtonUp(SDL_Event& event);
84+
85+
};
4386

4487
typedef InputHandler TheInputHandler; //only one singleton in all the program
4588

0 commit comments

Comments
 (0)