Beispiel #1
0
        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);
        }
Beispiel #2
0
        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();
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        //--------------------------------------------------------------
        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;
        }
Beispiel #5
0
        //--------------------------------------------------------------
        #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;
        }
Beispiel #6
0
        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();
        }