Skip to content

Commit c8e79d2

Browse files
PurSnakemoondroidcoder
authored andcommitted
Fix memory calls from FlxText
1 parent 757f2d0 commit c8e79d2

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

flixel/text/FlxText.hx

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,21 @@ class FlxText extends FlxSprite
261261
_formatAdjusted = null;
262262
_shadowOffset = FlxDestroyUtil.put(_shadowOffset);
263263
_graphicOffset = FlxDestroyUtil.put(_graphicOffset);
264+
265+
if (_borderPixels != null)
266+
{
267+
_borderPixels.dispose();
268+
_borderPixels = null;
269+
}
270+
271+
if (graphic != null)
272+
{
273+
graphic.destroy();
274+
graphic = null;
275+
}
276+
277+
_formatRanges = [];
278+
264279
super.destroy();
265280
}
266281

@@ -873,80 +888,87 @@ class FlxText extends FlxSprite
873888
{
874889
if (textField == null || !_regen)
875890
return;
876-
891+
892+
final oldGraphic:FlxGraphic = graphic;
893+
final oldBorderPixels:BitmapData = _borderPixels;
894+
877895
final oldWidth:Int = graphic != null ? graphic.width : 0;
878896
final oldHeight:Int = graphic != null ? graphic.height : VERTICAL_GUTTER;
879-
897+
880898
final newWidthFloat:Float = textField.width;
881899
final newHeightFloat:Float = _autoHeight ? textField.textHeight + VERTICAL_GUTTER : textField.height;
882-
900+
883901
var borderWidth:Float = 0;
884902
var borderHeight:Float = 0;
885-
switch(borderStyle)
903+
switch (borderStyle)
886904
{
887905
case SHADOW if (_shadowOffset.x != 1 || _shadowOffset.y != 1):
888906
borderWidth += Math.abs(_shadowOffset.x);
889907
borderHeight += Math.abs(_shadowOffset.y);
890-
908+
891909
case SHADOW: // With the default shadowOffset value
892910
borderWidth += Math.abs(borderSize);
893911
borderHeight += Math.abs(borderSize);
894-
912+
895913
case SHADOW_XY(offsetX, offsetY):
896914
borderWidth += Math.abs(offsetX);
897915
borderHeight += Math.abs(offsetY);
898-
916+
899917
case OUTLINE_FAST | OUTLINE:
900918
borderWidth += Math.abs(borderSize) * 2;
901919
borderHeight += Math.abs(borderSize) * 2;
902-
920+
903921
case NONE:
904922
}
905-
923+
906924
final newWidth:Int = Math.ceil(newWidthFloat + borderWidth);
907925
final newHeight:Int = Math.ceil(newHeightFloat + borderHeight);
908-
909-
// prevent text height from shrinking on flash if text == ""
910-
if (textField.textHeight != 0 && (oldWidth != newWidth || oldHeight != newHeight))
926+
927+
if (oldBorderPixels != null)
928+
{
929+
oldBorderPixels.dispose();
930+
_borderPixels = null;
931+
}
932+
933+
if (graphic == null || oldWidth != newWidth || oldHeight != newHeight)
911934
{
935+
if (oldGraphic != null)
936+
{
937+
oldGraphic.destroy();
938+
}
939+
912940
// Need to generate a new buffer to store the text graphic
913941
final key:String = FlxG.bitmap.getUniqueKey("text");
914942
makeGraphic(newWidth, newHeight, FlxColor.TRANSPARENT, false, key);
915943
width = Math.ceil(newWidthFloat);
916944
height = Math.ceil(newHeightFloat);
917-
945+
918946
#if FLX_TRACK_GRAPHICS
919947
graphic.trackingInfo = 'text($ID, $text)';
920948
#end
921-
922-
if (_hasBorderAlpha)
923-
_borderPixels = graphic.bitmap.clone();
924949

925-
if (_autoHeight)
926-
textField.height = newHeight;
950+
if (_autoHeight) textField.height = newHeight;
927951

928952
_flashRect.x = 0;
929953
_flashRect.y = 0;
930954
_flashRect.width = newWidth;
931955
_flashRect.height = newHeight;
932956
}
933-
else // Else just clear the old buffer before redrawing the text
957+
else
934958
{
935959
graphic.bitmap.fillRect(_flashRect, FlxColor.TRANSPARENT);
936-
if (_hasBorderAlpha)
937-
{
938-
if (_borderPixels == null)
939-
_borderPixels = new BitmapData(frameWidth, frameHeight, true);
940-
else
941-
_borderPixels.fillRect(_flashRect, FlxColor.TRANSPARENT);
942-
}
943960
}
944961

945-
if (textField != null && textField.text != null)
962+
if (_hasBorderAlpha)
946963
{
964+
_borderPixels = new BitmapData(frameWidth, frameHeight, true, FlxColor.TRANSPARENT);
965+
}
966+
967+
if (textField != null && textField.text != null && textField.text != "")
968+
{
947969
// Now that we've cleared a buffer, we need to actually render the text to it
948970
copyTextFormat(_defaultFormat, _formatAdjusted);
949-
971+
950972
_matrix.identity();
951973

952974
applyBorderStyle();
@@ -1204,7 +1226,7 @@ class FlxText extends FlxSprite
12041226

12051227
inline function applyBorderTransparency()
12061228
{
1207-
if (!_hasBorderAlpha)
1229+
if (!_hasBorderAlpha || _borderPixels == null)
12081230
return;
12091231

12101232
if (_borderColorTransform == null)

0 commit comments

Comments
 (0)