Skip to content

Commit 5635768

Browse files
committed
Implement sound_playuntildone block
1 parent 7d4f11b commit 5635768

File tree

3 files changed

+461
-3
lines changed

3 files changed

+461
-3
lines changed

src/blocks/soundblocks.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,27 @@ Rgb SoundBlocks::color() const
2727
void SoundBlocks::registerBlocks(IEngine *engine)
2828
{
2929
engine->addCompileFunction(this, "sound_play", &compilePlay);
30+
engine->addCompileFunction(this, "sound_playuntildone", &compilePlayUntilDone);
3031
}
3132

3233
CompilerValue *SoundBlocks::compilePlay(Compiler *compiler)
3334
{
3435
auto sound = compiler->addInput("SOUND_MENU");
35-
compiler->addTargetFunctionCall("sound_play", Compiler::StaticType::Void, { Compiler::StaticType::Unknown }, { sound });
36+
compiler->addTargetFunctionCall("sound_play", Compiler::StaticType::Pointer, { Compiler::StaticType::Unknown }, { sound });
37+
return nullptr;
38+
}
39+
40+
CompilerValue *SoundBlocks::compilePlayUntilDone(Compiler *compiler)
41+
{
42+
auto sound = compiler->addInput("SOUND_MENU");
43+
auto soundPtr = compiler->addTargetFunctionCall("sound_play", Compiler::StaticType::Pointer, { Compiler::StaticType::Unknown }, { sound });
44+
45+
compiler->beginLoopCondition();
46+
auto numType = Compiler::StaticType::Number;
47+
auto playing = compiler->addFunctionCall("sound_is_playing", Compiler::StaticType::Bool, { Compiler::StaticType::Pointer }, { soundPtr });
48+
compiler->beginWhileLoop(playing);
49+
compiler->endLoop();
50+
3651
return nullptr;
3752
}
3853

@@ -78,11 +93,23 @@ int sound_get_index(Target *target, const ValueData *sound)
7893
return -1;
7994
}
8095

81-
extern "C" void sound_play(Target *target, const ValueData *soundName)
96+
extern "C" Sound *sound_play(Target *target, const ValueData *soundName)
8297
{
8398
int index = sound_get_index(target, soundName);
8499
auto sound = target->soundAt(index);
85100

86-
if (sound)
101+
if (sound) {
87102
sound->start();
103+
return sound.get();
104+
}
105+
106+
return nullptr;
107+
}
108+
109+
extern "C" bool sound_is_playing(Sound *sound)
110+
{
111+
if (!sound)
112+
return false;
113+
114+
return sound->isPlaying();
88115
}

src/blocks/soundblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SoundBlocks : public IExtension
2020

2121
private:
2222
static CompilerValue *compilePlay(Compiler *compiler);
23+
static CompilerValue *compilePlayUntilDone(Compiler *compiler);
2324
};
2425

2526
} // namespace libscratchcpp

0 commit comments

Comments
 (0)