Simple Particle (100k)

by Thy forked from 重力マウス(100万パーティクル) (diff: 190)
IDK Why this code is heavier than 
http://wonderfl.net/c/eZhy
 リンクリストにしてみたけどそんなに速くない??
_bmd.fillRect()を_bmd.setPixel()に変更。
sin(),cos(),atan2(),sqrt()を排除。
Add final class / mouseEnalbled = false by clockmaker
CJ Cat: Used vector instead of linked list.
clockmaker: while文の処理を4回コピペしてイテレーションの回数を減らす 100万で30fps (FP10.1 Release))
♥0 | Line 82 | Modified 2010-06-22 04:12:55 | MIT License
play

ActionScript3 source code

/**
 * Copyright Thy ( http://wonderfl.net/user/Thy )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/AmyL
 */

//IDK Why this code is heavier than 
//http://wonderfl.net/c/eZhy

// forked from clockmaker's 重力マウス(100万パーティクル)
// forked from cjcat2266's 重力マウス(プチ軽量化:25万パーティクル)
// forked from clockmaker's 重力マウス(プチ軽量化:10万パーティクル)
// forked from coppieee's 重力マウス(さらに軽量化してみた)
// forked from paq's forked from: 重力マウス(ちょっぴり軽量化してみた)
// forked from fumix's 重力マウス(リンクリストにしてみた)
// forked from undo's 重力マウス
// リンクリストにしてみたけどそんなに速くない??
//_bmd.fillRect()を_bmd.setPixel()に変更。
//sin(),cos(),atan2(),sqrt()を排除。
// Add final class / mouseEnalbled = false by clockmaker
// CJ Cat: Used vector instead of linked list.
// clockmaker: while文の処理を4回コピペしてイテレーションの回数を減らす 100万で30fps (FP10.1 Release))
package {
    import flash.display.Sprite;
	import flash.geom.Rectangle;
    import flash.utils.ByteArray;
    import flash.events.Event;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import net.hires.debug.Stats;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    public class SimpleParticle extends Sprite 
    {
    		// number of particles
    		private var num:uint = 1000 * 100
    		// particle data storage
    		private var position:ByteArray, shift:ByteArray
    		// canvas
    		private var bitmap:Bitmap, bitmapData:BitmapData;
		private var RECT:Rectangle
    		// temp loop uint
    		private var i:uint
    		// temp variables
    		private var X:uint, Y:uint
    		//
    		
        public function SimpleParticle() 
        {
        		stage.align = StageAlign.TOP_LEFT
        		stage.scaleMode = StageScaleMode.NO_SCALE
        		stage.quality = "low"
        		this.mouseEnabled = false;
			this.mouseChildren = false;
        		
            // create particles proprieties
			position = new ByteArray()
			shift = new ByteArray()
            position.position = 0
            shift.position = 0
			//
            i = 0
            while(i < num)
            {
            		// x,y coordinates
            		position.writeUnsignedInt((Math.random() * (465 << 10)) >> 0)
            		position.writeUnsignedInt((Math.random() * (465 << 10)) >> 0)
            		// x,y velocity
            		shift.writeUnsignedInt   ((Math.random()* (1<<10)) >> 0)
            		shift.writeUnsignedInt   ((Math.random()* (1<<10)) >> 0)
            		++i
            }
			position.writeUnsignedInt(0xFFFFFF)//addChild an last uint, for the looping process
            // canvas
            bitmapData = new BitmapData(465,465,false,0xFFFFFF)
            bitmap = new Bitmap(bitmapData)
            stage.addChild(bitmap)
			RECT = bitmapData.rect
            // enterframe listener
            stage.addEventListener(Event.ENTER_FRAME, ef)
            stage.addChild(new Stats())
        }
        
        private function ef(e:Event):void
        {
        		position.position = 0
        		shift.position = 0
				
				// loop for each particle
				bitmapData.lock() // performace
				bitmapData.fillRect(RECT,0)
        		while( (X = position.readUnsignedInt()) != uint(0xFFFFFF) ) // while(position.position < position.length)
        		{
        			Y = position.readUnsignedInt()
					
					X += shift.readUnsignedInt() - 512
        			Y += shift.readUnsignedInt() - 512
					
					if (X > 1904640) // 465 << 12
					{
						X = 476160 // 465 << 10
					} else
					if (X > 476160)
					{
						X -= 476160
					} else
					
					if (Y > 1904640) 
					{
						Y = 476160
					} else
					if (Y > 476160)
					{
						Y -= 476160
					}
					
        			position.position -= 8 // 2x uint
        			position.writeUnsignedInt(X)
        			position.writeUnsignedInt(Y)
					
				bitmapData.setPixel(X>>10,Y>>10, 0xFFFFFF)
        		}
				bitmapData.unlock()
				// end of frame
        }
    }
}