Presentation is loading. Please wait.

Presentation is loading. Please wait.

Games at Bolton Joysticks Andrew Williams

Similar presentations


Presentation on theme: "Games at Bolton Joysticks Andrew Williams"— Presentation transcript:

1 Games at Bolton Joysticks Andrew Williams http://www.bolton.ac.uk/staff/adw1 A.Williams@bolton.ac.uk

2 Games at Bolton Reference  Joystick handling is covered in great detail here: –http://sdldoc.csn.ul.ie/guideinput.php#AEN163

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

4 Games at Bolton A PS2-Style Joypad Modern gamepads have both analogue and digital

5 Games at Bolton A PS2-Style Joypad “Hat”

6 Games at Bolton A PS2-Style Joypad 8-way Directional Control “Hat”

7 Games at Bolton A PS2-Style Joypad SDL treats it as digital “Hat”

8 Games at Bolton A PS2-Style Joypad Joysticks

9 Games at Bolton A PS2-Style Joypad SDL treats these as analogue Joysticks

10 Games at Bolton A PS2-Style Joypad Buttons

11 Games at Bolton A PS2-Style Joypad Buttons SDL treats these as digital

12 Games at Bolton A PS2-Style Joypad Buttons Don't forget the shoulder buttons

13 Games at Bolton Joysticks and SDL  Reference: –Joystick handling in SDL is covered in great detail here in the SDL documentation (see the GHAP webpage)

14 Games at Bolton 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 Games at Bolton 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 Games at Bolton /* 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 Games at Bolton 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 Games at Bolton 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 Games at Bolton 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 Games at Bolton 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 "Games at Bolton Joysticks Andrew Williams"

Similar presentations


Ads by Google