Skip to content

Instantly share code, notes, and snippets.

@cornedor
Created October 4, 2013 14:00
Show Gist options
  • Save cornedor/6826376 to your computer and use it in GitHub Desktop.
Save cornedor/6826376 to your computer and use it in GitHub Desktop.
Exploding images
package;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.Lib;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.Vector;
import haxe.FastList;
class Main extends flash.display.Bitmap
{
private var graphics:BitmapData;
private var loader:Loader;
private var particles:FastList<Particle>;
private var noise:BitmapData;
private var noiseVector:Vector<Vector<UInt>>;
private var fps:Int;
inline static private var __IMGURL:String = "pic.jpg";
inline static private var __DEBUG:Bool = true;
public function new():Void
{
graphics = new BitmapData(800, 600, false, 0x0);
super(graphics);
noise = new BitmapData(800, 600, false, 0x0);
noise.perlinNoise(200, 200, 5, Std.int(Math.random()*0xFFFFFF), false, true, flash.display.BitmapDataChannel.RED | flash.display.BitmapDataChannel.GREEN, false);
noiseVector = new Vector<Vector<UInt>>();
for(x in 0...800)
{
noiseVector.push(new Vector<UInt>());
for(y in 0...600)
{
noiseVector[x].push(noise.getPixel(x, y));
}
}
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(new URLRequest(__IMGURL));
if(__DEBUG)
{
var t:Timer = new Timer(1000, 99999);
t.addEventListener(TimerEvent.TIMER, onTick);
t.start();
}
}
private inline function onTick(e:TimerEvent):Void
{
haxe.Log.clear();
haxe.Log.setColor(0xFFFFFF);
trace("Debug:");
trace("V0.4.001");
trace("FPS: " + fps);
fps = 0;
}
private function onLoadComplete(e:Event):Void
{
var _imgBmd:BitmapData = new BitmapData(400, 300);
_imgBmd.draw(loader);
particles = new FastList<Particle>();
for(x in 0...400)
for(y in 0...300)
{
particles.add(new Particle(x+200, y+150, _imgBmd.getPixel(x, y)));
}
addEventListener(Event.ENTER_FRAME, draw);
}
private inline function draw(e:Event):Void
{
graphics.lock();
//graphics.fillRect(graphics.rect, 0x0);
graphics.colorTransform(graphics.rect, new flash.geom.ColorTransform(.9, .9, .9, 1, 0, 0, 0, 0));
for(p in particles)
{
if(p.x >= 800 || p.x <= 0 || p.y >= 600 || p.y <= 0 && !p.dead) p.dead = true;
if(!p.dead)
{
var px:Int = Std.int(p.x);
var py:Int = Std.int(p.y);
//var c:UInt = noise.getPixel(px, py);
var c:UInt = noiseVector[px][py];
var r:Int = c >> 16 & 0xFF;
var g:Int = c >> 8 & 0xFF;
p.x += (p.speedX -= (r-126)*.001);
p.y += (p.speedY -= (g-126)*.001);
/*if(p.x > 800) p.x -= 800;
else if(p.x < 0) p.x += 800;
if(p.y > 600) p.y -= 600;
else if(p.y < 0) p.y += 600;*/
graphics.setPixel(px, py, p.color);
}
}
graphics.unlock();
fps++;
}
static function main()
{
Lib.current.addChild(new Main());
}
}
class Particle
{
public var x:Float;
public var y:Float;
public var color:UInt;
public var speedX:Float;
public var speedY:Float;
public var dead:Bool;
public function new(x:Float, y:Float, color:UInt, ?speedX:Float = 0, ?speedY:Float = 0):Void
{
this.x = x;
this.y = y;
this.color = color;
this.speedX = speedX;
this.speedY = speedY;
this.dead = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment