Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DebugGraphicsScreen" /> class.
        /// </summary>
        /// <param name="services">The services.</param>
        public DebugGraphicsScreen(IServiceLocator services)
            : base(services?.GetInstance <IGraphicsService>())
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            Coverage      = GraphicsScreenCoverage.Partial;
            _spriteBatch  = GraphicsService.GetSpriteBatch();
            _whiteTexture = GraphicsService.GetDefaultTexture2DWhite();

            var contentManager = services.GetInstance <ContentManager>();
            var spriteFont     = contentManager.Load <SpriteFont>("DigitalRune.Editor.Game/Fonts/DejaVuSans");

            DebugRenderer          = new DebugRenderer(GraphicsService, spriteFont);
            _internalDebugRenderer = new DebugRenderer(GraphicsService, spriteFont);

            // To count the update frame rate, we handle the GameLogicUpdating event.
            // (We cannot use GraphicsScreen.OnUpdate because it is only called at the same rate if
            // the graphics screen is registered in the graphics service. If it is not registered,
            // then OnUpdate and OnRender are always called together.)
            var editor = services.GetInstance <IEditorService>();

            _gameExtension = editor.Extensions.OfType <GameExtension>().FirstOrDefault();
            if (_gameExtension != null)
            {
                _gameExtension.GameLogicUpdating += OnGameLogicUpdating;
            }
        }
Ejemplo n.º 2
0
        private void UpdateTimer()
        {
            if (WindowsHelper.IsInDesignMode)
            {
                return;
            }

            if (_gameTimer == null)
            {
                if (AssociatedObject.IsLoaded)
                {
                    var editor = EditorHelper.GetEditor(AssociatedObject).ThrowIfMissing();
                    _gameTimer     = editor.Services.GetInstance <IGameTimer>().ThrowIfMissing();
                    _gameExtension = editor.Extensions.OfType <GameExtension>().FirstOrDefault().ThrowIfMissing();
                }
                else
                {
                    AssociatedObject.Loaded += (s, e) => UpdateTimer();
                    return;
                }
            }

            // Start/stop timer based on current state.
            var view = AssociatedObject;

            if (view != null && view.IsFocused && IsEnabled)
            {
                _gameExtension.GameLoopNewFrame += OnTick;
            }
            else
            {
                _gameExtension.GameLoopNewFrame -= OnTick;
            }
        }