Sign in to follow this  

Why is SDL Cannot Open JoyStick for PS2 Controller

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using the psx/usb connecter to play this game i made in SDL using the psx controller the thing tell me it find one controller connected. but when i try to open it it cannot open it also if i try to get the name of the controller it returns a bunch of jiberish. So does it support it or not i want to make sure.

Share this post


Link to post
Share on other sites
And what platform are you running SDL on? I'm running Linux and my current SDL code fails to recognize my USB Logitech Wingman Gamepad, but on my older linux system the exact same gamepad was recognized (and worked with no tweaking) on SNES9x, which I believe uses SDL.

Share this post


Link to post
Share on other sites
I am sorry for not repplying for so long. The way that i fixed what that when i try to get the joystick name. it was not returning it to me so what i did since to Initialize the Joystick you have to pass and data type int so it know which port is open i just pass the whatever SDL_NumJoystickOpen() returns -1 this will set the correct port because the thing is that SDL will return 1 when you Querry for the controller if it is in port 1, so you migh think that to Initialize it you have to set it to 1 but don't forget that everything starts at 0 so if SDl returns 1 the port you have to Open then is 0 so the Best way to do it is Set SDL_OpenJoystick(SDL_NumJoystickOpen()-1); This will defintely work. If it doens't then your controller is not supported. Also when you Initialize SDL make sure you pass SDL_INIT_JOYSTICK as one of the parameters otherwise this whole thing will fail.

Share this post


Link to post
Share on other sites
So it was just an off-by-one error then. Well that's no fun. By the way calling SDL_JoystickOpen(SDL_NumJoysticks()-1); is not a good idea, because if there are no joysticks on your system then you're going to be passing -1 to the SDL_JoystickOpen function, which is a very bad thing. :P Here's some joystick debug code I wrote up for my project. It just prints out info about all the joysticks that SDL has recognized on the system, nothing special.


SDL_Joystick *js_test;
int js_num = SDL_NumJoysticks(); // Get the number of joysticks available
cout << "SDL has recognized " << js_num << " joystick(s) on this system." << endl;
for (int i = 0; i < js_num; i++) { // Print out information about each joystick
js_test = SDL_JoystickOpen(i);
if (js_test == NULL)
cout << "ERROR: SDL was unable to open joystick #" << i << endl;
else {
cout << "Joystick #" << i << endl;
cout << ">Name: " << SDL_JoystickName(i) << endl;
cout << ">Axes: " << SDL_JoystickNumAxes(js_test) << endl;
cout << ">Buttons: " << SDL_JoystickNumButtons(js_test) << endl;
cout << ">Trackballs: " << SDL_JoystickNumBalls(js_test) << endl;
cout << ">Hat Switches: " << SDL_JoystickNumHats(js_test) << endl;
SDL_JoystickClose(js_test);
}
}

Share this post


Link to post
Share on other sites
Hmmm, I can never get SDL to recognize my controller, even using your code...

In the control panel for windows XP it comes up as an MP 866-Dual USB Joypad and works fine.

Edit: Never mind, was forgetting to init the joystick subsystem in that example, that wasn't my problem before but at least it works now.

Share this post


Link to post
Share on other sites

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this