/// <Summary> // This is a game component that implements iupdateable. /// </Summary> public class frameratecounter: Microsoft. xNa. framework. drawablegamecomponent {contentmanager content; spritebatch; spritefont; int framerate = 0; int framecounter = 0; timespan elapsedtime = timespan. zero; Public frameratecounter (game, spritebatch): Base (game) {content = new contentmanager (game. services); loadgraphicscontent (true); this. spritebatch = spritebatch;} protected void loadgraphicscontent (bool loadallcontent) {If (loadallcontent) {spritefont = content. load <spritefont> (@ "content \ hudfont"); // specify spritefont} public override void Update (gametime) {elapsedtime + = gametime. elapsedgametime; If (elapsedtime> timespan. fromseconds (1) {elapsedtime-= timespan. fromseconds (1); framerate = framecounter; framecounter = 0 ;}} public override void draw (gametime) {framecounter ++; string FPS = string. format ("FPS: {0}", framerate); spritebatch. begin (); spritebatch. drawstring (spritefont, FPS, new vector2 (20, 20), color. white); // modify the spritebatch coordinates as needed. end ();}}
To use this function, you only need to use it after the spritebatch of loadcontent is initialized.
Component.Add(new FrameRateCounter(this, spriteBatch));
You can.
In addition, you need to modify the spritefont file you used to display FPS, And the coordinates to display FPS.