Reference
Joystick handling is covered in great detail here:– http://sdldoc.csn.ul.ie/
guideinput.php#AEN163
Digital vs Analogue Digital
– On or off– Buttons– The “Hat” (SDL)
Analog(ue)– Joysticks– A range of values
• (-32768 to +32767) for SDL
A PS2-Style Joypad
Modern gamepadshave both analogue
and digital
A PS2-Style Joypad“Hat”
A PS2-Style Joypad
8-wayDirectional
Control
“Hat”
A PS2-Style Joypad
SDL treatsit as digital
“Hat”
A PS2-Style JoypadJoysticks
A PS2-Style Joypad
SDL treatsthese asanalogue
Joysticks
A PS2-Style JoypadButtons
A PS2-Style JoypadButtons
SDL treatsthese asdigital
A PS2-Style JoypadButtons
Don't forgetthe shoulder
buttons
Joysticks and SDL
Reference:– Joystick handling in SDL is covered in
great detail here in the SDL documentation (see the GHAP webpage)
Initializing SDL This assumes only one joystick
– SDL has functionality for discovering how many joysticks there are and what type they are, etc
/* initialize SDL */SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK| SDL_INIT_TIMER);
/* Open the Joystick */SDL_Joystick *joystick;SDL_JoystickEventState(SDL_ENABLE);joystick = SDL_JoystickOpen(0);
Joystick Event-Handling while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: /* handle keyboard stuff here */ break;
case SDL_JOYAXISMOTION: /* handle joystick motion here * (see next slide) */ break; } }
/* handle joystick motion here */
case SDL_JOYAXISMOTION: /* handle joystick motion here */if(SDL_JoystickGetAxis(joystick, 0) < -3200) {
xMove = -2;} else if(SDL_JoystickGetAxis(joystick, 0) > +3200) {
xMove = +2;} else {
xMove = 0;}if(SDL_JoystickGetAxis(joystick, 1) < -3200) {
yMove = -2;} else if(SDL_JoystickGetAxis(joystick, 1) > +3200) {
yMove = +2;} else {
yMove = 0;}
break;
Analogue The example on the previous slide
simply treats the joystick as a digital device: it is moved either to the left or the right
This is not really appropriate for an analogue device– To use it properly, we should reflect how
far the joystick has been moved• The further it has been moved, the faster the
movement
Analogue Joystick Motioncase SDL_JOYAXISMOTION:
/* handle joystick motion here */if(SDL_JoystickGetAxis(joystick, 0) < -22000) {
xMove = -8;} else if(SDL_JoystickGetAxis(joystick, 0) < -12200) {
xMove = -4;} else if(SDL_JoystickGetAxis(joystick, 0) < -3200) {
xMove = -2;} else if(SDL_JoystickGetAxis(joystick, 0) > +22000) {
xMove = +8;} else if(SDL_JoystickGetAxis(joystick, 0) > +12200) {
xMove = +4;} else if(SDL_JoystickGetAxis(joystick, 0) > +3200) {
xMove = +2;} else {
xMove = 0;}/* You'd need something similar for the y dimension */break;
Joystick Buttonscase SDL_JOYBUTTONDOWN: /* Joystick Button Press */
if( event.jbutton.button == 0 ) { firingMachineGun = true;
}if(event.jbutton.button == 1) {
firingRockets = true;}break;
case SDL_JOYBUTTONUP: /* Joystick Button Release */if( event.jbutton.button == 0 ) {
firingMachineGun = false;}if(event.jbutton.button == 1) {
firingRockets = false;}break;
Continued overleaf
Joystick Buttons (continued) You fire machine guns and/or rockets after the switch statement:
switch(event.type) {/* blah blah
* see previous slides * blah blah */
}
if(firingMachineGuns) {/* handle machine gun firing */
}if(firingRockets) {
/* handle rocket firing */}