我有这个代码示例,我需要找到如何在屏幕上显示/显示每秒帧数。尝试使用该课程,但效果不佳,我不知道如何使用它。
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);
但这不是如何使用它。我如何使用它以及在我的代码中的什么位置?