/flash/General/FpsCounter.as

https://github.com/GunioRobot/box2d2-js
ActionScript | 132 lines | 64 code | 27 blank | 41 comment | 2 complexity | 92be390d7aa0d5e4ff82f084394314e5 MD5 | raw file
  1. //===========================================================
  2. //=========================================================//
  3. // -=ANTHEM=-
  4. // file: .as
  5. //
  6. // copyright: Matthew Bush 2007
  7. //
  8. // notes:
  9. //
  10. //=========================================================//
  11. //===========================================================
  12. //===========================================================
  13. // FPS COUNTER CLASS
  14. //===========================================================
  15. package General{
  16. import flash.display.Sprite;
  17. import flash.text.*;
  18. import flash.utils.getTimer;
  19. import flash.events.*;
  20. import flash.system.System;
  21. public class FpsCounter extends Sprite{
  22. //======================
  23. // constructor
  24. //======================
  25. public function FpsCounter(){
  26. // create text field
  27. textBox = new TextField();
  28. textBox.text = "...";
  29. textBox.textColor = 0xaa1144;
  30. textBox.selectable = false;
  31. textBox2 = new TextField();
  32. textBox2.text = "...";
  33. textBox2.width = 150;
  34. textBox2.textColor = 0xaa1144;
  35. textBox2.selectable = false;
  36. textBox2.y = 15;
  37. textBox3 = new TextField();
  38. textBox3.text = "...";
  39. textBox3.textColor = 0xaa1144;
  40. textBox3.selectable = false;
  41. textBox3.y = 30;
  42. // set initial lastTime
  43. oldT = getTimer();
  44. addChild(textBox);
  45. addChild(textBox2);
  46. addChild(textBox3);
  47. }
  48. //======================
  49. // update function
  50. //======================
  51. public function update():void{
  52. var newT:uint = getTimer();
  53. var f1:uint = newT-oldT;
  54. mfpsCount += f1;
  55. if (avgCount < 1){
  56. textBox.text = String(Math.round(1000/(mfpsCount/30))+" fps average");
  57. avgCount = 30;
  58. mfpsCount = 0;
  59. }
  60. avgCount--;
  61. oldT = getTimer();
  62. textBox3.text = Math.round(System.totalMemory/(1024*1024)) + " MB used"
  63. }
  64. public function updatePhys(oldT2:uint):void{
  65. var newT:uint = getTimer();
  66. var f1:uint = newT-oldT2;
  67. mfpsCount2 += f1;
  68. if (avgCount2 < 1){
  69. textBox2.text = String("Physics step: "+Math.round(mfpsCount2/30)+" ms (" +Math.round(1000/(mfpsCount2/30))+" fps)");
  70. avgCount2 = 30;
  71. mfpsCount2 = 0;
  72. }
  73. avgCount2--;
  74. }
  75. //======================
  76. // updateend function
  77. //======================
  78. public function updateEnd():void{
  79. // wrong
  80. /*var newT:uint = getTimer();
  81. var f1:uint = newT-oldT;
  82. mfpsCount2 += f1;
  83. if (avgCount2 < 1){
  84. textBox2.text = String(Math.round(1000/(mfpsCount2/30))+" fps uncapped");
  85. avgCount2 = 30;
  86. mfpsCount2 = 0;
  87. }
  88. avgCount2--;*/
  89. }
  90. //======================
  91. // private variables
  92. //======================
  93. private var textBox:TextField;
  94. private var textBox2:TextField;
  95. private var textBox3:TextField;
  96. private var mfpsCount:int = 0;
  97. private var mfpsCount2:int = 0;
  98. private var avgCount:int = 30;
  99. private var avgCount2:int = 30;
  100. private var oldT:uint;
  101. }
  102. }
  103. // End of file
  104. //===========================================================
  105. //===========================================================