@@ -19,7 +19,7 @@ static int SDLJoystickEventHandlerWrapper(void* userdata, SDL_Event* event)
1919 return 0 ;
2020}
2121
22- SDLJoystick::SDLJoystick (bool init_SDL ) : registeredAsEventHandler(false ) {
22+ SDLJoystick::SDLJoystick (bool init_SDL, int njoy ) : registeredAsEventHandler(false ) {
2323 SDL_SetHint (SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, " 1" );
2424 if (init_SDL) {
2525 SDL_Init (SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
@@ -41,16 +41,19 @@ SDLJoystick::SDLJoystick(bool init_SDL ) : registeredAsEventHandler(false) {
4141 cout << " gamecontrollerdb.txt missing" << endl;
4242 }
4343 cout << " SUCCESS!" << endl;
44- setUpControllers ();
44+ // setUpControllers();
45+ njoy_ = njoy;
46+
4547}
4648
4749void SDLJoystick::setUpControllers () {
4850 int numjoys = SDL_NumJoysticks ();
49- for (int i = 0 ; i < numjoys; i++) {
50- setUpController (i);
51- }
52- if (controllers.size () > 0 ) {
53- cout << " pad 1 has been assigned to control pad: " << SDL_GameControllerName (controllers.front ()) << endl;
51+
52+ if (njoy_ < numjoys) {
53+ setUpController (njoy_);
54+ if (controllers.size () > 0 ) {
55+ cout << " pad 1 has been assigned to control pad: " << SDL_JoystickNameForIndex (njoy_) << endl;
56+ }
5457 }
5558}
5659
@@ -163,7 +166,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
163166 KeyInput key;
164167 key.flags = KEY_DOWN;
165168 key.keyCode = code;
166- key.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex (event. cbutton . which ) ;
169+ key.deviceId = DEVICE_ID_PAD_0;
167170 NativeKey (key);
168171 }
169172 }
@@ -175,7 +178,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
175178 KeyInput key;
176179 key.flags = KEY_UP;
177180 key.keyCode = code;
178- key.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex (event. cbutton . which ) ;
181+ key.deviceId = DEVICE_ID_PAD_0;
179182 NativeKey (key);
180183 }
181184 }
@@ -186,7 +189,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
186189 axis.value = event.caxis .value / 32767 .0f ;
187190 if (axis.value > 1 .0f ) axis.value = 1 .0f ;
188191 if (axis.value < -1 .0f ) axis.value = -1 .0f ;
189- axis.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex (event. caxis . which ) ;
192+ axis.deviceId = DEVICE_ID_PAD_0;
190193 NativeAxis (axis);
191194 break ;
192195 case SDL_CONTROLLERDEVICEREMOVED:
@@ -200,11 +203,9 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
200203 }
201204 break ;
202205 case SDL_CONTROLLERDEVICEADDED:
203- // for add events, "which" is the device index!
204- int prevNumControllers = controllers.size ();
205- setUpController (event.cdevice .which );
206- if (prevNumControllers == 0 && controllers.size () > 0 ) {
207- cout << " pad 1 has been assigned to control pad: " << SDL_GameControllerName (controllers.front ()) << endl;
206+ if (event.cdevice .which == njoy_) {
207+ setUpController (njoy_);
208+ cout << " pad 1 has been assigned to control pad: " << SDL_JoystickNameForIndex (njoy_) << endl;
208209 }
209210 break ;
210211 }
0 commit comments