|
9 | 9 | #include <scratchcpp/stage.h> |
10 | 10 | #include <scratchcpp/sprite.h> |
11 | 11 | #include <scratchcpp/costume.h> |
| 12 | +#include <scratchcpp/rect.h> |
12 | 13 | #include <enginemock.h> |
13 | 14 |
|
14 | 15 | #include "../common.h" |
@@ -554,3 +555,75 @@ TEST_F(RenderedTargetTest, StageScale) |
554 | 555 | target.setStageScale(6.4); |
555 | 556 | ASSERT_EQ(target.stageScale(), 6.4); |
556 | 557 | } |
| 558 | + |
| 559 | +TEST_F(RenderedTargetTest, GetBounds) |
| 560 | +{ |
| 561 | + QOpenGLContext context; |
| 562 | + QOffscreenSurface surface; |
| 563 | + createContextAndSurface(&context, &surface); |
| 564 | + QOpenGLExtraFunctions glF(&context); |
| 565 | + glF.initializeOpenGLFunctions(); |
| 566 | + RenderedTarget target; |
| 567 | + |
| 568 | + Sprite sprite; |
| 569 | + sprite.setX(75.64); |
| 570 | + sprite.setY(-120.3); |
| 571 | + sprite.setDirection(-46.37); |
| 572 | + sprite.setSize(67.98); |
| 573 | + SpriteModel spriteModel; |
| 574 | + sprite.setInterface(&spriteModel); |
| 575 | + target.setSpriteModel(&spriteModel); |
| 576 | + EngineMock engine; |
| 577 | + target.setEngine(&engine); |
| 578 | + auto costume = std::make_shared<Costume>("", "", "png"); |
| 579 | + std::string costumeData = readFileStr("image.png"); |
| 580 | + costume->setData(costumeData.size(), static_cast<void *>(costumeData.data())); |
| 581 | + costume->setRotationCenterX(-15); |
| 582 | + costume->setRotationCenterY(48); |
| 583 | + costume->setBitmapResolution(3.25); |
| 584 | + sprite.addCostume(costume); |
| 585 | + |
| 586 | + EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480)); |
| 587 | + EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360)); |
| 588 | + target.loadCostumes(); |
| 589 | + target.updateCostume(costume.get()); |
| 590 | + target.beforeRedraw(); |
| 591 | + |
| 592 | + Texture texture = target.texture(); |
| 593 | + QOpenGLFramebufferObjectFormat format; |
| 594 | + format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); |
| 595 | + |
| 596 | + QOpenGLFramebufferObject fbo(texture.size(), format); |
| 597 | + fbo.bind(); |
| 598 | + glF.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.handle(), 0); |
| 599 | + target.updateHullPoints(&fbo); |
| 600 | + fbo.release(); |
| 601 | + |
| 602 | + Rect bounds = target.getBounds(); |
| 603 | + ASSERT_EQ(std::round(bounds.left() * 100) / 100, 66.13); |
| 604 | + ASSERT_EQ(std::round(bounds.top() * 100) / 100, -124.52); |
| 605 | + ASSERT_EQ(std::round(bounds.right() * 100) / 100, 66.72); |
| 606 | + ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -125.11); |
| 607 | + |
| 608 | + EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480)); |
| 609 | + EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360)); |
| 610 | + target.updateRotationStyle(Sprite::RotationStyle::LeftRight); |
| 611 | + |
| 612 | + bounds = target.getBounds(); |
| 613 | + ASSERT_EQ(std::round(bounds.left() * 100) / 100, 71.87); |
| 614 | + ASSERT_EQ(std::round(bounds.top() * 100) / 100, -110.47); |
| 615 | + ASSERT_EQ(std::round(bounds.right() * 100) / 100, 72.29); |
| 616 | + ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -110.89); |
| 617 | + |
| 618 | + EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480)); |
| 619 | + EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360)); |
| 620 | + target.setStageScale(20.75); |
| 621 | + |
| 622 | + bounds = target.getBounds(); |
| 623 | + ASSERT_EQ(std::round(bounds.left() * 100) / 100, 71.87); |
| 624 | + ASSERT_EQ(std::round(bounds.top() * 100) / 100, -110.47); |
| 625 | + ASSERT_EQ(std::round(bounds.right() * 100) / 100, 72.29); |
| 626 | + ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -110.89); |
| 627 | + |
| 628 | + context.doneCurrent(); |
| 629 | +} |
0 commit comments