Skip to content

Commit ba776aa

Browse files
committed
Implement looks_show block
1 parent 3211938 commit ba776aa

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
#include <scratchcpp/iengine.h>
4+
#include <scratchcpp/compiler.h>
5+
#include <scratchcpp/sprite.h>
6+
37
#include "looksblocks.h"
48

59
using namespace libscratchcpp;
@@ -11,4 +15,20 @@ std::string LooksBlocks::name() const
1115

1216
void LooksBlocks::registerBlocks(IEngine *engine)
1317
{
18+
engine->addCompileFunction(this, "looks_show", &compileShow);
19+
}
20+
21+
void LooksBlocks::compileShow(Compiler *compiler)
22+
{
23+
compiler->addFunctionCall(&show);
24+
}
25+
26+
unsigned int LooksBlocks::show(VirtualMachine *vm)
27+
{
28+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
29+
30+
if (sprite)
31+
sprite->setVisible(true);
32+
33+
return 0;
1434
}

src/blocks/looksblocks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class LooksBlocks : public IBlockSection
2626
std::string name() const override;
2727

2828
void registerBlocks(IEngine *engine) override;
29+
30+
static void compileShow(Compiler *compiler);
31+
32+
static unsigned int show(VirtualMachine *vm);
2933
};
3034

3135
} // namespace libscratchcpp

test/blocks/looks_blocks_test.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <scratchcpp/block.h>
33
#include <scratchcpp/input.h>
44
#include <scratchcpp/field.h>
5+
#include <scratchcpp/sprite.h>
56
#include <enginemock.h>
67

78
#include "../common.h"
@@ -90,5 +91,48 @@ TEST_F(LooksBlocksTest, CategoryVisible)
9091

9192
TEST_F(LooksBlocksTest, RegisterBlocks)
9293
{
94+
// Blocks
95+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_show", &LooksBlocks::compileShow));
96+
9397
m_section->registerBlocks(&m_engineMock);
9498
}
99+
100+
TEST_F(LooksBlocksTest, Show)
101+
{
102+
Compiler compiler(&m_engineMock);
103+
104+
auto block = std::make_shared<Block>("a", "looks_show");
105+
106+
EXPECT_CALL(m_engineMock, functionIndex(&LooksBlocks::show)).WillOnce(Return(0));
107+
108+
compiler.init();
109+
compiler.setBlock(block);
110+
LooksBlocks::compileShow(&compiler);
111+
compiler.end();
112+
113+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
114+
ASSERT_TRUE(compiler.constValues().empty());
115+
}
116+
117+
TEST_F(LooksBlocksTest, ShowImpl)
118+
{
119+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
120+
static BlockFunc functions[] = { &LooksBlocks::show };
121+
122+
Sprite sprite;
123+
sprite.setVisible(false);
124+
125+
VirtualMachine vm(&sprite, nullptr, nullptr);
126+
vm.setBytecode(bytecode);
127+
vm.setFunctions(functions);
128+
vm.run();
129+
130+
ASSERT_EQ(vm.registerCount(), 0);
131+
ASSERT_TRUE(sprite.visible());
132+
133+
vm.reset();
134+
vm.run();
135+
136+
ASSERT_EQ(vm.registerCount(), 0);
137+
ASSERT_TRUE(sprite.visible());
138+
}

0 commit comments

Comments
 (0)