|
| 1 | +/** |
| 2 | + * Stats |
| 3 | + * |
| 4 | + * Released under MIT license: |
| 5 | + * http://www.opensource.org/licenses/mit-license.php |
| 6 | + * |
| 7 | + * How to use: |
| 8 | + * |
| 9 | + * addChild( new Stats() ); |
| 10 | + * |
| 11 | + * or |
| 12 | + * |
| 13 | + * addChild( new Stats( { bg: 0xffffff } ); |
| 14 | + * |
| 15 | + * version log: |
| 16 | + * |
| 17 | + * 09.10.22 2.2 Mr.doob + FlipX of graph to be more logic. |
| 18 | + * + Destroy on Event.REMOVED_FROM_STAGE (thx joshtynjala) |
| 19 | + * 09.03.28 2.1 Mr.doob + Theme support. |
| 20 | + * 09.02.21 2.0 Mr.doob + Removed Player version, until I know if it's really needed. |
| 21 | + * + Added MAX value (shows Max memory used, useful to spot memory leaks) |
| 22 | + * + Reworked text system / no memory leak (original reason unknown) |
| 23 | + * + Simplified |
| 24 | + * 09.02.07 1.5 Mr.doob + onRemovedFromStage() (thx huihuicn.xu) |
| 25 | + * 08.12.14 1.4 Mr.doob + Code optimisations and version info on MOUSE_OVER |
| 26 | + * 08.07.12 1.3 Mr.doob + Some speed and code optimisations |
| 27 | + * 08.02.15 1.2 Mr.doob + Class renamed to Stats (previously FPS) |
| 28 | + * 08.01.05 1.2 Mr.doob + Click changes the fps of flash (half up increases, half down decreases) |
| 29 | + * 08.01.04 1.1 Mr.doob + Shameless ripoff of Alternativa's FPS look :P |
| 30 | + * Theo + Log shape for MEM |
| 31 | + * + More room for MS |
| 32 | + * 07.12.13 1.0 Mr.doob + First version |
| 33 | + **/ |
| 34 | + |
| 35 | +package net.hires.debug |
| 36 | +{ |
| 37 | + import flash.display.Bitmap; |
| 38 | + import flash.display.BitmapData; |
| 39 | + import flash.display.Sprite; |
| 40 | + import flash.events.Event; |
| 41 | + import flash.events.MouseEvent; |
| 42 | + import flash.geom.Rectangle; |
| 43 | + import flash.system.System; |
| 44 | + import flash.text.StyleSheet; |
| 45 | + import flash.text.TextField; |
| 46 | + import flash.utils.getTimer; |
| 47 | + |
| 48 | + public class Stats extends Sprite |
| 49 | + { |
| 50 | + protected const WIDTH : uint = 70; |
| 51 | + protected const HEIGHT : uint = 100; |
| 52 | + |
| 53 | + protected var xml : XML; |
| 54 | + |
| 55 | + protected var text : TextField; |
| 56 | + protected var style : StyleSheet; |
| 57 | + |
| 58 | + protected var timer : uint; |
| 59 | + protected var fps : uint; |
| 60 | + protected var ms : uint; |
| 61 | + protected var ms_prev : uint; |
| 62 | + protected var mem : Number; |
| 63 | + protected var mem_max : Number; |
| 64 | + |
| 65 | + protected var graph : Bitmap; |
| 66 | + protected var rectangle : Rectangle; |
| 67 | + |
| 68 | + protected var fps_graph : uint; |
| 69 | + protected var mem_graph : uint; |
| 70 | + protected var mem_max_graph : uint; |
| 71 | + |
| 72 | + protected var theme : Object = { bg: 0x000033, fps: 0xffff00, ms: 0x00ff00, mem: 0x00ffff, memmax: 0xff0070 }; |
| 73 | + |
| 74 | + /** |
| 75 | + * <b>Stats</b> FPS, MS and MEM, all in one. |
| 76 | + * |
| 77 | + * @param _theme Example: { bg: 0x202020, fps: 0xC0C0C0, ms: 0x505050, mem: 0x707070, memmax: 0xA0A0A0 } |
| 78 | + */ |
| 79 | + public function Stats( _theme : Object = null ) : void |
| 80 | + { |
| 81 | + if (_theme) |
| 82 | + { |
| 83 | + if (_theme.bg != null) theme.bg = _theme.bg; |
| 84 | + if (_theme.fps != null) theme.fps = _theme.fps; |
| 85 | + if (_theme.ms != null) theme.ms = _theme.ms; |
| 86 | + if (_theme.mem != null) theme.mem = _theme.mem; |
| 87 | + if (_theme.memmax != null) theme.memmax = _theme.memmax; |
| 88 | + } |
| 89 | + |
| 90 | + mem_max = 0; |
| 91 | + |
| 92 | + xml = <xml><fps>FPS:</fps><ms>MS:</ms><mem>MEM:</mem><memMax>MAX:</memMax></xml>; |
| 93 | + |
| 94 | + style = new StyleSheet(); |
| 95 | + style.setStyle("xml", {fontSize:'9px', fontFamily:'_sans', leading:'-2px'}); |
| 96 | + style.setStyle("fps", {color: hex2css(theme.fps)}); |
| 97 | + style.setStyle("ms", {color: hex2css(theme.ms)}); |
| 98 | + style.setStyle("mem", {color: hex2css(theme.mem)}); |
| 99 | + style.setStyle("memMax", {color: hex2css(theme.memmax)}); |
| 100 | + |
| 101 | + text = new TextField(); |
| 102 | + text.width = WIDTH; |
| 103 | + text.height = 50; |
| 104 | + text.styleSheet = style; |
| 105 | + text.condenseWhite = true; |
| 106 | + text.selectable = false; |
| 107 | + text.mouseEnabled = false; |
| 108 | + |
| 109 | + graph = new Bitmap(); |
| 110 | + graph.y = 50; |
| 111 | + |
| 112 | + rectangle = new Rectangle( WIDTH - 1, 0, 1, HEIGHT - 50 ); |
| 113 | + |
| 114 | + addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true); |
| 115 | + addEventListener(Event.REMOVED_FROM_STAGE, destroy, false, 0, true); |
| 116 | + } |
| 117 | + |
| 118 | + private function init(e : Event) : void |
| 119 | + { |
| 120 | + graphics.beginFill(theme.bg); |
| 121 | + graphics.drawRect(0, 0, WIDTH, HEIGHT); |
| 122 | + graphics.endFill(); |
| 123 | + |
| 124 | + addChild(text); |
| 125 | + |
| 126 | + graph.bitmapData = new BitmapData(WIDTH, HEIGHT - 50, false, theme.bg); |
| 127 | + addChild(graph); |
| 128 | + |
| 129 | + addEventListener(MouseEvent.CLICK, onClick); |
| 130 | + addEventListener(Event.ENTER_FRAME, update); |
| 131 | + } |
| 132 | + |
| 133 | + private function destroy(e : Event) : void |
| 134 | + { |
| 135 | + graphics.clear(); |
| 136 | + |
| 137 | + while(numChildren > 0) |
| 138 | + removeChildAt(0); |
| 139 | + |
| 140 | + graph.bitmapData.dispose(); |
| 141 | + |
| 142 | + removeEventListener(MouseEvent.CLICK, onClick); |
| 143 | + removeEventListener(Event.ENTER_FRAME, update); |
| 144 | + } |
| 145 | + |
| 146 | + private function update(e : Event) : void |
| 147 | + { |
| 148 | + timer = getTimer(); |
| 149 | + |
| 150 | + if( timer - 1000 > ms_prev ) |
| 151 | + { |
| 152 | + ms_prev = timer; |
| 153 | + mem = Number((System.totalMemory * 0.000000954).toFixed(3)); |
| 154 | + mem_max = mem_max > mem ? mem_max : mem; |
| 155 | + |
| 156 | + fps_graph = Math.min( graph.height, ( fps / stage.frameRate ) * graph.height ); |
| 157 | + mem_graph = Math.min( graph.height, Math.sqrt( Math.sqrt( mem * 5000 ) ) ) - 2; |
| 158 | + mem_max_graph = Math.min( graph.height, Math.sqrt( Math.sqrt( mem_max * 5000 ) ) ) - 2; |
| 159 | + |
| 160 | + graph.bitmapData.scroll( -1, 0 ); |
| 161 | + |
| 162 | + graph.bitmapData.fillRect( rectangle , theme.bg ); |
| 163 | + graph.bitmapData.setPixel( graph.width - 1, graph.height - fps_graph, theme.fps); |
| 164 | + graph.bitmapData.setPixel( graph.width - 1, graph.height - ( ( timer - ms ) >> 1 ), theme.ms ); |
| 165 | + graph.bitmapData.setPixel( graph.width - 1, graph.height - mem_graph, theme.mem); |
| 166 | + graph.bitmapData.setPixel( graph.width - 1, graph.height - mem_max_graph, theme.memmax); |
| 167 | + |
| 168 | + xml.fps = "FPS: " + fps + " / " + stage.frameRate; |
| 169 | + xml.mem = "MEM: " + mem; |
| 170 | + xml.memMax = "MAX: " + mem_max; |
| 171 | + |
| 172 | + fps = 0; |
| 173 | + } |
| 174 | + |
| 175 | + fps++; |
| 176 | + |
| 177 | + xml.ms = "MS: " + (timer - ms); |
| 178 | + ms = timer; |
| 179 | + |
| 180 | + text.htmlText = xml; |
| 181 | + } |
| 182 | + |
| 183 | + private function onClick(e : MouseEvent) : void |
| 184 | + { |
| 185 | + mouseY / height > .5 ? stage.frameRate-- : stage.frameRate++; |
| 186 | + xml.fps = "FPS: " + fps + " / " + stage.frameRate; |
| 187 | + text.htmlText = xml; |
| 188 | + } |
| 189 | + |
| 190 | + // .. Utils |
| 191 | + |
| 192 | + private function hex2css( color : int ) : String |
| 193 | + { |
| 194 | + return "#" + color.toString(16); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments