Skip to content

Commit 7be9b70

Browse files
committed
add clone, set _weak once + doc
1 parent 0860515 commit 7be9b70

2 files changed

Lines changed: 110 additions & 33 deletions

File tree

flixel/math/FlxRect.hx

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,31 @@ class FlxRect implements IFlxPooled
1414
static var _pool:FlxPool<FlxRect> = new FlxPool(FlxRect.new.bind(0, 0, 0, 0));
1515
// With the version below, this caused weird CI issues when FLX_NO_POINT_POOL is defined
1616
// static var _pool = new FlxPool<FlxRect>(FlxRect);
17-
17+
18+
static inline function getPooled(weak = false):FlxRect
19+
{
20+
final rect = _pool.get();
21+
rect._inPool = false;
22+
rect._weak = weak;
23+
return rect;
24+
}
25+
1826
/**
1927
* Recycle or create new FlxRect.
2028
* Be sure to put() them back into the pool after you're done with them!
2129
*/
22-
public static inline function get(X:Float = 0, Y:Float = 0, Width:Float = 0, Height:Float = 0):FlxRect
30+
public static inline function get(x = 0.0, y = 0.0, width = 0.0, height = 0.0):FlxRect
2331
{
24-
var rect = _pool.get().set(X, Y, Width, Height);
25-
rect._inPool = false;
26-
return rect;
32+
return getPooled().set(x, y, width, height);
2733
}
2834

2935
/**
3036
* Recycle or create a new FlxRect which will automatically be released
3137
* to the pool when passed into a flixel function.
3238
*/
33-
public static inline function weak(X:Float = 0, Y:Float = 0, Width:Float = 0, Height:Float = 0):FlxRect
39+
public static inline function weak(x = 0.0, y = 0.0, width = 0.0, height = 0.0):FlxRect
3440
{
35-
var rect = get(X, Y, Width, Height);
36-
rect._weak = true;
37-
return rect;
41+
return getPooled(true).set(x, y, width, height);
3842
}
3943

4044
public var x:Float;
@@ -84,7 +88,6 @@ class FlxRect implements IFlxPooled
8488
if (!_inPool)
8589
{
8690
_inPool = true;
87-
_weak = false;
8891
_pool.putUnsafe(this);
8992
}
9093
}
@@ -220,46 +223,56 @@ class FlxRect implements IFlxPooled
220223
{
221224
return setAbs(x1, y1, x2 - x1, y2 - y1);
222225
}
223-
226+
224227
/**
225228
* Helper function, just copies the values from the specified rectangle.
226229
*
227-
* @param Rect Any FlxRect.
228-
* @return A reference to itself.
230+
* @param rect Any FlxRect.
231+
* @return A reference to itself.
229232
*/
230-
public inline function copyFrom(Rect:FlxRect):FlxRect
233+
public inline function copyFrom(rect:FlxRect):FlxRect
231234
{
232-
x = Rect.x;
233-
y = Rect.y;
234-
width = Rect.width;
235-
height = Rect.height;
236-
237-
Rect.putWeak();
235+
x = rect.x;
236+
y = rect.y;
237+
width = rect.width;
238+
height = rect.height;
239+
240+
rect.putWeak();
238241
return this;
239242
}
240-
243+
241244
/**
242245
* Helper function, just copies the values from this rectangle to the specified rectangle.
243246
*
244-
* @param Point Any FlxRect.
245-
* @return A reference to the altered rectangle parameter.
247+
* @param point Any FlxRect.
248+
* @return A reference to the altered rectangle parameter.
246249
*/
247-
public inline function copyTo(Rect:FlxRect):FlxRect
250+
public inline function copyTo(rect:FlxRect):FlxRect
248251
{
249-
Rect.x = x;
250-
Rect.y = y;
251-
Rect.width = width;
252-
Rect.height = height;
253-
254-
Rect.putWeak();
255-
return Rect;
252+
rect.x = x;
253+
rect.y = y;
254+
rect.width = width;
255+
rect.height = height;
256+
257+
putWeak();
258+
return rect;
256259
}
257260

261+
/**
262+
* Copies this rect's data into a new instance (from the pool)
263+
*
264+
* @return A new rectangle
265+
*/
266+
public inline function clone():FlxRect
267+
{
268+
return copyTo(getPooled());
269+
}
270+
258271
/**
259272
* Helper function, just copies the values from the specified Flash rectangle.
260273
*
261-
* @param FlashRect Any Rectangle.
262-
* @return A reference to itself.
274+
* @param flashRect Any Rectangle.
275+
* @return A reference to itself.
263276
*/
264277
public inline function copyFromFlash(FlashRect:Rectangle):FlxRect
265278
{

tests/unit/src/flixel/math/FlxRectTest.hx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class FlxRectTest extends FlxTest
1313
{
1414
rect1 = new FlxRect();
1515
rect2 = new FlxRect();
16+
17+
// allows better testing of pooling issues
18+
@:privateAccess
19+
FlxRect._pool.clear();
1620
}
1721

1822
@Test
@@ -219,4 +223,64 @@ class FlxRectTest extends FlxTest
219223
rect1.setBounds(50, 50, 100, 100).pad(10);
220224
FlxAssert.rectsNearLTRD(50 - 10, 50 - 10, 100 + 10, 100 + 10, rect1);
221225
}
226+
227+
@Test
228+
function testCopyTo()
229+
{
230+
rect1.set(10, 10, 20, 20);
231+
rect1.copyTo(rect2);
232+
FlxAssert.rectsNear(rect1, rect2);
233+
234+
// test to weak
235+
final r2 = rect1.copyTo(FlxRect.weak());
236+
FlxRect.get(0, 0, 0, 0);
237+
238+
FlxAssert.rectsNear(rect1, r2);
239+
240+
// test from weak
241+
final r2 = FlxRect.weak(10, 10, 20, 20);
242+
r2.copyTo(rect1);
243+
final r3 = FlxRect.get(0, 0, 0, 0);
244+
245+
FlxAssert.rectsNearXYWH(10, 10, 20, 20, rect1);
246+
Assert.areEqual(r2, r3);
247+
}
248+
249+
@Test
250+
function testCopyFrom()
251+
{
252+
rect1.set(10, 10, 20, 20);
253+
rect2.copyFrom(rect1);
254+
FlxAssert.rectsNear(rect1, rect2);
255+
256+
// test from weak
257+
final r2 = FlxRect.weak(10, 10, 20, 20);
258+
rect1.copyFrom(r2);
259+
final r3 = FlxRect.get(0, 0, 0, 0);
260+
261+
FlxAssert.rectsNearXYWH(10, 10, 20, 20, rect1);
262+
Assert.areEqual(r2, r3);
263+
264+
// test to weak
265+
final r2 = FlxRect.weak().copyFrom(rect1);
266+
FlxRect.get(0, 0, 0, 0);
267+
268+
FlxAssert.rectsNear(rect1, r2);
269+
}
270+
271+
@Test
272+
function testClone()
273+
{
274+
rect1.set(10, 10, 20, 20);
275+
final r2 = rect1.clone();
276+
FlxAssert.rectsNear(rect1, r2);
277+
278+
// test from weak
279+
final r1 = FlxRect.weak(10, 10, 20, 20);
280+
final r2 = r1.clone();
281+
final r3 = FlxRect.get(0, 0, 0, 0);
282+
283+
FlxAssert.rectsNearXYWH(10, 10, 20, 20, r2);
284+
Assert.areEqual(r1, r3);
285+
}
222286
}

0 commit comments

Comments
 (0)