Skip to content
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

Improve performance of debug drawing #3168

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flixel/FlxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -1357,11 +1358,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
80 changes: 80 additions & 0 deletions flixel/system/debug/FlxDebugDrawGraphic.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package flixel.system.debug;

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

abstract FlxDebugDrawGraphic(Graphics) from Graphics to Graphics
{
inline function useFill()
{
// true for testing
return true;
//return #if (cpp || hl) false #else FlxG.renderTile #end;
}

public function drawBoundingBox(x:Float, y:Float, width:Float, height:Float, color:FlxColor, thickness = 1.0)
{
if (useFill())
{
this.beginFill(color.rgb, color.alphaFloat);

// outer
this.moveTo(x, y);
this.lineTo(x + width, y);
this.lineTo(x + width, y + height);
this.lineTo(x, y + height);
this.lineTo(x, y);
// inner
this.lineTo(x + thickness, y + thickness);
this.lineTo(x + thickness, y + height - thickness);
this.lineTo(x + width - thickness, y + height - thickness);
this.lineTo(x + width - thickness, y + thickness);
this.lineTo(x + thickness, y + thickness);

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)
{
if (useFill())
{
this.beginFill(color.rgb, color.alphaFloat);
final normal = FlxPoint.get(x2 - x1, y2 - y1).leftNormal();
normal.length = thickness;

this.moveTo(x1 + normal.x, y1 + normal.y);
this.lineTo(x2 + normal.x, y2 + normal.y);
this.lineTo(x2 - normal.x, y2 - normal.y);
this.lineTo(x1 - normal.x, y1 - normal.y);

this.endFill();
normal.put();
}
else
{

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)
{
// always use fill
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
Loading