Skip to content

Commit 8836ab1

Browse files
committed
Make sounds aware of target
1 parent d41aad0 commit 8836ab1

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

include/scratchcpp/sound.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace libscratchcpp
1010
{
1111

12+
class Target;
1213
class SoundPrivate;
1314

1415
/*! \brief The Sound class represents a Scratch sound. */
@@ -32,6 +33,9 @@ class LIBSCRATCHCPP_EXPORT Sound : public Asset
3233

3334
virtual bool isPlaying();
3435

36+
Target *target() const;
37+
void setTarget(Target *target);
38+
3539
std::shared_ptr<Sound> clone() const;
3640

3741
protected:

src/scratch/sound.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ bool Sound::isPlaying()
6262
return impl->player->isPlaying();
6363
}
6464

65+
/*! Returns the sprite or stage this variable belongs to. */
66+
Target *Sound::target() const
67+
{
68+
return impl->target;
69+
}
70+
71+
/*! Sets the sprite or stage this variable belongs to. */
72+
void Sound::setTarget(Target *target)
73+
{
74+
impl->target = target;
75+
}
76+
6577
/*! Returns an independent copy of the sound which is valid for as long as the original sound exists. */
6678
std::shared_ptr<Sound> Sound::clone() const
6779
{

src/scratch/sound_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace libscratchcpp
1111
{
1212

13+
class Target;
14+
1315
struct SoundPrivate
1416
{
1517
SoundPrivate();
@@ -19,6 +21,7 @@ struct SoundPrivate
1921
int sampleCount = 0;
2022
static IAudioOutput *audioOutput;
2123
std::shared_ptr<IAudioPlayer> player = nullptr;
24+
Target *target = nullptr;
2225
};
2326

2427
} // namespace libscratchcpp

src/scratch/target.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,10 @@ int Target::addSound(std::shared_ptr<Sound> sound)
344344

345345
assert(sound);
346346

347-
if (sound)
347+
if (sound) {
348+
sound->setTarget(this);
348349
sound->setVolume(impl->volume);
350+
}
349351

350352
impl->sounds.push_back(sound);
351353
return impl->sounds.size() - 1;

test/assets/sound_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <scratchcpp/sound.h>
2+
#include <scratchcpp/target.h>
23
#include <scratch/sound_p.h>
34
#include <audiooutputmock.h>
45
#include <audioplayermock.h>
@@ -106,6 +107,16 @@ TEST_F(SoundTest, IsPlaying)
106107
SoundPrivate::audioOutput = nullptr;
107108
}
108109

110+
TEST_F(SoundTest, Target)
111+
{
112+
Sound sound("sound1", "a", "wav");
113+
ASSERT_EQ(sound.target(), nullptr);
114+
115+
Target target;
116+
sound.setTarget(&target);
117+
ASSERT_EQ(sound.target(), &target);
118+
}
119+
109120
TEST_F(SoundTest, Clone)
110121
{
111122
Sound sound("sound1", "a", "wav");

test/scratch_classes/target_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ TEST(TargetTest, Sounds)
442442
ASSERT_EQ(target.findSound("sound2"), 1);
443443
ASSERT_EQ(target.findSound("sound3"), 2);
444444

445+
ASSERT_EQ(s1->target(), &target);
446+
ASSERT_EQ(s2->target(), &target);
447+
ASSERT_EQ(s3->target(), &target);
448+
445449
SoundPrivate::audioOutput = nullptr;
446450
}
447451

0 commit comments

Comments
 (0)