加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex FPS观察工具

发布时间:2020-12-15 04:21:05 所属栏目:百科 来源:网络整理
导读:package com.match.elements{import flash.display.BitmapData;import flash.display.Sprite;import flash.events.Event;import flash.events.MouseEvent;import flash.geom.Matrix;import flash.geom.Rectangle;import flash.system.System;import flash.te
package com.match.elements
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.system.System;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.utils.getTimer; 
	
	/**
	 * FPS观察工具
	 */
	public class GameFPS extends Sprite {
		
		protected const WIDTH : uint = 100;
		protected const HEIGHT : uint = 100;
		
		protected var xml : XML;
		
		protected var text : TextField;
		protected var style : StyleSheet;
		
		protected var timer : uint;
		protected var fps : uint;
		protected var ms : uint;
		protected var ms_prev : uint;
		protected var mem : Number;
		protected var mem_max : Number;
		
		protected var graph : BitmapData;
		protected var rectangle : Rectangle;
		
		protected var fps_graph : uint;
		protected var mem_graph : uint;
		protected var mem_max_graph : uint;
		
		protected var colors : Colors = new Colors();
		
		public function GameFPS() : void {
			
			mem_max = 0;
			
			
			        xml = <xml><fps>FPS:</fps><ms>MS:</ms><mem>MEM:</mem><memMax>MAX:</memMax></xml>;
			
			style = new StyleSheet();
			style.setStyle('xml',{fontSize:'9px',fontFamily:'_sans',leading:'-2px'});
			style.setStyle('fps',{color: hex2css(colors.fps)});
			style.setStyle('ms',{color: hex2css(colors.ms)});
			style.setStyle('mem',{color: hex2css(colors.mem)});
			style.setStyle('memMax',{color: hex2css(colors.memmax)});
			
			text = new TextField();
			text.width = WIDTH;
			text.height = 50;
			text.styleSheet = style;
			text.condenseWhite = true;
			text.selectable = false;
			text.mouseEnabled = false;
			
			rectangle = new Rectangle(WIDTH - 1,1,HEIGHT - 50);   
			
			addEventListener(Event.ADDED_TO_STAGE,init,false,true);
			addEventListener(Event.REMOVED_FROM_STAGE,destroy,true);
			
		}
		
		private function init(e : Event) : void {
			
			graphics.beginFill(colors.bg);
			graphics.drawRect(0,WIDTH,HEIGHT);
			graphics.endFill();
			
			addChild(text);
			
			graph = new BitmapData(WIDTH,HEIGHT - 50,colors.bg);
			graphics.beginBitmapFill(graph,new Matrix(1,50));
			graphics.drawRect(0,50,HEIGHT - 50);
			
			addEventListener(MouseEvent.CLICK,onClick);
			addEventListener(Event.ENTER_FRAME,update);
			
		}
		
		private function destroy(e : Event) : void {
			
			graphics.clear();
			
			while(numChildren > 0)
				removeChildAt(0);   
			
			graph.dispose();
			
			removeEventListener(MouseEvent.CLICK,onClick);
			removeEventListener(Event.ENTER_FRAME,update);
			
		}
		
		private function update(e : Event) : void {
			
			timer = getTimer();
			
			if( timer - 1000 > ms_prev ) {
				
				ms_prev = timer;
				mem = Number((System.totalMemory * 0.000000954).toFixed(3));
				mem_max = mem_max > mem ? mem_max : mem;
				
				fps_graph = Math.min(graph.height,( fps / stage.frameRate ) * graph.height);
				mem_graph = Math.min(graph.height,Math.sqrt(Math.sqrt(mem * 5000))) - 2;
				mem_max_graph = Math.min(graph.height,Math.sqrt(Math.sqrt(mem_max * 5000))) - 2;
				
				graph.scroll(-1,0);
				
				graph.fillRect(rectangle,colors.bg);
				graph.setPixel(graph.width - 1,graph.height - fps_graph,colors.fps);
				graph.setPixel(graph.width - 1,graph.height - ( ( timer - ms ) >> 1 ),colors.ms);
				graph.setPixel(graph.width - 1,graph.height - mem_graph,colors.mem);
				graph.setPixel(graph.width - 1,graph.height - mem_max_graph,colors.memmax);
				
				xml.fps = "FPS: " + fps + " / " + stage.frameRate; 
				xml.mem = "MEM: " + mem;
				xml.memMax = "MAX: " + mem_max;   
				
				fps = 0;
				
			}
			
			fps++;
			
			xml.ms = "MS: " + (timer - ms);
			ms = timer;
			
			text.htmlText = xml;
		}
		
		private function onClick(e : MouseEvent) : void {
			
			mouseY / height > .5 ? stage.frameRate-- : stage.frameRate++;
			xml.fps = "FPS: " + fps + " / " + stage.frameRate;  
			text.htmlText = xml;
			
		}
		
		// .. Utils
		
		private function hex2css( color : int ) : String {
			
			return "#" + color.toString(16);
			
		}
		
	}
	
}
class Colors {
	
	public var bg : uint = 0x000033;
	public var fps : uint = 0xffff00;
	public var ms : uint = 0x00ff00;
	public var mem : uint = 0x00ffff;
	public var memmax : uint = 0xff0070;
	
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读