flash 观察帧频率的工具 fps counter

fps counter
用法:
addChild( new Stats() );
/**
 * stats.as
 *  https://github.com/mrdoob/Hi-ReS-Stats
 * 
 * Released under MIT license:
 *  http://www.opensource.org/licenses/mit-license.php
 *
 * 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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值