1using Microsoft.Extensions.Logging;
9public sealed class SdlGamepadManager : IGamepadManager<SdlGamepadAdapter>
14 public static SdlGamepadManager?
Instance {
get;
private set; }
16 private readonly Dictionary<int, SdlGamepadAdapter> _connected =
new();
23 private SdlGamepadManager()
25 if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0)
26 throw new InvalidOperationException($
"SDL gamepad initialization failed: {SDL_GetError()}");
28 _ = SDL_GameControllerEventState(SDL_DISABLE);
30 Engine.
Logger.LogInformation(
"SdlGamepadManager initialized. Polling enabled.");
37 public static SdlGamepadManager
Start()
52 Engine.
Logger.LogInformation(
"SdlGamepadManager stopped. Disposing gamepad adapters...");
54 foreach (var adapter
in Instance._connected.Values)
67 var stillConnected =
new HashSet<int>();
69 for (
int i = 0; i < SDL_NumJoysticks(); i++)
71 if (SDL_IsGameController(i) == SDL_bool.SDL_FALSE)
74 stillConnected.Add(i);
76 if (!_connected.ContainsKey(i))
79 Engine.
Logger.LogInformation(
"Gamepad connected: SDL_CONTROLLER_{ControllerIndex}", i);
83 foreach (var index
in _connected.Keys.Except(stillConnected).ToList())
85 Engine.
Logger.LogInformation(
"Gamepad disconnected: SDL_CONTROLLER_{ControllerIndex}", index);
86 _connected[index].Dispose();
87 _connected.Remove(index);
96 SDL_GameControllerUpdate();
98 foreach (var adapter
in _connected.Values)
105 [Obsolete(
"Use UpdateConnections() and Poll() separately.")]
Represents the core game engine singleton responsible for managing the main update loop,...
static ILogger< Engine > Logger
Gets the logger instance used by the engine for diagnostic and informational messages.