Beispiel #1
0
        public void ProcessAxisEvent(SDL.SDL_ControllerAxisEvent e)
        {
            switch ((SDL.SDL_GameControllerAxis)e.axis)
            {
            case SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX:
                LeftX = e.axisValue;
                break;

            case SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY:
                LeftY = e.axisValue;
                break;

            case SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTX:
                RightX = e.axisValue;
                break;

            case SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTY:
                RightY = e.axisValue;
                break;

            case SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT:
                LeftTrigger = e.axisValue;
                break;

            case SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
                RightTrigger = e.axisValue;
                break;
            }
        }
        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();
            }
        }