Skip to content

Commit 293aed7

Browse files
committed
Implement sound_stopallsounds block
1 parent 8912b60 commit 293aed7

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
2020
// Blocks
2121
engine->addCompileFunction(this, "sound_play", &compilePlay);
2222
engine->addCompileFunction(this, "sound_playuntildone", &compilePlayUntilDone);
23+
engine->addCompileFunction(this, "sound_stopallsounds", &compileStopAllSounds);
2324
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
2425
engine->addCompileFunction(this, "sound_setvolumeto", &compileSetVolumeTo);
2526
engine->addCompileFunction(this, "sound_volume", &compileVolume);
@@ -92,6 +93,11 @@ void SoundBlocks::compilePlayUntilDone(Compiler *compiler)
9293
compiler->addFunctionCall(byIndex ? &checkSoundByIndex : &checkSound);
9394
}
9495

96+
void SoundBlocks::compileStopAllSounds(Compiler *compiler)
97+
{
98+
compiler->addFunctionCall(&stopAllSounds);
99+
}
100+
95101
void SoundBlocks::compileChangeVolumeBy(Compiler *compiler)
96102
{
97103
compiler->addInput(VOLUME);
@@ -257,6 +263,13 @@ unsigned int SoundBlocks::checkSoundByIndex(VirtualMachine *vm)
257263
return 1;
258264
}
259265

266+
unsigned int SoundBlocks::stopAllSounds(VirtualMachine *vm)
267+
{
268+
vm->engine()->stopSounds();
269+
m_waitingSounds.clear();
270+
return 0;
271+
}
272+
260273
unsigned int SoundBlocks::changeVolumeBy(VirtualMachine *vm)
261274
{
262275
if (Target *target = vm->target())

src/blocks/soundblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class SoundBlocks : public IBlockSection
4040
static bool compilePlayCommon(Compiler *compiler, bool untilDone, bool *byIndex = nullptr);
4141
static void compilePlay(Compiler *compiler);
4242
static void compilePlayUntilDone(Compiler *compiler);
43+
static void compileStopAllSounds(Compiler *compiler);
4344
static void compileChangeVolumeBy(Compiler *compiler);
4445
static void compileSetVolumeTo(Compiler *compiler);
4546
static void compileVolume(Compiler *compiler);
@@ -56,6 +57,8 @@ class SoundBlocks : public IBlockSection
5657
static unsigned int checkSound(VirtualMachine *vm);
5758
static unsigned int checkSoundByIndex(VirtualMachine *vm);
5859

60+
static unsigned int stopAllSounds(VirtualMachine *vm);
61+
5962
static unsigned int changeVolumeBy(VirtualMachine *vm);
6063
static unsigned int setVolumeTo(VirtualMachine *vm);
6164
static unsigned int volume(VirtualMachine *vm);

test/blocks/sound_blocks_test.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ TEST_F(SoundBlocksTest, RegisterBlocks)
100100
// Blocks
101101
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_play", &SoundBlocks::compilePlay));
102102
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_playuntildone", &SoundBlocks::compilePlayUntilDone));
103+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_stopallsounds", &SoundBlocks::compileStopAllSounds));
103104
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changevolumeby", &SoundBlocks::compileChangeVolumeBy));
104105
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_setvolumeto", &SoundBlocks::compileSetVolumeTo));
105106
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_volume", &SoundBlocks::compileVolume));
@@ -550,6 +551,39 @@ TEST_F(SoundBlocksTest, PlayUntilDoneImpl)
550551
SoundPrivate::playerFactory = nullptr;
551552
}
552553

554+
TEST_F(SoundBlocksTest, StopAllSounds)
555+
{
556+
Compiler compiler(&m_engineMock);
557+
558+
auto block = std::make_shared<Block>("a", "sound_stopallsounds");
559+
560+
EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::stopAllSounds)).WillOnce(Return(0));
561+
562+
compiler.init();
563+
compiler.setBlock(block);
564+
SoundBlocks::compileStopAllSounds(&compiler);
565+
compiler.end();
566+
567+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
568+
ASSERT_TRUE(compiler.constValues().empty());
569+
}
570+
571+
TEST_F(SoundBlocksTest, StopAllSoundsImpl)
572+
{
573+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
574+
static BlockFunc functions[] = { &SoundBlocks::stopAllSounds };
575+
576+
VirtualMachine vm(nullptr, &m_engineMock, nullptr);
577+
578+
vm.setBytecode(bytecode);
579+
vm.setFunctions(functions);
580+
581+
EXPECT_CALL(m_engineMock, stopSounds());
582+
vm.run();
583+
584+
ASSERT_EQ(vm.registerCount(), 0);
585+
}
586+
553587
TEST_F(SoundBlocksTest, ChangeVolumeBy)
554588
{
555589
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)