Games at Bolton Joysticks Andrew Williams [email protected].

20
Joysticks Andrew Williams http://www.bolton.ac.uk/s taff/adw1 [email protected]
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    1

Transcript of Games at Bolton Joysticks Andrew Williams [email protected].

Page 1: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

Joysticks

Andrew Williams

http://www.bolton.ac.uk/staff/adw1

[email protected]

Page 2: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

Reference

Joystick handling is covered in great detail here:– http://sdldoc.csn.ul.ie/

guideinput.php#AEN163

Page 3: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

Digital vs Analogue Digital

– On or off– Buttons– The “Hat” (SDL)

Analog(ue)– Joysticks– A range of values

• (-32768 to +32767) for SDL

Page 4: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style Joypad

Modern gamepadshave both analogue

and digital

Page 5: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style Joypad“Hat”

Page 6: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style Joypad

8-wayDirectional

Control

“Hat”

Page 7: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style Joypad

SDL treatsit as digital

“Hat”

Page 8: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style JoypadJoysticks

Page 9: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style Joypad

SDL treatsthese asanalogue

Joysticks

Page 10: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style JoypadButtons

Page 11: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style JoypadButtons

SDL treatsthese asdigital

Page 12: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

A PS2-Style JoypadButtons

Don't forgetthe shoulder

buttons

Page 13: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

Joysticks and SDL

Reference:– Joystick handling in SDL is covered in

great detail here in the SDL documentation (see the GHAP webpage)

Page 14: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

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);

Page 15: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

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; } }

Page 16: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

/* 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;

Page 17: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

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

Page 18: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

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;

Page 19: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

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

Page 20: Games at Bolton Joysticks Andrew Williams  A.Williams@bolton.ac.uk.

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 */}