|
Links of Interest
MainDownload Prerequisites Screenshots Articles/Code Snippets DBP Warmup Entries Blog Contact/Feedback Quick Downloads:
Diaspora
XNA Requirements Installer
External Links
Heather [layout designer]XNA |
Statistics Component
This component prints out the FPS and the average number of milliseconds it takes to update and draw a frame. It also supports one custom measurement, but it is easy to extend the class to have multiple custom measurements. The usage is simple. However, you want to retain a reference to the component to call the methods later.
statisticsComponent = new StatisticsComponent(this);
Repeat for the Draw method and/or your custom measurement. And here is the actual component:
this.Components.Add(statisticsComponent); protected override void Update(GameTime gameTime) { statisticsComponent.BeginUpdate(); // Process the game update statements. statisticsComponent.EndUpdate(); } class StatisticsComponent : DrawableGameComponent
{ Stopwatch stopwatch; Stopwatch customWatch; long updateMSPerSecond; long drawMSPerSecond; int frames; int totalframes; float elapsedSeconds; float totalElapsedSeconds; bool updateMSChecked; bool drawMSChecked; string customName; float elapsedCustom; bool customCheckEnabled; public StatisticsComponent(Game game) : base(game) { stopwatch = new Stopwatch(); customWatch = new Stopwatch(); game.Exiting += new EventHandler(game_Exiting); } public void BeginUpdate() { stopwatch.Start(); } public void EndUpdate() { updateMSPerSecond += stopwatch.ElapsedMilliseconds; stopwatch.Stop(); stopwatch.Reset(); updateMSChecked = true; } public void BeginDraw() { stopwatch.Start(); } public override void Draw(GameTime gameTime) { frames++; totalframes++; elapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds; totalElapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds; if (elapsedSeconds >= 1) { elapsedSeconds -= 1; string str = "FPS: " + frames; if (updateMSChecked) str += ", UpdateMS/Frame: " + (updateMSPerSecond / (float)frames); if (drawMSChecked) str += ", DrawMS/Frame: " + (drawMSPerSecond / (float)frames); if (customCheckEnabled) str += ", " + customName + " MS/Frame: " + (elapsedCustom / (float)frames); Console.WriteLine(str); Game.Window.Title = str; frames = 0; drawMSPerSecond = 0; updateMSPerSecond = 0; elapsedCustom = 0; } base.Draw(gameTime); } public void EndDraw() { drawMSPerSecond += stopwatch.ElapsedMilliseconds; stopwatch.Stop(); stopwatch.Reset(); drawMSChecked = true; } public void EnabledCustom(string name) { customName = name; customCheckEnabled = true; } public void DisableCustom() { customCheckEnabled = false; } public void BeginCustom() { customWatch.Start(); } public void EndCustom() { elapsedCustom += stopwatch.ElapsedMilliseconds; customWatch.Stop(); customWatch.Reset(); } void game_Exiting(object sender, EventArgs e) { Console.WriteLine("Average FPS: " + totalframes / totalElapsedSeconds); } } |