Example #1
0
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            // FIXME: Since Game.Exit may be called during an Update loop (and
            //        in fact that is quite likely to happen), this code is now
            //        littered with checks to _platform.IsRunning.  It would be
            //        nice if there weren't quite so many.  The move to a
            //        Game.Tick-centric architecture may eliminate this problem
            //        automatically.
            if (_game != null)
            {
                _now = DateTime.Now;
                if (_isFirstTime)
                {
                    // Initialize GameTime
                    _updateGameTime = new GameTime();
                    _drawGameTime   = new GameTime();
                    _lastUpdate     = DateTime.Now;
                    _isFirstTime    = false;
                }

                if (_needsToResetElapsedTime)
                {
                    _drawGameTime.ResetElapsedTime();
                    _needsToResetElapsedTime = false;
                }

                // Try to catch up with frames
                _drawGameTime.Update(_now - _lastUpdate);
                TimeSpan catchup = _drawGameTime.ElapsedGameTime;
                if (catchup > _game.TargetElapsedTime)
                {
                    while (catchup > _game.TargetElapsedTime)
                    {
                        //Console.WriteLine("Catching up " + (catchup - _game.TargetElapsedTime));
                        catchup -= _game.TargetElapsedTime;
                        _drawGameTime.ElapsedGameTime = _game.TargetElapsedTime;
                        if (_platform.IsRunning)
                        {
                            _game.DoUpdate(_drawGameTime);
                        }
                        _extraElapsedTime += catchup;
                    }
                    if (_extraElapsedTime > _game.TargetElapsedTime)
                    {
                        //Console.WriteLine("FastForward " + _extraElapsedTime);
                        if (_platform.IsRunning)
                        {
                            _game.DoUpdate(_drawGameTime);
                        }
                        _extraElapsedTime = TimeSpan.Zero;
                    }
                }
                else
                {
                    if (_platform.IsRunning)
                    {
                        _game.DoUpdate(_drawGameTime);
                    }
                }

                //Console.WriteLine("Render " + _drawGameTime.ElapsedGameTime);
//				_game.DoUpdate(_drawGameTime);
                if (_platform.IsRunning)
                {
                    _game.DoDraw(_drawGameTime);
                }
                _lastUpdate = _now;
            }
        }