Skip to content

Commit 389c27f

Browse files
committed
Implement sound_cleareffects block
1 parent 4e077a2 commit 389c27f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3737
engine->addCompileFunction(this, "sound_seteffectto", &compileSetEffectTo);
3838
engine->addCompileFunction(this, "sound_changeeffectby", &compileChangeEffectBy);
3939
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
40+
engine->addCompileFunction(this, "sound_cleareffects", &compileClearEffects);
4041
engine->addCompileFunction(this, "sound_setvolumeto", &compileSetVolumeTo);
4142
engine->addCompileFunction(this, "sound_volume", &compileVolume);
4243

@@ -169,6 +170,11 @@ void SoundBlocks::compileChangeEffectBy(Compiler *compiler)
169170
}
170171
}
171172

173+
void SoundBlocks::compileClearEffects(Compiler *compiler)
174+
{
175+
compiler->addFunctionCall(&clearEffects);
176+
}
177+
172178
void SoundBlocks::compileChangeVolumeBy(Compiler *compiler)
173179
{
174180
compiler->addInput(VOLUME);
@@ -379,6 +385,14 @@ unsigned int SoundBlocks::changePanEffectBy(VirtualMachine *vm)
379385
return 1;
380386
}
381387

388+
unsigned int SoundBlocks::clearEffects(VirtualMachine *vm)
389+
{
390+
if (Target *target = vm->target())
391+
target->clearSoundEffects();
392+
393+
return 0;
394+
}
395+
382396
unsigned int SoundBlocks::changeVolumeBy(VirtualMachine *vm)
383397
{
384398
if (Target *target = vm->target())

src/blocks/soundblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class SoundBlocks : public IBlockSection
4646
static void compileStopAllSounds(Compiler *compiler);
4747
static void compileSetEffectTo(Compiler *compiler);
4848
static void compileChangeEffectBy(Compiler *compiler);
49+
static void compileClearEffects(Compiler *compiler);
4950
static void compileChangeVolumeBy(Compiler *compiler);
5051
static void compileSetVolumeTo(Compiler *compiler);
5152
static void compileVolume(Compiler *compiler);
@@ -70,6 +71,7 @@ class SoundBlocks : public IBlockSection
7071
static unsigned int setPanEffectTo(VirtualMachine *vm);
7172
static unsigned int changePitchEffectBy(VirtualMachine *vm);
7273
static unsigned int changePanEffectBy(VirtualMachine *vm);
74+
static unsigned int clearEffects(VirtualMachine *vm);
7375

7476
static unsigned int changeVolumeBy(VirtualMachine *vm);
7577
static unsigned int setVolumeTo(VirtualMachine *vm);

test/blocks/sound_blocks_test.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ TEST_F(SoundBlocksTest, RegisterBlocks)
105105
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_seteffectto", &SoundBlocks::compileSetEffectTo));
106106
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changeeffectby", &SoundBlocks::compileChangeEffectBy));
107107
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changevolumeby", &SoundBlocks::compileChangeVolumeBy));
108+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_cleareffects", &SoundBlocks::compileClearEffects));
108109
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_setvolumeto", &SoundBlocks::compileSetVolumeTo));
109110
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_volume", &SoundBlocks::compileVolume));
110111

@@ -715,6 +716,40 @@ TEST_F(SoundBlocksTest, ChangeEffectByImpl)
715716
ASSERT_EQ(vm.registerCount(), 0);
716717
}
717718

719+
TEST_F(SoundBlocksTest, ClearEffects)
720+
{
721+
Compiler compiler(&m_engineMock);
722+
723+
auto block = std::make_shared<Block>("a", "sound_cleareffects");
724+
725+
EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::clearEffects)).WillOnce(Return(0));
726+
727+
compiler.init();
728+
compiler.setBlock(block);
729+
SoundBlocks::compileClearEffects(&compiler);
730+
compiler.end();
731+
732+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
733+
ASSERT_TRUE(compiler.constValues().empty());
734+
}
735+
736+
TEST_F(SoundBlocksTest, ClearEffectsImpl)
737+
{
738+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
739+
static BlockFunc functions[] = { &SoundBlocks::clearEffects };
740+
741+
TargetMock target;
742+
VirtualMachine vm(&target, nullptr, nullptr);
743+
744+
vm.setBytecode(bytecode);
745+
vm.setFunctions(functions);
746+
747+
EXPECT_CALL(target, clearSoundEffects());
748+
vm.run();
749+
750+
ASSERT_EQ(vm.registerCount(), 0);
751+
}
752+
718753
TEST_F(SoundBlocksTest, ChangeVolumeBy)
719754
{
720755
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)