SDL 1 or 2, there is no SDL_JoystickOpened in SDL 2
I’m reading the same book as you. It’s from June 2013 when SDL2 had not yet been released; clearly the API changed.
Here’s how I did it:
Code:
void InputHandler::initialiseJoysticks()
{
if ( SDL_WasInit( SDL_INIT_JOYSTICK ) == 0 )
SDL_InitSubSystem( SDL_INIT_JOYSTICK );
if ( SDL_NumJoysticks() > 0 )
{
for ( int i = 0; i < SDL_NumJoysticks(); i++ )
{
SDL_Joystick * joy = SDL_JoystickOpen( i );
if ( SDL_JoystickGetAttached( joy ) == 1 )
{
m_joysticks.push_back( joy );
m_joystickValues.push_back( std::make_pair( new Vector2D( 0, 0 ), new Vector2D( 0, 0 ) ) ); // add our pair
std::vector<bool> tempButtons;
for ( int j = 0; j != SDL_JoystickNumButtons( joy ); j++ )
tempButtons.push_back( false );
m_buttonStates.push_back( tempButtons );
}
else
std::cout << SDL_GetError();
}
SDL_JoystickEventState( SDL_ENABLE );
m_bJoysticksInitialised = true;
}
std::cout << "Initialised " << m_joysticks.size() << " joystick(s)" << std::endl;
}