Skip to content

Commit

Permalink
perfomance improvements for flash target
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Hohlov authored and Alexander Hohlov committed Apr 28, 2012
1 parent 26b310d commit c4230ca
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 16 deletions.
46 changes: 36 additions & 10 deletions src/pxBitmapFont/PxBitmapFont.hx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,32 @@ class PxBitmapFont
return resultBitmapData;
}

#if (flash || js)
public function getPreparedGlyphs(pScale:Float, pColor:Int):Array<BitmapData>
{
var result:Array<BitmapData> = [];

_matrix.identity();
_matrix.scale(pScale, pScale);
_colorTransform.color = pColor;

var glyph:BitmapData;
var preparedGlyph:BitmapData;
for (i in 0...(_glyphs.length))
{
glyph = _glyphs[i];
if (glyph != null)
{
preparedGlyph = new BitmapData(Math.floor(glyph.width * pScale), Math.floor(glyph.height * pScale), true, 0x00000000);
preparedGlyph.draw(glyph, _matrix, _colorTransform);
result[i] = preparedGlyph;
}
}

return result;
}
#end

/**
* Clears all resources used by the font.
*/
Expand Down Expand Up @@ -274,11 +300,11 @@ class PxBitmapFont
* @param pOffsetY Y position of thext output.
*/
#if flash
public function render(pBitmapData:BitmapData, pText:String, pColor:UInt, pOffsetX:Int, pOffsetY:Int, pScale:Float):Void
public function render(pBitmapData:BitmapData, pFontData:Array<BitmapData>, pText:String, pColor:UInt, pOffsetX:Int, pOffsetY:Int, pScale:Float, ?pAngle:Float = 0):Void
#elseif js
public function render(pBitmapData:BitmapData, pText:String, pColor:Int, pOffsetX:Int, pOffsetY:Int, pScale:Float):Void
public function render(pBitmapData:BitmapData, pFontData:Array<BitmapData>, pText:String, pColor:Int, pOffsetX:Int, pOffsetY:Int, pScale:Float, ?pAngle:Float = 0):Void
#else
public function render(drawData:Array<Float>, pText:String, pColor:Int, pAlpha:Float, pOffsetX:Int, pOffsetY:Int, pScale:Float):Void
public function render(drawData:Array<Float>, pText:String, pColor:Int, pAlpha:Float, pOffsetX:Int, pOffsetY:Int, pScale:Float, ?pAngle:Float = 0):Void
#end
{
_point.x = pOffsetX;
Expand All @@ -293,22 +319,22 @@ class PxBitmapFont
for (i in 0...(pText.length))
{
var charCode:Int = pText.charCodeAt(i);
#if (flash || js)
glyph = pFontData[charCode];
#else
glyph = _glyphs[charCode];
#end
if (glyph != null)
{
#if (flash || js)
_matrix.identity();
_matrix.scale(pScale, pScale);
_matrix.translate(_point.x, _point.y);
_colorTransform.color = pColor;
pBitmapData.draw(glyph, _matrix, _colorTransform);
_point.x += glyph.width * pScale;
pBitmapData.copyPixels(glyph, glyph.rect, _point, null, null, true);
_point.x += glyph.width;
#else
glyphWidth = _glyphWidthData[charCode];
var red:Float = (pColor >> 16 & 0xFF) / 255;
var green:Float = (pColor >> 8 & 0xFF) / 255;
var blue:Float = (pColor & 0xFF) / 255;
//x, y, tile_ID, scale, rotation, red, green, blue, alpha
// x, y, tile_ID, scale, rotation, red, green, blue, alpha
drawData.push(_point.x); // x
drawData.push(_point.y); // y
drawData.push(glyph); // tile_ID
Expand Down
76 changes: 70 additions & 6 deletions src/pxBitmapFont/PxTextFieldComponent.hx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class PxTextFieldComponent extends Sprite
#if (flash || js)
public var bitmapData:BitmapData;
private var _bitmap:Bitmap;

private var _preparedTextGlyphs:Array<BitmapData>;
private var _preparedShadowGlyphs:Array<BitmapData>;
private var _preparedOutlineGlyphs:Array<BitmapData>;
#else
private var _drawData:Array<Float>;
#end
Expand Down Expand Up @@ -78,6 +82,8 @@ class PxTextFieldComponent extends Sprite
}

#if (flash || js)
updateGlyphs(true, _shadow, _outline);

bitmapData = new BitmapData(1, 1, true);
_bitmap = new Bitmap(bitmapData);
this.addChild(_bitmap);
Expand All @@ -97,6 +103,10 @@ class PxTextFieldComponent extends Sprite
_bitmap = null;
bitmapData.dispose();
bitmapData = null;

clearPreparedGlyphs(_preparedTextGlyphs);
clearPreparedGlyphs(_preparedShadowGlyphs);
clearPreparedGlyphs(_preparedOutlineGlyphs);
#end
}

Expand Down Expand Up @@ -128,7 +138,11 @@ class PxTextFieldComponent extends Sprite

var calcFieldWidth:Int = _fieldWidth;
var rows:Array<String> = [];
#if (flash || js)
var fontHeight:Int = Math.floor(_font.getFontHeight() * _fontScale);
#else
var fontHeight:Int = _font.getFontHeight();
#end
var alignment:Int = _alignment;

// cut text into pices
Expand Down Expand Up @@ -169,7 +183,7 @@ class PxTextFieldComponent extends Sprite
if (!changed)
{
var subText:String = txt.substr(0, txt.length - 1);
calcFieldWidth = Math.floor(Math.max(calcFieldWidth, _font.getTextWidth(subText)));
calcFieldWidth = Math.floor(Math.max(calcFieldWidth, _font.getTextWidth(subText) * _fontScale));
rows.push(subText);
}
lineComplete = true;
Expand All @@ -179,7 +193,11 @@ class PxTextFieldComponent extends Sprite
}

var finalWidth:Int = calcFieldWidth + _padding * 2 + (_outline ? 2 : 0);
var finalHeight:Int = Math.floor(_padding * 2 + Math.max(1, (rows.length * fontHeight + (_shadow ? 1 : 0)) + (_outline ? 2 : 0)) * _fontScale) + _lineSpacing * (rows.length - 1);
#if (flash || js)
var finalHeight:Int = Math.floor(_padding * 2 + Math.max(1, (rows.length * fontHeight + (_shadow ? 1 : 0)) + (_outline ? 2 : 0))) + _lineSpacing * (rows.length - 1);
#else
var finalHeight:Int = Math.floor(_padding * 2 + Math.max(1, (rows.length * fontHeight * _fontScale + (_shadow ? 1 : 0)) + (_outline ? 2 : 0))) + _lineSpacing * (rows.length - 1);
#end

#if (flash || js)
if (bitmapData != null)
Expand Down Expand Up @@ -233,7 +251,7 @@ class PxTextFieldComponent extends Sprite
for (px in 0...(2 + 1))
{
#if (flash || js)
_font.render(bitmapData, t, _outlineColor, px + ox + _padding, py + row * (Math.floor(fontHeight * _fontScale) + _lineSpacing) + _padding, _fontScale);
_font.render(bitmapData, _preparedOutlineGlyphs, t, _outlineColor, px + ox + _padding, py + row * (fontHeight + _lineSpacing) + _padding, _fontScale);
#else
_font.render(_drawData, t, _outlineColor, _alpha, px + ox + _padding, py + row * (Math.floor(fontHeight * _fontScale) + _lineSpacing) + _padding, _fontScale);
#end
Expand All @@ -245,13 +263,13 @@ class PxTextFieldComponent extends Sprite
if (_shadow)
{
#if (flash || js)
_font.render(bitmapData, t, _shadowColor, 1 + ox + _padding, 1 + oy + row * (Math.floor(fontHeight * _fontScale) + _lineSpacing) + _padding, _fontScale);
_font.render(bitmapData, _preparedShadowGlyphs, t, _shadowColor, 1 + ox + _padding, 1 + oy + row * (fontHeight + _lineSpacing) + _padding, _fontScale);
#else
_font.render(_drawData, t, _shadowColor, _alpha, 1 + ox + _padding, 1 + oy + row * (Math.floor(fontHeight * _fontScale) + _lineSpacing) + _padding, _fontScale);
#end
}
#if (flash || js)
_font.render(bitmapData, t, _color, ox + _padding, oy + row * (Math.floor(fontHeight * _fontScale) + _lineSpacing) + _padding, _fontScale);
_font.render(bitmapData, _preparedTextGlyphs, t, _color, ox + _padding, oy + row * (fontHeight + _lineSpacing) + _padding, _fontScale);
#else
_font.render(_drawData, t, _color, _alpha, ox + _padding, oy + row * (Math.floor(fontHeight * _fontScale) + _lineSpacing) + _padding, _fontScale);
#end
Expand Down Expand Up @@ -322,6 +340,7 @@ class PxTextFieldComponent extends Sprite
if (_shadow)
{
_outline = false;
updateGlyphs(false, _shadow, false);
}

_pendingTextChange = true;
Expand All @@ -338,6 +357,7 @@ class PxTextFieldComponent extends Sprite
public function set_shadowColor(value:Int):Int
{
_shadowColor = value;
updateGlyphs(false, _shadow, false);
_pendingTextChange = true;
update();
return value;
Expand All @@ -360,12 +380,12 @@ class PxTextFieldComponent extends Sprite
/**
* Sets the color of the text.
*/

public var color(null, set_color):Int;

public function set_color(value:Int):Int
{
_color = value;
updateGlyphs(true, false, false);
_pendingTextChange = true;
update();
return value;
Expand Down Expand Up @@ -427,6 +447,7 @@ class PxTextFieldComponent extends Sprite
if (_outline)
{
_shadow = false;
updateGlyphs(false, false, true);
}
_pendingTextChange = true;
update();
Expand All @@ -442,6 +463,7 @@ class PxTextFieldComponent extends Sprite
public function set_outlineColor(value:Int):Int
{
_outlineColor = value;
updateGlyphs(false, false, _outline);
_pendingTextChange = true;
update();
return value;
Expand All @@ -456,6 +478,7 @@ class PxTextFieldComponent extends Sprite
public function set_font(pFont:PxBitmapFont):PxBitmapFont
{
_font = pFont;
updateGlyphs(true, _shadow, _outline);
_pendingTextChange = true;
update();
return pFont;
Expand Down Expand Up @@ -503,9 +526,50 @@ class PxTextFieldComponent extends Sprite
public function set_fontScale(pScale:Float):Float
{
_fontScale = Math.abs(pScale);
updateGlyphs(true, _shadow, _outline);
_pendingTextChange = true;
update();
return pScale;
}

private function updateGlyphs(?textGlyphs:Bool = false, ?shadowGlyphs:Bool = false, ?outlineGlyphs:Bool = false):Void
{
#if (flash || js)
if (textGlyphs)
{
clearPreparedGlyphs(_preparedTextGlyphs);
_preparedTextGlyphs = _font.getPreparedGlyphs(_fontScale, _color);
}

if (shadowGlyphs)
{
clearPreparedGlyphs(_preparedShadowGlyphs);
_preparedShadowGlyphs = _font.getPreparedGlyphs(_fontScale, _shadowColor);
}

if (outlineGlyphs)
{
clearPreparedGlyphs(_preparedOutlineGlyphs);
_preparedOutlineGlyphs = _font.getPreparedGlyphs(_fontScale, _outlineColor);
}
#end
}

#if (flash || js)
private function clearPreparedGlyphs(pGlyphs:Array<BitmapData>):Void
{
if (pGlyphs != null)
{
for (bmd in pGlyphs)
{
if (bmd != null)
{
bmd.dispose();
}
}
pGlyphs = null;
}
}
#end

}

0 comments on commit c4230ca

Please sign in to comment.