From cc394e71d7ca48e2757ec14497ee5ca958a18fd9 Mon Sep 17 00:00:00 2001 From: MaybeMaru <97055307+MaybeMaru@users.noreply.github.com> Date: Mon, 27 Nov 2023 02:21:40 +0100 Subject: [PATCH 1/4] fix angle, scale and origin for FlxStrip --- flixel/FlxCamera.hx | 13 ++++---- flixel/FlxStrip.hx | 5 ++-- flixel/graphics/tile/FlxDrawTrianglesItem.hx | 31 ++++++++++++++++++-- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/flixel/FlxCamera.hx b/flixel/FlxCamera.hx index acf179f8a4..1779e77bf5 100644 --- a/flixel/FlxCamera.hx +++ b/flixel/FlxCamera.hx @@ -851,7 +851,8 @@ class FlxCamera extends FlxBasic } public function drawTriangles(graphic:FlxGraphic, vertices:DrawData, indices:DrawData, uvtData:DrawData, ?colors:DrawData, - ?position:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false, ?transform:ColorTransform, ?shader:FlxShader):Void + ?position:FlxPoint, angle:Float = 0, ?scale:FlxPoint, ?origin:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false, + ?transform:ColorTransform, ?shader:FlxShader):Void { if (FlxG.renderBlit) { @@ -932,13 +933,13 @@ class FlxCamera extends FlxBasic var isColored:Bool = (colors != null && colors.length != 0); #if !flash - var hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets()); + final 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); + final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend, hasColorOffsets, shader); + drawItem.addTriangles(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds, transform); #else - var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend); - drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds); + final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend); + drawItem.addTriangles(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds); #end } } diff --git a/flixel/FlxStrip.hx b/flixel/FlxStrip.hx index 8647e1642a..6177cfe637 100644 --- a/flixel/FlxStrip.hx +++ b/flixel/FlxStrip.hx @@ -57,9 +57,10 @@ 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.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing, colorTransform, + shader); #else - camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, blend, repeat, antialiasing); + camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing); #end } } diff --git a/flixel/graphics/tile/FlxDrawTrianglesItem.hx b/flixel/graphics/tile/FlxDrawTrianglesItem.hx index 704d281ac5..6b14fc752a 100644 --- a/flixel/graphics/tile/FlxDrawTrianglesItem.hx +++ b/flixel/graphics/tile/FlxDrawTrianglesItem.hx @@ -1,5 +1,6 @@ package flixel.graphics.tile; +import flixel.math.FlxAngle; import flixel.FlxCamera; import flixel.graphics.frames.FlxFrame; import flixel.graphics.tile.FlxDrawBaseItem.FlxDrawItemType; @@ -21,6 +22,8 @@ typedef DrawData = #if (flash || openfl >= "4.0.0") openfl.Vector #else Ar class FlxDrawTrianglesItem extends FlxDrawBaseItem { static var point:FlxPoint = FlxPoint.get(); + static var size:FlxPoint = FlxPoint.get(); + static var origin:FlxPoint = FlxPoint.get(); static var rect:FlxRect = FlxRect.get(); #if !flash @@ -137,17 +140,24 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem } public function addTriangles(vertices:DrawData, indices:DrawData, uvtData:DrawData, ?colors:DrawData, ?position:FlxPoint, + angle:Float = 0, ?scale:FlxPoint, ?originPoint:FlxPoint, ?cameraBounds:FlxRect #if !flash , ?transform:ColorTransform #end):Void { if (position == null) position = point.set(); + if (size == null) + scale = size.set(1, 1); + + if (originPoint == null) + originPoint = origin.set(); + if (cameraBounds == null) cameraBounds = rect.set(0, 0, FlxG.width, FlxG.height); var verticesLength:Int = vertices.length; var prevVerticesLength:Int = this.vertices.length; - var numberOfVertices:Int = Std.int(verticesLength / 2); + var numberOfVertices:Int = Std.int(verticesLength * 0.5); var prevIndicesLength:Int = this.indices.length; var prevUVTDataLength:Int = this.uvtData.length; var prevColorsLength:Int = this.colors.length; @@ -159,8 +169,23 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem while (i < verticesLength) { - tempX = position.x + vertices[i]; - tempY = position.y + vertices[i + 1]; + var vertX:Float = (vertices[i] * scale.x) - originPoint.x; + var vertY:Float = (vertices[i + 1] * scale.x) - originPoint.y; + + if (angle != 0) + { + final _cos:Float = Math.cos(angle * FlxAngle.TO_RAD); + final _sin:Float = Math.sin(angle * FlxAngle.TO_RAD); + + final _vx:Float = vertX; + final _vy:Float = vertY; + + vertX = (_vx * _cos) + (_vy * -_sin); + vertY = (_vx * _sin) + (_vy * _cos); + } + + tempX = position.x + vertX; + tempY = position.y + vertY; this.vertices[currentVertexPosition++] = tempX; this.vertices[currentVertexPosition++] = tempY; From 3f3e3109a3aa1178f017099d6e6813e386c4a536 Mon Sep 17 00:00:00 2001 From: MaybeMaru <97055307+MaybeMaru@users.noreply.github.com> Date: Sat, 16 Dec 2023 16:55:20 +0100 Subject: [PATCH 2/4] optimization --- flixel/graphics/tile/FlxDrawTrianglesItem.hx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/flixel/graphics/tile/FlxDrawTrianglesItem.hx b/flixel/graphics/tile/FlxDrawTrianglesItem.hx index 6b14fc752a..4cdcd7da77 100644 --- a/flixel/graphics/tile/FlxDrawTrianglesItem.hx +++ b/flixel/graphics/tile/FlxDrawTrianglesItem.hx @@ -7,6 +7,7 @@ import flixel.graphics.tile.FlxDrawBaseItem.FlxDrawItemType; import flixel.math.FlxMatrix; import flixel.math.FlxPoint; import flixel.math.FlxRect; +import flixel.math.FlxMath; import flixel.system.FlxAssets.FlxShader; import flixel.util.FlxColor; import openfl.display.Graphics; @@ -167,16 +168,21 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem var i:Int = 0; var currentVertexPosition:Int = prevVerticesLength; + var _cos:Float = 1.0; + var _sin:Float = 0.0; + if (angle != 0) + { + _cos = FlxMath.fastCos(angle * FlxAngle.TO_RAD); + _sin = FlxMath.fastSin(angle * FlxAngle.TO_RAD); + } + while (i < verticesLength) { var vertX:Float = (vertices[i] * scale.x) - originPoint.x; - var vertY:Float = (vertices[i + 1] * scale.x) - originPoint.y; + var vertY:Float = (vertices[i + 1] * scale.y) - originPoint.y; if (angle != 0) { - final _cos:Float = Math.cos(angle * FlxAngle.TO_RAD); - final _sin:Float = Math.sin(angle * FlxAngle.TO_RAD); - final _vx:Float = vertX; final _vy:Float = vertY; From 4b54b058efbc5e65c6758eef808ad3986de445d8 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 22 Dec 2023 18:04:25 -0600 Subject: [PATCH 3/4] fix CI --- flixel/FlxCamera.hx | 18 +++++++++- flixel/FlxStrip.hx | 2 +- flixel/graphics/tile/FlxDrawTrianglesItem.hx | 35 ++++++++++++++------ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/flixel/FlxCamera.hx b/flixel/FlxCamera.hx index 1779e77bf5..fea5965ffb 100644 --- a/flixel/FlxCamera.hx +++ b/flixel/FlxCamera.hx @@ -850,7 +850,23 @@ class FlxCamera extends FlxBasic } } + // backwards compatibility for FlxClothSprite + #if flash public function drawTriangles(graphic:FlxGraphic, vertices:DrawData, indices:DrawData, uvtData:DrawData, ?colors:DrawData, + ?position:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false, ?transform:ColorTransform, ?shader:FlxShader):Void + { + drawTrianglesAdvanced(graphic, vertices, indices, uvtData, colors, position, 0, null, null, blend, repeat, smoothing, transform, shader); + } + #else + public function drawTriangles(graphic:FlxGraphic, vertices:DrawData, indices:DrawData, uvtData:DrawData, ?colors:DrawData, + ?position:FlxPoint, angle:Float = 0, ?scale:FlxPoint, ?origin:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false, + ?transform:ColorTransform, ?shader:FlxShader):Void + { + drawTrianglesAdvanced(graphic, vertices, indices, uvtData, colors, position, angle, scale, origin, blend, repeat, smoothing, transform, shader); + } + #end + + public function drawTrianglesAdvanced(graphic:FlxGraphic, vertices:DrawData, indices:DrawData, uvtData:DrawData, ?colors:DrawData, ?position:FlxPoint, angle:Float = 0, ?scale:FlxPoint, ?origin:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false, ?transform:ColorTransform, ?shader:FlxShader):Void { @@ -939,7 +955,7 @@ class FlxCamera extends FlxBasic drawItem.addTriangles(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds, transform); #else final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend); - drawItem.addTriangles(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds); + drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds); #end } } diff --git a/flixel/FlxStrip.hx b/flixel/FlxStrip.hx index 6177cfe637..e43ae9b37a 100644 --- a/flixel/FlxStrip.hx +++ b/flixel/FlxStrip.hx @@ -60,7 +60,7 @@ class FlxStrip extends FlxSprite camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing, colorTransform, shader); #else - camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing); + camera.drawTrianglesAdvanced(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing, null); #end } } diff --git a/flixel/graphics/tile/FlxDrawTrianglesItem.hx b/flixel/graphics/tile/FlxDrawTrianglesItem.hx index 4cdcd7da77..756b4de3a9 100644 --- a/flixel/graphics/tile/FlxDrawTrianglesItem.hx +++ b/flixel/graphics/tile/FlxDrawTrianglesItem.hx @@ -140,9 +140,22 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem #end } + #if flash public function addTriangles(vertices:DrawData, indices:DrawData, uvtData:DrawData, ?colors:DrawData, ?position:FlxPoint, - angle:Float = 0, ?scale:FlxPoint, ?originPoint:FlxPoint, - ?cameraBounds:FlxRect #if !flash , ?transform:ColorTransform #end):Void + ?cameraBounds:FlxRect):Void + { + addTrianglesAdvanced(vertices, indices, uvtData, colors, position, 0, null, null, cameraBounds, null); + } + #else + public function addTriangles(vertices:DrawData, indices:DrawData, uvtData:DrawData, ?colors:DrawData, ?position:FlxPoint, + angle:Float = 0, ?scale:FlxPoint, ?originPoint:FlxPoint, ?cameraBounds:FlxRect, ?transform:ColorTransform):Void + { + addTrianglesAdvanced(vertices, indices, uvtData, colors, position, angle, scale, originPoint, cameraBounds, transform); + } + #end + + public function addTrianglesAdvanced(vertices:DrawData, indices:DrawData, uvtData:DrawData, ?colors:DrawData, ?position:FlxPoint, + angle:Float = 0, ?scale:FlxPoint, ?originPoint:FlxPoint, ?cameraBounds:FlxRect, ?transform:ColorTransform):Void { if (position == null) position = point.set(); @@ -158,7 +171,7 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem var verticesLength:Int = vertices.length; var prevVerticesLength:Int = this.vertices.length; - var numberOfVertices:Int = Std.int(verticesLength * 0.5); + var numberOfVertices:Int = verticesLength >> 1; var prevIndicesLength:Int = this.indices.length; var prevUVTDataLength:Int = this.uvtData.length; var prevColorsLength:Int = this.colors.length; @@ -168,12 +181,12 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem var i:Int = 0; var currentVertexPosition:Int = prevVerticesLength; - var _cos:Float = 1.0; - var _sin:Float = 0.0; + var cos:Float = 1.0; + var sin:Float = 0.0; if (angle != 0) { - _cos = FlxMath.fastCos(angle * FlxAngle.TO_RAD); - _sin = FlxMath.fastSin(angle * FlxAngle.TO_RAD); + cos = FlxMath.fastCos(angle * FlxAngle.TO_RAD); + sin = FlxMath.fastSin(angle * FlxAngle.TO_RAD); } while (i < verticesLength) @@ -183,11 +196,11 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem if (angle != 0) { - final _vx:Float = vertX; - final _vy:Float = vertY; + final vx:Float = vertX; + final vy:Float = vertY; - vertX = (_vx * _cos) + (_vy * -_sin); - vertY = (_vx * _sin) + (_vy * _cos); + vertX = (vx * cos) + (vy * -sin); + vertY = (vx * sin) + (vy * cos); } tempX = position.x + vertX; From 8392666f7d7f1f01a28c77b85492ebcc48cc9f0e Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 22 Dec 2023 18:09:43 -0600 Subject: [PATCH 4/4] use advanced on flash --- flixel/FlxCamera.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/FlxCamera.hx b/flixel/FlxCamera.hx index fea5965ffb..846eaf0001 100644 --- a/flixel/FlxCamera.hx +++ b/flixel/FlxCamera.hx @@ -955,7 +955,7 @@ class FlxCamera extends FlxBasic drawItem.addTriangles(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds, transform); #else final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend); - drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds); + drawItem.addTrianglesAdvanced(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds); #end } }