forked from: Easy Benchmark
簡易ベンチマークです。
ランダムな速度で動くボールを毎フレームごとに100個ずつ追加していき、
直近10秒の平均fpsが30以下になった時点のボールの数をスコアとします。
スコアが出るまで少し時間がかかります。
ちょっと待ってね just a moment!
// forked from ton's Easy Benchmark
/*
簡易ベンチマークです。
ランダムな速度で動くボールを毎フレームごとに100個ずつ追加していき、
直近10秒の平均fpsが30以下になった時点のボールの数をスコアとします。
スコアが出るまで少し時間がかかります。
ちょっと待ってね just a moment!
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.utils.getTimer;
import flash.system.Capabilities;
[SWF(frameRate=240)]
public class EasyBenchmark extends Sprite {
private const W:int = 465;
private const H:int = 465;
private const MAX_V:int = 10;
private const MIN_V:int = 3;
private var balls:/*Ball*/Array = [];
private var frameRates:/*int*/Array = [];
private var index:int = 0;
private var ave:int = 120;
private var ballCnt:int = 0;
private var txtBallCnt:TextField;
private var frameCnt:int = 0;
private var txtFrameCnt:TextField;
private var oldTimer:int;
private var bmd:BitmapData;
private var bmp:Bitmap;
public function EasyBenchmark():void {
bmd = new BitmapData(W, H, false);
bmp = new Bitmap(bmd);
addChild(bmp);
txtBallCnt = new TextField();
txtBallCnt.autoSize = "left";
txtBallCnt.background = true;
addChild(txtBallCnt);
txtFrameCnt = new TextField();
txtFrameCnt.autoSize = "left";
txtFrameCnt.background = true;
txtFrameCnt.y = 30;
addChild(txtFrameCnt);
oldTimer = getTimer();
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void {
updateFrame();
if (ave <= 30) {
removeEventListener(Event.ENTER_FRAME, update);
complete();
return;
}
for (var i:int = 0; i < 100; i++){
var ball:Ball = new Ball(randomRange(MAX_V, MIN_V), randomRange(MAX_V, MIN_V));
ball.x = Math.random() * W;
ball.y = Math.random() * H;
balls.push(ball);
ballCnt++;
}
txtBallCnt.text = "score : " + ballCnt;
bmd.fillRect(bmd.rect, 0xffffff);
for each(ball in balls) {
ball.x += ball.vx;
ball.y += ball.vy;
bmd.setPixel(ball.x, ball.y, 0xff0000);
if (ball.x <= 0 || ball.x >= W) {
ball.vx *= -1;
}
if (ball.y <= 0 || ball.y >= H) {
ball.vy *= -1;
}
}
}
private function complete():void{
var tf:TextField = new TextField();
tf.autoSize = "left";
tf.text = "YOUR SCORE: " + ballCnt + "\nVersion: " + Capabilities.version;
tf.scaleX = tf.scaleY = 3;
tf.x = W / 2 - tf.width / 2;
tf.y = H / 2 - tf.height;
addChild(tf);
var button:TextField = new TextField();
button.background = true;
button.backgroundColor = 0xcccccc;
button.autoSize = "left";
button.selectable = false;
button.text = "twitterに投稿";
button.scaleX = button.scaleY = 2;
button.x = W / 2 - button.width / 2;
button.y = H / 2;
button.addEventListener(MouseEvent.CLICK, function():void {
navigateToURL(new URLRequest("http://twitter.com/?status=Easy Benchmark SCORE: "+ballCnt+ ":ver: " + Capabilities.version +" http://wonderfl.net/c/gWXM %23wonderfl"));
});
addChild(button);
}
private function updateFrame():void {
frameCnt++;
if (getTimer() - oldTimer >= 1000) {
frameRates[index] = frameCnt;
index = ++index % 10;
ave = average.apply(null, frameRates);
txtFrameCnt.text = "fps : " + frameCnt + "/" + stage.frameRate + "\nave : " + ave;
frameCnt = 0;
oldTimer = getTimer();
}
}
private function average(... rest):Number {
var sum:Number = 0;
for each(var n:Number in rest) sum += n;
return sum / rest.length;
}
private function randomRange(max:Number, min:Number):Number {
return Math.random()*(max - min) + min;
}
}
}
class Ball {
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public function Ball(vx:Number, vy:Number) {
this.vx = vx;
this.vy = vy;
}
}