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
SdlGamepadManager.cs
1using Microsoft.Extensions.Logging;
2using static SDL2.SDL;
3
5
9public sealed class SdlGamepadManager : IGamepadManager<SdlGamepadAdapter>
10{
14 public static SdlGamepadManager? Instance { get; private set; }
15
16 private readonly Dictionary<int, SdlGamepadAdapter> _connected = new();
17
21 public IReadOnlyCollection<SdlGamepadAdapter> ConnectedAdapters => _connected.Values;
22
23 private SdlGamepadManager()
24 {
25 if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0)
26 throw new InvalidOperationException($"SDL gamepad initialization failed: {SDL_GetError()}");
27
28 _ = SDL_GameControllerEventState(SDL_DISABLE); // Polling only
29
30 Engine.Logger.LogInformation("SdlGamepadManager initialized. Polling enabled.");
31 }
32
37 public static SdlGamepadManager Start()
38 {
39 if (Instance == null)
40 Instance = new SdlGamepadManager();
41
42 return Instance;
43 }
44
48 public static void Stop()
49 {
50 if (Instance != null)
51 {
52 Engine.Logger.LogInformation("SdlGamepadManager stopped. Disposing gamepad adapters...");
53
54 foreach (var adapter in Instance._connected.Values)
55 adapter.Dispose();
56
57 Instance._connected.Clear();
58 Instance = null;
59 }
60 }
61
65 public void UpdateConnections()
66 {
67 var stillConnected = new HashSet<int>();
68
69 for (int i = 0; i < SDL_NumJoysticks(); i++)
70 {
71 if (SDL_IsGameController(i) == SDL_bool.SDL_FALSE)
72 continue;
73
74 stillConnected.Add(i);
75
76 if (!_connected.ContainsKey(i))
77 {
78 _connected[i] = new SdlGamepadAdapter(i);
79 Engine.Logger.LogInformation("Gamepad connected: SDL_CONTROLLER_{ControllerIndex}", i);
80 }
81 }
82
83 foreach (var index in _connected.Keys.Except(stillConnected).ToList())
84 {
85 Engine.Logger.LogInformation("Gamepad disconnected: SDL_CONTROLLER_{ControllerIndex}", index);
86 _connected[index].Dispose();
87 _connected.Remove(index);
88 }
89 }
90
94 public void Poll()
95 {
96 SDL_GameControllerUpdate();
97
98 foreach (var adapter in _connected.Values)
99 adapter.Poll();
100 }
101
105 [Obsolete("Use UpdateConnections() and Poll() separately.")]
106 public void Update()
107 {
109 Poll();
110 }
111}
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static ILogger< Engine > Logger
Gets the logger instance used by the engine for diagnostic and informational messages.
Definition Engine.cs:59
Provides a gamepad adapter implementation using SDL2 for cross-platform gamepad input support.
static ? SdlGamepadManager Instance
Gets the singleton instance of the SdlGamepadManager, if it has been started.
static void Stop()
Stops the SDL gamepad manager, disposes all connected gamepad adapters, and clears the singleton inst...
void Poll()
Refreshes SDL controller state and polls all currently connected adapters.
IReadOnlyCollection< SdlGamepadAdapter > ConnectedAdapters
Gets the collection of currently connected gamepad adapters.
static SdlGamepadManager Start()
Starts the SDL gamepad manager and returns the singleton instance.
void UpdateConnections()
Checks SDL controller devices for connection and disconnection changes.
void Update()
Performs the legacy combined connection and state refresh.