Skip to content

Commit

Permalink
[DesktopGL] Remove string allocations when calling GamePad.GetCapabil…
Browse files Browse the repository at this point in the history
…ities. (MonoGame#8453)

* Remove string allocations when calling GamePad.GetCapabilities.

We were using some string parsing to get the capabilities of the
gamepad. We should have been using the appropriate
`SDL_GameControllerHasButton` and `SDL_GameControllerHasAxis`
functions to get this information.

* Fix formatting
  • Loading branch information
dellis1972 authored Aug 21, 2024
1 parent 39313ed commit efeb84e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 75 deletions.
96 changes: 21 additions & 75 deletions MonoGame.Framework/Platform/Input/GamePad.SDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,87 +118,33 @@ private static GamePadCapabilities PlatformGetCapabilities(int index)

var gamecontroller = Gamepads[index].Device;
var caps = new GamePadCapabilities();
var mapping = Sdl.GameController.GetMapping(gamecontroller).Split(',');

caps.IsConnected = true;
caps.DisplayName = Sdl.GameController.GetName(gamecontroller);
caps.Identifier = Sdl.Joystick.GetGUID(Sdl.GameController.GetJoystick(gamecontroller)).ToString();
caps.HasLeftVibrationMotor = caps.HasRightVibrationMotor = Sdl.GameController.HasRumble(gamecontroller) != 0;
caps.GamePadType = GamePadType.GamePad;

foreach (var map in mapping)
{
var split = map.Split(':');
if (split.Length != 2)
continue;

switch (split[0])
{
case "a":
caps.HasAButton = true;
break;
case "b":
caps.HasBButton = true;
break;
case "x":
caps.HasXButton = true;
break;
case "y":
caps.HasYButton = true;
break;
case "back":
caps.HasBackButton = true;
break;
case "guide":
caps.HasBigButton = true;
break;
case "start":
caps.HasStartButton = true;
break;
case "dpleft":
caps.HasDPadLeftButton = true;
break;
case "dpdown":
caps.HasDPadDownButton = true;
break;
case "dpright":
caps.HasDPadRightButton = true;
break;
case "dpup":
caps.HasDPadUpButton = true;
break;
case "leftshoulder":
caps.HasLeftShoulderButton = true;
break;
case "lefttrigger":
caps.HasLeftTrigger = true;
break;
case "rightshoulder":
caps.HasRightShoulderButton = true;
break;
case "righttrigger":
caps.HasRightTrigger = true;
break;
case "leftstick":
caps.HasLeftStickButton = true;
break;
case "rightstick":
caps.HasRightStickButton = true;
break;
case "leftx":
caps.HasLeftXThumbStick = true;
break;
case "lefty":
caps.HasLeftYThumbStick = true;
break;
case "rightx":
caps.HasRightXThumbStick = true;
break;
case "righty":
caps.HasRightYThumbStick = true;
break;
}
}
caps.HasAButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.A);
caps.HasBButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.B);
caps.HasXButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.X);
caps.HasYButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.Y);
caps.HasBackButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.Back);
caps.HasBigButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.Guide);
caps.HasStartButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.Start);
caps.HasDPadLeftButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.DpadLeft);
caps.HasDPadDownButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.DpadDown);
caps.HasDPadRightButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.DpadRight);
caps.HasDPadUpButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.DpadUp);
caps.HasLeftShoulderButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.LeftShoulder);
caps.HasLeftTrigger = Sdl.GameController.HasAxis(gamecontroller, Sdl.GameController.Axis.TriggerLeft);
caps.HasRightShoulderButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.RightShoulder);
caps.HasRightTrigger = Sdl.GameController.HasAxis(gamecontroller, Sdl.GameController.Axis.TriggerRight);
caps.HasLeftStickButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.LeftStick);
caps.HasRightStickButton = Sdl.GameController.HasButton(gamecontroller, Sdl.GameController.Button.RightStick);
caps.HasLeftXThumbStick = Sdl.GameController.HasAxis(gamecontroller, Sdl.GameController.Axis.LeftX);
caps.HasLeftYThumbStick = Sdl.GameController.HasAxis(gamecontroller, Sdl.GameController.Axis.LeftY);
caps.HasRightXThumbStick = Sdl.GameController.HasAxis(gamecontroller, Sdl.GameController.Axis.RightX);
caps.HasRightYThumbStick = Sdl.GameController.HasAxis(gamecontroller, Sdl.GameController.Axis.RightY);

return caps;
}
Expand Down
8 changes: 8 additions & 0 deletions MonoGame.Framework/Platform/SDL/SDL2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,14 @@ public struct DeviceEvent
public delegate int d_sdl_gamecontrolleraddmappingsfromrw(IntPtr rw, int freew);
public static d_sdl_gamecontrolleraddmappingsfromrw AddMappingFromRw = FuncLoader.LoadFunction<d_sdl_gamecontrolleraddmappingsfromrw>(NativeLibrary, "SDL_GameControllerAddMappingsFromRW");

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool d_sdl_gamecontrollerhasbutton(IntPtr gamecontroller, Button button);
public static d_sdl_gamecontrollerhasbutton HasButton = FuncLoader.LoadFunction<d_sdl_gamecontrollerhasbutton>(NativeLibrary, "SDL_GameControllerHasButton");

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool d_sdl_gamecontrollerhasaxis(IntPtr gamecontroller, Axis axis);
public static d_sdl_gamecontrollerhasaxis HasAxis = FuncLoader.LoadFunction<d_sdl_gamecontrollerhasaxis>(NativeLibrary, "SDL_GameControllerHasAxis");

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void d_sdl_gamecontrollerclose(IntPtr gamecontroller);
public static d_sdl_gamecontrollerclose Close = FuncLoader.LoadFunction<d_sdl_gamecontrollerclose>(NativeLibrary, "SDL_GameControllerClose");
Expand Down

0 comments on commit efeb84e

Please sign in to comment.