2

我正在使用 XNA 4.0 进行一些试验,遵循教程并创建非常基本的东西(比如三角形和一些线条;-))。在执行此操作时,我注意到我所有的应用程序从未以超过 50-51 fps 的速度运行(使用 Fraps)。这并不是说我在慢速计算机或显卡(Ati HD4870)上运行繁重的程序,它一定与 XNA 有关(游戏在这里运行得很好)。

现在,我读到的关于 XNA 的所有内容都说默认更新频率是每秒 60 次,我想得到它。

  • 全屏和窗口一样
  • 如果我将 SynchronizeWithVerticalRetrace 设置为 false 或 true:相同
  • 如果在没有 Visual Studio 的情况下运行程序,我只能得到 41 fps
  • TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 10);当我使用fps覆盖更新频率时,确实会显着增加。我注意到这仍然不正确:10 表示 10 毫秒,但我“只”得到 83 fps 而不是 100。在 1 毫秒时我得到 850 fps。所以我得到的 fps 和我应该得​​到的 fps 的偏差是相当一致的。在我看来,时间似乎有问题?

任何人都知道这里可能存在什么问题和/或有建议获得稳定的 60 fps?

谢谢!

4

2 回答 2

4

FRAPS 是不是不能给你准确的结果?您是否尝试过在游戏中添加自己的帧率计数器并查看这些结果的含义?Shawn Hargreaves 在他的博客上编写了一个方法,添加到一个新的空白 XNA 游戏项目中应该很容易。

http://blogs.msdn.com/b/shawnhar/archive/2007/06/08/displaying-the-framerate.aspx

当您自己进行计算时,您看到相同的 FPS 还是报告不同?

于 2011-01-12T17:14:03.487 回答
1

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.

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<SpriteFont>("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 < 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 > 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 > 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);
    }
}
}
于 2011-10-25T18:37:42.837 回答