Skip to content

Commit 7e2ab27

Browse files
committed
Add fastBoundingRect() method to Sprite
1 parent ff78bd4 commit 7e2ab27

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

include/scratchcpp/sprite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class LIBSCRATCHCPP_EXPORT Sprite
7070
void setLayerOrder(int newLayerOrder) override;
7171

7272
Rect boundingRect() const;
73+
Rect fastBoundingRect() const;
7374
void keepInFence(double newX, double newY, double *fencedX, double *fencedY) const;
7475

7576
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;

src/scratch/sprite.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ Rect Sprite::boundingRect() const
350350
return impl->iface->boundingRect();
351351
}
352352

353+
/*!
354+
* Returns the less accurate bounding rectangle of the sprite
355+
* which is calculated by transforming the costume rectangle.
356+
*/
357+
Rect Sprite::fastBoundingRect() const
358+
{
359+
if (!impl->iface)
360+
return Rect(impl->x, impl->y, impl->x, impl->y);
361+
362+
return impl->iface->fastBoundingRect();
363+
}
364+
353365
/*!
354366
* Keeps the desired position within the stage.
355367
* \param[in] New desired X position.

test/scratch_classes/sprite_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,18 @@ TEST(SpriteTest, DefaultBoundingRect)
602602
ASSERT_EQ(rect.bottom(), -45.01);
603603
}
604604

605+
TEST(SpriteTest, DefaultFastBoundingRect)
606+
{
607+
Sprite sprite;
608+
sprite.setX(65.5);
609+
sprite.setY(-45.01);
610+
Rect rect = sprite.fastBoundingRect();
611+
ASSERT_EQ(rect.left(), 65.5);
612+
ASSERT_EQ(rect.top(), -45.01);
613+
ASSERT_EQ(rect.right(), 65.5);
614+
ASSERT_EQ(rect.bottom(), -45.01);
615+
}
616+
605617
TEST(SpriteTest, GraphicsEffects)
606618
{
607619
Sprite sprite;

test/target_interfaces/ispritehandler_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,13 @@ TEST_F(ISpriteHandlerTest, BoundingRect)
199199
ASSERT_EQ(rect.right(), 20.5);
200200
ASSERT_EQ(rect.bottom(), -0.48);
201201
}
202+
203+
TEST_F(ISpriteHandlerTest, FastBoundingRect)
204+
{
205+
EXPECT_CALL(m_handler, fastBoundingRect()).WillOnce(Return(Rect(-44.6, 89.1, 20.5, -0.48)));
206+
Rect rect = m_sprite.fastBoundingRect();
207+
ASSERT_EQ(rect.left(), -44.6);
208+
ASSERT_EQ(rect.top(), 89.1);
209+
ASSERT_EQ(rect.right(), 20.5);
210+
ASSERT_EQ(rect.bottom(), -0.48);
211+
}

0 commit comments

Comments
 (0)