@@ -57,6 +57,7 @@ static KeyboardDef KbdDefs = {
5757};
5858
5959static SDL_Joystick *Joystick;
60+ static SDL_GameController *GameController;
6061int JoyNumButtons;
6162static int JoyNumHats;
6263
@@ -143,21 +144,22 @@ INL_GetMouseButtons(void)
143144// joystick (from +/-127)
144145//
145146// /////////////////////////////////////////////////////////////////////////
146- void IN_GetJoyDelta (int *dx,int *dy)
147+ void IN_GetJoyDelta (int *dx,int *dy, SDL_GameControllerAxis xaxis, SDL_GameControllerAxis yaxis )
147148{
148- if (!Joystick )
149+ if (!GameController )
149150 {
150151 *dx = *dy = 0 ;
151152 return ;
152153 }
153154
154155 SDL_JoystickUpdate ();
156+ SDL_GameControllerUpdate ();
155157#ifdef _arch_dreamcast
156158 int x = 0 ;
157159 int y = 0 ;
158160#else
159- int x = SDL_JoystickGetAxis (Joystick, 0 ) >> 8 ;
160- int y = SDL_JoystickGetAxis (Joystick, 1 ) >> 8 ;
161+ int x = SDL_GameControllerGetAxis (GameController, xaxis ) >> 8 ;
162+ int y = SDL_GameControllerGetAxis (GameController, yaxis ) >> 8 ;
161163#endif
162164
163165 if (param_joystickhat != -1 )
@@ -179,6 +181,10 @@ void IN_GetJoyDelta(int *dx,int *dy)
179181 else if (y > 127 ) y = 127 ;
180182 }
181183
184+ // Apply expo
185+ x = (int ) (pow (x / 127.0 , JOYEXPO) * (x > 0 ? 127 : -127 ));
186+ y = (int ) (pow (y / 127.0 , JOYEXPO) * (y > 0 ? 127 : -127 ));
187+
182188 *dx = x;
183189 *dy = y;
184190}
@@ -199,8 +205,9 @@ void IN_GetJoyFineDelta(int *dx, int *dy)
199205 }
200206
201207 SDL_JoystickUpdate ();
202- int x = SDL_JoystickGetAxis (Joystick, 0 );
203- int y = SDL_JoystickGetAxis (Joystick, 1 );
208+ SDL_GameControllerUpdate ();
209+ int x = SDL_GameControllerGetAxis (GameController, SDL_CONTROLLER_AXIS_LEFTX);
210+ int y = SDL_GameControllerGetAxis (GameController, SDL_CONTROLLER_AXIS_LEFTY);
204211
205212 if (x < -128 ) x = -128 ;
206213 else if (x > 127 ) x = 127 ;
@@ -222,19 +229,33 @@ void IN_GetJoyFineDelta(int *dx, int *dy)
222229
223230int IN_JoyButtons ()
224231{
225- if (!Joystick ) return 0 ;
232+ if (!GameController ) return 0 ;
226233
227- SDL_JoystickUpdate ();
234+ SDL_GameControllerUpdate ();
228235
236+ // Read triggers in as buttons
237+ int leftTrigger = (SDL_GameControllerGetAxis (GameController, SDL_CONTROLLER_AXIS_TRIGGERLEFT) >> 8 ) > JOYDEADZONE;
238+ int rightTrigger = (SDL_GameControllerGetAxis (GameController, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) >> 8 ) > JOYDEADZONE;
239+
240+ // Read in each button in the order we expect to define it
229241 int res = 0 ;
230- for (int i = 0 ; i < JoyNumButtons && i < 32 ; i++)
231- res |= SDL_JoystickGetButton (Joystick, i) << i;
242+ res |= (SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_A) || rightTrigger) << 0 ;
243+ res |= (SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_B) || leftTrigger) << 1 ;
244+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_X) << 2 ;
245+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_Y) << 3 ;
246+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_LEFTSHOULDER) << 4 ;
247+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) << 5 ;
248+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_LEFTSTICK) << 6 ;
249+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_RIGHTSTICK) << 7 ;
250+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_BACK) << 8 ;
251+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_START) << 9 ;
252+ res |= SDL_GameControllerGetButton (GameController, SDL_CONTROLLER_BUTTON_GUIDE) << 10 ;
232253 return res;
233254}
234255
235256boolean IN_JoyPresent ()
236257{
237- return Joystick != NULL ;
258+ return GameController != NULL ;
238259}
239260
240261static void processEvent (SDL_Event *event)
@@ -397,6 +418,8 @@ IN_Startup(void)
397418 if (param_joystickindex >= 0 && param_joystickindex < SDL_NumJoysticks ())
398419 {
399420 Joystick = SDL_JoystickOpen (param_joystickindex);
421+ GameController = SDL_GameControllerOpen (param_joystickindex);
422+
400423 if (Joystick)
401424 {
402425 JoyNumButtons = SDL_JoystickNumButtons (Joystick);
@@ -441,6 +464,9 @@ IN_Shutdown(void)
441464 if (Joystick)
442465 SDL_JoystickClose (Joystick);
443466
467+ if (GameController)
468+ SDL_GameControllerClose (GameController);
469+
444470 IN_Started = false ;
445471}
446472
@@ -512,6 +538,10 @@ IN_ReadControl(int player,ControlInfo *info)
512538 info->button1 = (buttons & (1 << 1 )) != 0 ;
513539 info->button2 = (buttons & (1 << 2 )) != 0 ;
514540 info->button3 = (buttons & (1 << 3 )) != 0 ;
541+ // info->button0 = (buttons & (1 << 0)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_A);
542+ // info->button1 = (buttons & (1 << 1)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_B);
543+ // info->button2 = (buttons & (1 << 2)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_X);
544+ // info->button3 = (buttons & (1 << 3)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_Y);
515545 info->dir = DirTable[((my + 1 ) * 3 ) + (mx + 1 )];
516546}
517547
0 commit comments