Skip to content

Commit 7eb661b

Browse files
committed
Refactor the IGraphicsEffect interface
1 parent 8a9cf36 commit 7eb661b

File tree

9 files changed

+2
-182
lines changed

9 files changed

+2
-182
lines changed

include/scratchcpp/costume.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace libscratchcpp
1111
{
1212

1313
class Broadcast;
14-
class IGraphicsEffect;
1514
class CostumePrivate;
1615

1716
/*! \brief The Costume class represents a Scratch costume. */
@@ -41,11 +40,6 @@ class LIBSCRATCHCPP_EXPORT Costume : public Asset
4140

4241
Rgb **bitmap() const;
4342

44-
double graphicsEffectValue(IGraphicsEffect *effect) const;
45-
void setGraphicsEffectValue(IGraphicsEffect *effect, double value);
46-
47-
void clearGraphicsEffects();
48-
4943
Broadcast *broadcast();
5044

5145
protected:

include/scratchcpp/igraphicseffect.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99
namespace libscratchcpp
1010
{
1111

12-
/*! \brief The IGraphicsEffects class is an interface for implementing custom graphics effects. */
12+
/*! \brief The IGraphicsEffects class is an interface for custom graphics effects. */
1313
class LIBSCRATCHCPP_EXPORT IGraphicsEffect
1414
{
1515
public:
1616
virtual ~IGraphicsEffect() { }
1717

1818
/*! Returns the name of the graphics effect. */
1919
virtual std::string name() const = 0;
20-
21-
/*! Applies the effect on the given bitmap. */
22-
virtual void apply(Rgb **bitmap, unsigned int width, unsigned int height, double value) const = 0;
2320
};
2421

2522
} // namespace libscratchcpp

src/scratch/costume.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -107,42 +107,6 @@ Rgb **Costume::bitmap() const
107107
return impl->bitmap;
108108
}
109109

110-
/*! Returns the value of the given graphics effect. */
111-
double Costume::graphicsEffectValue(IGraphicsEffect *effect) const
112-
{
113-
auto it = impl->graphicsEffects.find(effect);
114-
115-
if (it == impl->graphicsEffects.cend())
116-
return 0;
117-
else
118-
return it->second;
119-
}
120-
121-
/*! Sets the value of the given graphics effect (this is automatically set by the sprite). */
122-
void Costume::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
123-
{
124-
auto it = impl->graphicsEffects.find(effect);
125-
bool update = ((it == impl->graphicsEffects.cend()) || (it->second != value));
126-
127-
if (value == 0)
128-
impl->graphicsEffects.erase(effect);
129-
else
130-
impl->graphicsEffects[effect] = value;
131-
132-
if (update)
133-
impl->updateImage();
134-
}
135-
136-
/*! Clears all graphics effects (this is automatically called by the sprite). */
137-
void Costume::clearGraphicsEffects()
138-
{
139-
bool update = !impl->graphicsEffects.empty();
140-
impl->graphicsEffects.clear();
141-
142-
if (update)
143-
impl->updateImage();
144-
}
145-
146110
/*!
147111
* Returns the Broadcast linked with this costume.
148112
* \note This is used by the "switch backdrop to and wait" block.

src/scratch/costume_p.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3-
#include <scratchcpp/igraphicseffect.h>
4-
53
#include "costume_p.h"
64

75
using namespace libscratchcpp;
@@ -45,9 +43,6 @@ void CostumePrivate::updateImage()
4543
for (unsigned int j = 0; j < scaledWidth; j++)
4644
bitmap[i][j] = image->colorAt(mirrorHorizontally ? (scaledWidth - 1 - j) : j, i, actualScale);
4745
}
48-
49-
for (const auto &[effect, value] : graphicsEffects)
50-
effect->apply(bitmap, scaledWidth, scaledHeight, value);
5146
}
5247

5348
void CostumePrivate::freeImage()

src/scratch/costume_p.h

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

12-
class IGraphicsEffect;
13-
1412
struct CostumePrivate
1513
{
1614
CostumePrivate();
@@ -31,7 +29,6 @@ struct CostumePrivate
3129
double oldScale = 1;
3230
double scale = 1;
3331
bool mirrorHorizontally = false;
34-
std::unordered_map<IGraphicsEffect *, double> graphicsEffects;
3532
Broadcast broadcast;
3633
};
3734

src/scratch/sprite.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,6 @@ void Sprite::setCostumeIndex(int newCostumeIndex)
233233
if (costume) {
234234
costume->setScale(impl->size / 100);
235235
costume->setMirrorHorizontally(impl->rotationStyle == RotationStyle::LeftRight);
236-
237-
for (const auto &[effect, value] : impl->graphicsEffects)
238-
costume->setGraphicsEffectValue(effect, value);
239236
}
240237

241238
if (impl->visible) {
@@ -419,11 +416,6 @@ void Sprite::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
419416
{
420417
impl->graphicsEffects[effect] = value;
421418

422-
auto costume = currentCostume();
423-
424-
if (costume)
425-
costume->setGraphicsEffectValue(effect, value);
426-
427419
if (impl->visible) {
428420
IEngine *eng = engine();
429421

@@ -437,11 +429,6 @@ void Sprite::clearGraphicsEffects()
437429
{
438430
impl->graphicsEffects.clear();
439431

440-
auto costume = currentCostume();
441-
442-
if (costume)
443-
costume->clearGraphicsEffects();
444-
445432
if (impl->visible) {
446433
IEngine *eng = engine();
447434

test/assets/costume_test.cpp

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <scratchcpp/broadcast.h>
44
#include <imageformatfactorymock.h>
55
#include <imageformatmock.h>
6-
#include <graphicseffectmock.h>
76

87
#include "../common.h"
98

@@ -325,97 +324,6 @@ TEST_F(CostumeTest, ScaledBitmap)
325324
ASSERT_EQ(bitmap[3][5], 1);
326325
}
327326

328-
TEST_F(CostumeTest, BitmapWithGraphicsEffects)
329-
{
330-
EXPECT_CALL(*m_imageFormatFactory, createInstance()).WillOnce(Return(m_imageFormat));
331-
EXPECT_CALL(*m_imageFormat, width()).Times(3).WillRepeatedly(Return(0));
332-
EXPECT_CALL(*m_imageFormat, height()).Times(3).WillRepeatedly(Return(0));
333-
Costume costume("costume1", "a", "test");
334-
335-
GraphicsEffectMock effect1, effect2;
336-
costume.setGraphicsEffectValue(&effect1, 54.12);
337-
ASSERT_EQ(costume.graphicsEffectValue(&effect1), 54.12);
338-
ASSERT_EQ(costume.graphicsEffectValue(&effect2), 0);
339-
340-
costume.setGraphicsEffectValue(&effect2, -89.03);
341-
ASSERT_EQ(costume.graphicsEffectValue(&effect1), 54.12);
342-
ASSERT_EQ(costume.graphicsEffectValue(&effect2), -89.03);
343-
344-
EXPECT_CALL(*m_imageFormat, width()).WillOnce(Return(4));
345-
EXPECT_CALL(*m_imageFormat, height()).WillOnce(Return(3));
346-
347-
EXPECT_CALL(*m_imageFormat, colorAt(0, 0, 1.5)).WillOnce(Return(5));
348-
EXPECT_CALL(*m_imageFormat, colorAt(1, 0, 1.5)).WillOnce(Return(5));
349-
EXPECT_CALL(*m_imageFormat, colorAt(2, 0, 1.5)).WillOnce(Return(3));
350-
EXPECT_CALL(*m_imageFormat, colorAt(3, 0, 1.5)).WillOnce(Return(8));
351-
EXPECT_CALL(*m_imageFormat, colorAt(4, 0, 1.5)).WillOnce(Return(8));
352-
EXPECT_CALL(*m_imageFormat, colorAt(5, 0, 1.5)).WillOnce(Return(1));
353-
354-
EXPECT_CALL(*m_imageFormat, colorAt(0, 1, 1.5)).WillOnce(Return(5));
355-
EXPECT_CALL(*m_imageFormat, colorAt(1, 1, 1.5)).WillOnce(Return(5));
356-
EXPECT_CALL(*m_imageFormat, colorAt(2, 1, 1.5)).WillOnce(Return(3));
357-
EXPECT_CALL(*m_imageFormat, colorAt(3, 1, 1.5)).WillOnce(Return(8));
358-
EXPECT_CALL(*m_imageFormat, colorAt(4, 1, 1.5)).WillOnce(Return(8));
359-
EXPECT_CALL(*m_imageFormat, colorAt(5, 1, 1.5)).WillOnce(Return(1));
360-
361-
EXPECT_CALL(*m_imageFormat, colorAt(0, 2, 1.5)).WillOnce(Return(9));
362-
EXPECT_CALL(*m_imageFormat, colorAt(1, 2, 1.5)).WillOnce(Return(9));
363-
EXPECT_CALL(*m_imageFormat, colorAt(2, 2, 1.5)).WillOnce(Return(10));
364-
EXPECT_CALL(*m_imageFormat, colorAt(3, 2, 1.5)).WillOnce(Return(5));
365-
EXPECT_CALL(*m_imageFormat, colorAt(4, 2, 1.5)).WillOnce(Return(5));
366-
EXPECT_CALL(*m_imageFormat, colorAt(5, 2, 1.5)).WillOnce(Return(8));
367-
368-
EXPECT_CALL(*m_imageFormat, colorAt(0, 3, 1.5)).WillOnce(Return(1));
369-
EXPECT_CALL(*m_imageFormat, colorAt(1, 3, 1.5)).WillOnce(Return(1));
370-
EXPECT_CALL(*m_imageFormat, colorAt(2, 3, 1.5)).WillOnce(Return(9));
371-
EXPECT_CALL(*m_imageFormat, colorAt(3, 3, 1.5)).WillOnce(Return(4));
372-
EXPECT_CALL(*m_imageFormat, colorAt(4, 3, 1.5)).WillOnce(Return(4));
373-
EXPECT_CALL(*m_imageFormat, colorAt(5, 3, 1.5)).WillOnce(Return(8));
374-
375-
Rgb **bitmapPtr1, **bitmapPtr2;
376-
EXPECT_CALL(effect1, apply(_, 6, 4, 54.12)).WillOnce(SaveArg<0>(&bitmapPtr1));
377-
EXPECT_CALL(effect2, apply(_, 6, 4, -89.03)).WillOnce(SaveArg<0>(&bitmapPtr2));
378-
costume.setScale(1.5);
379-
380-
auto bitmap = costume.bitmap();
381-
ASSERT_TRUE(bitmap);
382-
ASSERT_EQ(bitmap, bitmapPtr1);
383-
ASSERT_EQ(bitmap, bitmapPtr2);
384-
ASSERT_EQ(bitmap[0][0], 5);
385-
ASSERT_EQ(bitmap[0][1], 5);
386-
ASSERT_EQ(bitmap[0][2], 3);
387-
ASSERT_EQ(bitmap[0][3], 8);
388-
ASSERT_EQ(bitmap[0][4], 8);
389-
ASSERT_EQ(bitmap[0][5], 1);
390-
391-
ASSERT_EQ(bitmap[1][0], 5);
392-
ASSERT_EQ(bitmap[1][1], 5);
393-
ASSERT_EQ(bitmap[1][2], 3);
394-
ASSERT_EQ(bitmap[1][3], 8);
395-
ASSERT_EQ(bitmap[1][4], 8);
396-
ASSERT_EQ(bitmap[1][5], 1);
397-
398-
ASSERT_EQ(bitmap[2][0], 9);
399-
ASSERT_EQ(bitmap[2][1], 9);
400-
ASSERT_EQ(bitmap[2][2], 10);
401-
ASSERT_EQ(bitmap[2][3], 5);
402-
ASSERT_EQ(bitmap[2][4], 5);
403-
ASSERT_EQ(bitmap[2][5], 8);
404-
405-
ASSERT_EQ(bitmap[3][0], 1);
406-
ASSERT_EQ(bitmap[3][1], 1);
407-
ASSERT_EQ(bitmap[3][2], 9);
408-
ASSERT_EQ(bitmap[3][3], 4);
409-
ASSERT_EQ(bitmap[3][4], 4);
410-
ASSERT_EQ(bitmap[3][5], 8);
411-
412-
EXPECT_CALL(*m_imageFormat, width()).WillOnce(Return(0));
413-
EXPECT_CALL(*m_imageFormat, height()).WillOnce(Return(0));
414-
costume.clearGraphicsEffects();
415-
ASSERT_EQ(costume.graphicsEffectValue(&effect1), 0);
416-
ASSERT_EQ(costume.graphicsEffectValue(&effect2), 0);
417-
}
418-
419327
TEST_F(CostumeTest, Broadcast)
420328
{
421329
Costume costume("costume1", "a", "svg");

test/mocks/graphicseffectmock.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ class GraphicsEffectMock : public IGraphicsEffect
99
{
1010
public:
1111
MOCK_METHOD(std::string, name, (), (const, override));
12-
MOCK_METHOD(void, apply, (Rgb **, unsigned int, unsigned int, double), (const, override));
1312
};

test/scratch_classes/sprite_test.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -598,43 +598,22 @@ TEST(SpriteTest, KeepInFence)
598598

599599
TEST(SpriteTest, GraphicsEffects)
600600
{
601-
auto c1 = std::make_shared<Costume>("", "", "");
602-
auto c2 = std::make_shared<Costume>("", "", "");
603-
604601
Sprite sprite;
605602

606603
EngineMock engine;
607604
sprite.setEngine(&engine);
608-
EXPECT_CALL(engine, requestRedraw()).Times(6);
609-
610-
sprite.addCostume(c1);
611-
sprite.addCostume(c2);
612-
sprite.setCostumeIndex(0);
605+
EXPECT_CALL(engine, requestRedraw()).Times(3);
613606

614607
GraphicsEffectMock effect1, effect2;
615608
sprite.setGraphicsEffectValue(&effect1, 48.21);
616609
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 48.21);
617610
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), 0);
618-
ASSERT_EQ(c1->graphicsEffectValue(&effect1), 48.21);
619-
ASSERT_EQ(c1->graphicsEffectValue(&effect2), 0);
620-
621-
sprite.setCostumeIndex(1);
622-
ASSERT_EQ(c1->graphicsEffectValue(&effect1), 48.21);
623-
ASSERT_EQ(c1->graphicsEffectValue(&effect2), 0);
624611

625612
sprite.setGraphicsEffectValue(&effect2, -107.08);
626613
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 48.21);
627614
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), -107.08);
628-
ASSERT_EQ(c2->graphicsEffectValue(&effect1), 48.21);
629-
ASSERT_EQ(c2->graphicsEffectValue(&effect2), -107.08);
630-
631-
sprite.setCostumeIndex(0);
632-
ASSERT_EQ(c1->graphicsEffectValue(&effect1), 48.21);
633-
ASSERT_EQ(c1->graphicsEffectValue(&effect2), -107.08);
634615

635616
sprite.clearGraphicsEffects();
636617
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 0);
637618
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), 0);
638-
ASSERT_EQ(c1->graphicsEffectValue(&effect1), 0);
639-
ASSERT_EQ(c1->graphicsEffectValue(&effect2), 0);
640619
}

0 commit comments

Comments
 (0)