/src/net/hires/debug/Stats.hx

https://github.com/shamruk/Hi-ReS-Stats · Haxe · 187 lines · 118 code · 50 blank · 19 comment · 1 complexity · 945f698047b1a4c065d082a53db1ada0 MD5 · raw file

  1. /**
  2. * stats.hx
  3. * http://github.com/mrdoob/stats.as
  4. *
  5. * Released under MIT license:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * How to use:
  9. *
  10. * addChild( new Stats() );
  11. *
  12. **/
  13. package net.hires.debug;
  14. import flash.display.BitmapData;
  15. import flash.display.Sprite;
  16. import flash.display.Stage;
  17. import flash.events.Event;
  18. import flash.events.MouseEvent;
  19. import flash.geom.Matrix;
  20. import flash.geom.Rectangle;
  21. import flash.system.System;
  22. import flash.text.StyleSheet;
  23. import flash.text.TextField;
  24. import flash.xml.XML;
  25. class Stats extends Sprite {
  26. static inline var GRAPH_WIDTH : Int = 70;
  27. static inline var XPOS : Int = 69;//width - 1
  28. static inline var GRAPH_HEIGHT : Int = 50;
  29. static inline var TEXT_HEIGHT : Int = 50;
  30. private var xml : XML;
  31. private var text : TextField;
  32. private var style : StyleSheet;
  33. private var timer : Int;
  34. private var fps : Int;
  35. private var ms : Int;
  36. private var ms_prev : Int;
  37. private var mem : Float;
  38. private var mem_max : Float;
  39. private var graph : BitmapData;
  40. private var rectangle : Rectangle;
  41. private var fps_graph : Int;
  42. private var mem_graph : Int;
  43. private var ms_graph : Int;
  44. private var mem_max_graph : Int;
  45. private var _stage:Stage;
  46. /**
  47. * <b>Stats</b> FPS, MS and MEM, all in one.
  48. */
  49. public function new() {
  50. super();
  51. mem_max = 0;
  52. fps = 0;
  53. xml = new XML("<xml><fps>FPS:</fps><ms>MS:</ms><mem>MEM:</mem><memMax>MAX:</memMax></xml>");
  54. style = new StyleSheet();
  55. style.setStyle('xml', {fontSize:'9px', fontFamily:'_sans', leading:'-2px'});
  56. style.setStyle('fps', {color: Colors.fpsCSS });
  57. style.setStyle('ms', {color: Colors.msCSS });
  58. style.setStyle('mem', {color: Colors.memCSS });
  59. style.setStyle('memMax', {color: Colors.memmaxCSS });
  60. text = new TextField();
  61. text.width = GRAPH_WIDTH;
  62. text.height = TEXT_HEIGHT;
  63. text.styleSheet = style;
  64. text.condenseWhite = true;
  65. text.selectable = false;
  66. text.mouseEnabled = false;
  67. rectangle = new Rectangle(GRAPH_WIDTH - 1, 0, 1, GRAPH_HEIGHT);
  68. this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
  69. this.addEventListener(Event.REMOVED_FROM_STAGE, destroy, false, 0, true);
  70. }
  71. private function init(e : Event) {
  72. _stage = flash.Lib.current.stage;
  73. graphics.beginFill(Colors.bg);
  74. graphics.drawRect(0, 0, GRAPH_WIDTH, TEXT_HEIGHT);
  75. graphics.endFill();
  76. this.addChild(text);
  77. graph = new BitmapData(GRAPH_WIDTH, GRAPH_HEIGHT, false, Colors.bg);
  78. graphics.beginBitmapFill(graph, new Matrix(1, 0, 0, 1, 0, TEXT_HEIGHT));
  79. graphics.drawRect(0, TEXT_HEIGHT, GRAPH_WIDTH, GRAPH_HEIGHT);
  80. this.addEventListener(Event.ENTER_FRAME, update);
  81. }
  82. private function destroy(e : Event) {
  83. graphics.clear();
  84. while(numChildren > 0)
  85. removeChildAt(0);
  86. graph.dispose();
  87. removeEventListener(Event.ENTER_FRAME, update);
  88. }
  89. private function update(e : Event) {
  90. timer = flash.Lib.getTimer();
  91. //after a second has passed
  92. if( timer - 1000 > ms_prev ) {
  93. mem = System.totalMemory * 0.000000954;
  94. mem_max = mem_max > mem ? mem_max : mem;
  95. fps_graph = GRAPH_HEIGHT - Std.int( Math.min(GRAPH_HEIGHT, ( fps / _stage.frameRate ) * GRAPH_HEIGHT) );
  96. mem_graph = GRAPH_HEIGHT - normalizeMem(mem);
  97. mem_max_graph = GRAPH_HEIGHT - normalizeMem(mem_max);
  98. //milliseconds since last frame -- this fluctuates quite a bit
  99. ms_graph = Std.int( GRAPH_HEIGHT - ( ( timer - ms ) >> 1 ));
  100. graph.scroll(-1, 0);
  101. graph.fillRect(rectangle, Colors.bg);
  102. graph.lock();
  103. graph.setPixel(XPOS, fps_graph, Colors.fps);
  104. graph.setPixel(XPOS, mem_graph, Colors.mem);
  105. graph.setPixel(XPOS, mem_max_graph, Colors.memmax);
  106. graph.setPixel(XPOS, ms_graph, Colors.ms);
  107. graph.unlock();
  108. xml.fps = "FPS: " + fps + " / " + stage.frameRate;
  109. xml.mem = "MEM: " + mem;
  110. xml.memMax = "MAX: " + mem_max;
  111. //reset frame and time counters
  112. fps = 0;
  113. ms_prev = timer;
  114. return;
  115. }
  116. //increment number of frames which have occurred in current second
  117. fps++;
  118. xml.ms = "MS: " + (timer - ms);
  119. ms = timer;
  120. text.htmlText = xml.toString();
  121. }
  122. function normalizeMem(_mem:Float):Int {
  123. return Std.int( Math.min( GRAPH_HEIGHT, Math.sqrt(Math.sqrt(_mem * 5000)) ) - 2);
  124. }
  125. }
  126. class Colors {
  127. public static inline var bg : Int = 0x000033;
  128. public static inline var fps : Int = 0xffff00;
  129. public static inline var ms : Int = 0x00ff00;
  130. public static inline var mem : Int = 0x00ffff;
  131. public static inline var memmax : Int = 0xff0070;
  132. public static inline var bgCSS : String = "#000033";
  133. public static inline var fpsCSS : String = "#ffff00";
  134. public static inline var msCSS : String = "#00ff00";
  135. public static inline var memCSS : String = "#00ffff";
  136. public static inline var memmaxCSS : String = "#ff0070";
  137. }