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

    } 

  • 相关阅读:
    TCP心跳 | TCP keepAlive(转)
    闲说HeartBeat心跳包和TCP协议的KeepAlive机制
    一个DNS统计,RCFs,工具站点
    JMX
    【转】如何实现一个配置中心
    用Netty开发中间件:高并发性能优化
    UDP server & client
    DNS缓存
    C正则库做DNS域名验证时的性能对比
    DNS压力测试工具dnsperf简介
  • 原文地址:https://www.cnblogs.com/fhmsha/p/1588701.html
Copyright © 2011-2022 走看看