src/haptic/SDL_haptic.c
changeset 5360 220d3af9121f
parent 4472 791b3256fb22
child 5362 074fc8730031
     1.1 --- a/src/haptic/SDL_haptic.c	Sun Feb 20 10:42:51 2011 -0800
     1.2 +++ b/src/haptic/SDL_haptic.c	Sun Feb 20 10:54:44 2011 -0800
     1.3 @@ -143,6 +143,7 @@
     1.4  
     1.5      /* Initialize the haptic device */
     1.6      SDL_memset(haptic, 0, (sizeof *haptic));
     1.7 +    haptic->rumble_id = -1;
     1.8      haptic->index = device_index;
     1.9      if (SDL_SYS_HapticOpen(haptic) < 0) {
    1.10          SDL_free(haptic);
    1.11 @@ -292,6 +293,7 @@
    1.12  
    1.13      /* Initialize the haptic device */
    1.14      SDL_memset(haptic, 0, sizeof(SDL_Haptic));
    1.15 +    haptic->rumble_id = -1;
    1.16      if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
    1.17          SDL_free(haptic);
    1.18          return NULL;
    1.19 @@ -706,3 +708,110 @@
    1.20  
    1.21      return SDL_SYS_HapticStopAll(haptic);
    1.22  }
    1.23 +
    1.24 +static int
    1.25 +SDL_HapticRumbleCreate(SDL_HapticEffect * efx)
    1.26 +{
    1.27 +   SDL_memset(efx, 0, sizeof(SDL_HapticEffect));
    1.28 +   efx->type = SDL_HAPTIC_SINE;
    1.29 +   efx->periodic.period = 1000;
    1.30 +   efx->periodic.magnitude = 0x4000;
    1.31 +   efx->periodic.length = 5000;
    1.32 +   efx->periodic.attack_length = 0;
    1.33 +   efx->periodic.fade_length = 0;
    1.34 +}
    1.35 +
    1.36 +/*
    1.37 + * Checks to see if rumble is supported.
    1.38 + */
    1.39 +int
    1.40 +SDL_HapticRumbleSupported(SDL_Haptic * haptic)
    1.41 +{
    1.42 +    SDL_HapticEffect efx;
    1.43 +
    1.44 +    if (!ValidHaptic(haptic)) {
    1.45 +        return -1;
    1.46 +    }
    1.47 +
    1.48 +    SDL_HapticRumbleCreate(&efx);
    1.49 +    return SDL_HapticEffectSupported(haptic, &efx);
    1.50 +}
    1.51 +
    1.52 +/*
    1.53 + * Initializes the haptic device for simple rumble playback.
    1.54 + */
    1.55 +int
    1.56 +SDL_HapticRumbleInit(SDL_Haptic * haptic)
    1.57 +{
    1.58 +    if (!ValidHaptic(haptic)) {
    1.59 +        return -1;
    1.60 +    }
    1.61 +
    1.62 +    /* Already allocated. */
    1.63 +    if (haptic->rumble_id >= 0) {
    1.64 +        return 0;
    1.65 +    }
    1.66 +
    1.67 +    /* Copy over. */
    1.68 +    SDL_HapticRumbleCreate(&haptic->rumble_effect);
    1.69 +    haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect);
    1.70 +    if (haptic->rumble_id >= 0) {
    1.71 +        return 0;
    1.72 +    }
    1.73 +    return -1;
    1.74 +}
    1.75 +
    1.76 +/*
    1.77 + * Runs simple rumble on a haptic device
    1.78 + */
    1.79 +int
    1.80 +SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length)
    1.81 +{
    1.82 +    int ret;
    1.83 +    SDL_HapticPeriodic *efx;
    1.84 +
    1.85 +    if (!ValidHaptic(haptic)) {
    1.86 +        return -1;
    1.87 +    }
    1.88 +
    1.89 +    if (haptic->rumble_id < 0) {
    1.90 +        SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
    1.91 +        return -1;
    1.92 +    }
    1.93 +
    1.94 +    /* Clamp strength. */
    1.95 +    if (strength > 1.0f) {
    1.96 +        strength = 1.0f;
    1.97 +    }
    1.98 +    else if (strength < 0.0f) {
    1.99 +        strength = 0.0f;
   1.100 +    }
   1.101 +
   1.102 +    /* New effect. */
   1.103 +    efx = &haptic->rumble_effect.periodic;
   1.104 +    efx->magnitude = (Sint16)(32767.0f*strength);
   1.105 +    efx->length = length;
   1.106 +    ret = SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect);
   1.107 +
   1.108 +    return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1);
   1.109 +}
   1.110 +
   1.111 +/*
   1.112 + * Stops the simple rumble on a haptic device.
   1.113 + */
   1.114 +int
   1.115 +SDL_HapticRumbleStop(SDL_Haptic * haptic)
   1.116 +{
   1.117 +    if (!ValidHaptic(haptic)) {
   1.118 +        return -1;
   1.119 +    }
   1.120 +
   1.121 +    if (haptic->rumble_id < 0) {
   1.122 +        SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
   1.123 +        return -1;
   1.124 +    }
   1.125 +
   1.126 +    return SDL_HapticStopEffect(haptic, haptic->rumble_id);
   1.127 +}
   1.128 +
   1.129 +