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); }
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); }
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); }
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); }
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); }
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); }
public Game(ApplicationWindow window) { // ----- Service Container // The MyGame uses a ServiceContainer, which is a simple service locator // and Inversion of Control (IoC) container. (The ServiceContainer can be // replaced by any other container that implements System.IServiceProvider.) var serviceContainer = (ServiceContainer)ServiceLocator.Current; // ----- Storage // Create a "virtual file system" for reading game assets. var vfsStorage = new VfsStorage(); vfsStorage.MountInfos.Add(new VfsMountInfo(new TitleStorage(String.Empty), null)); try { vfsStorage.MountInfos.Add(new VfsMountInfo(new GZipStorage(vfsStorage, "Data.bin"), null)); vfsStorage.MountInfos.Add(new VfsMountInfo(new GZipStorage(vfsStorage, "Kesmai.bin"), null)); vfsStorage.MountInfos.Add(new VfsMountInfo(new GZipStorage(vfsStorage, "Stormhalter.bin"), null)); vfsStorage.MountInfos.Add(new VfsMountInfo(new GZipStorage(vfsStorage, "UI.bin"), null)); } catch { MessageBox.Show("Missing either Data.bin, Kesmai.bin, Stormhalter.bin, or UI.bin."); throw; } vfsStorage.Readers.Add(typeof(XDocument), new XDocumentReader()); // ----- Content ContentManager contentManager = new StorageContentManager(ServiceLocator.Current, vfsStorage); serviceContainer.Register(typeof(IStorage), null, vfsStorage); serviceContainer.Register(typeof(ContentManager), null, contentManager); #if (DEBUG) /* Hack to allow content reading from external library. Release builds have the types IL merged. */ ContentTypeReaderManager.AddTypeCreator("DigitalRune.Game.UI.Content.ThemeReader", () => new ThemeReader()); ContentTypeReaderManager.AddTypeCreator("DigitalRune.Mathematics.Content.Vector4FReader", () => new Vector4FReader()); ContentTypeReaderManager.AddTypeCreator("DigitalRune.Game.UI.BitmapFontReader", () => new BitmapFontReader()); #endif // ----- Graphics // Create Direct3D 11 device. var presentationParameters = new PresentationParameters { // Do not associate graphics device with any window. DeviceWindowHandle = IntPtr.Zero, }; var graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.HiDef, presentationParameters); // An IGraphicsDeviceService is required by the MonoGame/XNA content manager. serviceContainer.Register(typeof(IGraphicsDeviceService), null, new GraphicsDeviceManager(graphicsDevice)); // Create and register the graphics manager. _graphicsManager = new GraphicsManager(graphicsDevice, contentManager); serviceContainer.Register(typeof(IGraphicsService), null, _graphicsManager); serviceContainer.Register(typeof(TerrainManager), null, new TerrainManager()); // ----- Timing // We can use the CompositionTarget.Rendering event to trigger our game loop. // The CompositionTarget.Rendering event is raised once per frame by WPF. // To measure the time that has passed, we use a HighPrecisionClock. var clock = new HighPrecisionClock(); clock.Start(); CompositionTarget.Rendering += (s, e) => clock.Update(); // The FixedStepTimer reads the clock and triggers the game loop at 60 Hz. //_timer = new FixedStepTimer(_clock) //{ // StepSize = new TimeSpan(166667), // ~60 Hz // AccumulateTimeSteps = false, //}; // The VariableStepTimer reads the clock and triggers the game loop as often // as possible. IGameTimer timer = new VariableStepTimer(clock); timer.TimeChanged += (s, e) => GameLoop(e.DeltaTime); timer.Start(); }
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(); }