Skip to content

Commit 66c1817

Browse files
authored
Merge pull request #237 from scratchcpp/stage_size
Store stage size in Engine
2 parents 8eadadf + 6d2ff6d commit 66c1817

File tree

7 files changed

+96
-28
lines changed

7 files changed

+96
-28
lines changed

include/scratchcpp/iengine.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ class LIBSCRATCHCPP_EXPORT IEngine
103103
/*! Sets the Y coordinate of the mouse pointer. */
104104
virtual void setMouseY(double y) = 0;
105105

106+
/*! Returns the stage width. */
107+
virtual unsigned int stageWidth() const = 0;
108+
109+
/*! Sets the stage width. */
110+
virtual void setStageWidth(unsigned int width) = 0;
111+
112+
/*! Returns the stage height. */
113+
virtual unsigned int stageHeight() const = 0;
114+
115+
/*! Sets the stage height. */
116+
virtual void setStageHeight(unsigned int height) = 0;
117+
106118
/*! Returns true if there are any running script of the broadcast with the given index. */
107119
virtual bool broadcastRunning(unsigned int index, VirtualMachine *sourceScript) = 0;
108120

src/blocks/motionblocks.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,8 @@ unsigned int MotionBlocks::pointTowards(VirtualMachine *vm)
304304
if (value == "_mouse_")
305305
pointTowardsPos(dynamic_cast<Sprite *>(vm->target()), vm->engine()->mouseX(), vm->engine()->mouseY());
306306
else if (value == "_random_") {
307-
// TODO: Read stage size from engine (#224)
308-
static const unsigned int stageWidth = 480;
309-
static const unsigned int stageHeight = 360;
307+
const unsigned int stageWidth = vm->engine()->stageWidth();
308+
const unsigned int stageHeight = vm->engine()->stageHeight();
310309

311310
if (!rng)
312311
rng = RandomGenerator::instance().get();
@@ -342,9 +341,8 @@ unsigned int MotionBlocks::pointTowardsMousePointer(VirtualMachine *vm)
342341

343342
unsigned int MotionBlocks::pointTowardsRandomPosition(VirtualMachine *vm)
344343
{
345-
// TODO: Read stage size from engine (#224)
346-
static const unsigned int stageWidth = 480;
347-
static const unsigned int stageHeight = 360;
344+
const unsigned int stageWidth = vm->engine()->stageWidth();
345+
const unsigned int stageHeight = vm->engine()->stageHeight();
348346

349347
if (!rng)
350348
rng = RandomGenerator::instance().get();
@@ -379,9 +377,8 @@ unsigned int MotionBlocks::goTo(VirtualMachine *vm)
379377
sprite->setX(vm->engine()->mouseX());
380378
sprite->setY(vm->engine()->mouseY());
381379
} else if (value == "_random_") {
382-
// TODO: Read stage size from engine (#224)
383-
static const unsigned int stageWidth = 480;
384-
static const unsigned int stageHeight = 360;
380+
const unsigned int stageWidth = vm->engine()->stageWidth();
381+
const unsigned int stageHeight = vm->engine()->stageHeight();
385382

386383
if (!rng)
387384
rng = RandomGenerator::instance().get();
@@ -432,9 +429,8 @@ unsigned int MotionBlocks::goToRandomPosition(VirtualMachine *vm)
432429
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
433430

434431
if (sprite) {
435-
// TODO: Read stage size from engine (#224)
436-
static const unsigned int stageWidth = 480;
437-
static const unsigned int stageHeight = 360;
432+
const unsigned int stageWidth = vm->engine()->stageWidth();
433+
const unsigned int stageHeight = vm->engine()->stageHeight();
438434

439435
if (!rng)
440436
rng = RandomGenerator::instance().get();
@@ -536,9 +532,8 @@ unsigned int MotionBlocks::startGlideTo(VirtualMachine *vm)
536532
if (value == "_mouse_")
537533
startGlidingToPos(vm, vm->engine()->mouseX(), vm->engine()->mouseY(), vm->getInput(0, 2)->toDouble());
538534
else if (value == "_random_") {
539-
// TODO: Read stage size from engine (#224)
540-
static const unsigned int stageWidth = 480;
541-
static const unsigned int stageHeight = 360;
535+
const unsigned int stageWidth = vm->engine()->stageWidth();
536+
const unsigned int stageHeight = vm->engine()->stageHeight();
542537

543538
if (!rng)
544539
rng = RandomGenerator::instance().get();
@@ -582,9 +577,8 @@ unsigned int MotionBlocks::startGlideToRandomPosition(VirtualMachine *vm)
582577
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
583578

584579
if (sprite) {
585-
// TODO: Read stage size from engine (#224)
586-
static const unsigned int stageWidth = 480;
587-
static const unsigned int stageHeight = 360;
580+
const unsigned int stageWidth = vm->engine()->stageWidth();
581+
const unsigned int stageHeight = vm->engine()->stageHeight();
588582

589583
if (!rng)
590584
rng = RandomGenerator::instance().get();

src/engine/internal/engine.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,26 @@ void Engine::setMouseY(double y)
383383
m_mouseY = y;
384384
}
385385

386+
unsigned int Engine::stageWidth() const
387+
{
388+
return m_stageWidth;
389+
}
390+
391+
void Engine::setStageWidth(unsigned int width)
392+
{
393+
m_stageWidth = width;
394+
}
395+
396+
unsigned int Engine::stageHeight() const
397+
{
398+
return m_stageHeight;
399+
}
400+
401+
void Engine::setStageHeight(unsigned int height)
402+
{
403+
m_stageHeight = height;
404+
}
405+
386406
bool Engine::broadcastRunning(unsigned int index, VirtualMachine *sourceScript)
387407
{
388408
const auto &scripts = m_runningBroadcastMap[index];

src/engine/internal/engine.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class Engine : public IEngine
4545
double mouseY() const override;
4646
void setMouseY(double y) override;
4747

48+
unsigned int stageWidth() const override;
49+
void setStageWidth(unsigned int width) override;
50+
51+
unsigned int stageHeight() const override;
52+
void setStageHeight(unsigned int height) override;
53+
4854
bool broadcastRunning(unsigned int index, VirtualMachine *sourceScript) override;
4955

5056
void breakFrame() override;
@@ -122,6 +128,8 @@ class Engine : public IEngine
122128
std::chrono::milliseconds m_frameDuration; // will be computed in run()
123129
double m_mouseX = 0;
124130
double m_mouseY = 0;
131+
unsigned int m_stageWidth = 480;
132+
unsigned int m_stageHeight = 360;
125133

126134
bool m_running = false;
127135
bool m_breakFrame = false;

test/blocks/motion_blocks_test.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,10 @@ TEST_F(MotionBlocksTest, PointTowardsImpl)
386386
sprite.setY(std::round(sprite.y()));
387387

388388
for (int i = 0; i < positions.size(); i++) {
389-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(std::round(positions[i].first)));
390-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(std::round(positions[i].second)));
389+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
390+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
391+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(std::round(positions[i].first)));
392+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(std::round(positions[i].second)));
391393

392394
// TODO: Move setBytecode() out of the loop and use reset() after task #215 is completed
393395
vm.setBytecode(bytecode2);
@@ -449,8 +451,10 @@ TEST_F(MotionBlocksTest, PointTowardsImpl)
449451
sprite.setY(std::round(sprite.y()));
450452

451453
for (int i = 0; i < positions.size(); i++) {
452-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(std::round(positions[i].first)));
453-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(std::round(positions[i].second)));
454+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
455+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
456+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(std::round(positions[i].first)));
457+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(std::round(positions[i].second)));
454458

455459
// TODO: Move setBytecode() out of the loop and use reset() after task #215 is completed
456460
vm.setBytecode(bytecode6);
@@ -590,8 +594,10 @@ TEST_F(MotionBlocksTest, GoToImpl)
590594
ASSERT_EQ(sprite.y(), 45.2);
591595

592596
// go to (join "_random_" "")
593-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(-158));
594-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(65));
597+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
598+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
599+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(-158));
600+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(65));
595601

596602
vm.setBytecode(bytecode2);
597603
vm.run();
@@ -633,8 +639,10 @@ TEST_F(MotionBlocksTest, GoToImpl)
633639
ASSERT_EQ(sprite.y(), -170.6);
634640

635641
// go to (random position)
636-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(220));
637-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(-16));
642+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
643+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
644+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(220));
645+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(-16));
638646

639647
vm.setBytecode(bytecode6);
640648
vm.run();
@@ -869,8 +877,10 @@ TEST_F(MotionBlocksTest, GlideToImpl)
869877
EXPECT_CALL(m_engineMock, mouseY()).WillOnce(Return(endY));
870878
break;
871879
case 3:
872-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(std::round(endX)));
873-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(std::round(endY)));
880+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
881+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
882+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(std::round(endX)));
883+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(std::round(endY)));
874884
default:
875885
break;
876886
}

test/engine/engine_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ TEST(EngineTest, MouseY)
6161
ASSERT_EQ(engine.mouseY(), 179.9258);
6262
}
6363

64+
TEST(EngineTest, StageWidth)
65+
{
66+
Engine engine;
67+
ASSERT_EQ(engine.stageWidth(), 480);
68+
69+
engine.setStageWidth(640);
70+
ASSERT_EQ(engine.stageWidth(), 640);
71+
}
72+
73+
TEST(EngineTest, StageHeight)
74+
{
75+
Engine engine;
76+
ASSERT_EQ(engine.stageHeight(), 360);
77+
78+
engine.setStageHeight(515);
79+
ASSERT_EQ(engine.stageHeight(), 515);
80+
}
81+
6482
TEST(EngineTest, BreakFrame)
6583
{
6684
Engine engine;

test/mocks/enginemock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class EngineMock : public IEngine
3333
MOCK_METHOD(double, mouseY, (), (const, override));
3434
MOCK_METHOD(void, setMouseY, (double y), (override));
3535

36+
MOCK_METHOD(unsigned int, stageWidth, (), (const, override));
37+
MOCK_METHOD(void, setStageWidth, (unsigned int), (override));
38+
39+
MOCK_METHOD(unsigned int, stageHeight, (), (const, override));
40+
MOCK_METHOD(void, setStageHeight, (unsigned int), (override));
41+
3642
MOCK_METHOD(bool, broadcastRunning, (unsigned int, VirtualMachine *), (override));
3743

3844
MOCK_METHOD(void, breakFrame, (), (override));

0 commit comments

Comments
 (0)