File tree Expand file tree Collapse file tree 6 files changed +37
-1
lines changed
Expand file tree Collapse file tree 6 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 99namespace libscratchcpp
1010{
1111
12+ class Target ;
1213class 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:
Original file line number Diff line number Diff 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. */
6678std::shared_ptr<Sound> Sound::clone () const
6779{
Original file line number Diff line number Diff line change 1010namespace libscratchcpp
1111{
1212
13+ class Target ;
14+
1315struct 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
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 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+
109120TEST_F (SoundTest, Clone)
110121{
111122 Sound sound (" sound1" , " a" , " wav" );
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments