Update() public static method

public static Update ( ) : void
return void
Exemplo n.º 1
0
        /// <summary>
        /// Runs the game. This method will return when Exit is called.
        /// </summary>
        public static void Run()
        {
            var    timer       = new Stopwatch();
            double accumulator = 0;

            frameTimer = new Stopwatch();
            frameTimer.Restart();
            lastFrameTime = frameTimer.ElapsedMilliseconds;

            while (running)
            {
                var time = timer.Elapsed.TotalSeconds;
                timer.Restart();
                accumulator += time;

                // Spiral of death fix
                var lagThreshold = GameOptions.Timestep * GameOptions.MaxUpdatesPerFrame;
                if (accumulator > lagThreshold)
                {
                    if (Lagging != null)
                    {
                        Lagging((float)accumulator - lagThreshold);
                    }

                    accumulator = lagThreshold;
                }

                #region Update
                while (accumulator >= GameOptions.Timestep)
                {
                    Window.DispatchEvents();
                    Timer.Update();

                    foreach (var state in StateStack)
                    {
                        if (state.IsActive || state.InactiveMode.HasFlag(State.UpdateMode.Update))
                        {
                            state.UpdateInternal();
                        }
                    }

                    accumulator -= GameOptions.Timestep;
                }
                #endregion

                #region Draw

                // Find bottom most state that renders. This state will provide the color to clear to.
                var clearState = StateStack.Find(s => s.IsActive || s.InactiveMode.HasFlag(State.UpdateMode.Draw));

                foreach (var state in StateStack)
                {
                    if (state == clearState)
                    {
                        Window.Clear(state.ClearColor);
                    }

                    if (!state.IsActive && !state.InactiveMode.HasFlag(State.UpdateMode.Draw))
                    {
                        continue;
                    }

                    state.DrawInternal(Window);
                }
                #endregion

                Window.Display();

                // Calculate framerate
                float currentFrameTime = 1000.0f / ((frameTime = frameTimer.ElapsedMilliseconds) - lastFrameTime);
                if (Math.Abs(frameTime - lastFrameTime) > float.Epsilon)
                {
                    // NOTE The 50 is not in reference to 50 fps, it's in reference quickly to
                    //      tween between framerates, to prevent sudden jumps that occur fairly
                    //      often.
                    Framerate    += (currentFrameTime - Framerate) / 50;
                    lastFrameTime = frameTime;
                }
            }

            while (StateStack.Count > 0)
            {
                PopState();
            }

            Window.Close();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs the game. This method will return when Exit is called.
        /// </summary>
        public static void Run()
        {
            var    timer       = new Stopwatch();
            double accumulator = 0;

            while (running)
            {
                var time = timer.Elapsed.TotalSeconds;
                timer.Restart();
                accumulator += time;

                // Spiral of death fix
                var lagThreshold = GameOptions.Timestep * GameOptions.MaxUpdatesPerFrame;
                if (accumulator > lagThreshold)
                {
                    if (Lagging != null)
                    {
                        Lagging((float)accumulator - lagThreshold);
                    }

                    accumulator = lagThreshold;
                }

                #region Update
                while (accumulator >= GameOptions.Timestep)
                {
                    Window.DispatchEvents();
                    Timer.Update();

                    foreach (var state in StateStack)
                    {
                        if (state.IsActive || state.InactiveMode.HasFlag(State.UpdateMode.Update))
                        {
                            state.UpdateInternal();
                        }
                    }

                    accumulator -= GameOptions.Timestep;
                }
                #endregion

                #region Draw

                // Find bottom most state that renders. This state will provide the color to clear to.
                var clearState = StateStack.Find(s => s.IsActive || s.InactiveMode.HasFlag(State.UpdateMode.Draw));

                foreach (var state in StateStack)
                {
                    if (state == clearState)
                    {
                        Window.Clear(state.ClearColor);
                    }

                    if (!state.IsActive && !state.InactiveMode.HasFlag(State.UpdateMode.Draw))
                    {
                        continue;
                    }

                    state.DrawInternal(Window);
                }
                #endregion

                Window.Display();
            }

            while (StateStack.Count > 0)
            {
                PopState();
            }

            Window.Close();
        }