Skip to content

Commit

Permalink
Make rumble functions configurable
Browse files Browse the repository at this point in the history
Affects GameControllerManager::rumble() and JoystickManager::rumble().

The functions were overloaded so as to be made configurable, but there
is a default rumble (that now lasts 100ms instead of 300ms).
Just a small refactoring job.
  • Loading branch information
n0toose committed Jan 28, 2024
1 parent d7e327d commit b1e6f7c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
Binary file modified data/sounds/brick.wav
Binary file not shown.
Binary file modified data/sounds/jump.wav
Binary file not shown.
10 changes: 8 additions & 2 deletions src/control/game_controller_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.


#include "control/game_controller_manager.hpp"

#include <algorithm>
Expand Down Expand Up @@ -325,6 +326,12 @@ GameControllerManager::has_corresponding_game_controller(int player_id) const

int
GameControllerManager::rumble(SDL_GameController* controller) const
{
return rumble(controller, 0xFFFF, 0xFFFF, 100);
}

int
GameControllerManager::rumble(SDL_GameController* controller, uint16_t low_frequency_rumble, uint16_t high_frequency_rumble, uint32_t duration_ms) const
{
#if SDL_VERSION_ATLEAST(2, 0, 9)
if (g_config->multiplayer_buzz_controllers)
Expand All @@ -333,8 +340,7 @@ GameControllerManager::rumble(SDL_GameController* controller) const
if (SDL_GameControllerHasRumble(controller))
{
#endif
// TODO: Rumble intensity setting (like volume).
SDL_GameControllerRumble(controller, 0xFFFF, 0xFFFF, 300);
SDL_GameControllerRumble(controller, low_frequency_rumble, high_frequency_rumble, duration_ms);
#if SDL_VERSION_ATLEAST(2, 0, 18)
}
else
Expand Down
2 changes: 2 additions & 0 deletions src/control/game_controller_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef HEADER_SUPERTUX_CONTROL_GAME_CONTROLLER_MANAGER_HPP
#define HEADER_SUPERTUX_CONTROL_GAME_CONTROLLER_MANAGER_HPP

#include <stdint.h>
#include <array>
#include <vector>
#include <unordered_map>
Expand Down Expand Up @@ -51,6 +52,7 @@ class GameControllerManager final

/** @returns 0 if success, 1 if controller doesn't support rumbling, 2 if game doesn't support rumbling */
int rumble(SDL_GameController* controller) const;
int rumble(SDL_GameController* controller, uint16_t low_frequency_rumble, uint16_t high_frequency_rumble, uint32_t duration_ms) const;

void bind_controller(SDL_GameController* controller, int player_id);

Expand Down
15 changes: 11 additions & 4 deletions src/control/joystick_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ JoystickManager::on_joystick_added(int joystick_index)
parent->push_user();

joysticks[joystick] = id;
// joystick->rumble();

if (GameSession::current() && !GameSession::current()->get_savegame().is_title_screen() && id != 0)
{
Expand Down Expand Up @@ -327,17 +328,23 @@ JoystickManager::has_corresponding_joystick(int player_id) const
}

int
JoystickManager::rumble(SDL_Joystick* controller) const
JoystickManager::rumble(SDL_Joystick* joystick) const
{
return rumble(joystick, 0xFFFF, 0xFFFF, 100);
}

int
JoystickManager::rumble(SDL_Joystick* joystick, uint16_t low_frequency_rumble, uint16_t high_frequency_rumble, uint32_t duration_ms) const
{
#if SDL_VERSION_ATLEAST(2, 0, 9)
// In the game, joysticks are controllers. SDL treats them as two separate things.
if (g_config->multiplayer_buzz_controllers)
{
#if SDL_VERSION_ATLEAST(2, 0, 18)
if (SDL_JoystickHasRumble(controller))
if (SDL_JoystickHasRumble(joystick))
{
#endif
// TODO: Rumble intensity setting (like volume)
SDL_JoystickRumble(controller, 0xFFFF, 0xFFFF, 300);
SDL_JoystickRumble(joystick, low_frequency_rumble, high_frequency_rumble, duration_ms);
#if SDL_VERSION_ATLEAST(2, 0, 18)
}
else
Expand Down
5 changes: 4 additions & 1 deletion src/control/joystick_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
#ifndef HEADER_SUPERTUX_CONTROL_JOYSTICK_MANAGER_HPP
#define HEADER_SUPERTUX_CONTROL_JOYSTICK_MANAGER_HPP

#include <SDL.h>
#include <stdint.h>
#include <vector>
#include <unordered_map>

#include <SDL.h>

#include "control/controller.hpp"

class InputManager;
Expand Down Expand Up @@ -59,6 +61,7 @@ class JoystickManager final

/** @returns 0 if success, 1 if controller doesn't support rumbling, 2 if game doesn't support rumbling */
int rumble(SDL_Joystick* joystick) const;
int rumble(SDL_Joystick* joystick, uint16_t low_frequency_rumble, uint16_t high_frequency_rumble, uint32_t duration_ms) const;

void bind_joystick(SDL_Joystick* joystick, int player_id);

Expand Down

0 comments on commit b1e6f7c

Please sign in to comment.