src/haptic/SDL_haptic.c
changeset 7621 5caa5fb3deb6
parent 7536 5b6e09ec3dd3
child 7719 31b5f9ff36ca
     1.1 --- a/src/haptic/SDL_haptic.c	Sat Aug 10 13:20:45 2013 -0400
     1.2 +++ b/src/haptic/SDL_haptic.c	Sat Aug 10 13:38:09 2013 -0400
     1.3 @@ -23,7 +23,7 @@
     1.4  #include "SDL_syshaptic.h"
     1.5  #include "SDL_haptic_c.h"
     1.6  #include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
     1.7 -
     1.8 +#include "SDL_assert.h"
     1.9  
    1.10  Uint8 SDL_numhaptics = 0;
    1.11  SDL_Haptic **SDL_haptics = NULL;
    1.12 @@ -723,32 +723,18 @@
    1.13      return SDL_SYS_HapticStopAll(haptic);
    1.14  }
    1.15  
    1.16 -static void
    1.17 -SDL_HapticRumbleCreate(SDL_HapticEffect * efx)
    1.18 -{
    1.19 -   SDL_memset(efx, 0, sizeof(SDL_HapticEffect));
    1.20 -   efx->type = SDL_HAPTIC_SINE;
    1.21 -   efx->periodic.period = 1000;
    1.22 -   efx->periodic.magnitude = 0x4000;
    1.23 -   efx->periodic.length = 5000;
    1.24 -   efx->periodic.attack_length = 0;
    1.25 -   efx->periodic.fade_length = 0;
    1.26 -}
    1.27 -
    1.28  /*
    1.29   * Checks to see if rumble is supported.
    1.30   */
    1.31  int
    1.32  SDL_HapticRumbleSupported(SDL_Haptic * haptic)
    1.33  {
    1.34 -    SDL_HapticEffect efx;
    1.35 -
    1.36      if (!ValidHaptic(haptic)) {
    1.37          return -1;
    1.38      }
    1.39  
    1.40 -    SDL_HapticRumbleCreate(&efx);
    1.41 -    return SDL_HapticEffectSupported(haptic, &efx);
    1.42 +    /* Most things can use SINE, but XInput only has LEFTRIGHT. */
    1.43 +    return ((haptic->supported & (SDL_HAPTIC_SINE|SDL_HAPTIC_LEFTRIGHT)) != 0);
    1.44  }
    1.45  
    1.46  /*
    1.47 @@ -757,6 +743,8 @@
    1.48  int
    1.49  SDL_HapticRumbleInit(SDL_Haptic * haptic)
    1.50  {
    1.51 +    SDL_HapticEffect *efx = &haptic->rumble_effect;
    1.52 +
    1.53      if (!ValidHaptic(haptic)) {
    1.54          return -1;
    1.55      }
    1.56 @@ -766,8 +754,23 @@
    1.57          return 0;
    1.58      }
    1.59  
    1.60 -    /* Copy over. */
    1.61 -    SDL_HapticRumbleCreate(&haptic->rumble_effect);
    1.62 +    SDL_zerop(efx);
    1.63 +    if (haptic->supported & SDL_HAPTIC_SINE) {
    1.64 +        efx->type = SDL_HAPTIC_SINE;
    1.65 +        efx->periodic.period = 1000;
    1.66 +        efx->periodic.magnitude = 0x4000;
    1.67 +        efx->periodic.length = 5000;
    1.68 +        efx->periodic.attack_length = 0;
    1.69 +        efx->periodic.fade_length = 0;
    1.70 +    } else if (haptic->supported & SDL_HAPTIC_LEFTRIGHT) {  /* XInput? */
    1.71 +        efx->type = SDL_HAPTIC_LEFTRIGHT;
    1.72 +        efx->leftright.length = 5000;
    1.73 +        efx->leftright.large_magnitude = 0x4000;
    1.74 +        efx->leftright.small_magnitude = 0x4000;
    1.75 +    } else {
    1.76 +        return SDL_SetError("Device doesn't support rumble");
    1.77 +    }
    1.78 +
    1.79      haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect);
    1.80      if (haptic->rumble_id >= 0) {
    1.81          return 0;
    1.82 @@ -781,7 +784,8 @@
    1.83  int
    1.84  SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length)
    1.85  {
    1.86 -    SDL_HapticPeriodic *efx;
    1.87 +    SDL_HapticEffect *efx;
    1.88 +    Sint16 magnitude;
    1.89  
    1.90      if (!ValidHaptic(haptic)) {
    1.91          return -1;
    1.92 @@ -794,15 +798,22 @@
    1.93      /* Clamp strength. */
    1.94      if (strength > 1.0f) {
    1.95          strength = 1.0f;
    1.96 -    }
    1.97 -    else if (strength < 0.0f) {
    1.98 +    } else if (strength < 0.0f) {
    1.99          strength = 0.0f;
   1.100      }
   1.101 +    magnitude = (Sint16)(32767.0f*strength);
   1.102  
   1.103 -    /* New effect. */
   1.104 -    efx = &haptic->rumble_effect.periodic;
   1.105 -    efx->magnitude = (Sint16)(32767.0f*strength);
   1.106 -    efx->length = length;
   1.107 +    efx = &haptic->rumble_effect;
   1.108 +    if (efx->type == SDL_HAPTIC_SINE) {
   1.109 +        efx->periodic.magnitude = magnitude;
   1.110 +        efx->periodic.length = length;
   1.111 +    } else if (efx->type == SDL_HAPTIC_LEFTRIGHT) {
   1.112 +        efx->leftright.small_magnitude = efx->leftright.large_magnitude = magnitude;
   1.113 +        efx->leftright.length = length;
   1.114 +    } else {
   1.115 +        SDL_assert(0 && "This should have been caught elsewhere");
   1.116 +    }
   1.117 +
   1.118      if (SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect) < 0) {
   1.119          return -1;
   1.120      }
   1.121 @@ -827,4 +838,3 @@
   1.122      return SDL_HapticStopEffect(haptic, haptic->rumble_id);
   1.123  }
   1.124  
   1.125 -