Ejemplo n.º 1
0
        public void MainLoop()
        {
            _threadId = Native.GetCurrentThreadId();

            if (Config.UseController && !_input.Initialize(this))
            {
                throw new InvalidOperationException("Failed to init DirectX Input");
            }

            _audioRenderer.Initialize(this, 4096);// Good default buffer size
            _videoRenderer.Initialize(this);

            _decoder.Start();

            if (Config.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 (!Native.PeekMessage(out MSG m, IntPtr.Zero, 0, 0, Native.PM_REMOVE))
                {
                    continue;
                }

                switch (SDL_(m.msg))
                {
                case SDL_EventType.QUIT:
                    Debug.WriteLine("Quit, bye!");
                    _cancellationTokenSource.Cancel();
                    break;

                default:
                    break;
                }
            }

            // closes input controller
            if (Config.UseController)
            {
                _input.CloseController();
            }

            _audioRenderer.Close();
            //_decoder.Dispose();
        }
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();
            }
        }