Question

I tried to use SDL_JoystickOpened() in a SDL-2.0 code, but I got the error message SDL_JoystickOpened was not declared in this scope.

I've searched and found that SDL_JoystickOpened() no longer exists in SDL-2.0, but I didn't find the function that replaced it. Can anyone please tell me how can I replace SDL_JoystickOpened() in this piece of code:

SDL_Joystick *joy = SDL_JoystickOpen(0);
if (SDL_JoystickOpened(0) == 1)
{
    // do something
}

Thanks.

Was it helpful?

Solution

The documentation says the SDL_JoystickOpen() call returns a NULL pointer if it fails so just do a check for that:

SDL_Joystick *joy = SDL_JoystickOpen(0);
if (joy) {
    // joy is a valid pointer so do stuff
}

OTHER TIPS

An alternative in 2.0 could be SDL_JoystickGetAttached

There is also an example in test/testjoystick.c of SDL2 source-code (at least in SDL2-2.0.1).


Also note the Joysticks section of the SDL 1.2 to 2.0 Migration Guide. It gives various information on new features and ways in 2.0. It also links to the GameController section with e.g. SDL_GameControllerGetJoystick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top