2013-10-20 122 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文件。