zoukankan      html  css  js  c++  java
  • 显示fps的DrawableGameComponent组件

    还是用DrawableGameComponent比较方便管理。
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    #endregion

    namespace Jewels {
        
    public class FrameRateCounter : DrawableGameComponent {
            ContentManager content;
            SpriteBatch spriteBatch;
            SpriteFont spriteFont;

            
    int frameRate = 0;
            
    int frameCounter = 0;
            TimeSpan elapsedTime 
    = TimeSpan.Zero;
            Game parentGame;


            
    public FrameRateCounter(Game game)
                : 
    base(game) {
                parentGame 
    = game;
                content 
    = new ContentManager(game.Services, Jewels.Game.CONTENT_DIR);
            }


            
    protected override void LoadContent() {
                spriteBatch 
    = new SpriteBatch(GraphicsDevice);
                spriteFont 
    = content.Load<SpriteFont>("fpsfont");
            }

            
    protected override void UnloadContent() {
                content.Unload();
            }


            
    public override void Update(GameTime gameTime) {
                elapsedTime 
    += gameTime.ElapsedGameTime;

                
    if (elapsedTime > TimeSpan.FromSeconds(1)) {
                    elapsedTime 
    -= TimeSpan.FromSeconds(1);
                    frameRate 
    = frameCounter;
                    frameCounter 
    = 0;
                }
            }


            
    public override void Draw(GameTime gameTime) {
                frameCounter
    ++;

                
    string fps = string.Format("FPS: {0}", frameRate);
                Vector2 pos 
    = new Vector2(parentGame.ViewportWidth - 100, parentGame.ViewportHeight / 2);

                spriteBatch.Begin();

                spriteBatch.DrawString(spriteFont, fps, 
    new Vector2(pos.X + 1, pos.Y + 1), Color.Black);
                spriteBatch.DrawString(spriteFont, fps, pos, Color.White);

                spriteBatch.End();
            }
        }

    } 

  • 相关阅读:
    javascript DOM操作
    DirectX编译出现link错误
    PPT快捷键
    Windows GDI笔记
    VC++键盘消息
    VC++6.0快捷键
    C#值类型和引用类型
    C#转义字符
    关于C#里“浅表副本”的解释
    C#中override和overload的区别
  • 原文地址:https://www.cnblogs.com/fhmsha/p/1588701.html
Copyright © 2011-2022 走看看