Esempio n. 1
0
        private static void Run()
        {
            // timer
            var timer            = Stopwatch.StartNew();
            var lastTime         = new TimeSpan();
            var fixedAccumulator = TimeSpan.Zero;
            var framecount       = 0;
            var frameticks       = 0L;

            while (Running)
            {
                var forceFixedTimestep = ForceFixedTimestep;
                if (!forceFixedTimestep)
                {
                    System.Input.Step();
                }

                Modules.FrameStart();

                // get current time & diff
                var currTime = TimeSpan.FromTicks(timer.Elapsed.Ticks);
                var diffTime = (currTime - lastTime);
                lastTime = currTime;

                // fixed timestep update
                {
                    // fixed delta time is always the same
                    var fixedTarget = TimeSpan.FromSeconds(1f / Time.FixedStepTarget);
                    Time.RawDelta = Time.RawFixedDelta = (float)fixedTarget.TotalSeconds;
                    Time.Delta    = Time.FixedDelta = Time.RawFixedDelta * Time.DeltaScale;

                    if (forceFixedTimestep)
                    {
                        Time.RawVariableDelta = Time.RawFixedDelta;
                        Time.VariableDelta    = Time.FixedDelta;
                    }

                    // increment time
                    fixedAccumulator += diffTime;

                    // if we're forcing fixed timestep and running too fast,
                    // we should sleep the thread while we wait
                    if (forceFixedTimestep)
                    {
                        while (fixedAccumulator < fixedTarget)
                        {
                            Thread.Sleep((int)((fixedTarget - fixedAccumulator).TotalMilliseconds));

                            currTime          = TimeSpan.FromTicks(timer.Elapsed.Ticks);
                            diffTime          = (currTime - lastTime);
                            lastTime          = currTime;
                            fixedAccumulator += diffTime;
                        }
                    }

                    // Do not allow any update to take longer than our maximum.
                    if (fixedAccumulator > Time.FixedMaxElapsedTime)
                    {
                        fixedAccumulator = Time.FixedMaxElapsedTime;
                    }

                    // do as many fixed updates as we can
                    while (fixedAccumulator >= fixedTarget)
                    {
                        Time.FixedDuration += fixedTarget;
                        fixedAccumulator   -= fixedTarget;

                        if (forceFixedTimestep)
                        {
                            Time.Duration += fixedTarget;

                            System.Input.Step();
                            Modules.FixedUpdate();
                            Modules.Update();
                        }
                        else
                        {
                            Modules.FixedUpdate();
                        }

                        if (Exiting)
                        {
                            break;
                        }
                    }
                }

                // variable timestep update
                if (!forceFixedTimestep)
                {
                    // get Time values
                    Time.Duration += diffTime;
                    Time.RawDelta  = Time.RawVariableDelta = (float)diffTime.TotalSeconds;
                    Time.Delta     = Time.VariableDelta = Time.RawDelta * Time.DeltaScale;

                    // update
                    Modules.Update();
                }

                Modules.FrameEnd();

                // Check if the Primary Window has been closed
                if (_primaryWindow == null || !_primaryWindow.Opened)
                {
                    Exit();
                }

                // render
                if (!Exiting)
                {
                    Modules.BeforeRender();

                    for (int i = 0; i < System.Windows.Count; i++)
                    {
                        if (System.Windows[i].Opened)
                        {
                            System.Windows[i].Render();
                        }
                    }

                    for (int i = 0; i < System.Windows.Count; i++)
                    {
                        if (System.Windows[i].Opened)
                        {
                            System.Windows[i].Present();
                        }
                    }

                    Modules.AfterRender();
                }

                // get fps
                framecount++;
                if (TimeSpan.FromTicks(timer.Elapsed.Ticks - frameticks).TotalSeconds >= 1)
                {
                    Time.FPS   = framecount;
                    frameticks = timer.Elapsed.Ticks;
                    framecount = 0;
                }
            }

            // finalize
            Modules.Shutdown();
            _primaryWindow = null;
            Exiting        = false;

            Log.Info("Exited");
        }