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
}
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.
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
SDL_JoystickOpened(int index)
(The old way SDL 1.2)
Determines whether a joystick has already been opened within the application. index refers to the N'th joystick on the system.
SDL_JoystickGetAttached(SDL_Joystick* joystick)
(The new way SDL 2.0)
Returns SDL_TRUE if the joystick has been opened, SDL_FALSE if it has not; call SDL_GetError() for more information
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.