微软在Windows Phone 7.1系统里面取消了XNA最大帧率只能达到30帧的限制,但是直接修改Game.TargetElapsedTime属性在模拟器上有效,但在真实设备上无效,按照下面的步骤就可以解决这个问题:
1.在Game的构造函数中修改Game.TargetElapsedTime属性(如果要达到60帧,只要在数字后面加上"/2")就行了
2.插入这样下面代码:
graphics.PreparingDeviceSettings += (sender, e) => le.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.One;
提供一个测试XNA帧率的例程(60帧):
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
namespace WindowsPhoneGame
{
public class MyGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private TimeSpan _elapsedTime = TimeSpan.Zero;
private int _frameCounter;
private int _frameRate;
private Vector2 _position=new Vector2(0,0);
private SpriteFont _spriteFont;
{
public class MyGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private TimeSpan _elapsedTime = TimeSpan.Zero;
private int _frameCounter;
private int _frameRate;
private Vector2 _position=new Vector2(0,0);
private SpriteFont _spriteFont;
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreparingDeviceSettings += (sender, e) => e.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.One;
TargetElapsedTime = TimeSpan.FromTicks(333333/2);
InactiveSleepTime = TimeSpan.FromSeconds(1);
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreparingDeviceSettings += (sender, e) => e.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.One;
TargetElapsedTime = TimeSpan.FromTicks(333333/2);
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
protected override void Initialize()
{ base.Initialize(); }
{ base.Initialize(); }
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
_spriteFont = Content.Load<SpriteFont>("SpriteFont");//请自行添加SpriteFont资源.
}
{
spriteBatch = new SpriteBatch(GraphicsDevice);
_spriteFont = Content.Load<SpriteFont>("SpriteFont");//请自行添加SpriteFont资源.
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
_elapsedTime += gameTime.ElapsedGameTime;
if (_elapsedTime <= TimeSpan.FromSeconds(1))
return;
_elapsedTime -= TimeSpan.FromSeconds(1);
_frameRate = _frameCounter;
_frameCounter = 0;
base.Update(gameTime);
}
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
_elapsedTime += gameTime.ElapsedGameTime;
if (_elapsedTime <= TimeSpan.FromSeconds(1))
return;
_elapsedTime -= TimeSpan.FromSeconds(1);
_frameRate = _frameCounter;
_frameCounter = 0;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_frameCounter++;
string fps = string.Format("{0} fps", _frameRate);
spriteBatch.Begin();
spriteBatch.DrawString(_spriteFont, fps, _position, Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_frameCounter++;
string fps = string.Format("{0} fps", _frameRate);
spriteBatch.Begin();
spriteBatch.DrawString(_spriteFont, fps, _position, Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
本文介绍如何在Windows Phone 7.1系统中通过修改Game.TargetElapsedTimer属性及插入特定代码来解决XNA帧率限制问题,实现60帧流畅运行。
1577

被折叠的 条评论
为什么被折叠?



