import flash.display.Sprite; import flash.display.Bitmap; import flash.display.BitmapData; import flash.text.TextField; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer; import flash.geom.Point; class Mandelbrot { static function main() : Void { new Mandelbrot(); } public var colorModifier:Int; public var bitmapData:BitmapData; public var paletteData:BitmapData; public var fps:TextField; public var rgbArray:Array; static inline var width:Int = 320; static inline var height:Int = 240; public function new() { rgbArray = new Array(); var setBitmap:Bitmap = new Bitmap(); #if !flash bitmapData = new BitmapData( width, height, false, neash.RGB.BLACK); #else bitmapData = new BitmapData( flash.Lib.current.stage.stageWidth , flash.Lib.current.stage.stageHeight , false , 0x000000 ); paletteData = new BitmapData( flash.Lib.current.stage.stageWidth , flash.Lib.current.stage.stageHeight , false , 0x000000 ); #end setBitmap.bitmapData = bitmapData; flash.Lib.current.addChild( setBitmap ); var maxIterations:Int = 127; var beforeTime = flash.Lib.getTimer(); var xtemp:Float; var iteration:Int; var x0:Float = 0; var y0:Float = 0; for(ix in 0...width) { for(iy in 0...height) { x0 = 0; y0 = 0; iteration = 127; while ( x0*x0 + y0*y0 <= 4 && iteration > 0 ) { xtemp = x0*x0 - y0*y0 + (ix-14*5000)/50000; y0 = 2*x0*y0 + (iy-(height/0.6))/50000; x0 = xtemp; iteration--; } bitmapData.setPixel(ix, iy, iteration); paletteData.setPixel(ix, iy, iteration); } } var afterTime = flash.Lib.getTimer(); var tf = new TextField(); tf.width = 300; tf.text = "Generating fractal took "+(afterTime-beforeTime)+" ms"; flash.Lib.current.addChild(tf); fps = new TextField(); fps.width = 300; fps.y = 10; fps.text = "FPS: "; flash.Lib.current.addChild(fps); colorModifier = 1; flash.Lib.current.addEventListener(Event.ENTER_FRAME, runLoop); } public inline function runLoop(inEvent:Event) { var r:Int=0, b:Int=0, g:Int=0; var beforeTime = flash.Lib.getTimer(); for(i in 0...128) { r = i + colorModifier; g = i + colorModifier + r; b = i + colorModifier + g; rgbArray[i] = (r<<16 | g<<8 | b); } bitmapData.paletteMap(paletteData, bitmapData.rect, new Point(0,0), rgbArray, rgbArray, rgbArray); var afterTime = flash.Lib.getTimer(); fps.text = "FPS: "+Math.round(1000/(afterTime-beforeTime)); colorModifier += 1; if(colorModifier > 254) colorModifier = 0; } }