private unsafe void RegisterInitialGamepads() { var numJoySticks = Sdl2Native.SDL_NumJoysticks(); var numGameControllers = 0; for (var c = 0; c < numJoySticks; c++) { if (Sdl2Native.SDL_IsGameController(c)) { var index = c; var controller = Sdl2Native.SDL_GameControllerOpen(index); var joystick = Sdl2Native.SDL_GameControllerGetJoystick(controller); var instanceId = index; //Sdl2Native.SDL_JoystickInstanceID(joystick); //How to check for controller NULL? var name = Marshal.PtrToStringAnsi((IntPtr)Sdl2Native.SDL_GameControllerName(controller)); _frameworkMessenger.Report(string.Concat("Detected Controller name: ", name)); if (_controllers.ContainsKey(instanceId)) { _frameworkMessenger.Report("Cannot add controller as instanceId already exists in dictionary, skipping..."); continue; } var gameController = new GameController(name, controller, joystick); _controllers.Add(instanceId, gameController); numGameControllers++; } } _frameworkMessenger.Report(string.Concat("Number of Joysticks detected: ", numJoySticks, " -> of those, number of GameControllers: ", numGameControllers)); }
private unsafe void UpdateGamepadRegister() { var validIds = new List <int>(); var numJoySticks = Sdl2Native.SDL_NumJoysticks(); var numGameControllers = 0; for (var c = 0; c < numJoySticks; c++) { if (Sdl2Native.SDL_IsGameController(c)) { var index = c; var controller = Sdl2Native.SDL_GameControllerOpen(index); var joystick = Sdl2Native.SDL_GameControllerGetJoystick(controller); var instanceId = Sdl2Native.SDL_JoystickInstanceID(joystick); var name = Marshal.PtrToStringAnsi((IntPtr)Sdl2Native.SDL_GameControllerName(controller)); _frameworkMessenger.Report(string.Concat("Detected Controller name: ", name)); var gameController = new GameController(name, controller, joystick); if (_controllers.ContainsKey(instanceId)) { if (_controllers[instanceId].Name != name) { _frameworkMessenger.Report(string.Concat("Warning: Controller replaced at instance id: ", instanceId, " does not have the same name as the previous controller here")); } _controllers.Remove(instanceId); } _controllers.Add(instanceId, gameController); validIds.Add(instanceId); numGameControllers++; } } var ids = _controllers.Keys.ToList(); ids.ForEach(x => { if (!validIds.Contains(x)) { _controllers.Remove(x); } }); _frameworkMessenger.Report(string.Concat("Number of Joysticks detected: ", numJoySticks, " -> of those, number of GameControllers: ", numGameControllers)); }