Skip to content

Commit 2601ce2

Browse files
authored
use UV rect for better clarity (#3371)
* use UV rect for better clarity * fix code climate
1 parent 46dabbb commit 2601ce2

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

flixel/graphics/frames/FlxFrame.hx

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ class FlxFrame implements IFlxDestroyable
105105

106106
/**
107107
* UV coordinates for this frame.
108-
* WARNING: For optimization purposes, width and height of this rect
109-
* contain right and bottom coordinates (`x + width` and `y + height`).
110108
*/
111-
public var uv:FlxRect;
109+
public var uv:FlxUVRect;
112110

113111
public var parent:FlxGraphic;
114112

@@ -683,6 +681,7 @@ class FlxFrame implements IFlxDestroyable
683681
clone.frame = FlxRect.get().copyFrom(frame);
684682
clone.type = type;
685683
clone.name = name;
684+
clone.duration = duration;
686685
clone.cacheFrameMatrix();
687686
return clone;
688687
}
@@ -709,7 +708,7 @@ class FlxFrame implements IFlxDestroyable
709708
if (value != null)
710709
{
711710
if (uv == null)
712-
uv = FlxRect.get();
711+
uv = FlxUVRect.get();
713712

714713
uv.set(value.x / parent.width, value.y / parent.height, value.right / parent.width, value.bottom / parent.height);
715714
}
@@ -736,3 +735,50 @@ enum abstract FlxFrameAngle(Int) from Int to Int
736735
var ANGLE_NEG_90 = -90;
737736
var ANGLE_270 = -90;
738737
}
738+
739+
/**
740+
* FlxRect, but instead of `x`, `y`, `width` and `height`, it takes a `left`, `right`, `top` and
741+
* `bottom`. This is for optimization reasons, to reduce arithmetic when drawing vertices
742+
*/
743+
@:forward(put)
744+
abstract FlxUVRect(FlxRect) from FlxRect to flixel.util.FlxPool.IFlxPooled
745+
{
746+
public var left(get, set):Float;
747+
inline function get_left():Float { return this.x; }
748+
inline function set_left(value):Float { return this.x = value; }
749+
750+
/** Top */
751+
public var right(get, set):Float;
752+
inline function get_right():Float { return this.y; }
753+
inline function set_right(value):Float { return this.y = value; }
754+
755+
/** Right */
756+
public var top(get, set):Float;
757+
inline function get_top():Float { return this.width; }
758+
inline function set_top(value):Float { return this.width = value; }
759+
760+
/** Bottom */
761+
public var bottom(get, set):Float;
762+
inline function get_bottom():Float { return this.height; }
763+
inline function set_bottom(value):Float { return this.height = value; }
764+
765+
public inline function set(l, t, r, b)
766+
{
767+
this.set(l, t, r, b);
768+
}
769+
770+
public inline function copyTo(uv:FlxUVRect)
771+
{
772+
uv.set(left, top, right, bottom);
773+
}
774+
775+
public inline function copyFrom(uv:FlxUVRect)
776+
{
777+
set(uv.left, uv.top, uv.right, uv.bottom);
778+
}
779+
780+
public static function get(l = 0.0, t = 0.0, r = 0.0, b = 0.0)
781+
{
782+
return FlxRect.get(l, t, r, b);
783+
}
784+
}

flixel/graphics/tile/FlxDrawTrianglesItem.hx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,26 +305,26 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
305305
vertices[prevVerticesPos] = point.x;
306306
vertices[prevVerticesPos + 1] = point.y;
307307

308-
uvtData[prevVerticesPos] = frame.uv.x;
309-
uvtData[prevVerticesPos + 1] = frame.uv.y;
308+
uvtData[prevVerticesPos] = frame.uv.left;
309+
uvtData[prevVerticesPos + 1] = frame.uv.top;
310310

311311
point.set(frame.frame.width, 0);
312312
point.transform(matrix);
313313

314314
vertices[prevVerticesPos + 2] = point.x;
315315
vertices[prevVerticesPos + 3] = point.y;
316316

317-
uvtData[prevVerticesPos + 2] = frame.uv.width;
318-
uvtData[prevVerticesPos + 3] = frame.uv.y;
317+
uvtData[prevVerticesPos + 2] = frame.uv.right;
318+
uvtData[prevVerticesPos + 3] = frame.uv.top;
319319

320320
point.set(frame.frame.width, frame.frame.height);
321321
point.transform(matrix);
322322

323323
vertices[prevVerticesPos + 4] = point.x;
324324
vertices[prevVerticesPos + 5] = point.y;
325325

326-
uvtData[prevVerticesPos + 4] = frame.uv.width;
327-
uvtData[prevVerticesPos + 5] = frame.uv.height;
326+
uvtData[prevVerticesPos + 4] = frame.uv.right;
327+
uvtData[prevVerticesPos + 5] = frame.uv.bottom;
328328

329329
point.set(0, frame.frame.height);
330330
point.transform(matrix);
@@ -334,8 +334,8 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
334334

335335
point.put();
336336

337-
uvtData[prevVerticesPos + 6] = frame.uv.x;
338-
uvtData[prevVerticesPos + 7] = frame.uv.height;
337+
uvtData[prevVerticesPos + 6] = frame.uv.left;
338+
uvtData[prevVerticesPos + 7] = frame.uv.bottom;
339339

340340
indices[prevIndicesPos] = prevNumberOfVertices;
341341
indices[prevIndicesPos + 1] = prevNumberOfVertices + 1;

0 commit comments

Comments
 (0)