Created
September 8, 2013 18:13
-
-
Save ryzed/6487102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.BitmapData; | |
import flash.display.Sprite; | |
import flash.display.Stage; | |
import flash.events.Event; | |
import flash.geom.Rectangle; | |
import flash.text.TextField; | |
import flash.utils.getTimer; | |
import flash.utils.setInterval; | |
/** | |
* ... | |
* @author ryzed | |
*/ | |
[Frame(factoryClass="Preloader")] | |
public class Main extends Sprite | |
{ | |
public var maxFPS : int; | |
protected var fps : TextField; | |
protected var bitmapData : BitmapData; | |
public var colorModifier : int; | |
public var bdrc : Rectangle; | |
public var outPixels : Vector.<uint>; | |
public var srcPixels : Vector.<uint>; | |
public var stH : int; | |
public var stW : int; | |
public function Main():void | |
{ | |
if (stage) init(); | |
else addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(e:Event = null):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
// entry point | |
var w : int = (stage.stageWidth); | |
var h : int = (stage.stageHeight); | |
this.stW = w; | |
this.stH = h; | |
this.bitmapData = new flash.display.BitmapData(w,h,false,0); | |
stage.addChild(new flash.display.Bitmap(this.bitmapData)); | |
var maxIterations : int = 128; | |
this.bdrc = this.bitmapData.rect; | |
this.srcPixels = new Vector.<uint>(w * h,true); | |
this.outPixels = new Vector.<uint>(w * h,true); | |
var beforeTime : int = getTimer(); | |
var xtemp : Number; | |
var iteration : int; | |
var x0 : Number = 0; | |
var y0 : Number = 0; | |
var adr : int = 0; | |
var iy : int = 0; | |
while(iy < h) { | |
var ix : int = 0; | |
while(ix < w) { | |
x0 = 0; | |
y0 = 0; | |
iteration = 128; | |
while(x0 * x0 + y0 * y0 <= 4 && iteration > 0) { | |
xtemp = x0 * x0 - y0 * y0 + (ix - 70000) / 50000; | |
y0 = 2 * x0 * y0 + (iy - h / 0.6) / 50000; | |
x0 = xtemp; | |
iteration--; | |
}; | |
this.srcPixels[adr++] = iteration; | |
ix++; | |
}; | |
iy++; | |
}; | |
var afterTime : int = getTimer(); | |
var tf : flash.text.TextField = new flash.text.TextField(); | |
tf.width = 400; | |
tf.text = "Generating fractal took " + (afterTime - beforeTime) + " ms"; | |
stage.addChild(tf); | |
this.fps = new flash.text.TextField(); | |
this.fps.width = 400; | |
this.fps.y = 10; | |
this.fps.text = "FPS: "; | |
stage.addChild(this.fps); | |
this.colorModifier = 2; | |
setInterval(runLoop, 5); | |
} | |
public function runLoop() : void { | |
var w : int = this.stW; | |
var h : int = this.stH; | |
var cm : int = this.colorModifier; | |
var bd : flash.display.BitmapData = this.bitmapData; | |
var sPixels : Vector.<uint> = this.srcPixels; | |
var oPixels : Vector.<uint> = this.outPixels; | |
var len : int = w * h; | |
var beforeTime : int = getTimer(); | |
var adr : int = 0; | |
while(adr < len) { | |
var r : uint = sPixels[adr] + cm; | |
var g : uint = r + r; | |
oPixels[adr++] = ((r << 16 | g << 8) | r + g); | |
}; | |
bd.setVector(this.bdrc,oPixels); | |
var afterTime : int = getTimer(); | |
var dt : int = afterTime - beforeTime; | |
var curFPS : int = Math.round(1000 / (afterTime - beforeTime)); | |
if(curFPS > this.maxFPS) this.maxFPS = curFPS; | |
this.fps.text = "FPS: " + curFPS + ", dt=" + dt + "\rMax FPS: " + this.maxFPS; | |
this.colorModifier += 2; | |
if(this.colorModifier > 65530) this.colorModifier = 0; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment