[SDL] Multiple joystick detection

Josiah knowyotoe at yahoo.com
Fri May 14 22:20:06 PDT 2004


I have 2 joysticks attached to my computer which both definitely work with other
programs simultaneously. When calling SDL_NumJoysticks(), however, only one is
reported as existing. The following code is what I used.

    // SDL init
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
    {
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }
	
	// joystick detection
	SDL_JoystickEventState(SDL_ENABLE);
	
	printf("Number of joysticks: %i\n", SDL_NumJoysticks());
    printf("Opening joysticks:\n");
    for (int i = 0; i < SDL_NumJoysticks(); i++) 
    {
		joysticks.push_back(SDL_JoystickOpen(i));
        printf("  name: %s, axes: %d, buttons: %d, hats: %d, balls: %d\n", 
			SDL_JoystickName(i),
			SDL_JoystickNumAxes(joysticks[i]),
			SDL_JoystickNumButtons(joysticks[i]),
			SDL_JoystickNumHats(joysticks[i]),
			SDL_JoystickNumBalls(joysticks[i]));
    }

Support for multiple joysticks is crucial to the game I am working on. Any help
with this problem would be greatly appreciated.






More information about the SDL mailing list