The SDL haptic subsystem manages haptic (force feedback) devices.
The basic usage is as follows:
Simple rumble example:
SDL_Haptic *haptic = NULL;
// Open the device
SDL_HapticID *haptics = SDL_GetHaptics(NULL);if (haptics) {
0]);
haptic = SDL_OpenHaptic(haptics[
SDL_free(haptics);
}if (haptic == NULL)
return;
// Initialize simple rumble
if (!SDL_InitHapticRumble(haptic))
return;
// Play effect at 50% strength for 2 seconds
if (!SDL_PlayHapticRumble(haptic, 0.5, 2000))
return;
2000);
SDL_Delay(
// Clean up
SDL_CloseHaptic(haptic);
Complete example:
SDL_bool test_haptic(SDL_Joystick *joystick)
{
SDL_Haptic *haptic;
SDL_HapticEffect effect;int effect_id;
// Open the device
haptic = SDL_OpenHapticFromJoystick(joystick);if (haptic == NULL) return SDL_FALSE; // Most likely joystick isn't haptic
// See if it can do sine waves
if ((SDL_GetHapticFeatures(haptic) & SDL_HAPTIC_SINE)==0) {
// No sine effect
SDL_CloseHaptic(haptic); return SDL_FALSE;
}
// Create the effect
0, sizeof(SDL_HapticEffect)); // 0 is safe default
SDL_memset(&effect,
effect.type = SDL_HAPTIC_SINE;// Polar coordinates
effect.periodic.direction.type = SDL_HAPTIC_POLAR; 0] = 18000; // Force comes from south
effect.periodic.direction.dir[1000; // 1000 ms
effect.periodic.period = 20000; // 20000/32767 strength
effect.periodic.magnitude = 5000; // 5 seconds long
effect.periodic.length = 1000; // Takes 1 second to get max strength
effect.periodic.attack_length = 1000; // Takes 1 second to fade away
effect.periodic.fade_length =
// Upload the effect
effect_id = SDL_CreateHapticEffect(haptic, &effect);
// Test the effect
1);
SDL_RunHapticEffect(haptic, effect_id, 5000); // Wait for the effect to finish
SDL_Delay(
// We destroy the effect, although closing the device also does this
SDL_DestroyHapticEffect(haptic, effect_id);
// Close the device
SDL_CloseHaptic(haptic);
return SDL_TRUE; // Success
}
Note that the SDL haptic subsystem is not thread-safe.