none
XNA帧率的问题 RRS feed

  • 问题

  • 先贴测试代码:

    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.Input.Touch;
    using Microsoft.Xna.Framework.Media;
    using System.Globalization;
    
    namespace WindowsPhoneGame
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : 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 Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
    
                // Frame rate is 30 fps by default for Windows Phone.
                TargetElapsedTime = TimeSpan.FromTicks(333333/2);
    
                // Extend battery life under lock.
                InactiveSleepTime = TimeSpan.FromSeconds(1);
    
            }
    
            /// <summary>
            /// Allows the game to perform any initialization it needs to before starting to run.
            /// This is where it can query for any required services and load any non-graphic
            /// related content.  Calling base.Initialize will enumerate through any components
            /// and initialize them as well.
            /// </summary>
            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
                GraphicsDevice.PresentationParameters.PresentationInterval = PresentInterval.One;
                base.Initialize();
            }
    
            /// <summary>
            /// LoadContent will be called once per game and is the place to load
            /// all of your content.
            /// </summary>
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                _spriteFont = Content.Load<SpriteFont>("SpriteFont");
                // TODO: use this.Content to load your game content here
            }
    
            /// <summary>
            /// UnloadContent will be called once per game and is the place to unload
            /// all content.
            /// </summary>
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }
    
            /// <summary>
            /// Allows the game to run logic such as updating the world,
            /// checking for collisions, gathering input, and playing audio.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Update(GameTime gameTime)
            {
                // Allows the game to exit
                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;
                // TODO: Add your update logic here
    
                base.Update(gameTime);
            }
    
            /// <summary>
            /// This is called when the game should draw itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            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();
                // TODO: Add your drawing code here
                base.Draw(gameTime);
            }
        }
    }
    
    


    问题:

    模拟器调试显示60fps,但联机调试(HTC 7 Mozart T8698)只显示30fps,关于帧率限制见:http://www.dayoo.com/roll/201107/24/10000307_104579653.htm

    我想知道,是我的代码问题还是我的设备问题!

    手机系统:Windows Phone 7.1 7740


    我是包子!
    2011年12月24日 12:48

答案

  • 你好,

    试一下:

    public Game1()
    {
      graphics = new GraphicsDeviceManager(this);

      graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>  
       (graphics_PreparingDeviceSettings);
      
      // Frame rate is 60 fps
      TargetElapsedTime = TimeSpan.FromTicks(166667);
    }

    void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
    {
      e.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.One;
    }

    如果还是不行应该是机器硬件的问题。使用PresentInterval.One会使用机器的刷新率。 

     


     

    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    2011年12月27日 6:55

全部回复

  • 你好,


    应该是设备问题。Windows Phone 7只能到30fps。如果你使用Windows Phone Mango才可能提升。


    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年12月26日 7:07
  • 你好,我的手机系统是Windows Phone 7.1 7740.
    我是包子!
    2011年12月26日 9:34
  • 你好,

    试一下:

    public Game1()
    {
      graphics = new GraphicsDeviceManager(this);

      graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>  
       (graphics_PreparingDeviceSettings);
      
      // Frame rate is 60 fps
      TargetElapsedTime = TimeSpan.FromTicks(166667);
    }

    void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
    {
      e.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.One;
    }

    如果还是不行应该是机器硬件的问题。使用PresentInterval.One会使用机器的刷新率。 

     


     

    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    2011年12月27日 6:55
  • 多谢,可以了!
    我是包子!
    2011年12月27日 7:52