Skip to content

Commit

Permalink
[rcore] Adds implementation to SetGamepadVibration() on `PLATFORM_W…
Browse files Browse the repository at this point in the history
…EB` and updates it on `PLATFORM_DESKTOP_SDL` to handle duration (#4410)

* Updates SetGamepadVibration()

* Handle MAX_GAMEPAD_VIBRATION_TIME

* Revert low/high parameters back to left/rightMotor

* Fix missin semicolon

* Convert duration to seconds

* Add SetGamepadVibration() implementation to PLATFORM_WEB
  • Loading branch information
asdqwe authored Oct 21, 2024
1 parent ae150e9 commit f8f6aa0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/platforms/rcore_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ int SetGamepadMappings(const char *mappings)
}

// Set gamepad vibration
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor)
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
{
TRACELOG(LOG_WARNING, "GamepadSetVibration() not implemented on target platform");
}
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/rcore_desktop_glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ int SetGamepadMappings(const char *mappings)
}

// Set gamepad vibration
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor)
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
{
TRACELOG(LOG_WARNING, "GamepadSetVibration() not available on target platform");
}
Expand Down
18 changes: 9 additions & 9 deletions src/platforms/rcore_desktop_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -953,17 +953,17 @@ int SetGamepadMappings(const char *mappings)
}

// Set gamepad vibration
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor)
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
{
// Limit input values to between 0.0f and 1.0f
leftMotor = (0.0f > leftMotor)? 0.0f : leftMotor;
rightMotor = (0.0f > rightMotor)? 0.0f : rightMotor;
leftMotor = (1.0f < leftMotor)? 1.0f : leftMotor;
rightMotor = (1.0f < rightMotor)? 1.0f : rightMotor;

if (IsGamepadAvailable(gamepad))
if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (duration > 0.0f))
{
SDL_GameControllerRumble(platform.gamepad[gamepad], (Uint16)(leftMotor*65535.0f), (Uint16)(rightMotor*65535.0f), (Uint32)(MAX_GAMEPAD_VIBRATION_TIME*1000.0f));
if (leftMotor < 0.0f) leftMotor = 0.0f;
if (leftMotor > 1.0f) leftMotor = 1.0f;
if (rightMotor < 0.0f) rightMotor = 0.0f;
if (rightMotor > 1.0f) rightMotor = 1.0f;
if (duration > MAX_GAMEPAD_VIBRATION_TIME) duration = MAX_GAMEPAD_VIBRATION_TIME;

SDL_GameControllerRumble(platform.gamepad[gamepad], (Uint16)(leftMotor*65535.0f), (Uint16)(rightMotor*65535.0f), (Uint32)(duration*1000.0f));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/platforms/rcore_drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ int SetGamepadMappings(const char *mappings)
}

// Set gamepad vibration
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor)
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
{
TRACELOG(LOG_WARNING, "GamepadSetVibration() not implemented on target platform");
}
Expand Down Expand Up @@ -763,7 +763,7 @@ int InitPlatform(void)

drmModeConnector *con = drmModeGetConnector(platform.fd, res->connectors[i]);
TRACELOG(LOG_TRACE, "DISPLAY: Connector modes detected: %i", con->count_modes);

// In certain cases the status of the conneciton is reported as UKNOWN, but it is still connected.
// This might be a hardware or software limitation like on Raspberry Pi Zero with composite output.
if (((con->connection == DRM_MODE_CONNECTED) || (con->connection == DRM_MODE_UNKNOWNCONNECTION)) && (con->encoder_id))
Expand Down
29 changes: 27 additions & 2 deletions src/platforms/rcore_web.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,34 @@ int SetGamepadMappings(const char *mappings)
}

// Set gamepad vibration
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor)
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
{
TRACELOG(LOG_WARNING, "GamepadSetVibration() not implemented on target platform");
if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (duration > 0.0f))
{
if (leftMotor < 0.0f) leftMotor = 0.0f;
if (leftMotor > 1.0f) leftMotor = 1.0f;
if (rightMotor < 0.0f) rightMotor = 0.0f;
if (rightMotor > 1.0f) rightMotor = 1.0f;
if (duration > MAX_GAMEPAD_VIBRATION_TIME) duration = MAX_GAMEPAD_VIBRATION_TIME;
duration *= 1000.0f; // Convert duration to ms

// Note: At the moment (2024.10.21) Chrome, Edge, Opera, Safari, Android Chrome, Android Webview only support the vibrationActuator API,
// and Firefox only supports the hapticActuators API
EM_ASM({
try
{
navigator.getGamepads()[$0].vibrationActuator.playEffect('dual-rumble', { startDelay: 0, duration: $3, weakMagnitude: $1, strongMagnitude: $2 });
}
catch (e)
{
try
{
navigator.getGamepads()[$0].hapticActuators[0].pulse($2, $3);
}
catch (e) { }
}
}, gamepad, leftMotor, rightMotor, duration);
}
}

// Set mouse position XY
Expand Down
2 changes: 1 addition & 1 deletion src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ RLAPI int GetGamepadButtonPressed(void);
RLAPI int GetGamepadAxisCount(int gamepad); // Get gamepad axis count for a gamepad
RLAPI float GetGamepadAxisMovement(int gamepad, int axis); // Get axis movement value for a gamepad axis
RLAPI int SetGamepadMappings(const char *mappings); // Set internal gamepad mappings (SDL_GameControllerDB)
RLAPI void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor); // Set gamepad vibration for both motors
RLAPI void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration); // Set gamepad vibration for both motors (duration in seconds)

// Input-related functions: mouse
RLAPI bool IsMouseButtonPressed(int button); // Check if a mouse button has been pressed once
Expand Down

0 comments on commit f8f6aa0

Please sign in to comment.