Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I put together the following code on XNA 4 and it's getting a locked 60 fps. Try it out on your system (You'll just have to add the appropriate sprite font) and see if you get 60 fps too. If so, put the adjustments in your problem code and see if you get the same result.</p> <pre class="lang-cs prettyprint-override"><code>using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace _60fps { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont OutputFont; float Fps = 0f; private const int NumberSamples = 50; //Update fps timer based on this number of samples int[] Samples = new int[NumberSamples]; int CurrentSample = 0; int TicksAggregate = 0; int SecondSinceStart = 0; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); graphics.SynchronizeWithVerticalRetrace = false; int DesiredFrameRate = 60; TargetElapsedTime = new TimeSpan(TimeSpan.TicksPerSecond / DesiredFrameRate); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); OutputFont = Content.Load&lt;SpriteFont&gt;("MessageFont"); } protected override void UnloadContent() {/* Nothing to do */} protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) this.Exit(); base.Update(gameTime); } private float Sum(int[] Samples) { float RetVal = 0f; for (int i = 0; i &lt; Samples.Length; i++) { RetVal += (float)Samples[i]; } return RetVal; } private Color ClearColor = Color.FromNonPremultiplied(20, 20, 40, 255); protected override void Draw(GameTime gameTime) { Samples[CurrentSample++] = (int)gameTime.ElapsedGameTime.Ticks; TicksAggregate += (int)gameTime.ElapsedGameTime.Ticks; if (TicksAggregate &gt; TimeSpan.TicksPerSecond) { TicksAggregate -= (int)TimeSpan.TicksPerSecond; SecondSinceStart += 1; } if (CurrentSample == NumberSamples) //We are past the end of the array since the array is 0-based and NumberSamples is 1-based { float AverageFrameTime = Sum(Samples) / NumberSamples; Fps = TimeSpan.TicksPerSecond / AverageFrameTime; CurrentSample = 0; } GraphicsDevice.Clear(ClearColor); spriteBatch.Begin(); if (Fps &gt; 0) { spriteBatch.DrawString(OutputFont, string.Format("Current FPS: {0}\r\nTime since startup: {1}", Fps.ToString("000"), TimeSpan.FromSeconds(SecondSinceStart).ToString()), new Vector2(10,10), Color.White); } spriteBatch.End(); base.Draw(gameTime); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload