Exemplo n.º 1
0
        public void SwitchClocks()
        {
            ManualClock clock1 = new ManualClock();
            ManualClock clock2 = new ManualClock();

            clock1.Start();
            clock2.Start();

            IGameTimer timer = new VariableStepTimer(clock1);

            timer.TimeChanged += timer_TimeChanged;
            timer.Start();

            clock1.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

            timer.Clock = clock2;
            Assert.AreSame(clock2, timer.Clock);
            clock1.Update(TimeSpan.FromMilliseconds(10));
            CheckNoTimeChangedEvent();

            clock2.Update(TimeSpan.FromMilliseconds(20));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(20));

            timer.Clock = null;
            Assert.IsNull(timer.Clock);
            clock1.Update(TimeSpan.FromMilliseconds(10));
            clock2.Update(TimeSpan.FromMilliseconds(20));
            CheckNoTimeChangedEvent();
            Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.DeltaTime);
        }
Exemplo n.º 2
0
        public void IdleTime()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              VariableStepTimer timer = new VariableStepTimer(clock);
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;
              timer.MinDeltaTime = TimeSpan.FromMilliseconds(20);
              timer.Start();

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20));
              Assert.AreEqual(TimeSpan.Zero, timer.IdleTime);

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(40), TimeSpan.FromMilliseconds(20));
              Assert.AreEqual(TimeSpan.Zero, timer.IdleTime);

              clock.Update(TimeSpan.FromMilliseconds(15));
              CheckIdleEvent(TimeSpan.FromMilliseconds(20 - 15));
              CheckNoTimeChangedEvent();
              Assert.AreEqual(TimeSpan.FromMilliseconds(20 - 15), timer.IdleTime);

              clock.Update(TimeSpan.FromMilliseconds(2));
              CheckIdleEvent(TimeSpan.FromMilliseconds(20 - (15 + 2)));
              CheckNoTimeChangedEvent();
              Assert.AreEqual(TimeSpan.FromMilliseconds(20 - (15 + 2)), timer.IdleTime);
        }
Exemplo n.º 3
0
        public void IdleTime()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            VariableStepTimer timer = new VariableStepTimer(clock);

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;
            timer.MinDeltaTime = TimeSpan.FromMilliseconds(20);
            timer.Start();

            clock.Update(TimeSpan.FromMilliseconds(20));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20));
            Assert.AreEqual(TimeSpan.Zero, timer.IdleTime);

            clock.Update(TimeSpan.FromMilliseconds(20));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(40), TimeSpan.FromMilliseconds(20));
            Assert.AreEqual(TimeSpan.Zero, timer.IdleTime);

            clock.Update(TimeSpan.FromMilliseconds(15));
            CheckIdleEvent(TimeSpan.FromMilliseconds(20 - 15));
            CheckNoTimeChangedEvent();
            Assert.AreEqual(TimeSpan.FromMilliseconds(20 - 15), timer.IdleTime);

            clock.Update(TimeSpan.FromMilliseconds(2));
            CheckIdleEvent(TimeSpan.FromMilliseconds(20 - (15 + 2)));
            CheckNoTimeChangedEvent();
            Assert.AreEqual(TimeSpan.FromMilliseconds(20 - (15 + 2)), timer.IdleTime);
        }
Exemplo n.º 4
0
        public void TimerReset()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            VariableStepTimer timer = new VariableStepTimer(clock);

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;
            timer.Reset();
            timer.Start();

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

            timer.Reset();
            Assert.IsFalse(timer.IsRunning);
            Assert.AreEqual(TimeSpan.Zero, timer.Time);
            Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);

            timer.Start();
            clock.Update(TimeSpan.FromMilliseconds(10));
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));

            timer.Stop();
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
            Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);

            timer.Reset();
            Assert.AreEqual(TimeSpan.Zero, timer.Time);
            Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);
        }
Exemplo n.º 5
0
        public void LostTime()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              VariableStepTimer timer = new VariableStepTimer(clock);
              timer.TimeChanged += timer_TimeChanged;
              timer.MaxDeltaTime = TimeSpan.FromMilliseconds(50);
              timer.Start();

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20));
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(40), TimeSpan.FromMilliseconds(20));
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

              clock.Update(TimeSpan.FromMilliseconds(50));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(90), TimeSpan.FromMilliseconds(50));
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

              clock.Update(TimeSpan.FromMilliseconds(70));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(140), TimeSpan.FromMilliseconds(50));
              Assert.AreEqual(TimeSpan.FromMilliseconds(70 - 50), timer.LostTime);

              clock.Update(TimeSpan.FromMilliseconds(80));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(190), TimeSpan.FromMilliseconds(50));
              Assert.AreEqual(TimeSpan.FromMilliseconds(80 - 50), timer.LostTime);

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(210), TimeSpan.FromMilliseconds(20));
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);
        }
Exemplo n.º 6
0
        public void TimeChanged()
        {
            var clock = new ManualClock();
            var step  = new TimeSpan(100000);

            int      numberOfEvents = 0;
            TimeSpan deltaTime      = TimeSpan.Zero;
            TimeSpan gameTime       = TimeSpan.Zero;
            TimeSpan totalTime      = TimeSpan.Zero;

            clock.TimeChanged += (s, e) =>
            {
                numberOfEvents++;
                deltaTime = e.DeltaTime;
                gameTime  = e.GameTime;
                totalTime = e.TotalTime;
            };

            clock.Update(step);

            Assert.AreEqual(0, numberOfEvents);

            clock.Start();
            clock.Update(step);
            clock.Update(step);

            Assert.AreEqual(step.Ticks, deltaTime.Ticks);
            Assert.AreEqual(2 * step.Ticks, gameTime.Ticks);
            Assert.AreEqual(2 * step.Ticks, totalTime.Ticks);

            clock.Stop();
            clock.Update(step);
        }
Exemplo n.º 7
0
        public void AccumulatedStepsAndPendingSteps()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            FixedStepTimer timer = new FixedStepTimer(clock)
            {
                StepSize         = TimeSpan.FromMilliseconds(10),
                MaxNumberOfSteps = 5,
            };

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;
            timer.Start();

            timer.AccumulateTimeSteps = true;

            int desiredPendingSteps    = 0;
            int desiredAccumlatedSteps = 2;

            timer.TimeChanged += (s, e) =>
            {
                Assert.AreEqual(desiredPendingSteps, timer.PendingSteps);
                Assert.AreEqual(desiredAccumlatedSteps, timer.AccumulatedSteps);
                desiredPendingSteps--;
            };

            clock.Update(TimeSpan.FromMilliseconds(22));

            timer.Reset();
            timer.Start();

            timer.AccumulateTimeSteps = false;

            desiredPendingSteps    = 3;
            desiredAccumlatedSteps = 1;
            clock.Update(TimeSpan.FromMilliseconds(42));

            Assert.AreEqual(-1, desiredPendingSteps);
        }
Exemplo n.º 8
0
        public void AccumlateTimeSteps()
        {
            int handleTimeStepCallCount = 0;

            ManualClock clock = new ManualClock();

            clock.Start();

            var            timeIncrement = new TimeSpan(166666);
            FixedStepTimer timer         = new FixedStepTimer(clock)
            {
                StepSize         = timeIncrement,
                MaxNumberOfSteps = 8,
            };

            timer.TimeChanged += (obj, timeEventArgs) =>
            {
                Assert.AreEqual(timeIncrement, timeEventArgs.DeltaTime);
                handleTimeStepCallCount++;
            };

            Assert.AreEqual(true, timer.AccumulateTimeSteps);
            timer.AccumulateTimeSteps = false;
            Assert.AreEqual(false, timer.AccumulateTimeSteps);

            timer.Start();
            handleTimeStepCallCount = 0;
            clock.Update(new TimeSpan(timeIncrement.Ticks * 6));
            Assert.AreEqual(6, handleTimeStepCallCount);

            handleTimeStepCallCount = 0;
            clock.Update(timeIncrement);
            Assert.AreEqual(1, handleTimeStepCallCount);

            handleTimeStepCallCount = 0;
            clock.Update(new TimeSpan(timeIncrement.Ticks / 2));
            Assert.AreEqual(0, handleTimeStepCallCount);
            clock.Update(new TimeSpan(timeIncrement.Ticks / 2));
            Assert.AreEqual(1, handleTimeStepCallCount);
        }
    public void LostTime()
    {
      ManualClock clock = new ManualClock();
      clock.Start();

      VariableStepTimer timer = new VariableStepTimer(clock);
      timer.TimeChanged += timer_TimeChanged;
      timer.MaxDeltaTime = TimeSpan.FromMilliseconds(50);
      timer.Start();

      clock.Update(TimeSpan.FromMilliseconds(20));
      CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20));
      Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

      clock.Update(TimeSpan.FromMilliseconds(20));
      CheckTimeChangedEvent(TimeSpan.FromMilliseconds(40), TimeSpan.FromMilliseconds(20));
      Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

      clock.Update(TimeSpan.FromMilliseconds(50));
      CheckTimeChangedEvent(TimeSpan.FromMilliseconds(90), TimeSpan.FromMilliseconds(50));
      Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

      clock.Update(TimeSpan.FromMilliseconds(70));
      CheckTimeChangedEvent(TimeSpan.FromMilliseconds(140), TimeSpan.FromMilliseconds(50));
      Assert.AreEqual(TimeSpan.FromMilliseconds(70 - 50), timer.LostTime);

      clock.Update(TimeSpan.FromMilliseconds(80));
      CheckTimeChangedEvent(TimeSpan.FromMilliseconds(190), TimeSpan.FromMilliseconds(50));
      Assert.AreEqual(TimeSpan.FromMilliseconds(80 - 50), timer.LostTime);

      clock.Update(TimeSpan.FromMilliseconds(20));
      CheckTimeChangedEvent(TimeSpan.FromMilliseconds(210), TimeSpan.FromMilliseconds(20));
      Assert.AreEqual(TimeSpan.Zero, timer.LostTime);
    }
Exemplo n.º 10
0
        public void Reset()
        {
            var clock = new ManualClock();
              clock.Start();
              clock.Update(new TimeSpan(100000));
              clock.Reset();

              Assert.IsFalse(clock.IsRunning);
              Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime);
              Assert.AreEqual(TimeSpan.Zero, clock.GameTime);
              Assert.AreEqual(TimeSpan.Zero, clock.TotalTime);

              clock.Start();
              clock.Update(new TimeSpan(100000));
              clock.Stop();
              clock.Reset();

              Assert.IsFalse(clock.IsRunning);
              Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime);
              Assert.AreEqual(TimeSpan.Zero, clock.GameTime);
              Assert.AreEqual(TimeSpan.Zero, clock.TotalTime);
        }
Exemplo n.º 11
0
        public void AccumlateTimeSteps()
        {
            int handleTimeStepCallCount = 0;

              ManualClock clock = new ManualClock();
              clock.Start();

              var timeIncrement = new TimeSpan(166666);
              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = timeIncrement,
            MaxNumberOfSteps = 8,
              };
              timer.TimeChanged += (obj, timeEventArgs) =>
                             {
                               Assert.AreEqual(timeIncrement, timeEventArgs.DeltaTime);
                               handleTimeStepCallCount++;
                             };

              Assert.AreEqual(true, timer.AccumulateTimeSteps);
              timer.AccumulateTimeSteps = false;
              Assert.AreEqual(false, timer.AccumulateTimeSteps);

              timer.Start();
              handleTimeStepCallCount = 0;
              clock.Update(new TimeSpan(timeIncrement.Ticks * 6));
              Assert.AreEqual(6, handleTimeStepCallCount);

              handleTimeStepCallCount = 0;
              clock.Update(timeIncrement);
              Assert.AreEqual(1, handleTimeStepCallCount);

              handleTimeStepCallCount = 0;
              clock.Update(new TimeSpan(timeIncrement.Ticks / 2));
              Assert.AreEqual(0, handleTimeStepCallCount);
              clock.Update(new TimeSpan(timeIncrement.Ticks / 2));
              Assert.AreEqual(1, handleTimeStepCallCount);
        }
Exemplo n.º 12
0
        public void Reset()
        {
            var clock = new ManualClock();

            clock.Start();
            clock.Update(new TimeSpan(100000));
            clock.Reset();

            Assert.IsFalse(clock.IsRunning);
            Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime);
            Assert.AreEqual(TimeSpan.Zero, clock.GameTime);
            Assert.AreEqual(TimeSpan.Zero, clock.TotalTime);

            clock.Start();
            clock.Update(new TimeSpan(100000));
            clock.Stop();
            clock.Reset();

            Assert.IsFalse(clock.IsRunning);
            Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime);
            Assert.AreEqual(TimeSpan.Zero, clock.GameTime);
            Assert.AreEqual(TimeSpan.Zero, clock.TotalTime);
        }
Exemplo n.º 13
0
        public void AccumulatedTime()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            FixedStepTimer timer = new FixedStepTimer(clock)
            {
                StepSize         = TimeSpan.FromMilliseconds(10),
                MaxNumberOfSteps = 5,
            };

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;
            timer.Start();

            clock.Update(TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.Zero, timer.AccumulatedTime);

            clock.Update(TimeSpan.FromMilliseconds(3));
            Assert.AreEqual(TimeSpan.FromMilliseconds(3), timer.AccumulatedTime);

            clock.Update(TimeSpan.FromMilliseconds(6));
            Assert.AreEqual(TimeSpan.FromMilliseconds(9), timer.AccumulatedTime);

            clock.Update(TimeSpan.FromMilliseconds(12));
            Assert.AreEqual(TimeSpan.FromMilliseconds(1), timer.AccumulatedTime);

            // MaxNumberOfSteps exceeded:
            clock.Update(TimeSpan.FromMilliseconds(1000000));
            Assert.AreEqual(TimeSpan.Zero, timer.AccumulatedTime);

            // Accumulated steps.
            timer.AccumulateTimeSteps = true;
            clock.Update(TimeSpan.FromMilliseconds(22));
            Assert.AreEqual(TimeSpan.FromMilliseconds(2), timer.AccumulatedTime);

            timer.AccumulateTimeSteps = false;
            clock.Update(TimeSpan.FromMilliseconds(22));
            timer.TimeChanged += (s, e) =>
            {
                if (timer.PendingSteps > 0)
                {
                    Assert.AreEqual(TimeSpan.Zero, timer.AccumulatedTime);
                }
                else
                {
                    Assert.AreEqual(TimeSpan.FromMilliseconds(4), timer.AccumulatedTime);
                }
            };
        }
Exemplo n.º 14
0
        public void LostTime()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            FixedStepTimer timer = new FixedStepTimer(clock)
            {
                StepSize         = TimeSpan.FromMilliseconds(10),
                MaxNumberOfSteps = 5,
            };

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;
            timer.Start();

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

            clock.Update(TimeSpan.FromMilliseconds(50));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(60), TimeSpan.FromMilliseconds(50));
            Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

            clock.Update(TimeSpan.FromMilliseconds(51));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(110), TimeSpan.FromMilliseconds(50));
            Assert.AreEqual(TimeSpan.FromMilliseconds(1), timer.LostTime);

            clock.Update(TimeSpan.FromMilliseconds(49));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(150), TimeSpan.FromMilliseconds(40));
            Assert.AreEqual(TimeSpan.Zero, timer.LostTime);
        }
Exemplo n.º 15
0
        public void StartStop()
        {
            var clock = new ManualClock();
              var step = new TimeSpan(100000);

              clock.Update(step);

              Assert.IsFalse(clock.IsRunning);
              Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime);
              Assert.AreEqual(TimeSpan.Zero, clock.GameTime);
              Assert.AreEqual(TimeSpan.Zero, clock.TotalTime);

              clock.Start();

              Assert.IsTrue(clock.IsRunning);
              Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime);
              Assert.AreEqual(TimeSpan.Zero, clock.GameTime);
              Assert.AreEqual(TimeSpan.Zero, clock.TotalTime);

              clock.Update(step);

              Assert.IsTrue(clock.IsRunning);
              Assert.AreEqual(step.Ticks, clock.DeltaTime.Ticks);
              Assert.AreEqual(1 * step.Ticks, clock.GameTime.Ticks);
              Assert.AreEqual(1 * step.Ticks, clock.TotalTime.Ticks);

              clock.Stop();
              clock.Update(step);

              Assert.IsFalse(clock.IsRunning);
              Assert.AreEqual(step.Ticks, clock.DeltaTime.Ticks);
              Assert.AreEqual(1 * step.Ticks, clock.GameTime.Ticks);
              Assert.AreEqual(1 * step.Ticks, clock.TotalTime.Ticks);

              clock.Start();
              clock.Update(step);
              clock.Update(step);
              clock.Update(new TimeSpan(step.Ticks * 2));

              Assert.IsTrue(clock.IsRunning);
              Assert.AreEqual(2 * step.Ticks, clock.DeltaTime.Ticks);
              Assert.AreEqual(5 * step.Ticks, clock.GameTime.Ticks);
              Assert.AreEqual(5 * step.Ticks, clock.TotalTime.Ticks);
        }
Exemplo n.º 16
0
        public void NegativeScale()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            VariableStepTimer timer = new VariableStepTimer(clock);

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;
            timer.Speed        = -2.0;
            timer.Start();
            Assert.AreEqual(-2.0, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-20), TimeSpan.FromMilliseconds(-20));

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-40), TimeSpan.FromMilliseconds(-20));

            timer.MinDeltaTime = TimeSpan.FromMilliseconds(20);
            timer.MaxDeltaTime = TimeSpan.FromMilliseconds(50);

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromSeconds(-0.06f), TimeSpan.FromMilliseconds(-20));

            clock.Update(TimeSpan.FromMilliseconds(9));
            CheckIdleEvent(TimeSpan.FromMilliseconds(20 - 18));
            CheckNoTimeChangedEvent();

            clock.Update(TimeSpan.FromTicks(5000)); // 0.5 ms
            CheckIdleEvent(TimeSpan.FromMilliseconds(20 - 19));
            CheckNoTimeChangedEvent();

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-60 - 39), TimeSpan.FromMilliseconds(-39));

            clock.Update(TimeSpan.FromMilliseconds(30));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-60 - 39 - 50), TimeSpan.FromMilliseconds(-50));
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.LostTime);
        }
Exemplo n.º 17
0
        public void Scale()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            FixedStepTimer timer = new FixedStepTimer(clock)
            {
                StepSize         = TimeSpan.FromMilliseconds(10),
                MaxNumberOfSteps = 5,
            };

            timer.TimeChanged += timer_TimeChanged;
            timer.Start();
            Assert.AreEqual(1.0, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));

            timer.Speed = 0.5;
            Assert.AreEqual(0.5, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoTimeChangedEvent();

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(10));

            timer.Speed = 2.0;
            Assert.AreEqual(2.0, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(20));

            timer.Speed = -3.0;
            Assert.AreEqual(-3.0, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(-30));
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(-30), timer.DeltaTime);
        }
Exemplo n.º 18
0
        public void StartStop()
        {
            var clock = new ManualClock();
            var step  = new TimeSpan(100000);

            clock.Update(step);

            Assert.IsFalse(clock.IsRunning);
            Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime);
            Assert.AreEqual(TimeSpan.Zero, clock.GameTime);
            Assert.AreEqual(TimeSpan.Zero, clock.TotalTime);

            clock.Start();

            Assert.IsTrue(clock.IsRunning);
            Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime);
            Assert.AreEqual(TimeSpan.Zero, clock.GameTime);
            Assert.AreEqual(TimeSpan.Zero, clock.TotalTime);

            clock.Update(step);

            Assert.IsTrue(clock.IsRunning);
            Assert.AreEqual(step.Ticks, clock.DeltaTime.Ticks);
            Assert.AreEqual(1 * step.Ticks, clock.GameTime.Ticks);
            Assert.AreEqual(1 * step.Ticks, clock.TotalTime.Ticks);

            clock.Stop();
            clock.Update(step);

            Assert.IsFalse(clock.IsRunning);
            Assert.AreEqual(step.Ticks, clock.DeltaTime.Ticks);
            Assert.AreEqual(1 * step.Ticks, clock.GameTime.Ticks);
            Assert.AreEqual(1 * step.Ticks, clock.TotalTime.Ticks);

            clock.Start();
            clock.Update(step);
            clock.Update(step);
            clock.Update(new TimeSpan(step.Ticks * 2));

            Assert.IsTrue(clock.IsRunning);
            Assert.AreEqual(2 * step.Ticks, clock.DeltaTime.Ticks);
            Assert.AreEqual(5 * step.Ticks, clock.GameTime.Ticks);
            Assert.AreEqual(5 * step.Ticks, clock.TotalTime.Ticks);
        }
Exemplo n.º 19
0
        public void Scale()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            VariableStepTimer timer = new VariableStepTimer(clock);

            timer.TimeChanged += timer_TimeChanged;
            timer.Start();
            Assert.AreEqual(1.0, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(20));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20));

            clock.Update(TimeSpan.FromMilliseconds(20));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(40), TimeSpan.FromMilliseconds(20));

            timer.Speed = 0.5;
            Assert.AreEqual(0.5, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(20));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(10));

            clock.Update(TimeSpan.FromMilliseconds(20));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(60), TimeSpan.FromMilliseconds(10));

            timer.Speed = 2.0;
            Assert.AreEqual(2.0, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(20));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(40));

            timer.Speed = -3.0;
            Assert.AreEqual(-3.0, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(20));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(40), TimeSpan.FromMilliseconds(-60));
            Assert.AreEqual(TimeSpan.FromMilliseconds(40), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(-60), timer.DeltaTime);
        }
Exemplo n.º 20
0
        public void NegativeScale()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            FixedStepTimer timer = new FixedStepTimer(clock)
            {
                StepSize         = TimeSpan.FromMilliseconds(10),
                MaxNumberOfSteps = 5,
            };

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;
            timer.Speed        = -2.0;
            timer.Start();
            Assert.AreEqual(-2.0, timer.Speed);

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-20), TimeSpan.FromMilliseconds(-20));
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-40), TimeSpan.FromMilliseconds(-20));
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            clock.Update(TimeSpan.FromMilliseconds(21));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-80), TimeSpan.FromMilliseconds(-40));
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            clock.Update(TimeSpan.FromMilliseconds(2));
            CheckIdleEvent(TimeSpan.FromMilliseconds(4));
            CheckNoTimeChangedEvent();
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            clock.Update(TimeSpan.FromMilliseconds(4));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-90), TimeSpan.FromMilliseconds(-10));
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            clock.Update(TimeSpan.FromMilliseconds(1));
            CheckIdleEvent(TimeSpan.FromMilliseconds(4));
            CheckNoTimeChangedEvent();
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            clock.Update(TimeSpan.FromMilliseconds(13));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-120), TimeSpan.FromMilliseconds(-30));
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            // 1 ms wall clock time remaining from previous step.

            clock.Update(TimeSpan.FromMilliseconds(25));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-170), TimeSpan.FromMilliseconds(-50));
            Assert.AreEqual(TimeSpan.FromMilliseconds(2), timer.LostTime);
        }
Exemplo n.º 21
0
        public void NormalRun()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            FixedStepTimer timer = new FixedStepTimer(clock)
            {
                StepSize         = TimeSpan.FromMilliseconds(10),
                MaxNumberOfSteps = 5,
            };

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;

            // Clock is running. Timer is stopped.
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckNoTimeChangedEvent();

            // Start
            timer.Start();
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);

            // Pause
            timer.Stop();
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckNoTimeChangedEvent();
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);

            // Resume
            timer.Start();
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            // Time step = 3 ∙ step size
            clock.Update(TimeSpan.FromMilliseconds(34));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(60), TimeSpan.FromMilliseconds(30));
            Assert.AreEqual(TimeSpan.FromMilliseconds(60), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.DeltaTime);
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            // Time step = 3 ∙ step size
            // (27 ms + 4 ms from previous step = 31 ms)
            clock.Update(TimeSpan.FromMilliseconds(27));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(90), TimeSpan.FromMilliseconds(30));
            Assert.AreEqual(TimeSpan.FromMilliseconds(90), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.DeltaTime);
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            // Time step = step size
            // (9 ms + 1 ms from previous step = 10 ms)
            clock.Update(TimeSpan.FromMilliseconds(9));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.FromMilliseconds(100), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);
        }
Exemplo n.º 22
0
        public void AccumulatedTime()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = TimeSpan.FromMilliseconds(10),
            MaxNumberOfSteps = 5,
              };
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;
              timer.Start();

              clock.Update(TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.Zero, timer.AccumulatedTime);

              clock.Update(TimeSpan.FromMilliseconds(3));
              Assert.AreEqual(TimeSpan.FromMilliseconds(3), timer.AccumulatedTime);

              clock.Update(TimeSpan.FromMilliseconds(6));
              Assert.AreEqual(TimeSpan.FromMilliseconds(9), timer.AccumulatedTime);

              clock.Update(TimeSpan.FromMilliseconds(12));
              Assert.AreEqual(TimeSpan.FromMilliseconds(1), timer.AccumulatedTime);

              // MaxNumberOfSteps exceeded:
              clock.Update(TimeSpan.FromMilliseconds(1000000));
              Assert.AreEqual(TimeSpan.Zero, timer.AccumulatedTime);

              // Accumulated steps.
              timer.AccumulateTimeSteps = true;
              clock.Update(TimeSpan.FromMilliseconds(22));
              Assert.AreEqual(TimeSpan.FromMilliseconds(2), timer.AccumulatedTime);

              timer.AccumulateTimeSteps = false;
              clock.Update(TimeSpan.FromMilliseconds(22));
              timer.TimeChanged += (s, e) =>
              {
            if (timer.PendingSteps > 0)
              Assert.AreEqual(TimeSpan.Zero, timer.AccumulatedTime);
            else
              Assert.AreEqual(TimeSpan.FromMilliseconds(4), timer.AccumulatedTime);
              };
        }
Exemplo n.º 23
0
        public void TimerReset()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              VariableStepTimer timer = new VariableStepTimer(clock);
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;
              timer.Reset();
              timer.Start();

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

              timer.Reset();
              Assert.IsFalse(timer.IsRunning);
              Assert.AreEqual(TimeSpan.Zero, timer.Time);
              Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);

              timer.Start();
              clock.Update(TimeSpan.FromMilliseconds(10));
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));

              timer.Stop();
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
              Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);

              timer.Reset();
              Assert.AreEqual(TimeSpan.Zero, timer.Time);
              Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);
        }
Exemplo n.º 24
0
        public void SwitchClocks()
        {
            ManualClock clock1 = new ManualClock();
              ManualClock clock2 = new ManualClock();
              clock1.Start();
              clock2.Start();

              IGameTimer timer = new VariableStepTimer(clock1);
              timer.TimeChanged += timer_TimeChanged;
              timer.Start();

              clock1.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

              timer.Clock = clock2;
              Assert.AreSame(clock2, timer.Clock);
              clock1.Update(TimeSpan.FromMilliseconds(10));
              CheckNoTimeChangedEvent();

              clock2.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(20));

              timer.Clock = null;
              Assert.IsNull(timer.Clock);
              clock1.Update(TimeSpan.FromMilliseconds(10));
              clock2.Update(TimeSpan.FromMilliseconds(20));
              CheckNoTimeChangedEvent();
              Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.DeltaTime);
        }
Exemplo n.º 25
0
        public void Scale()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = TimeSpan.FromMilliseconds(10),
            MaxNumberOfSteps = 5,
              };
              timer.TimeChanged += timer_TimeChanged;
              timer.Start();
              Assert.AreEqual(1.0, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));

              timer.Speed = 0.5;
              Assert.AreEqual(0.5, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoTimeChangedEvent();

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(10));

              timer.Speed = 2.0;
              Assert.AreEqual(2.0, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(20));

              timer.Speed = -3.0;
              Assert.AreEqual(-3.0, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(-30));
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(-30), timer.DeltaTime);
        }
Exemplo n.º 26
0
        public void NormalRun()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              VariableStepTimer timer = new VariableStepTimer(clock);
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;

              // Clock is not running
              clock.Update(TimeSpan.FromMilliseconds(10));
              Assert.IsFalse(timer.IsRunning);
              CheckNoIdleEvent();
              CheckNoTimeChangedEvent();

              // Start/Stop ... not running
              timer.Start();
              timer.Stop();
              clock.Update(TimeSpan.FromMilliseconds(10));
              Assert.IsFalse(timer.IsRunning);
              CheckNoIdleEvent();
              CheckNoTimeChangedEvent();

              // Start
              timer.Start();
              clock.Update(TimeSpan.FromMilliseconds(10));
              Assert.IsTrue(timer.IsRunning);
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
              Assert.AreEqual(TimeSpan.Zero, timer.IdleTime);
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

              // Pause
              timer.Stop();
              clock.Update(TimeSpan.FromMilliseconds(10));
              Assert.IsFalse(timer.IsRunning);
              CheckNoIdleEvent();
              CheckNoTimeChangedEvent();
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);

              // Resume
              timer.Start();
              clock.Update(TimeSpan.FromMilliseconds(10));
              Assert.IsTrue(timer.IsRunning);
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
              Assert.AreEqual(TimeSpan.Zero, timer.IdleTime);
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

              // Stop
              timer.Stop();
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckNoTimeChangedEvent();
        }
Exemplo n.º 27
0
        public void NegativeScale()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              VariableStepTimer timer = new VariableStepTimer(clock);
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;
              timer.Speed = -2.0;
              timer.Start();
              Assert.AreEqual(-2.0, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-20), TimeSpan.FromMilliseconds(-20));

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-40), TimeSpan.FromMilliseconds(-20));

              timer.MinDeltaTime = TimeSpan.FromMilliseconds(20);
              timer.MaxDeltaTime = TimeSpan.FromMilliseconds(50);

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromSeconds(-0.06f), TimeSpan.FromMilliseconds(-20));

              clock.Update(TimeSpan.FromMilliseconds(9));
              CheckIdleEvent(TimeSpan.FromMilliseconds(20 - 18));
              CheckNoTimeChangedEvent();

              clock.Update(TimeSpan.FromTicks(5000)); // 0.5 ms
              CheckIdleEvent(TimeSpan.FromMilliseconds(20 - 19));
              CheckNoTimeChangedEvent();

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-60 - 39), TimeSpan.FromMilliseconds(-39));

              clock.Update(TimeSpan.FromMilliseconds(30));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-60 - 39 - 50), TimeSpan.FromMilliseconds(-50));
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.LostTime);
        }
Exemplo n.º 28
0
        public void NegativeScale()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = TimeSpan.FromMilliseconds(10),
            MaxNumberOfSteps = 5,
              };
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;
              timer.Speed = -2.0;
              timer.Start();
              Assert.AreEqual(-2.0, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-20), TimeSpan.FromMilliseconds(-20));
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-40), TimeSpan.FromMilliseconds(-20));
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              clock.Update(TimeSpan.FromMilliseconds(21));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-80), TimeSpan.FromMilliseconds(-40));
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              clock.Update(TimeSpan.FromMilliseconds(2));
              CheckIdleEvent(TimeSpan.FromMilliseconds(4));
              CheckNoTimeChangedEvent();
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              clock.Update(TimeSpan.FromMilliseconds(4));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-90), TimeSpan.FromMilliseconds(-10));
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              clock.Update(TimeSpan.FromMilliseconds(1));
              CheckIdleEvent(TimeSpan.FromMilliseconds(4));
              CheckNoTimeChangedEvent();
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              clock.Update(TimeSpan.FromMilliseconds(13));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-120), TimeSpan.FromMilliseconds(-30));
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              // 1 ms wall clock time remaining from previous step.

              clock.Update(TimeSpan.FromMilliseconds(25));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(-170), TimeSpan.FromMilliseconds(-50));
              Assert.AreEqual(TimeSpan.FromMilliseconds(2), timer.LostTime);
        }
Exemplo n.º 29
0
        public void NormalRun()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            VariableStepTimer timer = new VariableStepTimer(clock);

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;

            // Clock is not running
            clock.Update(TimeSpan.FromMilliseconds(10));
            Assert.IsFalse(timer.IsRunning);
            CheckNoIdleEvent();
            CheckNoTimeChangedEvent();

            // Start/Stop ... not running
            timer.Start();
            timer.Stop();
            clock.Update(TimeSpan.FromMilliseconds(10));
            Assert.IsFalse(timer.IsRunning);
            CheckNoIdleEvent();
            CheckNoTimeChangedEvent();

            // Start
            timer.Start();
            clock.Update(TimeSpan.FromMilliseconds(10));
            Assert.IsTrue(timer.IsRunning);
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
            Assert.AreEqual(TimeSpan.Zero, timer.IdleTime);
            Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

            // Pause
            timer.Stop();
            clock.Update(TimeSpan.FromMilliseconds(10));
            Assert.IsFalse(timer.IsRunning);
            CheckNoIdleEvent();
            CheckNoTimeChangedEvent();
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);

            // Resume
            timer.Start();
            clock.Update(TimeSpan.FromMilliseconds(10));
            Assert.IsTrue(timer.IsRunning);
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
            Assert.AreEqual(TimeSpan.Zero, timer.IdleTime);
            Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

            // Stop
            timer.Stop();
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckNoTimeChangedEvent();
        }
Exemplo n.º 30
0
        public void NormalRun()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = TimeSpan.FromMilliseconds(10),
            MaxNumberOfSteps = 5,
              };
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;

              // Clock is running. Timer is stopped.
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckNoTimeChangedEvent();

              // Start
              timer.Start();
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);

              // Pause
              timer.Stop();
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckNoTimeChangedEvent();
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);

              // Resume
              timer.Start();
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              // Time step = 3 ∙ step size
              clock.Update(TimeSpan.FromMilliseconds(34));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(60), TimeSpan.FromMilliseconds(30));
              Assert.AreEqual(TimeSpan.FromMilliseconds(60), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.DeltaTime);
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              // Time step = 3 ∙ step size
              // (27 ms + 4 ms from previous step = 31 ms)
              clock.Update(TimeSpan.FromMilliseconds(27));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(90), TimeSpan.FromMilliseconds(30));
              Assert.AreEqual(TimeSpan.FromMilliseconds(90), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.DeltaTime);
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              // Time step = step size
              // (9 ms + 1 ms from previous step = 10 ms)
              clock.Update(TimeSpan.FromMilliseconds(9));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.FromMilliseconds(100), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);
        }
Exemplo n.º 31
0
        public void TimeChanged()
        {
            var clock = new ManualClock();
              var step = new TimeSpan(100000);

              int numberOfEvents = 0;
              TimeSpan deltaTime = TimeSpan.Zero;
              TimeSpan gameTime = TimeSpan.Zero;
              TimeSpan totalTime = TimeSpan.Zero;

              clock.TimeChanged += (s, e) =>
                           {
                             numberOfEvents++;
                             deltaTime = e.DeltaTime;
                             gameTime = e.GameTime;
                             totalTime = e.TotalTime;
                           };

              clock.Update(step);

              Assert.AreEqual(0, numberOfEvents);

              clock.Start();
              clock.Update(step);
              clock.Update(step);

              Assert.AreEqual(step.Ticks, deltaTime.Ticks);
              Assert.AreEqual(2 * step.Ticks, gameTime.Ticks);
              Assert.AreEqual(2 * step.Ticks, totalTime.Ticks);

              clock.Stop();
              clock.Update(step);
        }
Exemplo n.º 32
0
        public void LostTime()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = TimeSpan.FromMilliseconds(10),
            MaxNumberOfSteps = 5,
              };
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;
              timer.Start();

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

              clock.Update(TimeSpan.FromMilliseconds(50));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(60), TimeSpan.FromMilliseconds(50));
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);

              clock.Update(TimeSpan.FromMilliseconds(51));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(110), TimeSpan.FromMilliseconds(50));
              Assert.AreEqual(TimeSpan.FromMilliseconds(1), timer.LostTime);

              clock.Update(TimeSpan.FromMilliseconds(49));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(150), TimeSpan.FromMilliseconds(40));
              Assert.AreEqual(TimeSpan.Zero, timer.LostTime);
        }
Exemplo n.º 33
0
        public void Scale()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              VariableStepTimer timer = new VariableStepTimer(clock);
              timer.TimeChanged += timer_TimeChanged;
              timer.Start();
              Assert.AreEqual(1.0, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20));

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(40), TimeSpan.FromMilliseconds(20));

              timer.Speed = 0.5;
              Assert.AreEqual(0.5, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(10));

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(60), TimeSpan.FromMilliseconds(10));

              timer.Speed = 2.0;
              Assert.AreEqual(2.0, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(40));

              timer.Speed = -3.0;
              Assert.AreEqual(-3.0, timer.Speed);

              clock.Update(TimeSpan.FromMilliseconds(20));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(40), TimeSpan.FromMilliseconds(-60));
              Assert.AreEqual(TimeSpan.FromMilliseconds(40), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(-60), timer.DeltaTime);
        }
Exemplo n.º 34
0
        public void AccumulatedStepsAndPendingSteps()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = TimeSpan.FromMilliseconds(10),
            MaxNumberOfSteps = 5,
              };
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;
              timer.Start();

              timer.AccumulateTimeSteps = true;

              int desiredPendingSteps = 0;
              int desiredAccumlatedSteps = 2;

              timer.TimeChanged += (s, e) =>
              {
            Assert.AreEqual(desiredPendingSteps, timer.PendingSteps);
            Assert.AreEqual(desiredAccumlatedSteps, timer.AccumulatedSteps);
            desiredPendingSteps--;
              };

              clock.Update(TimeSpan.FromMilliseconds(22));

              timer.Reset();
              timer.Start();

              timer.AccumulateTimeSteps = false;

              desiredPendingSteps = 3;
              desiredAccumlatedSteps = 1;
              clock.Update(TimeSpan.FromMilliseconds(42));

              Assert.AreEqual(-1, desiredPendingSteps);
        }