2013-10-20 58 views
0

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

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

但那不是如何使用它。 我如何使用它以及我的代码在哪里?

回答

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

user2888973它在线上的FPS类上thrwoing null异常:SpriteBatch。DrawString(Font,“FPS:”+ this.Value.ToString(),Location,Color);变量Font为null –

+0

在Game1.cs中,我想我需要用字体变量来做些什么。我如何使用它? –

+0

你的'fonts'变量是否设置为有效的'SpriteFont'?加载一个字体,在'LoadContent()'内使用:'fonts = Content.Load (“MyFont”);' – Sarkilas

0

不能得出FPS的初始化方法。您需要绘制Draw方法,并在Update方法中进行更新。如果它不绘制Draw方法,你的FPS将永远不会绘制。 Initialize只被调用一次,并且GraphicsDevice每一帧都被清除。

Game1.csInitialize方法,删除这些行:

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类需要从你的内容项目经过编译的文件! 只需右键单击内容项目,选择添加>新建项目,然后选择一个SpriteFont文件。