Skip to content

Commit b4cdd16

Browse files
committed
Fix calculation of bounds
1 parent f4aa357 commit b4cdd16

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

src/renderedtarget.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,12 @@ Rect RenderedTarget::getBounds() const
362362
if (!m_costume || !m_skin || !m_texture.isValid() || !m_cpuTexture.isValid())
363363
return Rect(m_x, m_y, m_x, m_y);
364364

365-
const double width = m_cpuTexture.width() * m_size / m_costume->bitmapResolution();
366-
const double height = m_cpuTexture.height() * m_size / m_costume->bitmapResolution();
367-
const double originX = m_costume->rotationCenterX() * m_size / m_costume->bitmapResolution() - width / 2;
368-
const double originY = -m_costume->rotationCenterY() * m_size / m_costume->bitmapResolution() + height / 2;
365+
const double textureScale = m_skin->getTextureScale(m_cpuTexture);
366+
const double bitmapRes = m_costume->bitmapResolution();
367+
const double width = m_cpuTexture.width() * m_size / textureScale;
368+
const double height = m_cpuTexture.height() * m_size / textureScale;
369+
const double originX = m_costume->rotationCenterX() * m_size / bitmapRes - width / 2;
370+
const double originY = -m_costume->rotationCenterY() * m_size / bitmapRes + height / 2;
369371
const double rot = -rotation() * pi / 180;
370372
const double sinRot = std::sin(rot);
371373
const double cosRot = std::cos(rot);
@@ -377,9 +379,11 @@ Rect RenderedTarget::getBounds() const
377379
const std::vector<QPoint> &points = hullPoints();
378380

379381
for (const QPointF &point : points) {
380-
QPointF transformed = transformPoint(point.x() - width / 2, height / 2 - point.y(), originX, originY, sinRot, cosRot);
381-
const double x = transformed.x() * (m_mirrorHorizontally ? -1 : 1);
382-
const double y = transformed.y();
382+
double x = point.x() * m_size / textureScale / bitmapRes - width / 2;
383+
double y = height / 2 - point.y() * m_size / textureScale / bitmapRes;
384+
const QPointF transformed = transformPoint(x, y, originX, originY, sinRot, cosRot);
385+
x = transformed.x() * (m_mirrorHorizontally ? -1 : 1);
386+
y = transformed.y();
383387

384388
if (x < left)
385389
left = x;
@@ -422,10 +426,11 @@ Rect RenderedTarget::getFastBounds() const
422426
return Rect(m_x, m_y, m_x, m_y);
423427

424428
const double textureScale = m_skin->getTextureScale(m_cpuTexture);
425-
const double width = m_cpuTexture.width() * m_size / textureScale;
426-
const double height = m_cpuTexture.height() * m_size / textureScale;
427-
const double originX = m_costume->rotationCenterX() * m_size / m_costume->bitmapResolution() - width / 2;
428-
const double originY = -m_costume->rotationCenterY() * m_size / m_costume->bitmapResolution() + height / 2;
429+
const double bitmapRes = m_costume->bitmapResolution();
430+
const double width = m_cpuTexture.width() * m_size / textureScale / bitmapRes;
431+
const double height = m_cpuTexture.height() * m_size / textureScale / bitmapRes;
432+
const double originX = m_costume->rotationCenterX() * m_size / bitmapRes - width / 2;
433+
const double originY = -m_costume->rotationCenterY() * m_size / bitmapRes + height / 2;
429434
const double rot = -rotation() * pi / 180;
430435
const double sinRot = std::sin(rot);
431436
const double cosRot = std::cos(rot);

test/renderedtarget/renderedtarget_test.cpp

+41-41
Original file line numberDiff line numberDiff line change
@@ -663,64 +663,64 @@ TEST_F(RenderedTargetTest, GetBounds)
663663
target.beforeRedraw();
664664

665665
Rect bounds = target.getBounds();
666-
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 64.96);
667-
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -121.16);
668-
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 67.79);
669-
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -123.99);
666+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 66.13);
667+
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -124.52);
668+
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 66.72);
669+
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -125.11);
670670

671671
QRectF bubbleBounds = target.getBoundsForBubble();
672-
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 64.96);
673-
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -121.16);
674-
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 67.79);
675-
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -123.99);
672+
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 66.13);
673+
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -124.52);
674+
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 66.72);
675+
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -125.11);
676676

677677
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
678678
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
679679
target.updateRotationStyle(Sprite::RotationStyle::LeftRight);
680680

681681
bounds = target.getBounds();
682-
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 69.5);
683-
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -111.26);
684-
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 71.5);
685-
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -113.26);
682+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 71.87);
683+
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -110.47);
684+
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 72.29);
685+
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -110.89);
686686

687687
bubbleBounds = target.getBoundsForBubble();
688-
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 69.5);
689-
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -111.26);
690-
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 71.5);
691-
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -113.26);
688+
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 71.87);
689+
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -110.47);
690+
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 72.29);
691+
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -110.89);
692692

693693
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
694694
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
695695
target.setStageScale(20.75);
696696

697697
bounds = target.getBounds();
698-
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 69.5);
699-
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -111.26);
700-
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 71.5);
701-
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -113.26);
698+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 71.87);
699+
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -110.47);
700+
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 72.29);
701+
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -110.89);
702702

703703
bubbleBounds = target.getBoundsForBubble();
704-
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 69.5);
705-
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -111.26);
706-
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 71.5);
707-
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -113.26);
704+
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 71.87);
705+
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -110.47);
706+
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 72.29);
707+
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -110.89);
708708

709709
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
710710
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
711711
target.updateSize(9780.6);
712712

713713
bounds = target.getBounds();
714-
ASSERT_EQ(std::round(bounds.left() * 100) / 100, -378.77);
715-
ASSERT_EQ(std::round(bounds.top() * 100) / 100, 1323.22);
716-
ASSERT_EQ(std::round(bounds.right() * 100) / 100, -376.77);
717-
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, 1321.22);
714+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, -466.05);
715+
ASSERT_EQ(std::round(bounds.top() * 100) / 100, 1294.13);
716+
ASSERT_EQ(std::round(bounds.right() * 100) / 100, -405.87);
717+
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, 1233.94);
718718

719719
bubbleBounds = target.getBoundsForBubble();
720-
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, -378.77);
721-
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, 1323.22);
722-
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, -376.77);
723-
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, 1321.22);
720+
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, -466.05);
721+
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, 1294.13);
722+
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, -405.87);
723+
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, 1286.13);
724724

725725
context.doneCurrent();
726726
}
@@ -759,40 +759,40 @@ TEST_F(RenderedTargetTest, GetFastBounds)
759759
target.beforeRedraw();
760760

761761
Rect bounds = target.getFastBounds();
762-
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 64.47);
763-
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -120.57);
764-
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 69.26);
762+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 65.84);
763+
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -123.92);
764+
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 67.31);
765765
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -125.4);
766766

767767
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
768768
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
769769
target.updateRotationStyle(Sprite::RotationStyle::LeftRight);
770770

771771
bounds = target.getFastBounds();
772-
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 69.78);
772+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 71.67);
773773
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -110.26);
774774
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 72.5);
775-
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -114.34);
775+
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -111.51);
776776

777777
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
778778
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
779779
target.setStageScale(20.75);
780780

781781
bounds = target.getFastBounds();
782-
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 69.78);
782+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, 71.67);
783783
ASSERT_EQ(std::round(bounds.top() * 100) / 100, -110.26);
784784
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 72.5);
785-
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -114.34);
785+
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -111.51);
786786

787787
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
788788
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
789789
target.updateSize(9780.6);
790790

791791
bounds = target.getFastBounds();
792-
ASSERT_EQ(std::round(bounds.left() * 100) / 100, -767);
792+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, -496.15);
793793
ASSERT_EQ(std::round(bounds.top() * 100) / 100, 1324.22);
794794
ASSERT_EQ(std::round(bounds.right() * 100) / 100, -375.77);
795-
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, 737.38);
795+
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, 1143.65);
796796

797797
context.doneCurrent();
798798
}

0 commit comments

Comments
 (0)