如何在屏幕上显示/显示FPS?

时间:2013-10-20 09:14:42

标签: c# xna xna-4.0 xnanimation

我有这个代码示例我需要找到如何在屏幕上显示/显示每秒帧数。试图使用该类,但它不能正常工作我不知道如何使用它。

protected override void Initialize()
{
    base.Initialize();

    fpsm = new FpsMonitor();
    fpsm.Update();
    fpsm.Draw(spriteBatch, fonts, new Vector2(5,5), Color.Red);

}

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
        ButtonState.Pressed)
        this.Exit();

    modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds *
        MathHelper.ToRadians(0.1f);

    base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    // Copy any parent transforms.
    Matrix[] transforms = new Matrix[myModel.Bones.Count];
    myModel.CopyAbsoluteBoneTransformsTo(transforms);

    // Draw the model. A model can have multiple meshes, so loop.
    foreach (ModelMesh mesh in myModel.Meshes)
    {
        // This is where the mesh orientation is set, as well 
        // as our camera and projection.
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();
            effect.World = transforms[mesh.ParentBone.Index] *
                Matrix.CreateRotationY(modelRotation)
                * Matrix.CreateTranslation(modelPosition);
            effect.View = Matrix.CreateLookAt(cameraPosition,
                Vector3.Zero, Vector3.Up);
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45.0f), aspectRatio,
                1.0f, 10000.0f);
        }
        // Draw the mesh, using the effects set above.
        mesh.Draw();
    }
    base.Draw(gameTime);
}

和FPS课程:

public class FpsMonitor
{
    public float Value { get; private set; }
    public TimeSpan Sample { get; set; }
    private Stopwatch sw;
    private int Frames;
    public FpsMonitor()
    {
        this.Sample = TimeSpan.FromSeconds(1);
        this.Value = 0;
        this.Frames = 0;
        this.sw = Stopwatch.StartNew();
    }
    public void Update()
    {
        if (sw.Elapsed > Sample)
        {
            this.Value = (float)(Frames / sw.Elapsed.TotalSeconds);
            this.sw.Reset();
            this.sw.Start();
            this.Frames = 0;
        }
    }
    public void Draw(SpriteBatch SpriteBatch, SpriteFont Font, Vector2 Location, Color Color)
    {
        this.Frames++;
        SpriteBatch.DrawString(Font, "FPS: " + this.Value.ToString(), Location, Color);
    }
}

我在Game1.cs中尝试在构造函数中使用它:

fpsm = new FpsMonitor();
fpsm.Update();
fpsm.Draw(spriteBatch, fonts, new Vector2(5,5), Color.Red);

但那不是如何使用它。 我如何使用它以及我的代码中的哪个位置?

2 个答案:

答案 0 :(得分:1)

从我看到的

fpsm = new FpsMonitor(); // in constructor
fpsm.Update();// in update I think first line

spriteBatch.Begin();  // in draw Last lines probably even after base.Draw(gameTime);
fpsm.Draw(spriteBatch, fonts, new Vector2(5,5), Color.Red);
spriteBatch.End();

这应该像魅力

[编辑spritefont说明]

您需要创建一个SpriteFont。要做到这一点,请右键单击内容,然后选择添加新建并创建spritefont。它是一个XML文件,在编译时将被转换。你可以改变我建议Arial和10的字体和高度....你总能改变它。

接下来你需要加载它。我通常这样做。

在班级

private Spritefont Arial10;
LoadContent中的

Arial10 = Content.Load<Spritefont>("Arial10");

现在你可以这样做

spriteBatch.Begin();  // in draw Last lines probably even after base.Draw(gameTime);
fpsm.Draw(spriteBatch, Arial10 , new Vector2(5,5), Color.Red);
spriteBatch.End();

答案 1 :(得分:0)

您无法在Initialize方法中绘制FPS。您需要绘制Draw方法,并在Update方法中更新。如果它没有在Draw方法上绘制它,你的FPS将永远不会绘制。 Initialize只被调用一次,并且每一帧都清除了GraphicsDevice。

Game1.cs方法的Initialize内删除以下行:

fpsm.Update();
fpsm.Draw(spriteBatch, fonts, new Vector2(5,5), Color.Red);

fpsm.Update();添加到Update()方法,并将其添加到Game1.cs中的Draw方法(GraphicsDevice.Clear(Color.CornflowerBlue);之后):

spriteBatch.Begin();
fpsm.Draw(spriteBatch, fonts, new Vector2(5,5), Color.Red);
spriteBatch.End();

我很确定这是你需要做的。

不要忘记通过执行以下操作加载LoadContent()方法中的字体:

fonts = Content.Load<SpriteFont>("MySpriteFont");

spritefont也需要是你内容项目的编译文件! 只需右键单击内容项目,选择添加&gt; New Item并选择一个SpriteFont文件。