Closed IonSwitz closed 10 years ago
package
{
import flash.display.Shape;
import starling.display.Sprite;
import starling.display.graphicsEx.ShapeEx;
/**
* ...
* Simple class to draw numbers (useful for profiling)
*/
public class DebugNumbers extends Sprite
{
protected var _shapeEx:ShapeEx;
public var _characterHeight:Number = 20;
public var _characterWidth:Number = 10;
public var _lineWidth:Number = 2;
public var _lineColor:Number = 0xFFFFFF;
public function DebugNumbers()
{
_shapeEx = new ShapeEx();
addChild(_shapeEx);
}
public function drawNumber(value:uint) : void
{
var valueString:String = "" + value;
var xPos:Number = x;
_shapeEx.graphics.clear();
_shapeEx.graphics.lineStyle(_lineWidth, _lineColor, 1.0);
for ( var i:int = 0; i < valueString.length; i++)
{
var currentChar:String = valueString.charAt(i);
if ( currentChar == "0" )
xPos += draw_0(xPos);
else if (currentChar == "1" )
xPos += draw_1(xPos);
else if (currentChar == "2" )
xPos += draw_2(xPos);
else if (currentChar == "3" )
xPos += draw_3(xPos);
else if (currentChar == "4" )
xPos += draw_4(xPos);
else if (currentChar == "5" )
xPos += draw_5(xPos);
else if (currentChar == "6" )
xPos += draw_6(xPos);
else if (currentChar == "7" )
xPos += draw_7(xPos);
else if (currentChar == "8" )
xPos += draw_8(xPos);
else if (currentChar == "9" )
xPos += draw_9(xPos);
}
}
protected function draw_0(xpos:Number) : Number
{
_shapeEx.graphics.drawEllipse(xpos + _characterWidth * 0.5, _characterHeight * 0.5, _characterWidth, _characterHeight);
return _characterWidth + _lineWidth;
}
protected function draw_1(xpos:Number) : Number
{
_shapeEx.graphics.moveTo(xpos + _characterWidth * 0.5, 0);
_shapeEx.graphics.lineTo(xpos + _characterWidth * 0.5, _characterHeight);
return _characterWidth + _lineWidth;
}
protected function draw_2(xpos:Number) : Number
{
_shapeEx.graphics.moveTo(xpos , _characterHeight * 0.2 );
_shapeEx.graphics.curveTo(xpos + _characterWidth * 0.5, -_characterHeight * 0.2, xpos + _characterWidth, _characterHeight * 0.2);
_shapeEx.graphics.lineTo(xpos , _characterHeight);
_shapeEx.graphics.lineTo(xpos + _characterWidth, _characterHeight);
return _characterWidth + _lineWidth;
}
protected function draw_3(xpos:Number) : Number
{
_shapeEx.graphics.moveTo(xpos , _characterHeight * 0.2 );
_shapeEx.graphics.curveTo(xpos + _characterWidth * 0.5, -_characterHeight * 0.2, xpos + _characterWidth, _characterHeight * 0.2);
_shapeEx.graphics.lineTo(xpos + _characterWidth * 0.5, _characterHeight * 0.5);
_shapeEx.graphics.lineTo(xpos + _characterWidth , _characterHeight * 0.8);
_shapeEx.graphics.curveTo(xpos + _characterWidth * 0.5, _characterHeight * 1.2, xpos , _characterHeight * 0.8);
return _characterWidth + _lineWidth;
}
protected function draw_4(xpos:Number) : Number
{
_shapeEx.graphics.moveTo(xpos + _characterWidth * 0.8 , 0);
_shapeEx.graphics.lineTo(xpos + _characterWidth * 0.8 , _characterHeight);
_shapeEx.graphics.moveTo(xpos + _characterWidth * 0.2 , 0);
_shapeEx.graphics.lineTo(xpos + _characterWidth * 0.2 , _characterHeight * 0.4);
_shapeEx.graphics.lineTo(xpos + _characterWidth , _characterHeight * 0.4);
return _characterWidth + _lineWidth;
}
protected function draw_5(xpos:Number) : Number
{
_shapeEx.graphics.moveTo(xpos + _characterWidth , 0 );
_shapeEx.graphics.lineTo(xpos , 0);
_shapeEx.graphics.lineTo(xpos , _characterHeight * 0.4);
_shapeEx.graphics.curveTo(xpos + _characterWidth * 2.0, _characterHeight * 0.75, xpos , _characterHeight);
return _characterWidth + _lineWidth;
}
protected function draw_6(xpos:Number) : Number
{
_shapeEx.graphics.moveTo(xpos + _characterWidth * 0.8 , 0);
_shapeEx.graphics.lineTo(xpos , _characterHeight * 0.65);
_shapeEx.graphics.drawEllipse(xpos + _characterWidth * 0.5 , _characterHeight * 0.75, _characterWidth, _characterHeight*0.5);
return _characterWidth + _lineWidth;
}
protected function draw_7(xpos:Number) : Number
{
_shapeEx.graphics.moveTo(xpos , 0);
_shapeEx.graphics.lineTo(xpos + _characterWidth , 0);
_shapeEx.graphics.lineTo(xpos + 0.5 * _characterWidth , _characterHeight);
return _characterWidth + _lineWidth;
}
protected function draw_8(xpos:Number) : Number
{
_shapeEx.graphics.drawEllipse(xpos + _characterWidth * 0.5, _characterHeight * 0.25, _characterWidth * .8, _characterHeight * 0.4);
_shapeEx.graphics.drawEllipse(xpos + _characterWidth * 0.5 , _characterHeight * 0.75, _characterWidth, _characterHeight*0.5);
return _characterWidth + _lineWidth;
}
protected function draw_9(xpos:Number) : Number
{
_shapeEx.graphics.drawEllipse(xpos + _characterWidth * 0.5 , _characterHeight * 0.25, _characterWidth, _characterHeight * 0.5);
_shapeEx.graphics.moveTo(xpos + _characterWidth , _characterHeight* 0.4 );
_shapeEx.graphics.lineTo(xpos + _characterWidth*0.2 , _characterHeight);
return _characterWidth + _lineWidth;
}
}
}
One thing that has bothered me is how hard it can be to do speed comparisons in Release builds. trace-output wont work for me in FlashDevelop Release builds, for some reason, in spite of the flag saying allow trace output.
So I decided to do this piece of madness. A class to output the numbers 0,1,2,3,4,5,6,7,8 and 9 through the Graphics package, using only lineTo, moveTo, curveTo, etc.
If anyone finds any use what so ever for this, I'm happy to have helped. To me, it just gives me an easy way to compare performance runs of the type: Adding 5000 vertexes, is Method A faster than Method B?
What REALLY should be done, ofcourse, is to import SVG graphics through this Graphics API... But, well, it seems like an amazingly daunting task. ;)
The only "API" is
public function drawNumber(value:uint) : void
var debug:DebugNumbers = new DebugNumbers(); debug.x = 100; debug.y = 100; addChild(debug); debug.drawNumber(1234); // etc
You can set the public properties:
public var _characterHeight:Number = 20; public var _characterWidth:Number = 10; public var _lineWidth:Number = 2; public var _lineColor:Number = 0xFFFFFF;
from the outside of the class, if that makes you happy. :)