Exemple #1
0
        public void Run()
        {
            IsActive = true;

            GraphicsDevice = CreateGraphicsDevice();

            Initialize();
            InitializeWorld();

            _renderer = new Renderer(World, ResourceFactory, Framebuffer, GraphicsDevice.SubmitCommands);
            _framebufferSizeProvider = new FramebufferSizeProvider(
                World, GraphicsDevice.SwapchainFramebuffer.Width, GraphicsDevice.SwapchainFramebuffer.Height
                );

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            while (IsActive)
            {
                _gameTime = new GameTime(TotalElapsedTime + stopwatch.Elapsed, stopwatch.Elapsed);
                var deltaSeconds = _gameTime.ElapsedGameTime.TotalSeconds;
                stopwatch.Restart();

                while (LimitFrameRate && deltaSeconds < DesiredFrameLengthSeconds)
                {
                    var elapsed = stopwatch.Elapsed;
                    _gameTime     = new GameTime(TotalElapsedTime + elapsed, _gameTime.ElapsedGameTime + elapsed);
                    deltaSeconds += elapsed.TotalSeconds;
                    stopwatch.Restart();

                    // Don't gobble up all available cycles while waiting
                    var deltaMilliseconds = DesiredFrameLengthSeconds * 1000.0 - deltaSeconds * 1000.0;
                    if (deltaMilliseconds > 8)
                    {
                        Thread.Sleep(5);
                    }
                    else
                    {
                        Thread.SpinWait(100);
                    }
                }

                if (deltaSeconds > DesiredFrameLengthSeconds * 1.25)
                {
                    _gameTime = GameTime.RunningSlowly(_gameTime);
                }

                _frameTimeAverager.AddTime(deltaSeconds);

                Update(_gameTime);
                if (!IsActive)
                {
                    break;
                }

                Render(_gameTime);
            }
        }