Gondwana Game Engine Development (master) - 2.6.1
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
SdlGamepadAdapter.cs
1using static SDL2.SDL;
2
4
8public sealed class SdlGamepadAdapter : IGamepadAdapter
9{
10 private readonly IntPtr _controller;
11 private readonly HashSet<string> _pressedButtons = new();
12
16 public string GamepadId { get; }
17
21 public IReadOnlyCollection<string> PressedButtons => _pressedButtons;
22
26 public GamepadStickState? LeftStick { get; private set; }
27
31 public GamepadStickState? RightStick { get; private set; }
32
36 public float LeftTrigger { get; private set; }
37
41 public float RightTrigger { get; private set; }
42
48 public SdlGamepadAdapter(int deviceIndex)
49 {
50 _controller = SDL_GameControllerOpen(deviceIndex);
51 if (_controller == IntPtr.Zero)
52 throw new InvalidOperationException($"Failed to open SDL_GameController at index {deviceIndex}");
53
54 GamepadId = $"SDL_CONTROLLER_{deviceIndex}";
55 }
56
60 public void Poll()
61 {
62 _pressedButtons.Clear();
63
64 if (SDL_GameControllerGetAttached(_controller) == SDL_bool.SDL_FALSE)
65 {
66 ClearState();
67 return;
68 }
69
70 foreach (SDL_GameControllerButton button in Enum.GetValues(typeof(SDL_GameControllerButton)))
71 {
72 if (button == SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID)
73 continue;
74
75 if (SDL_GameControllerGetButton(_controller, button) == 1)
76 _pressedButtons.Add(button.ToString());
77 }
78
79 int lx = SDL_GameControllerGetAxis(_controller, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX);
80 int ly = SDL_GameControllerGetAxis(_controller, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY);
81 int rx = SDL_GameControllerGetAxis(_controller, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTX);
82 int ry = SDL_GameControllerGetAxis(_controller, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTY);
83
84 int lt = SDL_GameControllerGetAxis(_controller, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT);
85 int rt = SDL_GameControllerGetAxis(_controller, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
86
89 LeftTrigger = NormalizeTrigger(lt);
90 RightTrigger = NormalizeTrigger(rt);
91 }
92
93 private void ClearState()
94 {
95 _pressedButtons.Clear();
96 LeftStick = null;
97 RightStick = null;
98 LeftTrigger = 0f;
99 RightTrigger = 0f;
100 }
101
102 private static float NormalizeTrigger(int raw)
103 {
104 return raw / 32767f; // SDL uses signed 16-bit axes
105 }
106
110 public void Dispose()
111 {
112 if (_controller != IntPtr.Zero)
113 SDL_GameControllerClose(_controller);
114 }
115}
GamepadStickState? RightStick
Gets the current state of the right analog stick.
float LeftTrigger
Gets the current pressure value of the left trigger (0.0 to 1.0).
string GamepadId
Gets the unique identifier for this gamepad.
void Poll()
Polls the gamepad for the current state of all buttons, analog sticks, and triggers.
SdlGamepadAdapter(int deviceIndex)
Initializes a new instance of the SdlGamepadAdapter class for the specified device.
float RightTrigger
Gets the current pressure value of the right trigger (0.0 to 1.0).
IReadOnlyCollection< string > PressedButtons
Gets the collection of currently pressed button names.
void Dispose()
Releases all resources used by the SdlGamepadAdapter and closes the SDL gamepad controller.
GamepadStickState? LeftStick
Gets the current state of the left analog stick.
Defines an adapter interface for accessing gamepad input state and information.
Represents the state of an analog stick on a gamepad, including both normalized floating-point values...
static GamepadStickState FromRaw16(int rawX, int rawY)
Creates a GamepadStickState instance from raw 16-bit signed integer values.