All pastes #783397 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-11-19 05:27 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Ultima.XNA.Diagnostics
{
    public class FramerateComponent : DrawableGameComponent
    {
        private double _fps = 0;
        private float _elapsedRealTime = 0;
        private string _gameWindowTitle;
        private ushort _updateFrequency = 1000;
        private float _updateFrequencyRatio;
        
        public double CurrentFramerate
        {
            get
            {
                return _fps;
            }
        }

        public ushort UpdateFrequency
        {
            get
            {
                return _updateFrequency;
            }
            set
            {
                if (value % 100 != 0 || value < 100)
                {
                    throw new ArgumentOutOfRangeException("UpdateFrequency", value, "The UpdateFrequency for the Framerate must is based on milliseconds and must be must be a positive number that is greater then or equal to 100, and the number must be divisable by 100.");
                }

                _updateFrequency = value;
                _updateFrequencyRatio = 1000 / (float)_updateFrequency;
            }
        }

        public FramerateComponent(Game game)
            : base(game)
        {
            _gameWindowTitle = game.Window.Title;
            _updateFrequencyRatio = 1000 / (float)_updateFrequency;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (!this.Enabled)
            {
                return;
            }

            float elapsed = (float)gameTime.ElapsedRealTime.TotalMilliseconds;

            _elapsedRealTime += elapsed;

            if (_elapsedRealTime > _updateFrequency)
            {
                _fps = (_updateFrequency / elapsed) * _updateFrequencyRatio;
                _elapsedRealTime -= _updateFrequency;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            if (!this.Visible || !this.Enabled)
            {
                return;
            }

            this.Game.Window.Title = _gameWindowTitle + " FPS: " + _fps.ToString("F");
        }

        protected override void OnEnabledChanged(object sender, EventArgs args)
        {
            if (!this.Enabled)
            {
                this.Game.Window.Title = _gameWindowTitle;
            }

            base.OnEnabledChanged(sender, args);
        }

        protected override void OnVisibleChanged(object sender, EventArgs args)
        {
            if (!this.Visible)
            {
                this.Game.Window.Title = _gameWindowTitle;
            }

            base.OnVisibleChanged(sender, args);
        }

    }
}