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 1 commit
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
15 changes: 11 additions & 4 deletions flixel/FlxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1292,11 +1292,11 @@ class FlxObject extends FlxBasic

var rect = getBoundingBox(camera);
var gfx:Graphics = beginDrawDebug(camera);
drawDebugBoundingBox(gfx, rect, allowCollisions, immovable);
drawDebugBoundingBox(gfx, rect, allowCollisions, immovable, camera.zoom);
endDrawDebug(camera);
}

function drawDebugBoundingBox(gfx:Graphics, rect:FlxRect, allowCollisions:Int, partial:Bool)
function drawDebugBoundingBox(gfx:Graphics, rect:FlxRect, allowCollisions:Int, partial:Bool, ?zoom:Float = 1.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to simply use a size of 0.5 like it has been, in fact, there's benefits to having this not perfectly align with the camera's pixel resolution, as it more discernible, plus adding new args may break overriding classes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... I don't think you would want that, I didn't want to do it either, but without doing a "zoom" variable, this is what it looks like:
imagen

Compared to this:
imagen

This is normal btw, this is OpenGL doing its shenanigans and sometimes its not 100% accurate to display stuff in exchange of performance.

One thing that I tried as possible to make it not break a lot, is adding a "Nulled" already set variable at the end, so people with older code and trying to port it could use this function and would not give an error

Copy link
Member

@Geokureli Geokureli Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to omit the zoom arg, without missing lines (I didn't see any at zoom = 0.3, but I see a little at zoom=0.2, which I'm okay with)

rather than drawing the rect at ±.5 from the rect, I draw a rect where the outer rect matches the given rect, and the inner rect is 1px smaller, this aligns them more with the actual pixels and more closely matches the old behavior

{
// Find the color to use
var color:Null<Int> = debugBoundingBoxColor;
Expand All @@ -1313,8 +1313,13 @@ class FlxObject extends FlxBasic
}

// 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);
gfx.beginFill(color, 0.5);
var size = Math.max(1 / zoom, 1);

gfx.drawRect(rect.x - size, rect.y - size, size, rect.height + size);
gfx.drawRect(rect.x, rect.y - size, rect.width + size, size);
gfx.drawRect(rect.x + rect.width, rect.y, size, rect.height - size);
gfx.drawRect(rect.x, rect.y + rect.height - size, rect.width + size, size);
Copy link
Member

@Geokureli Geokureli Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will these have stronger color in the overlapping corners? Rather than drawing 4 rects could we specify 8 points to make a hollow rect shape?
also 0.5 alpha should be .75

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, this won't have stronger color cos they're not overlapping, they're drawn in a specific way so they don't touch, that's why all the weird math between the positions and the sizes lol

The alpha was set 0.5 before I changed it, so it was like that. altho I could change it to .75 no problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imagen
this is how the corners look lol

}

inline function beginDrawDebug(camera:FlxCamera):Graphics
Expand All @@ -1334,6 +1339,8 @@ class FlxObject extends FlxBasic
{
if (FlxG.renderBlit)
camera.buffer.draw(FlxSpriteUtil.flashGfxSprite);
else if (FlxG.renderTile)
camera.debugLayer.graphics.endFill();
}
#end

Expand Down
2 changes: 1 addition & 1 deletion flixel/tile/FlxTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
{
rect.x = _helperPoint.x + (columnIndex % widthInTiles) * rectWidth;
rect.y = _helperPoint.y + Math.floor(columnIndex / widthInTiles) * rectHeight;
drawDebugBoundingBox(camera.debugLayer.graphics, rect, tile.allowCollisions, tile.allowCollisions != ANY);
drawDebugBoundingBox(camera.debugLayer.graphics, rect, tile.allowCollisions, tile.allowCollisions != ANY, camera.zoom);
}

columnIndex++;
Expand Down
Loading