简体   繁体   English

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

[英]How do i display/show FPS on screen?

I have this code sample i need to find how to show/display the frames per second on the screen. 我有此代码示例,我需要查找如何在屏幕上每秒显示/显示帧。 Tried to use the class but its not working good im not sure how to use it. 试图使用该类,但无法正常工作,我不确定如何使用它。

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);
}

And the FPS class: 和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);
    }
}

I tried in the Game1.cs to use it in the constructor: 我在Game1.cs中尝试在构造函数中使用它:

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

But thats not how to use it. 但这不是如何使用它。 How do i use it and where in my code ? 我如何使用它以及在代码中的何处?

From what I see 从我所看到的

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();

That should work like a charm 那应该像魅力一样工作

[Edit for spritefont explanation] [编辑spritefont说明]

You will need to create a SpriteFont. 您将需要创建一个SpriteFont。 To do that right click Content chose add new and create spritefont. 为此,请右键单击内容,然后选择添加新内容并创建spritefont。 It is an XML file that will be converted when you compile. 这是一个XML文件,将在您编译时进行转换。 you can change the font and height I suggest Arial and 10.... you can always change that. 您可以更改字体和高度,我建议Arial和10。...您可以随时更改。

Next you need to load it. 接下来,您需要加载它。 I usually do it like this. 我通常这样做。

in the class 在课堂里

private Spritefont Arial10;

in the LoadContent 在LoadContent中

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

Now you can do this 现在您可以执行此操作

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

You can't draw the FPS in the Initialize method. 您不能在Initialize方法中绘制FPS。 You need to draw in the Draw method, and update in the Update method. 您需要绘制Draw方法,并在Update方法中进行更新。 Your FPS will never draw if it doesnt draw it on the Draw method. 如果您的FPS没有在Draw方法上绘制,它将永远不会绘制。 Initialize is only called once ever, and your GraphicsDevice is cleared every single frame. 初始化仅被调用一次,并且GraphicsDevice每隔一帧被清除一次。

Within Game1.cs from the Initialize method, remove these lines: Game1.cs中的Initialize方法中,删除以下行:

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

Add fpsm.Update(); 添加fpsm.Update(); to the Update() method, and add this to your Draw method within Game1.cs (after GraphicsDevice.Clear(Color.CornflowerBlue); ): Update()方法,并将其添加到Game1.cs中的Draw方法中(在GraphicsDevice.Clear(Color.CornflowerBlue); ):

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

I am pretty sure this is what you need to do. 我很确定这是您需要做的。

Don't forget to load the font in the LoadContent() method by doing: 不要忘记通过执行以下操作在LoadContent()方法中加载字体:

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

Also the spritefont needs to be a compiled file from your contents project! 另外,spritefont还需要是内容项目中的编译文件! Just right-click on the content project, select Add > New Item and choose a SpriteFont file. 只需右键单击内容项目,选择“添加”>“新建项目”,然后选择一个SpriteFont文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM