博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
flash 观察帧频率的工具 fps counter
阅读量:6079 次
发布时间:2019-06-20

本文共 5115 字,大约阅读时间需要 17 分钟。

fps counter
用法:
addChild( new Stats() );
/**
 * stats.as
 * 
 * 
 * Released under MIT license:
 * 
 *
 * How to use:
 * 
 * addChild( new Stats() );
 *
 **/
1 package{
2 3 import flash.display.BitmapData; 4 import flash.display.Sprite; 5 import flash.events.Event; 6 import flash.events.MouseEvent; 7 import flash.geom.Matrix; 8 import flash.geom.Rectangle; 9 import flash.system.System; 10 import flash.text.StyleSheet; 11 import flash.text.TextField; 12 import flash.utils.getTimer; 13 14 public class Stats extends Sprite { 15 16 protected const WIDTH : uint = 70; 17 protected const HEIGHT : uint = 100; 18 19 protected var xml : XML; 20 21 protected var text : TextField; 22 protected var style : StyleSheet; 23 24 protected var timer : uint; 25 protected var fps : uint; 26 protected var ms : uint; 27 protected var ms_prev : uint; 28 protected var mem : Number; 29 protected var mem_max : Number; 30 31 protected var graph : BitmapData; 32 protected var rectangle : Rectangle; 33 34 protected var fps_graph : uint; 35 protected var mem_graph : uint; 36 protected var mem_max_graph : uint; 37 38 protected var colors : Colors = new Colors(); 39 40 /** 41 * Stats FPS, MS and MEM, all in one. 42 */ 43 public function Stats() : void {
44 45 mem_max = 0; 46 47

        xml = <xml><fps>FPS:</fps><ms>MS:</ms><mem>MEM:</mem><memMax>MAX:</memMax></xml>;

48     49    style = new StyleSheet();  50    style.setStyle('xml', {fontSize:'9px', fontFamily:'_sans', leading:'-2px'});  51    style.setStyle('fps', {color: hex2css(colors.fps)});  52    style.setStyle('ms', {color: hex2css(colors.ms)});  53    style.setStyle('mem', {color: hex2css(colors.mem)});  54    style.setStyle('memMax', {color: hex2css(colors.memmax)});  55     56    text = new TextField();  57    text.width = WIDTH;  58    text.height = 50;  59    text.styleSheet = style;  60    text.condenseWhite = true;  61    text.selectable = false;  62    text.mouseEnabled = false;  63     64    rectangle = new Rectangle(WIDTH - 1, 0, 1, HEIGHT - 50);    65     66    addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);  67    addEventListener(Event.REMOVED_FROM_STAGE, destroy, false, 0, true);  68     69   }  70    71   private function init(e : Event) : void {
72 73 graphics.beginFill(colors.bg); 74 graphics.drawRect(0, 0, WIDTH, HEIGHT); 75 graphics.endFill(); 76 77 addChild(text); 78 79 graph = new BitmapData(WIDTH, HEIGHT - 50, false, colors.bg); 80 graphics.beginBitmapFill(graph, new Matrix(1, 0, 0, 1, 0, 50)); 81 graphics.drawRect(0, 50, WIDTH, HEIGHT - 50); 82 83 addEventListener(MouseEvent.CLICK, onClick); 84 addEventListener(Event.ENTER_FRAME, update); 85 86 } 87 88 private function destroy(e : Event) : void {
89 90 graphics.clear(); 91 92 while(numChildren > 0) 93 removeChildAt(0); 94 95 graph.dispose(); 96 97 removeEventListener(MouseEvent.CLICK, onClick); 98 removeEventListener(Event.ENTER_FRAME, update); 99 100 } 101 102 private function update(e : Event) : void {
103 104 timer = getTimer(); 105 106 if( timer - 1000 > ms_prev ) {
107 108 ms_prev = timer; 109 mem = Number((System.totalMemory * 0.000000954).toFixed(3)); 110 mem_max = mem_max > mem ? mem_max : mem; 111 112 fps_graph = Math.min(graph.height, ( fps / stage.frameRate ) * graph.height); 113 mem_graph = Math.min(graph.height, Math.sqrt(Math.sqrt(mem * 5000))) - 2; 114 mem_max_graph = Math.min(graph.height, Math.sqrt(Math.sqrt(mem_max * 5000))) - 2; 115 116 graph.scroll(-1, 0); 117 118 graph.fillRect(rectangle, colors.bg); 119 graph.setPixel(graph.width - 1, graph.height - fps_graph, colors.fps); 120 graph.setPixel(graph.width - 1, graph.height - ( ( timer - ms ) >> 1 ), colors.ms); 121 graph.setPixel(graph.width - 1, graph.height - mem_graph, colors.mem); 122 graph.setPixel(graph.width - 1, graph.height - mem_max_graph, colors.memmax); 123 124 xml.fps = "FPS: " + fps + " / " + stage.frameRate; 125 xml.mem = "MEM: " + mem; 126 xml.memMax = "MAX: " + mem_max; 127 128 fps = 0; 129 130 } 131 132 fps++; 133 134 xml.ms = "MS: " + (timer - ms); 135 ms = timer; 136 137 text.htmlText = xml; 138 } 139 140 private function onClick(e : MouseEvent) : void {
141 142 mouseY / height > .5 ? stage.frameRate-- : stage.frameRate++; 143 xml.fps = "FPS: " + fps + " / " + stage.frameRate; 144 text.htmlText = xml; 145 146 } 147 148 // .. Utils 149 150 private function hex2css( color : int ) : String {
151 152 return "#" + color.toString(16); 153 154 } 155 156 } 157 158 } 159 class Colors {
160 161 public var bg : uint = 0x000033; 162 public var fps : uint = 0xffff00; 163 public var ms : uint = 0x00ff00; 164 public var mem : uint = 0x00ffff; 165 public var memmax : uint = 0xff0070; 166 167 }

转载地址:http://rnhgx.baihongyu.com/

你可能感兴趣的文章
需要学习的mysql函数
查看>>
sublime-text 使用记录
查看>>
Python: 函数与方法的区别 以及 Bound Method 和 Unbound Method
查看>>
从 Google 的一道面试题说起·
查看>>
GitHub采用了新的GraphQL API
查看>>
从责任界定和问题预警角度 解读全栈溯源对DevOps的价值
查看>>
面向桌面开发的Windows Template Studio
查看>>
TriggerMesh开源用于多云环境的Knative Event Sources
查看>>
SSPL的MongoDB再被抛弃,GUN Health也合流PostgreSQL
查看>>
微软表示Edge的性能更优于Chrome和Firefox
查看>>
基于容器服务的持续集成与云端交付(三)- 从零搭建持续交付系统
查看>>
Microsoft使用.NET Core SDK遥测数据
查看>>
《Spark GraphX in Action》书评及作者访谈
查看>>
IBM推出了针对区块链部署的云服务
查看>>
关于5G被激烈讨论的那些争端和冲突
查看>>
使用Apache Spark构建实时分析Dashboard
查看>>
2017年InfoQ最受欢迎30项内容清单
查看>>
OpenAI披露最新研究成果:AI训练如何扩展到更大规模?
查看>>
周下载量过200万的npm包被注入恶意代码,Vue、Node项目恐受影响
查看>>
写给Java工程师的面试指南
查看>>