In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: Bitmap vs. BitmapData.draw()

Get Adobe Flash player
by yonatan 02 Oct 2011
  • Forked from o8que's Bitmap vs. BitmapData.draw()
  • Diff: 44
  • Related works: 2
  • Talk

    makc3d at 02 Oct 2011 11:24
    not so unexpected if you think, flash doing composition internally vs you doing it by hand in as3.
    yonatan at 02 Oct 2011 11:49
    I dunno, I would understand if it was the same speed, but this... what could be the overhead with draw()?
    makc3d at 02 Oct 2011 14:46
    I guess it's passing control from as3 to native code and back. such as computing the matrix from Particle vars in 5 calls to Matrix methods, then the draw call itself - the loop happens in as3 (vs somewhere in flash player rendering engine in case of bitmaps)
    yonatan at 02 Oct 2011 15:51
    i added some extra stuff to the bitmap testing loop, looks like it's the draw() call itself that's slow
    wonderwhyer at 02 Oct 2011 16:09
    There is a lot of things going on there. Stage and Bitmap are not the same. Also .draw is not same as what happens with stage. This needs exploration but few things come to mind: 1) firstly check redraw regions with debug player, they will probably be different for stage and bitmap 2) as far as I can say bitmap forces complete redraw while on stage entities use "caching" when nothing changes 3) also bitmap itself is on stage and is drawn
    Embed
/**
 * Copyright yonatan ( http://wonderfl.net/user/yonatan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/85sO
 */

 // forked from o8que's Bitmap vs. BitmapData.draw()
package {
    import com.bit101.components.PushButton;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import net.hires.debug.Stats;
    
    [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "0xFFFFFF")]
    public class Main extends Sprite {
        private var _bmd:BitmapData;
        
        private var _container1:Sprite;
        private var _container2:Bitmap;
        private var _screen2:BitmapData;
        
        private var _particles1:Vector.<Bitmap>;
        private var _particles2:Vector.<Particle>;
        
        private var _isTesting1:Boolean;
        
        public static const NUM_PARTICLES:int = 1000;
        public static const STAGE_RECT:Rectangle = new Rectangle(0, 0, 465, 465);
        public static const DEG_TO_RAD:Number = Math.PI / 180;
        
        public function Main() {
            _bmd = new BitmapData(32, 32, true, 0x40000000);
            
            _container1 = new Sprite();
            _container2 = new Bitmap(_screen2 = new BitmapData(465, 465, false, 0x00FFFFFF));
            
            _particles1 = new Vector.<Bitmap>(NUM_PARTICLES, true);
            _particles2 = new Vector.<Particle>(NUM_PARTICLES, true);
            for (var i:int = 0; i < NUM_PARTICLES; i++) {
                var bitmap:Bitmap = new Bitmap(_bmd, "auto", true);
                bitmap.x = 465 * Math.random();
                bitmap.y = 465 * Math.random();
                bitmap.rotation = 180 * Math.random();
                bitmap.scaleX = bitmap.scaleY = 0.5;
                _container1.addChild(_particles1[i] = bitmap);
                
                var particle:Particle = new Particle();
                particle.x = 465 * Math.random();
                particle.y = 465 * Math.random();
                particle.rotation = 180 * Math.random();
                _particles2[i] = particle;
            }
            
            _isTesting1 = true;
            addChild(_container1);
            
            stage.addChild(new Stats());
            var pb:PushButton = new PushButton(null, 100, 400, "Testing Bitmap - click to test draw()", pushButtonHandler);
            pb.width = 232;
            stage.addChild(pb);
            
            addEventListener(Event.ENTER_FRAME, update);
        }
        
        private function pushButtonHandler(event:MouseEvent):void {
            if (_isTesting1) {
                event.currentTarget.label = "Testing draw() - click to test Bitmap";
                removeChild(_container1); addChild(_container2);
            } else {
                event.currentTarget.label = "Testing Bitmap - click to test draw()";
                removeChild(_container2); addChild(_container1);
            }
            _isTesting1 = !_isTesting1;
        }
        
        private var matrix:Matrix = new Matrix();
        private function update(event:Event):void {
            var particle:Particle;
            if (_isTesting1) {
                for (var i:int = 0; i < NUM_PARTICLES; i++) {
                    var bitmap:Bitmap = _particles1[i];
                    bitmap.x = (bitmap.x + 2) % 465;
                    bitmap.y = (bitmap.y + 2) % 465;
                    bitmap.rotation++;
                    
                    // extra stuff

                    particle = _particles2[i];
                    particle.x = (particle.x + 2) % 465;
                    particle.y = (particle.y + 2) % 465;
                    particle.rotation++;
                    
                    matrix.identity();
                    matrix.translate( -16, -16);
                    matrix.scale(0.5, 0.5);
                    matrix.rotate(particle.rotation * DEG_TO_RAD);
                    matrix.translate(particle.x, particle.y);
                    particle = _particles2[i];
                    particle.x = (particle.x + 2) % 465;
                    particle.y = (particle.y + 2) % 465;
                    particle.rotation++;
                    
                    matrix.identity();
                    matrix.translate( -16, -16);
                    matrix.scale(0.5, 0.5);
                    matrix.rotate(particle.rotation * DEG_TO_RAD);
                    matrix.translate(particle.x, particle.y);
                    particle = _particles2[i];
                    particle.x = (particle.x + 2) % 465;
                    particle.y = (particle.y + 2) % 465;
                    particle.rotation++;
                    
                    matrix.identity();
                    matrix.translate( -16, -16);
                    matrix.scale(0.5, 0.5);
                    matrix.rotate(particle.rotation * DEG_TO_RAD);
                    matrix.translate(particle.x, particle.y);
                }
            } else {
                _screen2.lock();
                _screen2.fillRect(STAGE_RECT, 0x00FFFFFF);
                //var matrix:Matrix = new Matrix();
                for (i = 0; i < NUM_PARTICLES; i++) {
                    particle = _particles2[i];
                    particle.x = (particle.x + 2) % 465;
                    particle.y = (particle.y + 2) % 465;
                    particle.rotation++;
                    
                    matrix.identity();
                    matrix.translate( -16, -16);
                    matrix.scale(0.5, 0.5);
                    matrix.rotate(particle.rotation * DEG_TO_RAD);
                    matrix.translate(particle.x, particle.y);
                    
                    _screen2.draw(_bmd, matrix, null, null, null, true);
                }
                _screen2.unlock();
            }
        }
    }
}

class Particle {
    public var x:Number;
    public var y:Number;
    public var rotation:Number;
}