Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2002 Bolton Institute Joysticks Andrew Williams

Similar presentations


Presentation on theme: "Copyright © 2002 Bolton Institute Joysticks Andrew Williams"— Presentation transcript:

1 Copyright © 2002 Bolton Institute Joysticks Andrew Williams http://www.bolton.ac.uk/staff/adw1 A.Williams@bolton.ac.uk

2 Copyright © 2002 Bolton Institute Reference Joystick handling is covered in great detail here: –http://sdldoc.csn.ul.ie/guideinput.php#AEN1 63

3 Copyright © 2002 Bolton Institute Digital vs Analogue Digital –On or off –Buttons –The “Hat” (SDL) Analog(ue) –Joysticks –A range of values (-32768 to +32767) for SDL

4 Copyright © 2002 Bolton Institute A PS2-Style Joypad Modern gamepads have both analogue and digital

5 Copyright © 2002 Bolton Institute A PS2-Style Joypad “Hat”

6 Copyright © 2002 Bolton Institute A PS2-Style Joypad 8-way Directional Control “Hat”

7 Copyright © 2002 Bolton Institute A PS2-Style Joypad SDL treats it as digital “Hat”

8 Copyright © 2002 Bolton Institute A PS2-Style Joypad Joysticks

9 Copyright © 2002 Bolton Institute A PS2-Style Joypad SDL treats these as analogue Joysticks

10 Copyright © 2002 Bolton Institute A PS2-Style Joypad Buttons

11 Copyright © 2002 Bolton Institute A PS2-Style Joypad Buttons SDL treats these as digital

12 Copyright © 2002 Bolton Institute A PS2-Style Joypad Buttons Don't forget the shoulder buttons

13 Copyright © 2002 Bolton Institute Joysticks and SDL Reference: –Joystick handling in SDL is covered in great detail here: http://sdldoc.csn.ul.ie/guideinput.php#AEN163

14 Copyright © 2002 Bolton Institute 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);

15 Copyright © 2002 Bolton Institute 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; }

16 Copyright © 2002 Bolton Institute /* 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;

17 Copyright © 2002 Bolton Institute 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

18 Copyright © 2002 Bolton Institute Analogue Joystick Motion case 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;

19 Copyright © 2002 Bolton Institute Joystick Buttons case 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

20 Copyright © 2002 Bolton Institute 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 */ }


Download ppt "Copyright © 2002 Bolton Institute Joysticks Andrew Williams"

Similar presentations


Ads by Google