Skip to content

Commit

Permalink
Move target property from Sound to Asset
Browse files Browse the repository at this point in the history
Resolves: #540
  • Loading branch information
adazem009 committed Jan 13, 2025
1 parent 37f9604 commit acff91c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 30 deletions.
4 changes: 4 additions & 0 deletions include/scratchcpp/asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace libscratchcpp
{

class Target;
class AssetPrivate;

/*! \brief The Asset class represents a Scratch asset, for example a Costume or a Sound. */
Expand All @@ -32,6 +33,9 @@ class LIBSCRATCHCPP_EXPORT Asset : public Entity
unsigned int dataSize() const;
void setData(unsigned int size, void *data);

Target *target() const;
void setTarget(Target *target);

protected:
virtual void processData(unsigned int size, void *data) { }

Expand Down
4 changes: 0 additions & 4 deletions include/scratchcpp/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace libscratchcpp
{

class Target;
class SoundPrivate;

/*! \brief The Sound class represents a Scratch sound. */
Expand Down Expand Up @@ -40,9 +39,6 @@ class LIBSCRATCHCPP_EXPORT Sound : public Asset

virtual bool isPlaying() const;

Target *target() const;
void setTarget(Target *target);

std::shared_ptr<Sound> clone() const;

protected:
Expand Down
12 changes: 12 additions & 0 deletions src/scratch/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ void Asset::setData(unsigned int size, void *data)
impl->data = data;
processData(size, data);
}

/*! Returns the sprite or stage this asset belongs to. */
Target *Asset::target() const
{
return impl->target;
}

/*! Sets the sprite or stage this asset belongs to. */
void Asset::setTarget(Target *target)
{
impl->target = target;
}
3 changes: 3 additions & 0 deletions src/scratch/asset_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace libscratchcpp
{

class Target;

struct AssetPrivate
{
AssetPrivate(const std::string &name, const std::string &format);
Expand All @@ -19,6 +21,7 @@ struct AssetPrivate
std::string fileName;
const void *data = nullptr;
unsigned int dataSize = 0;
Target *target = nullptr;
};

} // namespace libscratchcpp
18 changes: 4 additions & 14 deletions src/scratch/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ bool Sound::isPlaying() const
return impl->player->isPlaying();
}

/*! Returns the sprite or stage this variable belongs to. */
Target *Sound::target() const
{
return impl->target;
}

/*! Sets the sprite or stage this variable belongs to. */
void Sound::setTarget(Target *target)
{
impl->target = target;
}

/*! Returns an independent copy of the sound which is valid for as long as the original sound exists. */
std::shared_ptr<Sound> Sound::clone() const
{
Expand Down Expand Up @@ -130,8 +118,10 @@ void Sound::processData(unsigned int size, void *data)

void Sound::stopCloneSounds()
{
if (impl->target && !impl->target->isStage()) {
Sprite *sprite = static_cast<Sprite *>(impl->target);
Target *target = this->target();

if (target && !target->isStage()) {
Sprite *sprite = static_cast<Sprite *>(target);

if (sprite->isClone())
sprite = sprite->cloneSprite();
Expand Down
2 changes: 0 additions & 2 deletions src/scratch/sound_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace libscratchcpp
{

class Target;
class Sound;

struct SoundPrivate
Expand All @@ -22,7 +21,6 @@ struct SoundPrivate
int sampleCount = 0;
static IAudioOutput *audioOutput;
std::shared_ptr<IAudioPlayer> player = nullptr;
Target *target = nullptr;
const Sound *cloneRoot = nullptr;
};

Expand Down
11 changes: 11 additions & 0 deletions test/assets/asset_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <scratchcpp/asset.h>
#include <scratchcpp/target.h>

#include "../common.h"
#include "testasset.h"
Expand Down Expand Up @@ -35,3 +36,13 @@ TEST(AssetTest, Data)
ASSERT_EQ(asset.processedData, data);
ASSERT_EQ(asset.callCount, 1);
}

TEST(AssetTest, Target)
{
Asset asset("sound1", "a", "wav");
ASSERT_EQ(asset.target(), nullptr);

Target target;
asset.setTarget(&target);
ASSERT_EQ(asset.target(), &target);
}
10 changes: 0 additions & 10 deletions test/assets/sound_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,6 @@ TEST_F(SoundTest, IsPlaying)
SoundPrivate::audioOutput = nullptr;
}

TEST_F(SoundTest, Target)
{
Sound sound("sound1", "a", "wav");
ASSERT_EQ(sound.target(), nullptr);

Target target;
sound.setTarget(&target);
ASSERT_EQ(sound.target(), &target);
}

TEST_F(SoundTest, Clone)
{
auto sound = std::make_shared<Sound>("sound1", "a", "wav");
Expand Down

0 comments on commit acff91c

Please sign in to comment.