예제 #1
0
        private static void DisposeGamepadDevice(GamePadDevice device)
        {
            if (device.HapticType > 0)
            {
                SDL.SDL_HapticClose(device.HapticDevice);
            }

            SDL.SDL_GameControllerClose(device.Device);
        }
예제 #2
0
        public static void AddGamepadDevice(int deviceId)
        {
            var gamepad = new GamePadDevice()
            {
                Device       = SDL.SDL_GameControllerOpen(deviceId),
                HapticDevice = SDL.SDL_HapticOpen(deviceId)
            };

            if (gamepad.Device == IntPtr.Zero)
            {
                return;
            }

            var id = 1;

            while (_gamepads.ContainsKey(id))
            {
                id++;
            }

            _gamepads.Add(id, gamepad);

            if (gamepad.HapticDevice == IntPtr.Zero)
            {
                return;
            }

            try
            {
                if (SDL.SDL_HapticEffectSupported(gamepad.HapticDevice, ref _hapticEffectLeftRight) == 1)
                {
                    SDL.SDL_HapticNewEffect(gamepad.HapticDevice, ref _hapticEffectLeftRight);
                    gamepad.HapticType = 1;
                }
                else if (SDL.SDL_HapticRumbleSupported(gamepad.HapticDevice) == 1)
                {
                    SDL.SDL_HapticRumbleInit(gamepad.HapticDevice);
                    gamepad.HapticType = 2;
                }
                else
                {
                    SDL.SDL_HapticClose(gamepad.HapticDevice);
                }

                string gamepadName = SDL.SDL_GameControllerName(gamepad.Device);

                GamePadAdded?.Invoke(null, new Tuple <int, string>(id, gamepadName));
            }
            catch
            {
                SDL.SDL_HapticClose(gamepad.HapticDevice);
                gamepad.HapticDevice = IntPtr.Zero;
                SDL.SDL_ClearError();
            }
        }