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

Flx strip debug draw #2909

Open
wants to merge 11 commits into
base: release6
Choose a base branch
from
Open
99 changes: 97 additions & 2 deletions flixel/FlxCamera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import openfl.geom.Rectangle;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxFrame;
import flixel.graphics.tile.FlxDrawBaseItem;
import flixel.graphics.tile.FlxDrawTriangleData;
import flixel.graphics.tile.FlxDrawTrianglesItem;
import flixel.math.FlxMath;
import flixel.math.FlxMatrix;
Expand Down Expand Up @@ -850,6 +851,100 @@ class FlxCamera extends FlxBasic
}
}

public function drawTriangleData(graphic:FlxGraphic, triangles:FlxDrawTriangleData, ?position:FlxPoint, ?blend:BlendMode, repeat = false,
smoothing = false, ?transform:ColorTransform, ?shader:FlxShader):Void
{
if (FlxG.renderBlit)
{
if (position == null)
position = renderPoint.set();

_bounds.set(0, 0, width, height);

var verticesLength:Int = triangles.vertices.length;
var currentVertexPosition:Int = 0;

var tempX:Float, tempY:Float;
var i:Int = 0;
var bounds = renderRect.set();
drawVertices.splice(0, drawVertices.length);

while (i < verticesLength)
{
tempX = position.x + triangles.vertices[i];
tempY = position.y + triangles.vertices[i + 1];

drawVertices[currentVertexPosition++] = tempX;
drawVertices[currentVertexPosition++] = tempY;

if (i == 0)
{
bounds.set(tempX, tempY, 0, 0);
}
else
{
FlxDrawTrianglesItem.inflateBounds(bounds, tempX, tempY);
}

i += 2;
}

position.putWeak();

if (!_bounds.overlaps(bounds))
{
drawVertices.splice(drawVertices.length - verticesLength, verticesLength);
}
else
{
trianglesSprite.graphics.clear();
trianglesSprite.graphics.beginBitmapFill(graphic.bitmap, null, repeat, smoothing);
trianglesSprite.graphics.drawTriangles(drawVertices, triangles.indices, triangles.uvs);
trianglesSprite.graphics.endFill();

// TODO: check this block of code for cases, when zoom < 1 (or initial zoom?)...
if (_useBlitMatrix)
_helperMatrix.copyFrom(_blitMatrix);
else
{
_helperMatrix.identity();
_helperMatrix.translate(-viewMarginLeft, -viewMarginTop);
}

buffer.draw(trianglesSprite, _helperMatrix);
#if FLX_DEBUG
if (FlxG.debugger.drawDebug)
{
var gfx:Graphics = FlxSpriteUtil.flashGfx;
gfx.clear();
gfx.lineStyle(1, FlxColor.BLUE, 0.5);
gfx.drawTriangles(drawVertices, triangles.indices);
buffer.draw(FlxSpriteUtil.flashGfxSprite, _helperMatrix);
}
#end
// End of TODO...
}

bounds.put();
}
else
{
_bounds.set(0, 0, width, height);
var isColored:Bool = (triangles.colors != null && triangles.colors.length != 0);

#if !flash
var hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets());
isColored = isColored || (transform != null && transform.hasRGBMultipliers());
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend, hasColorOffsets, shader);
drawItem.addTriangleData(triangles, position, _bounds, transform);
#else
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend);
drawItem.addTriangleData(triangles, position, _bounds);
#end
}
}

// @:deprecated("FlxCamera's drawTriangles is deprecated use drawTriangleData, instead") // TODO
public function drawTriangles(graphic:FlxGraphic, vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>,
?position:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false, ?transform:ColorTransform, ?shader:FlxShader):Void
{
Expand Down Expand Up @@ -935,10 +1030,10 @@ class FlxCamera extends FlxBasic
var hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets());
isColored = isColored || (transform != null && transform.hasRGBMultipliers());
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend, hasColorOffsets, shader);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds, transform);
drawItem.addTrianglesHelper(vertices, indices, uvtData, colors, position, _bounds, transform);
#else
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds);
drawItem.addTrianglesHelper(vertices, indices, uvtData, colors, position, _bounds);
#end
}
}
Expand Down
14 changes: 13 additions & 1 deletion flixel/FlxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,18 @@ class FlxObject extends FlxBasic
* @return Whether the object is on screen or not.
*/
public function isOnScreen(?camera:FlxCamera):Bool
{
return isColliderOnScreen(camera);
}

/**
* Check and see if this object's hitbox is currently on screen.
*
* @param camera Specify which game camera you want.
* If `null`, it will just grab the first global camera.
* @return Whether the object is on screen or not.
*/
public function isColliderOnScreen(?camera:FlxCamera):Bool
{
if (camera == null)
camera = FlxG.camera;
Expand Down Expand Up @@ -1270,7 +1282,7 @@ class FlxObject extends FlxBasic
*/
public function drawDebugOnCamera(camera:FlxCamera):Void
{
if (!camera.visible || !camera.exists || !isOnScreen(camera))
if (!camera.visible || !camera.exists || !isColliderOnScreen(camera))
return;

var rect = getBoundingBox(camera);
Expand Down
128 changes: 114 additions & 14 deletions flixel/FlxStrip.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package flixel;

import flixel.graphics.tile.FlxDrawTrianglesItem.DrawData;
import flixel.graphics.tile.FlxDrawTriangleData;
import flixel.graphics.tile.FlxDrawTrianglesItem;
import flixel.math.FlxPoint;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
import openfl.Vector;
import openfl.display.Graphics;
import openfl.display.GraphicsPathCommand;

/**
* A very basic rendering component which uses `drawTriangles()`.
Expand All @@ -15,39 +22,78 @@ import flixel.graphics.tile.FlxDrawTrianglesItem.DrawData;
*/
class FlxStrip extends FlxSprite
{
/**
* List of triangles
*/
public var triangles:FlxDrawTriangleData;

/**
* A `Vector` of floats where each pair of numbers is treated as a coordinate location (an x, y pair).
*/
public var vertices:DrawData<Float> = new DrawData<Float>();
// @:deprecated("FlxStrip's vertices is deprecated, use triangles.vertices, instead") // TODO
public var vertices(get, set):DrawData<Float>;
inline function get_vertices() return triangles.vertices;
inline function set_vertices(value:DrawData<Float>) return triangles.vertices = value;

/**
* A `Vector` of integers or indexes, where every three indexes define a triangle.
*/
public var indices:DrawData<Int> = new DrawData<Int>();
// @:deprecated("FlxStrip's indices is deprecated, use triangles.indices, instead") // TODO
public var indices(get, set):DrawData<Int>;
inline function get_indices() return triangles.indices;
inline function set_indices(value:DrawData<Int>) return triangles.indices = value;

/**
* A `Vector` of normalized coordinates used to apply texture mapping.
*/
public var uvtData:DrawData<Float> = new DrawData<Float>();
// @:deprecated("FlxStrip's uvtData is deprecated, use triangles.uvs, instead") // TODO
public var uvtData(get, set):DrawData<Float>;
inline function get_uvtData() return triangles.uvs;
inline function set_uvtData(value:DrawData<Float>) return triangles.uvs = value;

public var colors:DrawData<Int> = new DrawData<Int>();
// @:deprecated("FlxStrip's color is deprecated, use triangles.colors, instead") // TODO
public var colors(get, set):DrawData<Int>;
inline function get_colors() return triangles.colors;
inline function set_colors(value:DrawData<Int>) return triangles.colors = value;

public var repeat:Bool = false;
// @:deprecated("FlxStrip's repeat is deprecated, use triangles.repeat, instead") // TODO
public var repeat(get, set):Bool;
inline function get_repeat() return triangles.repeat;
inline function set_repeat(value:Bool) return triangles.repeat = value;

#if FLX_DEBUG
/**
* Overriding this will force a specific color to be used when debug drawing triangles
*/
public var debugTrianglesColor:FlxColor = FlxColor.BLUE;

/**
* If true, draws the triangles of this strip, unless ignoreDrawDebug is true
*/
public var drawDebugTriangles:Bool = false;

/**
* If true, draws the collision bounding box of this strip, unless ignoreDrawDebug is true
*/
public var drawDebugCollider:Bool = true;
#end

public function new(x = 0.0, y = 0.0, ?graphic)
{
triangles = new FlxDrawTriangleData();
super(x, y, graphic);
}

override public function destroy():Void
{
vertices = null;
indices = null;
uvtData = null;
colors = null;

triangles = FlxDestroyUtil.destroy(triangles);
super.destroy();
}

// TODO: check this for cases when zoom is less than initial zoom...
override public function draw():Void
{
if (alpha == 0 || graphic == null || vertices == null)
if (alpha == 0 || graphic == null || triangles.vertices == null)
return;

for (camera in cameras)
Expand All @@ -57,10 +103,64 @@ class FlxStrip extends FlxSprite

getScreenPosition(_point, camera).subtractPoint(offset);
#if !flash
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, blend, repeat, antialiasing, colorTransform, shader);
camera.drawTriangleData(graphic, triangles, _point, blend, repeat, antialiasing, colorTransform, shader);
#else
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, blend, repeat, antialiasing);
camera.drawTriangleData(graphic, triangles, _point, blend, repeat, antialiasing);
#end
}

#if FLX_DEBUG
if (FlxG.debugger.drawDebug)
drawDebug();
#end
}

#if FLX_DEBUG
override function drawDebugOnCamera(camera:FlxCamera)
{
final boundsOnScreen = isColliderOnScreen(camera);
final graphicOnScreen = isOnScreen(camera);
if (!camera.visible || !camera.exists || !(boundsOnScreen || graphicOnScreen))
return;

final gfx:Graphics = beginDrawDebug(camera);
if (boundsOnScreen && drawDebugCollider)
{
final rect = getBoundingBox(camera);
drawDebugBoundingBox(gfx, rect, allowCollisions, immovable);
}

if (graphicOnScreen && drawDebugTriangles)
{
final pos = getScreenPosition(camera);
drawDebugTrianglesToBuffer(gfx, pos);
}

endDrawDebug(camera);
}

function drawDebugTrianglesToBuffer(gfx:Graphics, screenPos:FlxPoint)
{
gfx.lineStyle(1, debugTrianglesColor, 0.5);
// draw a path for each triangle
final numTriangles = Std.int(triangles.indices.length / 3);
final commands = new Vector<Int>();
final data = new Vector<Float>();
for (i in 0...numTriangles)
{
// add triangle vertices 0, 1, 2, 0
commands.push(MOVE_TO);
commands.push(LINE_TO);
commands.push(LINE_TO);
commands.push(LINE_TO);
for (j in 0...4)
{
final index = triangles.indices[i * 3 + (j % 3)];
data.push(screenPos.x + triangles.vertices[index * 2 + 0]);
data.push(screenPos.y + triangles.vertices[index * 2 + 1]);
}
}
gfx.drawPath(commands, data);
}
#end
}
47 changes: 47 additions & 0 deletions flixel/graphics/tile/FlxDrawTriangleData.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package flixel.graphics.tile;

import flixel.graphics.tile.FlxDrawTrianglesItem;
import flixel.util.FlxDestroyUtil;

class FlxDrawTriangleData implements IFlxDestroyable
{
/**
* A `Vector` of floats where each pair of numbers is treated as a coordinate location (an x, y pair).
*/
public var vertices = new DrawData<Float>();

/**
* A `Vector` of integers or indexes, where every three indexes define a triangle.
*/
public var indices = new DrawData<Int>();

/**
* A `Vector` of normalized coordinates used to apply texture mapping.
*/
public var uvs = new DrawData<Float>();

public var colors = new DrawData<Int>();

/**
* Whether the image is tiled
*/
public var repeat:Bool = false;

public function new () {}

public function destroy()
{
vertices = null;
indices = null;
uvs = null;
colors = null;
}

public function clear()
{
vertices = new DrawData<Float>();
uvs = new DrawData<Float>();
indices = new DrawData<Int>();
colors = new DrawData<Int>();
}
}
Loading
Loading