Ignore axis jitter when mapping controllers
authorSam Lantinga <slouken@libsdl.org>
Thu, 05 Dec 2019 13:18:56 -0800
changeset 13315ddca1f52d44d
parent 13314 2d936252374c
child 13316 c0c751a347e0
Ignore axis jitter when mapping controllers
test/controllermap.c
     1.1 --- a/test/controllermap.c	Thu Dec 05 13:18:53 2019 -0800
     1.2 +++ b/test/controllermap.c	Thu Dec 05 13:18:56 2019 -0800
     1.3 @@ -143,6 +143,7 @@
     1.4  typedef struct
     1.5  {
     1.6      SDL_bool m_bMoving;
     1.7 +    int m_nLastValue;
     1.8      int m_nStartingValue;
     1.9      int m_nFarthestValue;
    1.10  } AxisState;
    1.11 @@ -461,14 +462,20 @@
    1.12                  break;
    1.13              case SDL_JOYAXISMOTION:
    1.14                  if (event.jaxis.which == nJoystickID) {
    1.15 +                    const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80;  /* ShanWan PS3 controller needed 96 */
    1.16                      AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis];
    1.17                      int nValue = event.jaxis.value;
    1.18                      int nCurrentDistance, nFarthestDistance;
    1.19                      if (!pAxisState->m_bMoving) {
    1.20                          Sint16 nInitialValue;
    1.21                          pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, event.jaxis.axis, &nInitialValue);
    1.22 +                        pAxisState->m_nLastValue = nInitialValue;
    1.23                          pAxisState->m_nStartingValue = nInitialValue;
    1.24                          pAxisState->m_nFarthestValue = nInitialValue;
    1.25 +                    } else if (SDL_abs(nValue - pAxisState->m_nLastValue) <= MAX_ALLOWED_JITTER) {
    1.26 +                        break;
    1.27 +                    } else {
    1.28 +                        pAxisState->m_nLastValue = nValue;
    1.29                      }
    1.30                      nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
    1.31                      nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);