Skip to content

Commit 0d63b3b

Browse files
committed
Implement sound_volume block
1 parent fc7b850 commit 0d63b3b

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/blocks/soundblocks.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3939
engine->addCompileFunction(this, "sound_cleareffects", &compileClearEffects);
4040
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
4141
engine->addCompileFunction(this, "sound_setvolumeto", &compileSetVolumeTo);
42+
engine->addCompileFunction(this, "sound_volume", &compileVolume);
4243
}
4344

4445
void SoundBlocks::onInit(IEngine *engine)
@@ -144,6 +145,11 @@ CompilerValue *SoundBlocks::compileSetVolumeTo(Compiler *compiler)
144145
return nullptr;
145146
}
146147

148+
CompilerValue *SoundBlocks::compileVolume(Compiler *compiler)
149+
{
150+
return compiler->addTargetFunctionCall("sound_volume", Compiler::StaticType::Number);
151+
}
152+
147153
int sound_wrap_clamp_index(Target *target, int index)
148154
{
149155
const long soundCount = target->sounds().size();
@@ -248,3 +254,8 @@ extern "C" void sound_setvolumeto(Target *target, double volume)
248254
{
249255
target->setVolume(volume);
250256
}
257+
258+
extern "C" double sound_volume(Target *target)
259+
{
260+
return target->volume();
261+
}

src/blocks/soundblocks.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SoundBlocks : public IExtension
2828
static CompilerValue *compileClearEffects(Compiler *compiler);
2929
static CompilerValue *compileChangeVolumeBy(Compiler *compiler);
3030
static CompilerValue *compileSetVolumeTo(Compiler *compiler);
31+
static CompilerValue *compileVolume(Compiler *compiler);
3132
};
3233

3334
} // namespace libscratchcpp

test/blocks/sound_blocks_test.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <scratchcpp/compiler.h>
55
#include <scratchcpp/script.h>
66
#include <scratchcpp/thread.h>
7+
#include <scratchcpp/list.h>
78
#include <scratchcpp/test/scriptbuilder.h>
89
#include <scratch/sound_p.h>
910
#include <enginemock.h>
@@ -1088,3 +1089,41 @@ TEST_F(SoundBlocksTest, SetVolumeTo_Stage)
10881089
builder.run();
10891090
ASSERT_EQ(stage->volume(), 38.4);
10901091
}
1092+
1093+
TEST_F(SoundBlocksTest, Volume_Sprite)
1094+
{
1095+
auto sprite = std::make_shared<Sprite>();
1096+
sprite->setVolume(50);
1097+
1098+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1099+
builder.addBlock("sound_volume");
1100+
builder.captureBlockReturnValue();
1101+
builder.build();
1102+
1103+
sprite->setVolume(1.9);
1104+
builder.run();
1105+
ASSERT_EQ(sprite->volume(), 1.9);
1106+
1107+
List *list = builder.capturedValues();
1108+
ASSERT_EQ(list->size(), 1);
1109+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 1.9);
1110+
}
1111+
1112+
TEST_F(SoundBlocksTest, Volume_Stage)
1113+
{
1114+
auto stage = std::make_shared<Stage>();
1115+
stage->setVolume(75);
1116+
1117+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1118+
builder.addBlock("sound_volume");
1119+
builder.captureBlockReturnValue();
1120+
builder.build();
1121+
1122+
stage->setVolume(43.7);
1123+
builder.run();
1124+
ASSERT_EQ(stage->volume(), 43.7);
1125+
1126+
List *list = builder.capturedValues();
1127+
ASSERT_EQ(list->size(), 1);
1128+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 43.7);
1129+
}

0 commit comments

Comments
 (0)