Advertisement
Guest User

SDL2 rumble patch

a guest
Sep 8th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.09 KB | None | 0 0
  1. --- old/Joystick_SDL.cpp    2013-08-31 23:33:41.000000000 +0200
  2. +++ new/Joystick_SDL.cpp    2015-09-08 10:58:41.000000000 +0200
  3. @@ -31,11 +31,14 @@
  4.   ~Joystick_SDL();
  5.  
  6.   void UpdateInternal(void);
  7. +
  8. + virtual void SetRumble(uint8 weak_intensity, uint8 strong_intensity);
  9.  
  10.   virtual unsigned HatToButtonCompat(unsigned hat);
  11.  
  12.   private:
  13.   SDL_Joystick *sdl_joy;
  14. + SDL_Haptic *sdl_haptic;
  15.   unsigned sdl_num_axes;
  16.   unsigned sdl_num_hats;
  17.   unsigned sdl_num_balls;
  18. @@ -47,6 +50,32 @@
  19.   return(sdl_num_buttons + (hat * 4));
  20.  }
  21.  
  22. +void Joystick_SDL::SetRumble(uint8 weak_intensity, uint8 strong_intensity)
  23. +{
  24. + if (sdl_haptic == NULL) {
  25. +  return;
  26. + }
  27. +
  28. + static int effectID = -1;
  29. +
  30. + float intensity = 0.3 * weak_intensity + 0.7 * strong_intensity;
  31. +
  32. + if (effectID != -1) {
  33. +  SDL_HapticDestroyEffect(sdl_haptic, effectID);
  34. +  effectID = -1;
  35. + }
  36. +
  37. + if (intensity > 0) {
  38. +  SDL_HapticEffect effect;
  39. +  memset( &effect, 0, sizeof(SDL_HapticEffect) );
  40. +  effect.type = SDL_HAPTIC_CONSTANT;
  41. +  effect.constant.length = SDL_HAPTIC_INFINITY;
  42. +  effect.constant.level = intensity / 255.0 * 32767;
  43. +  effectID = SDL_HapticNewEffect(sdl_haptic, &effect);
  44. +  SDL_HapticRunEffect(sdl_haptic, effectID, SDL_HAPTIC_INFINITY);
  45. + }
  46. +}
  47. +
  48.  Joystick_SDL::Joystick_SDL(unsigned index) : sdl_joy(NULL)
  49.  {
  50.   sdl_joy = SDL_JoystickOpen(index);
  51. @@ -72,10 +101,21 @@
  52.   axis_state.resize(num_axes);
  53.   rel_axis_state.resize(num_rel_axes);
  54.   button_state.resize(num_buttons);
  55. +    
  56. + sdl_haptic = SDL_HapticOpenFromJoystick(sdl_joy);
  57. + if (sdl_haptic != NULL && (SDL_HapticQuery(sdl_haptic) & SDL_HAPTIC_CONSTANT)) {
  58. +  MDFNI_printf("Rumble supported by: %s", name);
  59. + } else {
  60. +  sdl_haptic = NULL;
  61. + }
  62.  }
  63.  
  64.  Joystick_SDL::~Joystick_SDL()
  65.  {
  66. + if (sdl_haptic) {
  67. +  SDL_HapticClose(sdl_haptic);
  68. +  sdl_haptic = NULL;
  69. + }
  70.   if(sdl_joy)
  71.   {
  72.    SDL_JoystickClose(sdl_joy);
  73. @@ -136,7 +176,7 @@
  74.  
  75.  JoystickDriver_SDL::JoystickDriver_SDL()
  76.  {
  77. - SDL_InitSubSystem(SDL_INIT_JOYSTICK);
  78. + SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
  79.  
  80.   for(int n = 0; n < SDL_NumJoysticks(); n++)
  81.   {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement