示例#1
0
        private void AppLoop()
        {
            //http://gafferongames.com/game-physics/fix-your-timestep/
            var accumulator = 0.0;
            var currentTime = _timer.ElapsedMilliseconds;

            while (_running)
            {
                var newTime   = _timer.ElapsedMilliseconds;
                var frameSpan = Math.Min(newTime - currentTime, MaxMillisecondsUpdate);
                accumulator += frameSpan;

                currentTime = newTime;

                while (accumulator >= MillisecondsPerFrame)
                {
                    _currentAppState.UpdatePhysics(MillisecondsPerFrame);
                    accumulator -= MillisecondsPerFrame;
                }

                Render(accumulator / MillisecondsPerFrame);

                var cur = (int)(_timer.ElapsedMilliseconds - currentTime);
                if (cur < MillisecondsPerFrame)
                {
                    Thread.Sleep((int)MillisecondsPerFrame - cur);
                }
            }
        }