Advertisement
cornedor

Blowing up images

Jul 9th, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package;
  2. import flash.display.BitmapData;
  3. import flash.display.Loader;
  4. import flash.events.Event;
  5. import flash.events.TimerEvent;
  6. import flash.Lib;
  7. import flash.net.URLRequest;
  8. import flash.utils.Timer;
  9. import flash.Vector;
  10. import haxe.FastList;
  11.  
  12. class Main extends flash.display.Bitmap
  13. {
  14.     private var graphics:BitmapData;
  15.     private var loader:Loader;
  16.     private var particles:FastList<Particle>;
  17.     private var noise:BitmapData;
  18.     private var noiseVector:Vector<Vector<UInt>>;
  19.     private var fps:Int;
  20.    
  21.     inline static private var __IMGURL:String = "pic.jpg";
  22.     inline static private var __DEBUG:Bool = true;
  23.     public function new():Void
  24.     {
  25.         graphics = new BitmapData(800, 600, false, 0x0);       
  26.         super(graphics);
  27.        
  28.         noise = new BitmapData(800, 600, false, 0x0);
  29.         noise.perlinNoise(200, 200, 5, Std.int(Math.random()*0xFFFFFF), false, true, flash.display.BitmapDataChannel.RED | flash.display.BitmapDataChannel.GREEN, false);
  30.         noiseVector = new Vector<Vector<UInt>>();
  31.         for(x in 0...800)
  32.         {
  33.             noiseVector.push(new Vector<UInt>());
  34.             for(y in 0...600)
  35.             {
  36.                 noiseVector[x].push(noise.getPixel(x, y));
  37.             }
  38.         }
  39.         loader = new Loader();
  40.         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
  41.         loader.load(new URLRequest(__IMGURL));
  42.         if(__DEBUG)
  43.         {
  44.             var t:Timer = new Timer(1000, 99999);
  45.             t.addEventListener(TimerEvent.TIMER, onTick);
  46.             t.start();
  47.         }
  48.     }
  49.     private inline function onTick(e:TimerEvent):Void
  50.     {
  51.         haxe.Log.clear();
  52.         haxe.Log.setColor(0xFFFFFF);
  53.         trace("Debug:");
  54.         trace("V0.4.001");
  55.         trace("FPS: " + fps);
  56.         fps = 0;
  57.     }
  58.     private function onLoadComplete(e:Event):Void
  59.     {
  60.         var _imgBmd:BitmapData = new BitmapData(400, 300);
  61.         _imgBmd.draw(loader);
  62.         particles = new FastList<Particle>();
  63.         for(x in 0...400)
  64.             for(y in 0...300)
  65.             {
  66.                 particles.add(new Particle(x+200, y+150, _imgBmd.getPixel(x, y)));
  67.             }
  68.         addEventListener(Event.ENTER_FRAME, draw);
  69.     }
  70.     private inline function draw(e:Event):Void
  71.     {
  72.         graphics.lock();
  73.         //graphics.fillRect(graphics.rect, 0x0);
  74.         graphics.colorTransform(graphics.rect, new flash.geom.ColorTransform(.9, .9, .9, 1, 0, 0, 0, 0));
  75.         for(p in particles)
  76.         {
  77.             if(p.x >= 800 || p.x <= 0 || p.y >= 600 || p.y <= 0 && !p.dead) p.dead = true;
  78.             if(!p.dead)
  79.             {
  80.                 var px:Int = Std.int(p.x);
  81.                 var py:Int = Std.int(p.y);
  82.                 var c:UInt = noiseVector[px][py];
  83.                 var r:Int = c >> 16 & 0xFF;
  84.                 var g:Int = c >> 8 & 0xFF;
  85.                 p.x += (p.speedX -= (r-126)*.001);
  86.                 p.y += (p.speedY -= (g-126)*.001);
  87.                
  88.                 /*if(p.x > 800) p.x -= 800;
  89.                 else if(p.x < 0) p.x += 800;
  90.                 if(p.y > 600) p.y -= 600;
  91.                 else if(p.y < 0) p.y += 600;*/
  92.                 graphics.setPixel(px, py, p.color);
  93.             }
  94.         }
  95.         graphics.unlock();
  96.         fps++;         
  97.            
  98.     }
  99.     static function main()
  100.     {
  101.         Lib.current.addChild(new Main());
  102.     }
  103. }
  104. class Particle
  105. {
  106.     public var x:Float;
  107.     public var y:Float;
  108.     public var color:UInt;
  109.     public var speedX:Float;
  110.     public var speedY:Float;
  111.     public var dead:Bool;
  112.     public function new(x:Float, y:Float, color:UInt, ?speedX:Float = 0, ?speedY:Float = 0):Void
  113.     {
  114.         this.x = x;
  115.         this.y = y;
  116.         this.color = color;
  117.         this.speedX = speedX;
  118.         this.speedY = speedY;
  119.         this.dead = false;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement