Ejemplo n.º 1
0
        public void ProcessButtonEvent(SDL.SDL_ControllerButtonEvent e)
        {
            var state = e.state == SDL.SDL_PRESSED ? GameControllerButtonState.Down : GameControllerButtonState.Up;

            switch ((SDL.SDL_GameControllerButton)e.button)
            {
            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A:
                A = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B:
                B = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK:
                Back = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN:
                DpadDown = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT:
                DpadLeft = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
                DpadRight = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP:
                DpadUp = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE:
                Guide = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
                LeftShoulder = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK:
                LeftStick = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
                RightShoulder = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK:
                RightStick = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START:
                Start = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X:
                X = state;
                break;

            case SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y:
                Y = state;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 2
0
        public void MainLoop()
        {
            if (_useController && !Input.Initialize())
            {
                throw new InvalidOperationException("Failed to init SDL Input");
            }

            _audioRenderer.Initialize(1024);
            _videoRenderer.Initialize();

            Decoder.Start();

            if (_useController)
            {
                StartInputFrameSendingTask();
            }

            while (!_cancellationTokenSource.IsCancellationRequested)
            {
                if (Decoder.DecodedAudioQueue.Count > 0)
                {
                    var sample = Decoder.DecodedAudioQueue.Dequeue();
                    _audioRenderer.Update(sample);
                }

                if (Decoder.DecodedVideoQueue.Count > 0)
                {
                    var frame = Decoder.DecodedVideoQueue.Dequeue();
                    _videoRenderer.Update(frame);
                }

                if (SDL.SDL_PollEvent(out SDL.SDL_Event sdlEvent) <= 0)
                {
                    continue;
                }

                switch (sdlEvent.type)
                {
                case SDL.SDL_EventType.SDL_QUIT:
                    Console.WriteLine("SDL Quit, bye!");
                    SDL.SDL_Quit();
                    _cancellationTokenSource.Cancel();
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERDEVICEADDED:
                    _useController = true;
                    HandleInputEvent?.Invoke(this,
                                             new InputEventArgs()
                    {
                        EventType       = InputEventType.ControllerAdded,
                        Timestamp       = sdlEvent.cdevice.timestamp,
                        ControllerIndex = sdlEvent.cdevice.which
                    });
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERDEVICEREMOVED:
                    _useController = false;
                    HandleInputEvent?.Invoke(this,
                                             new InputEventArgs()
                    {
                        EventType       = InputEventType.ControllerRemoved,
                        Timestamp       = sdlEvent.cdevice.timestamp,
                        ControllerIndex = sdlEvent.cdevice.which
                    });
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERBUTTONDOWN:
                    SDL.SDL_ControllerButtonEvent pressedButton = sdlEvent.cbutton;
                    HandleInputEvent?.Invoke(this,
                                             new InputEventArgs()
                    {
                        EventType       = InputEventType.ButtonPressed,
                        ControllerIndex = sdlEvent.cdevice.which,
                        Timestamp       = pressedButton.timestamp,
                        Button          = SdlInputMapping.GetButton((SDL.SDL_GameControllerButton)pressedButton.button)
                    });
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERBUTTONUP:
                    SDL.SDL_ControllerButtonEvent releasedButton = sdlEvent.cbutton;
                    HandleInputEvent?.Invoke(this,
                                             new InputEventArgs()
                    {
                        EventType       = InputEventType.ButtonReleased,
                        ControllerIndex = sdlEvent.cdevice.which,
                        Timestamp       = releasedButton.timestamp,
                        Button          = SdlInputMapping.GetButton((SDL.SDL_GameControllerButton)releasedButton.button)
                    });
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERAXISMOTION:
                    SDL.SDL_ControllerAxisEvent axisEvent = sdlEvent.caxis;
                    HandleInputEvent?.Invoke(this,
                                             new InputEventArgs()
                    {
                        EventType       = InputEventType.AxisMoved,
                        ControllerIndex = sdlEvent.cdevice.which,
                        Timestamp       = axisEvent.timestamp,
                        Axis            = SdlInputMapping.GetAxis((SDL.SDL_GameControllerAxis)axisEvent.axis),
                        AxisValue       = axisEvent.axisValue
                    });
                    break;
                }
            }

            // closes input controller
            if (_useController)
            {
                Input.CloseController();
            }
        }