Skip to content

Instantly share code, notes, and snippets.

@brettchalupa
Last active February 19, 2023 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettchalupa/758a6f98e2b6285dec8fa6568715ce67 to your computer and use it in GitHub Desktop.
Save brettchalupa/758a6f98e2b6285dec8fa6568715ce67 to your computer and use it in GitHub Desktop.
SDL2 Windows Rumble Tester

Windows variant of https://github.com/brettchalupa/sdl_rumble since that didn't work as expected on Windows like it does on macOS and Linux.

Notes to self:

  • Used Code::Blocks with mingw 64 bit
  • SDL 2.26.3
  • PS4 hint allows for DualShock 4 rumble without any special drivers

Learnings:

  • XInput rumbles without issue with SDL2
  • DualShock 4 (PS4 controller) requires a special hint to rumble with SDL2; curious what that does
  • Nintendo Switch via 8BitDo Pro rumbles, but it's faint and seems off
  • DirectInput doesn't rumble (I don't think it's supported)
#include <iostream>
#include <SDL.h>
#include <SDL_gamecontroller.h>
#include <SDL_joystick.h>
int main( int argc, char * argv[] )
{
SDL_SetHint(SDL_HINT_JOYSTICK_RAWINPUT, "0");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1");
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
printf("%i joysticks were found.\n\n", SDL_NumJoysticks());
int rumbleTime = 500;
for(int i=0; i < SDL_NumJoysticks(); i++ )
{
printf("rumbling: %s\n", SDL_GameControllerNameForIndex(i));
auto gamecontroller = SDL_GameControllerOpen(i);
if (gamecontroller == nullptr)
printf("ERROR\n");
SDL_GameControllerUpdate();
if (SDL_GameControllerRumble(gamecontroller, 0xFFFF * 3 / 4, 0xFFFF * 3 / 4, rumbleTime) != 0)
{
printf("Warning: Unable to start rumbling! %s\n", SDL_GetError());
}
SDL_Delay(rumbleTime);
}
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment