Skip to content

Improve performance of debug drawing #3168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
8 changes: 4 additions & 4 deletions flixel/FlxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.math.FlxVelocity;
import flixel.path.FlxPath;
import flixel.system.debug.FlxDebugDrawGraphic;
import flixel.tile.FlxBaseTilemap;
import flixel.util.FlxAxes;
import flixel.util.FlxColor;
Expand Down Expand Up @@ -1274,11 +1275,10 @@ class FlxObject extends FlxBasic

}

function drawDebugBoundingBoxColor(gfx:Graphics, rect:FlxRect, color:FlxColor)
function drawDebugBoundingBoxColor(gfx:FlxDebugDrawGraphic, rect:FlxRect, color:FlxColor)
{
// fill static graphics object with square shape
gfx.lineStyle(1, color, 0.75);
gfx.drawRect(rect.x + 0.5, rect.y + 0.5, rect.width - 1.0, rect.height - 1.0);
color.alphaFloat = 0.75;
gfx.drawBoundingBox(rect.x, rect.y, rect.width, rect.height, color, 1.0);
}

inline function beginDrawDebug(camera:FlxCamera):Graphics
Expand Down
20 changes: 7 additions & 13 deletions flixel/path/FlxBasePath.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import flixel.FlxBasic;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.math.FlxPoint;
import flixel.system.debug.FlxDebugDrawGraphic;
import flixel.util.FlxAxes;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
Expand Down Expand Up @@ -396,25 +397,18 @@ class FlxTypedBasePath<TTarget:FlxBasic> extends FlxBasic implements IFlxDestroy
return result;
}

inline function drawNode(gfx:Graphics, node:FlxPoint, size:Int, color:FlxColor)
inline function drawNode(gfx:FlxDebugDrawGraphic, node:FlxPoint, size:Int, color:FlxColor)
{
gfx.beginFill(color.rgb, color.alphaFloat);
gfx.lineStyle();
final offset = Math.floor(size * 0.5);
gfx.drawRect(node.x - offset, node.y - offset, size, size);
gfx.endFill();
final offset = size * 0.5;
gfx.drawRect(node.x - offset, node.y - offset, size, size, color);
}

function drawLine(gfx:Graphics, node1:FlxPoint, node2:FlxPoint)
function drawLine(gfx:FlxDebugDrawGraphic, p1:FlxPoint, p2:FlxPoint)
{
// then draw a line to the next node
final color = debugDrawData.lineColor;
final size = debugDrawData.lineSize;
gfx.lineStyle(size, color.rgb, color.alphaFloat);

final lineOffset = debugDrawData.lineSize / 2;
gfx.moveTo(node1.x + lineOffset, node1.y + lineOffset);
gfx.lineTo(node2.x + lineOffset, node2.y + lineOffset);
final half = debugDrawData.lineSize / 2;
gfx.drawLine(p1.x + half, p1.y + half, p2.x + half, p2.y + half, color, size);
}
#end
}
Expand Down
49 changes: 49 additions & 0 deletions flixel/system/debug/FlxDebugDrawGraphic.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package flixel.system.debug;

import flixel.FlxG;
import flixel.math.FlxPoint;
import flixel.util.FlxColor;
import openfl.display.Graphics;

abstract FlxDebugDrawGraphic(Graphics) from Graphics to Graphics
{
inline function useHardware(sizeSquared:Float)
{
return FlxG.renderTile && sizeSquared > 100 * 100;
}

public function drawBoundingBox(x:Float, y:Float, width:Float, height:Float, color:FlxColor, thickness = 1.0)
{
if (useHardware(width * height))
{
this.beginFill(color.rgb, color.alphaFloat);
this.drawRect(x, y, thickness, height); // left
this.drawRect(x + thickness, y, width - thickness * 2, thickness); // top
this.drawRect(x + width - thickness, y, thickness, height); // right
this.drawRect(x + thickness, y + height - thickness, width - thickness * 2, thickness); // bottom
this.endFill();
}
else
{
this.lineStyle(thickness, color.rgb, color.alphaFloat);
final half = thickness * 0.5;

this.drawRect(x + half, y + half, width - thickness, height - thickness);
}
}

public function drawLine(x1:Float, y1:Float, x2:Float, y2:Float, color:FlxColor, thickness = 1.0)
{
this.lineStyle(thickness, color.rgb, color.alphaFloat);

this.moveTo(x1, y1);
this.lineTo(x2, y2);
}

public function drawRect(x:Float, y:Float, width:Float, height:Float, color:FlxColor)
{
this.beginFill(color.rgb, color.alphaFloat);
this.drawRect(x, y, width, height);
this.endFill();
}
}
4 changes: 2 additions & 2 deletions flixel/tile/FlxTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import flixel.math.FlxMath;
import flixel.math.FlxMatrix;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.system.FlxAssets.FlxShader;
import flixel.system.FlxAssets.FlxTilemapGraphicAsset;
import flixel.system.FlxAssets;
import flixel.system.debug.FlxDebugDrawGraphic;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
import flixel.util.FlxDirectionFlags;
Expand Down