(1 << 17) Particles + Fixed-Point Math
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
[SWF(width = '512', height = '512')]
public class FixedP extends Sprite//
{
private const NUM_P : uint = 1 << 17;
// :: INLINED :: FM = (1 << 12)
/* */
private var bmd : BitmapData;
private var fmap : BitmapData;
private var ppp : Particle;
private var fmx : Vector.<int>;
private var fmy : Vector.<int>;
private var fcnt : int;
function FixedP()//
{
stage.stageFocusRect = mouseEnabled = tabEnabled = tabChildren = false;
stage.scaleMode = "noScale";
stage.align = "TL";
stage.quality = "low";
stage.frameRate = 64;
opaqueBackground = 0x0;
var bm : Bitmap = new Bitmap(bmd = new BitmapData(512, 512, false, 0x0));
opaqueBackground = 0x0;
addChild(bm);
fmap = new BitmapData(128, 128, false);
fmx = new Vector.<int>(fmap.width * fmap.height, true);
fmy = new Vector.<int>(fmap.width * fmap.height, true);
var p : Particle = ppp = new Particle();
var n : int = NUM_P;
while (n-- != 0)//
{
p.x = (512 * Math.random()) << 12;
p.y = (512 * Math.random()) << 12;
p.c = (0xFFFFFF * Math.random()) >> 0;
p = p.next = new Particle();
}
addEventListener(Event.ENTER_FRAME, oef);
}
private function oef(e : Event) : void//
{
var pos : uint;
fcnt++;
if ((fcnt & 63) == 1)//
{
fmap.perlinNoise(128, 128, 8, Math.random() * 0xFFFF, false, true, 7, false);
for (var py : uint = 0; py < 128; py++)//
{
for (var px : uint = 0; px < 128; px++)//
{
var f : uint = fmap.getPixel(px, py);
pos = (py << 7) + px;
fmx[pos] = ((f >> 16 & 0xFF) - 128) << 12;
fmy[pos] = ((f >> 8 & 0xFF) - 128) << 12;
}
}
}
bmd.lock();
//bmd.fillRect(bmd.rect, 0x0);
var p : Particle = ppp;
while (p != null)//
{
pos = ((p.y >> 14) << 7) + (p.x >> 14);
p.vx = (((p.vx * 4050) >> 0) + (fmx[pos] << 1)) >> 12;
p.vy = (((p.vy * 4050) >> 0) + (fmy[pos] << 1)) >> 12;
p.x += p.vx;
p.y += p.vy;
p.x = p.x & (((1 << 12) << 9) - 1);
p.y = p.y & (((1 << 12) << 9) - 1);
bmd.setPixel(p.x >> 12, p.y >> 12, p.c);
p = p.next;
}
bmd.unlock();
}
}
}
internal class Particle {
public var next : Particle;
public var c : uint;
public var x : uint;
public var y : uint;
public var vx : int;
public var vy : int;
function Particle() {
}
}