public void Reset() { var clock = new HighPrecisionClock(); clock.Start(); clock.Update(); Thread.Sleep(2); 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(); Thread.Sleep(2); clock.Update(); 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); }
public void StartStop() { const long ticks = 100000; TimeSpan timeSpan = new TimeSpan(ticks); var clock = new HighPrecisionClock(); Wait(timeSpan); clock.Update(); Assert.IsFalse(clock.IsRunning); Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime); Assert.AreEqual(TimeSpan.Zero, clock.GameTime); Assert.AreEqual(TimeSpan.Zero, clock.TotalTime); clock.Start(); Stopwatch w = Stopwatch.StartNew(); Wait(timeSpan); w.Stop(); Assert.IsTrue(clock.IsRunning); Assert.AreEqual(TimeSpan.Zero, clock.DeltaTime); Assert.AreEqual(TimeSpan.Zero, clock.GameTime); Assert.AreEqual(TimeSpan.Zero, clock.TotalTime); clock.Update(); Assert.IsTrue(clock.IsRunning); Assert.IsTrue(clock.DeltaTime.Ticks > ticks && clock.DeltaTime.Ticks < 2 * ticks); Assert.IsTrue(clock.GameTime.Ticks > ticks && clock.GameTime.Ticks < 2 * ticks); Assert.IsTrue(clock.TotalTime.Ticks > ticks && clock.TotalTime.Ticks < 2 * ticks); clock.Stop(); Wait(timeSpan); clock.Update(); Assert.IsFalse(clock.IsRunning); Assert.IsTrue(clock.DeltaTime.Ticks > ticks && clock.DeltaTime.Ticks < 2 * ticks); Assert.IsTrue(clock.GameTime.Ticks > ticks && clock.GameTime.Ticks < 2 * ticks); Assert.IsTrue(clock.TotalTime.Ticks > ticks && clock.TotalTime.Ticks < 2 * ticks); clock.Start(); Wait(timeSpan); clock.Update(); Wait(timeSpan); Wait(timeSpan); clock.Update(); Assert.IsTrue(clock.IsRunning); Assert.IsTrue(clock.DeltaTime.Ticks > 2 * ticks && clock.DeltaTime.Ticks < 3 * ticks); Assert.IsTrue(clock.GameTime.Ticks > 4 * ticks && clock.GameTime.Ticks < 5 * ticks); Assert.IsTrue(clock.TotalTime.Ticks > 4 * ticks && clock.TotalTime.Ticks < 5 * ticks); }
public void IsRunning() { var clock = new HighPrecisionClock(); Assert.IsFalse(clock.IsRunning); clock.Start(); Assert.IsTrue(clock.IsRunning); clock.Start(); Assert.IsTrue(clock.IsRunning); clock.Stop(); Assert.IsFalse(clock.IsRunning); clock.Start(); Assert.IsTrue(clock.IsRunning); clock.Stop(); clock.Stop(); Assert.IsFalse(clock.IsRunning); }
/// <summary> /// Initializes the game. /// </summary> private static void Initialize() { _clock = new HighPrecisionClock(); _timer = new FixedStepTimer(_clock); _clock.Start(); _timer.Start(); }
/// <summary> /// Initializes the game. /// </summary> public static void Initialize() { _clock = new HighPrecisionClock(); _timer = new FixedStepTimer(_clock); _timer.TimeChanged += Update; _timer.Idle += Idle; _clock.Start(); _timer.Start(); }
public void ResetDeltaTime() { var clock = new HighPrecisionClock(); clock.Start(); Wait(new TimeSpan(100000)); clock.Update(); Assert.IsTrue(clock.DeltaTime.Ticks >= 100000); Assert.AreEqual(clock.GameTime, clock.TotalTime); clock.ResetDeltaTime(); // DeltaTime is only changed at TimeChanged events. Assert.IsTrue(clock.DeltaTime.Ticks >= 100000); Assert.AreEqual(clock.GameTime, clock.TotalTime); clock.Update(); Assert.IsTrue(clock.DeltaTime.Ticks < 100000); Assert.IsTrue(clock.GameTime < clock.TotalTime); }
private void AddGameLoop() { _clock = new HighPrecisionClock(); _clock.Start(); CompositionTarget.Rendering += OnCompositionTargetRendering; ComponentDispatcher.ThreadIdle += OnApplicationIdle; // The FixedStepTimer reads the clock and triggers the game loop at 60 Hz. _timer = new FixedStepTimer(_clock) { StepSize = new TimeSpan(166667), // ~60 Hz //StepSize = new TimeSpan(333333), // ~30 Hz AccumulateTimeSteps = false, }; // The VariableStepTimer reads the clock and triggers the game loop as often // as possible. //_timer = new VariableStepTimer(_clock); _timer.TimeChanged += (s, e) => GameLoop(e.DeltaTime); _timer.Start(); Editor.Services.Register(typeof(IGameTimer), null, _timer); }
public void TimeChanged() { const long ticks = 100000; TimeSpan timeSpan = new TimeSpan(ticks); var clock = new HighPrecisionClock(); 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; }; Wait(timeSpan); clock.Update(); Assert.AreEqual(0, numberOfEvents); clock.Start(); Wait(timeSpan); clock.Update(); Wait(timeSpan); clock.Update(); Assert.IsTrue(clock.DeltaTime.Ticks > 1 * ticks && clock.DeltaTime.Ticks < 2 * ticks); Assert.IsTrue(clock.GameTime.Ticks > 2 * ticks && clock.GameTime.Ticks < 3 * ticks); Assert.IsTrue(clock.TotalTime.Ticks > 2 * ticks && clock.TotalTime.Ticks < 3 * ticks); clock.Stop(); clock.Update(); }
//-------------------------------------------------------------- public MyGame() { Lock = new object(); // ----- Service Container // 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.) _serviceContainer = new ServiceContainer(); ServiceLocator.SetLocatorProvider(() => _serviceContainer); _serviceContainer.Register(typeof(MyGame), null, this); // ----- Storage // Create a "virtual file system" for reading game assets. _titleStorage = new TitleStorage("Content"); _assetsStorage = new ZipStorage(_titleStorage, "Content.zip"); _digitalRuneStorage = new ZipStorage(_titleStorage, "DigitalRune.zip"); _vfsStorage = new VfsStorage(); _vfsStorage.MountInfos.Add(new VfsMountInfo(_assetsStorage, null)); _vfsStorage.MountInfos.Add(new VfsMountInfo(_digitalRuneStorage, null)); // ----- Content _contentManager = new StorageContentManager(ServiceLocator.Current, _vfsStorage); _serviceContainer.Register(typeof(ContentManager), null, _contentManager); // ----- Graphics // Create Direct3D 11 device. var presentationParameters = new PresentationParameters { BackBufferWidth = 1, BackBufferHeight = 1, // 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 DummyGraphicsDeviceManager(graphicsDevice)); // Get DXGIOutput to call WaitForVerticalBlank() in the game loop. using (var dxgiFactory = new SharpDX.DXGI.Factory1()) using (var dxgiAdapter = dxgiFactory.GetAdapter1(0)) _dxgiOutput = dxgiAdapter.GetOutput(0); // Create and register the graphics manager. _graphicsManager = new GraphicsManager(graphicsDevice, _contentManager); _serviceContainer.Register(typeof(IGraphicsService), null, _graphicsManager); // ----- Timing // The game loop runs in a parallel thread to keep the UI thread responsive. // To measure the time that has passed, we use a HighPrecisionClock. _clock = new HighPrecisionClock(); _clock.Start(); _gameLoopTask = ThreadPool.RunAsync(GameLoopTaskAction, WorkItemPriority.High, WorkItemOptions.TimeSliced) .AsTask(); // 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. _timer = new VariableStepTimer(_clock); _timer.TimeChanged += (s, e) => GameLoop(e.DeltaTime); _timer.Start(); CoreApplication.Suspending += OnCoreApplicationSuspending; // DirectX buffers only a limit amount of Present calls per frame which is controlled by // the MaximumFrameLatency property. The default value is usually 3. If the application // uses more SwapChainPresentationTargets we must increase this property. //var d3dDevice = (SharpDX.Direct3D11.Device)_graphicsManager.GraphicsDevice.Handle; //using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>()) // dxgiDevice2.MaximumFrameLatency = numberOfSwapChainPanels; }
//-------------------------------------------------------------- #region Creation & Cleanup //-------------------------------------------------------------- public MyGame() { Lock = new object(); // ----- Service Container // 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.) _serviceContainer = new ServiceContainer(); ServiceLocator.SetLocatorProvider(() => _serviceContainer); _serviceContainer.Register(typeof(MyGame), null, this); // ----- Storage // Create a "virtual file system" for reading game assets. _titleStorage = new TitleStorage("Content"); _assetsStorage = new ZipStorage(_titleStorage, "Content.zip"); _digitalRuneStorage = new ZipStorage(_titleStorage, "DigitalRune.zip"); _vfsStorage = new VfsStorage(); _vfsStorage.MountInfos.Add(new VfsMountInfo(_assetsStorage, null)); _vfsStorage.MountInfos.Add(new VfsMountInfo(_digitalRuneStorage, null)); // ----- Content _contentManager = new StorageContentManager(ServiceLocator.Current, _vfsStorage); _serviceContainer.Register(typeof(ContentManager), null, _contentManager); // ----- Graphics // Create Direct3D 11 device. var presentationParameters = new PresentationParameters { BackBufferWidth = 1, BackBufferHeight = 1, // 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 DummyGraphicsDeviceManager(graphicsDevice)); // Get DXGIOutput to call WaitForVerticalBlank() in the game loop. using (var dxgiFactory = new SharpDX.DXGI.Factory1()) using (var dxgiAdapter = dxgiFactory.GetAdapter1(0)) _dxgiOutput = dxgiAdapter.GetOutput(0); // Create and register the graphics manager. _graphicsManager = new GraphicsManager(graphicsDevice, _contentManager); _serviceContainer.Register(typeof(IGraphicsService), null, _graphicsManager); // ----- Timing // The game loop runs in a parallel thread to keep the UI thread responsive. // To measure the time that has passed, we use a HighPrecisionClock. _clock = new HighPrecisionClock(); _clock.Start(); _gameLoopTask = ThreadPool.RunAsync(GameLoopTaskAction, WorkItemPriority.High, WorkItemOptions.TimeSliced) .AsTask(); // 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. _timer = new VariableStepTimer(_clock); _timer.TimeChanged += (s, e) => GameLoop(e.DeltaTime); _timer.Start(); CoreApplication.Suspending += OnCoreApplicationSuspending; // DirectX buffers only a limit amount of Present calls per frame which is controlled by // the MaximumFrameLatency property. The default value is usually 3. If the application // uses more SwapChainPresentationTargets we must increase this property. //var d3dDevice = (SharpDX.Direct3D11.Device)_graphicsManager.GraphicsDevice.Handle; //using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>()) // dxgiDevice2.MaximumFrameLatency = numberOfSwapChainPanels; }
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 MyGame() { // ----- 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.) _serviceContainer = new ServiceContainer(); ServiceLocator.SetLocatorProvider(() => _serviceContainer); // ----- Storage // Create a "virtual file system" for reading game assets. var titleStorage = new TitleStorage("Content"); var assetsStorage = new ZipStorage(titleStorage, "Content.zip"); var digitalRuneStorage = new ZipStorage(titleStorage, "DigitalRune.zip"); var vfsStorage = new VfsStorage(); vfsStorage.MountInfos.Add(new VfsMountInfo(titleStorage, null)); vfsStorage.MountInfos.Add(new VfsMountInfo(assetsStorage, null)); vfsStorage.MountInfos.Add(new VfsMountInfo(digitalRuneStorage, null)); // ----- Content _contentManager = new StorageContentManager(ServiceLocator.Current, vfsStorage); _serviceContainer.Register(typeof(ContentManager), null, _contentManager); // ----- Graphics // Create Direct3D 11 device. var presentationParameters = new PresentationParameters { BackBufferWidth = 1, BackBufferHeight = 1, // 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 DummyGraphicsDeviceManager(graphicsDevice)); // Create and register the graphics manager. _graphicsManager = new GraphicsManager(graphicsDevice, _contentManager); _serviceContainer.Register(typeof(IGraphicsService), null, _graphicsManager); // ----- 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. _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. _timer = new VariableStepTimer(_clock); _timer.TimeChanged += (s, e) => GameLoop(e.DeltaTime); _timer.Start(); }