Skip to content

Commit a77513c

Browse files
committed
Implement sound_volume block
1 parent 3c3428d commit a77513c

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
1818
// Blocks
1919
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
2020
engine->addCompileFunction(this, "sound_setvolumeto", &compileSetVolumeTo);
21+
engine->addCompileFunction(this, "sound_volume", &compileVolume);
2122

2223
// Inputs
2324
engine->addInput(this, "VOLUME", VOLUME);
@@ -35,6 +36,11 @@ void SoundBlocks::compileSetVolumeTo(Compiler *compiler)
3536
compiler->addFunctionCall(&setVolumeTo);
3637
}
3738

39+
void SoundBlocks::compileVolume(Compiler *compiler)
40+
{
41+
compiler->addFunctionCall(&volume);
42+
}
43+
3844
unsigned int SoundBlocks::changeVolumeBy(VirtualMachine *vm)
3945
{
4046
if (Target *target = vm->target())
@@ -50,3 +56,13 @@ unsigned int SoundBlocks::setVolumeTo(VirtualMachine *vm)
5056

5157
return 1;
5258
}
59+
60+
unsigned int SoundBlocks::volume(VirtualMachine *vm)
61+
{
62+
if (Target *target = vm->target())
63+
vm->addReturnValue(target->volume());
64+
else
65+
vm->addReturnValue(0);
66+
67+
return 0;
68+
}

src/blocks/soundblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ class SoundBlocks : public IBlockSection
3232

3333
static void compileChangeVolumeBy(Compiler *compiler);
3434
static void compileSetVolumeTo(Compiler *compiler);
35+
static void compileVolume(Compiler *compiler);
3536

3637
static unsigned int changeVolumeBy(VirtualMachine *vm);
3738
static unsigned int setVolumeTo(VirtualMachine *vm);
39+
static unsigned int volume(VirtualMachine *vm);
3840
};
3941

4042
} // namespace libscratchcpp

test/blocks/sound_blocks_test.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ TEST_F(SoundBlocksTest, RegisterBlocks)
9696
// Blocks
9797
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changevolumeby", &SoundBlocks::compileChangeVolumeBy));
9898
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_setvolumeto", &SoundBlocks::compileSetVolumeTo));
99+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_volume", &SoundBlocks::compileVolume));
99100

100101
// Inputs
101102
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "VOLUME", SoundBlocks::VOLUME));
@@ -198,3 +199,38 @@ TEST_F(SoundBlocksTest, SetVolumeToImpl)
198199
ASSERT_EQ(vm.registerCount(), 0);
199200
ASSERT_EQ(target.volume(), 43.409);
200201
}
202+
203+
TEST_F(SoundBlocksTest, Volume)
204+
{
205+
Compiler compiler(&m_engineMock);
206+
207+
auto block = std::make_shared<Block>("a", "sound_volume");
208+
209+
EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::volume)).WillOnce(Return(0));
210+
211+
compiler.init();
212+
compiler.setBlock(block);
213+
SoundBlocks::compileVolume(&compiler);
214+
compiler.end();
215+
216+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
217+
ASSERT_TRUE(compiler.constValues().empty());
218+
}
219+
220+
TEST_F(SoundBlocksTest, VolumeImpl)
221+
{
222+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
223+
static BlockFunc functions[] = { &SoundBlocks::volume };
224+
225+
Target target;
226+
target.setVolume(42.4);
227+
228+
VirtualMachine vm(&target, nullptr, nullptr);
229+
230+
vm.setBytecode(bytecode);
231+
vm.setFunctions(functions);
232+
vm.run();
233+
234+
ASSERT_EQ(vm.registerCount(), 1);
235+
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), 42.4);
236+
}

0 commit comments

Comments
 (0)