问题
I'm just wondering, while making a game, I noticed that the GameTime value for IsRunningSlowly returns false when my game has focus (like it should), but when I change applications, it changes to true. I even made a empty game, and even when it losses focus, the GameTime value for IsRunningSlowly returns true as well. I'm wondering why does it do this? Is it just my computer, or did the creators of XNA design it this way? The frame rate seems fine, but the value is true. No big deal really, I'm just really curious!
[Empty Game]
public class Game1 : Microsoft.Xna.Framework.Game
{
#region Constuctors
public Game1()
{
this.GraphicsManager = new Microsoft.Xna.Framework.GraphicsDeviceManager(this);
this.Content.RootDirectory = "Content";
}
#endregion
#region Overrides
protected override void LoadContent()
{
this.SpriteBatch = new Microsoft.Xna.Framework.Graphics.SpriteBatch(this.GraphicsDevice);
base.LoadContent();
}
protected override void Update(Microsoft.Xna.Framework.GameTime GameTime)
{
System.Console.WriteLine(GameTime.IsRunningSlowly);
Microsoft.Xna.Framework.Input.KeyboardState Keyboard = Microsoft.Xna.Framework.Input.Keyboard.GetState();
if (Keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape)) this.Exit();
base.Update(GameTime);
}
protected override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
{
GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
base.Draw(gameTime);
}
#endregion
#region Variables
private Microsoft.Xna.Framework.GraphicsDeviceManager GraphicsManager { get; set; }
private Microsoft.Xna.Framework.Graphics.SpriteBatch SpriteBatch { get; set; }
#endregion
}
回答1:
I'll try and answer the question, and not post a "this is just how it is". The XNA thread sleeps when it loses focus. It is configured in XNA's game class.
InactiveSleepTime = new TimeSpan(0);
It's set to 20ms by default, and setting it to 0 will make XNA run full speed while it has lost focus.
IsRunningSlowly gets set to true since the framerate drops below 60 (which is the default when IsFixedTimeStep is set)
回答2:
This is by design. The games are one of the most resource consuming kinds of applications out there, so naturally XNA framework developers thought it might be a good idea to drop some frames when the game is not in focus to save some CPU for other operations you might want to do while the game is in background.
XNA game also doesn't make Draw
calls when its main window is minimized. I believe the Update
calls are never dropped (though I may be wrong).
来源:https://stackoverflow.com/questions/15031898/xna-is-running-slow-when-focus-is-removed