The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
PCSXR: Properly? implmented Haptic fails
quequotion


Joined: 08 Aug 2014
Posts: 6
PCSXR has implemented Haptic almost exactly as it's done in the testrumble.c example.

I cannot find any problem with this code, but it fails to play the effect with the error:
Code:
Haptic: Error updating the effect: Invalid argument


Code:
void JoyInitHaptic()
{
#if SDL_VERSION_ATLEAST(2,0,0)
   uint8_t i;
    //unsigned int haptic_query = 0;
   for (i = 0; i < 2; i++)
   {
      SDL_Joystick *curJoy = g.PadState[i].JoyDev;
      if (!curJoy && g.PadState[i].GCDev)
                {
         curJoy = SDL_GameControllerGetJoystick(g.PadState[i].GCDev);
      }
      if (SDL_JoystickIsHaptic(curJoy))
      {
         if (g.PadState[i].haptic != NULL)
         {
            SDL_HapticClose(g.PadState[i].haptic);
            g.PadState[i].haptic = NULL;
         }

         g.PadState[i].haptic = SDL_HapticOpenFromJoystick(curJoy);
         if (g.PadState[i].haptic == NULL)
            continue;

         if (SDL_HapticRumbleSupported(g.PadState[i].haptic) == SDL_FALSE)
         {
            printf("\nRumble not supported\n");
            g.PadState[i].haptic = NULL;
            continue;
         }

         if (SDL_HapticRumbleInit(g.PadState[i].haptic) != 0)
         {
            printf("\nFailed to initialize rumble: %s\n", SDL_GetError());
            g.PadState[i].haptic = NULL;
            continue;
         }
      }
   }
#endif
}

int JoyHapticRumble(int pad, uint32_t low, uint32_t high)
{
#if SDL_VERSION_ATLEAST(2,0,0)
  float mag;

  if (g.PadState[pad].haptic)
  {

    /* Stop the effect if it was playing. */
    SDL_HapticRumbleStop(g.PadState[pad].haptic);

    mag = ((high * 2 + low) / 6) / 127.5;
    printf("low: %d high: %d mag: %f\n", low, high, mag); //debug

    if (SDL_HapticRumblePlay(g.PadState[pad].haptic, mag, 500) != 0)
    {
      printf("\nFailed to play rumble: %s\n", SDL_GetError());
      return 1;
    }
  }
#endif
  return 0;
}