关于Windows Phone 7.1中XNA的帧率问题

本文介绍如何在Windows Phone 7.1系统中通过修改Game.TargetElapsedTimer属性及插入特定代码来解决XNA帧率限制问题,实现60帧流畅运行。
 微软在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;
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 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);
        }
        protected override void Initialize()
        { base.Initialize(); }
        protected override void LoadContent()
        {
            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);
        }
        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);
        }
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值