Skip to content

Commit 0086d8b

Browse files
committed
Implement sound_stopallsounds block
1 parent f6e7df7 commit 0086d8b

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3232
{
3333
engine->addCompileFunction(this, "sound_play", &compilePlay);
3434
engine->addCompileFunction(this, "sound_playuntildone", &compilePlayUntilDone);
35+
engine->addCompileFunction(this, "sound_stopallsounds", &compileStopAllSounds);
3536
}
3637

3738
void SoundBlocks::onInit(IEngine *engine)
@@ -71,6 +72,12 @@ CompilerValue *SoundBlocks::compilePlayUntilDone(Compiler *compiler)
7172
return nullptr;
7273
}
7374

75+
CompilerValue *SoundBlocks::compileStopAllSounds(Compiler *compiler)
76+
{
77+
compiler->addFunctionCallWithCtx("sound_stopallsounds");
78+
return nullptr;
79+
}
80+
7481
int sound_wrap_clamp_index(Target *target, int index)
7582
{
7683
const long soundCount = target->sounds().size();
@@ -135,3 +142,8 @@ extern "C" bool sound_is_waiting(ExecutionContext *ctx, Sound *sound)
135142

136143
return sound->owner() == ctx->thread() && sound->isPlaying();
137144
}
145+
146+
extern "C" void sound_stopallsounds(ExecutionContext *ctx)
147+
{
148+
ctx->engine()->stopSounds();
149+
}

src/blocks/soundblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SoundBlocks : public IExtension
2222
private:
2323
static CompilerValue *compilePlay(Compiler *compiler);
2424
static CompilerValue *compilePlayUntilDone(Compiler *compiler);
25+
static CompilerValue *compileStopAllSounds(Compiler *compiler);
2526
};
2627

2728
} // namespace libscratchcpp

test/blocks/sound_blocks_test.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,3 +849,39 @@ TEST_F(SoundBlocksTest, PlayUntilDone_Stage)
849849
thread.run();
850850
ASSERT_TRUE(thread.isFinished());
851851
}
852+
853+
TEST_F(SoundBlocksTest, StopAllSounds_Sprite)
854+
{
855+
auto sprite = std::make_shared<Sprite>();
856+
857+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
858+
builder.addBlock("sound_stopallsounds");
859+
auto block = builder.currentBlock();
860+
861+
Compiler compiler(&m_engineMock, sprite.get());
862+
auto code = compiler.compile(block);
863+
Script script(sprite.get(), block, &m_engineMock);
864+
script.setCode(code);
865+
Thread thread(sprite.get(), &m_engineMock, &script);
866+
867+
EXPECT_CALL(m_engineMock, stopSounds());
868+
thread.run();
869+
}
870+
871+
TEST_F(SoundBlocksTest, StopAllSounds_Stage)
872+
{
873+
auto stage = std::make_shared<Stage>();
874+
875+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
876+
builder.addBlock("sound_stopallsounds");
877+
auto block = builder.currentBlock();
878+
879+
Compiler compiler(&m_engineMock, stage.get());
880+
auto code = compiler.compile(block);
881+
Script script(stage.get(), block, &m_engineMock);
882+
script.setCode(code);
883+
Thread thread(stage.get(), &m_engineMock, &script);
884+
885+
EXPECT_CALL(m_engineMock, stopSounds());
886+
thread.run();
887+
}

0 commit comments

Comments
 (0)