FPS

Simple class that create a bar at the top of a SWF file to display Frame Per Second and Memory usage, click on the bar to hide it and type "fps" to get it back.

Version 1.0.1

demo

docs

download source

Actionscript:
  1. package com.soundstep.utils {
  2.  
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.     import flash.events.MouseEvent;
  6.     import flash.events.KeyboardEvent;
  7.     import flash.system.System;
  8.     import flash.text.TextField;
  9.     import flash.text.TextFieldAutoSize;
  10.     import flash.utils.getTimer;
  11.  
  12.     /**
  13.      * <b>Author:</b> Romuald Quantin - <a href="http://www.soundstep.com/" target="_blank">www.soundstep.com</a><br />
  14.      * <b>Class version:</b> 1.0.1<br />
  15.      * <b>Actionscript version:</b> 3.0<br />
  16.      * <b>Copyright:</b> Free to use and change (except to include in a framework), an notification email will be welcome for a commercial use (just for information).<br />
  17.      * <b>Date:</b> 04-2008<br /><br />
  18.      * <b>Usage:</b> Create a bar at the top of a swf file to display Frame Per Second and Memory usage.
  19.      * @example
  20.      * <listing version="3.0">addChild(new FPS());</listing>
  21.      * <listing version="3.0">addChild(new FPS(0xFF0000, 0xFFFF00, 0x000000, .5, FPS.MEMORY_MEGABYTES, true));</listing>
  22.      */
  23.    
  24.     public class FPS extends Sprite {
  25.        
  26.         //------------------------------------
  27.         // private, protected properties
  28.         //------------------------------------
  29.        
  30.         private var _barColor:uint;
  31.         private var _bgColor:uint;
  32.         private var _textColor:uint;
  33.         private var _bgAlpha:uint;
  34.         private var _bar:Sprite = new Sprite();
  35.         private var _bg:Sprite = new Sprite();
  36.         private var _text:TextField = new TextField();
  37.         private var _time:Number;
  38.         private var _frameTime:Number;
  39.         private var _prevFrameTime:Number = getTimer();
  40.         private var _secondTime:Number;
  41.         private var _prevSecondTime:Number = getTimer();
  42.         private var _frames:Number = 0;
  43.         private var _fps:String = "...";
  44.         private var _other:String = "";
  45.         private var _memory:String;
  46.         private var _typeMemory:uint;
  47.         private var _arrayKey:Array;
  48.        
  49.         //------------------------------------
  50.         // public properties
  51.         //------------------------------------
  52.        
  53.         public static const MEMORY_BYTES:uint = 1;
  54.         public static const MEMORY_KILOBYTES:uint = 2;
  55.         public static const MEMORY_MEGABYTES:uint = 3;
  56.        
  57.         //------------------------------------
  58.         // constructor
  59.         //------------------------------------
  60.        
  61.         /**
  62.          * Constructor
  63.          * @param bgColor background color
  64.          * @param barColor color of the bar that displays the information
  65.          * @param textColor text color that displays the information
  66.          * @param bgAlpha alpha of the background
  67.          * @param typeMemory type of the memory displayed (bytes, kilobytes, megabytes)
  68.          */
  69.         public function FPS(bgColor:uint = 0xCCCCCC, barColor:uint = 0xFFFFFF, textColor:uint = 0x333333, bgAlpha:Number = 1, typeMemory:uint = 2, barVisible:Boolean = true) {
  70.             buttonMode = true;
  71.             mouseChildren = false;
  72.             _bgColor = bgColor;
  73.             _barColor = barColor;
  74.             _textColor = textColor;
  75.             _bgAlpha = bgAlpha;
  76.             _typeMemory = typeMemory;
  77.             visible = barVisible;
  78.             addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
  79.             addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
  80.         }
  81.        
  82.         //
  83.         // PRIVATE, PROTECTED
  84.         //________________________________________________________________________________________________
  85.        
  86.         private function init(e:Event):void {
  87.             stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler, false, 0, true);
  88.             removeEventListener(Event.ADDED_TO_STAGE, init, false);
  89.             _bg.graphics.beginFill(_bgColor, 1);
  90.             _bg.graphics.drawRect(0, 0, stage.stageWidth, 10);
  91.             _bg.graphics.endFill();
  92.             _bg.alpha = _bgAlpha;
  93.             addChild(_bg);
  94.             _bar.graphics.beginFill(_barColor, 1);
  95.             _bar.graphics.drawRect(0, 0, 25, 10);
  96.             _bar.graphics.endFill();
  97.             addChild(_bar);
  98.             _text.autoSize=TextFieldAutoSize.LEFT;
  99.             _text.textColor = _textColor;
  100.             _text.selectable = false;
  101.             addChild(_text);
  102.             scaleX = 2;
  103.             scaleY = 2;
  104.             addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
  105.         }
  106.        
  107.         private function clickHandler(e:MouseEvent):void {
  108.             if (e.target == e.currentTarget) {
  109.                 e.stopImmediatePropagation();
  110.                 visible = false;
  111.             }
  112.         }
  113.        
  114.         private function onEnterFrame(e:Event):void {
  115.             _time = getTimer();
  116.             _frameTime = _time - _prevFrameTime;
  117.             _secondTime = _time - _prevSecondTime;
  118.             if(_secondTime>= 1000){
  119.                 _fps = _frames.toString();
  120.                 _frames = 0;
  121.                 _prevSecondTime = _time;
  122.             }
  123.             else _frames++;
  124.             _prevFrameTime = _time;
  125.             if(_typeMemory == FPS.MEMORY_BYTES){
  126.                 _memory = flash.system.System.totalMemory.toPrecision(4);
  127.                 _memory = convert(Number(_memory)) + " bytes";
  128.             }
  129.             else if(_typeMemory == FPS.MEMORY_KILOBYTES){
  130.                 _memory = (flash.system.System.totalMemory / 1000).toPrecision(4);
  131.                 _memory = convert(Number(_memory)) + " kbs";
  132.             }
  133.             else if (_typeMemory ==  FPS.MEMORY_MEGABYTES){
  134.                 _memory = (flash.system.System.totalMemory / 1000000).toPrecision(4);
  135.                 _memory = convert(Number(_memory)) + " mbs";
  136.             }
  137.             _text.htmlText = "<font face=\"arial\" size=\"5\"> Framerate: "+ _fps +" fps / "+ _frameTime +"ms - Memory: "+ _memory +" - "+ _other.toString() +"</font>";
  138.             _bar.scaleX = _bar.scaleX - ((_bar.scaleX - (_frameTime/10)) / 5);
  139.         }
  140.        
  141.         private function convert(value:Number):Number {
  142.             return Math.round(value * Math.pow(10, 2)) / Math.pow(10, 2);
  143.         }
  144.        
  145.         private function keyHandler(e:KeyboardEvent):void {
  146.             if (e.keyCode == 70) {
  147.                 _arrayKey = []
  148.                 _arrayKey.push(e.keyCode);
  149.             }
  150.             else if (_arrayKey.length == 1 && e.keyCode == 80) {
  151.                 _arrayKey.push(e.keyCode);
  152.             }
  153.             else if (_arrayKey.length == 2 && e.keyCode == 83) {
  154.                 visible = true;
  155.             }
  156.         }
  157.  
  158.         // PUBLIC
  159.         //________________________________________________________________________________________________
  160.        
  161.         /**
  162.          * Add a value to show in the bar
  163.          * @param value
  164.          */
  165.         public function add(value:String):void {
  166.             _other = value;
  167.         }
  168.        
  169.         public function get memory():uint {
  170.             return _typeMemory;
  171.         }
  172.        
  173.         public function set memory(typeMemory:uint):void {
  174.             _typeMemory = typeMemory;
  175.         }
  176.     }
  177. }

3 Responses to “FPS”
  1. Niklas says:

    Wow - this one is super!
    I used GSkinner memory component for this but I think that this one is even better.
    The ability to hide it is super!!
    Is there a way to make it show up again besides refreshing the page again?

    I really find your site VERY useful - so much good stuff in here!!

    Thanks, Niklas

  2. Niklas says:

    I noticed a little thing - when you open a new tab with a site that contains flash the memory display get kind funky with the numbers. No biggie but I wanted to let you know....

    Niklas

  3. Romuald says:

    Thanks niklas,

    I've got a better version on my computer, I changed the way I show the values and the show/hide functionality is a good idea. I'll implement that when I've got a chance and put a new version in this page.

    Romu

Leave a Reply