Browse Source

Validate gamepad button count

pull/3089/head
staphen 5 years ago committed by Anders Jenbo
parent
commit
fd2f0e3f0d
  1. 3
      Source/controls/devices/game_controller.cpp
  2. 5
      Source/controls/devices/joystick.cpp
  3. 9
      Source/utils/sdl2_backports.h

3
Source/controls/devices/game_controller.cpp

@ -8,6 +8,7 @@
#include "controls/devices/joystick.h"
#include "utils/log.hpp"
#include "utils/sdl_ptrs.h"
#include "utils/sdl2_backports.h"
#include "utils/stubs.h"
namespace devilution {
@ -123,7 +124,7 @@ SDL_GameControllerButton GameController::ToSdlGameControllerButton(ControllerBut
bool GameController::IsPressed(ControllerButton button) const
{
const SDL_GameControllerButton gcButton = ToSdlGameControllerButton(button);
return gcButton != SDL_CONTROLLER_BUTTON_INVALID && SDL_GameControllerGetButton(sdl_game_controller_, gcButton) != 0;
return SDL_GameControllerHasButton(sdl_game_controller_, gcButton) && SDL_GameControllerGetButton(sdl_game_controller_, gcButton) != 0;
}
bool GameController::ProcessAxisMotion(const SDL_Event &event)

5
Source/controls/devices/joystick.cpp

@ -210,7 +210,10 @@ bool Joystick::IsPressed(ControllerButton button) const
if (IsHatButtonPressed(button))
return true;
const int joyButton = ToSdlJoyButton(button);
return joyButton != -1 && SDL_JoystickGetButton(sdl_joystick_, joyButton) != 0;
if (joyButton == -1)
return false;
const int numButtons = SDL_JoystickNumButtons(sdl_joystick_);
return joyButton < numButtons && SDL_JoystickGetButton(sdl_joystick_, joyButton) != 0;
}
bool Joystick::ProcessAxisMotion(const SDL_Event &event)

9
Source/utils/sdl2_backports.h

@ -49,3 +49,12 @@ SDL_CreateRGBSurfaceWithFormatFrom(void *pixels,
return surface;
}
#endif
#if !SDL_VERSION_ATLEAST(2, 0, 14)
inline SDL_bool
SDL_GameControllerHasButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
{
SDL_GameControllerButtonBind bind = SDL_GameControllerGetBindForButton(gamecontroller, button);
return (bind.bindType != SDL_CONTROLLER_BINDTYPE_NONE) ? SDL_TRUE : SDL_FALSE;
}
#endif

Loading…
Cancel
Save