Wiki Page Content

Differences between revisions 7 and 8
Revision 7 as of 2013-07-22 20:55:54
Size: 2047
Comment: Updated to use SDL_JoystickNameForIndex.
Revision 8 as of 2013-08-10 19:47:51
Size: 2101
Comment: Fixed function name and formatting in example.
Deletions are marked like this. Additions are marked like this.
Line 29: Line 29:
if(SDL_NumJoysticks()>0){
  // Open joystick
  joy=SDL_JoystickOpen(0);
if (SDL_NumJoysticks() > 0) {
    // Open joystick
    joy = SDL_JoystickOpen(0);

    if (joy) {
        printf("Opened Joystick 0\n");
        printf("Name: %s\n", SDL_JoystickNameForIndex(0));
        printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
        printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
        printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
    } else {
        printf("Couldn't open Joystick 0\n");
    }
Line 33: Line 43:
  if(joy)
  {
    printf("Opened Joystick 0\n");
    printf("Name: %s\n", SDL_SDL_JoystickNameForIndex(0));
    printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
    printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
    printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
  }
  else
    printf("Couldn't open Joystick 0\n");
  
  // Close if opened
  if(SDL_JoystickGetAttached(joy))
    SDL_JoystickClose(joy);
    // Close if opened
    if (SDL_JoystickGetAttached(joy)) {
        SDL_JoystickClose(joy);
    }

DRAFT

SDL_JoystickOpen

Use this function to open a joystick for use.

Syntax

SDL_Joystick* SDL_JoystickOpen(int device_index)

Function Parameters

device_index

the index of the joystick to query

Return Value

Returns a joystick identifier or NULL if an error occurred; call SDL_GetError() for more information.

Code Examples

SDL_Joystick *joy;

// Initialize the joystick subsystem
SDL_InitSubSystem(SDL_INIT_JOYSTICK);

// Check for joystick
if (SDL_NumJoysticks() > 0) {
    // Open joystick
    joy = SDL_JoystickOpen(0);

    if (joy) {
        printf("Opened Joystick 0\n");
        printf("Name: %s\n", SDL_JoystickNameForIndex(0));
        printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
        printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
        printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
    } else {
        printf("Couldn't open Joystick 0\n");
    }
  
    // Close if opened
    if (SDL_JoystickGetAttached(joy)) {
        SDL_JoystickClose(joy);
    }
}

Remarks

The index passed as an argument refers to the N'th joystick on the system. This index is the value which will identify this joystick in future joystick events. green

*
Remember to initialize the joystick subsystem
* green

green


CategoryAPI, CategoryJoystick

None: SDL_JoystickOpen (last edited 2015-05-31 18:48:51 by PhilippWiesemann)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit