Skip to content

Commit cb7a5f9

Browse files
committed
Add antialiasingEnabled property to PenLayer
1 parent b4800de commit cb7a5f9

File tree

8 files changed

+32
-4
lines changed

8 files changed

+32
-4
lines changed

src/ipenlayer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class IPenLayer : public QNanoQuickItem
2626

2727
virtual ~IPenLayer() { }
2828

29+
virtual bool antialiasingEnabled() const = 0;
30+
virtual void setAntialiasingEnabled(bool enabled) = 0;
31+
2932
virtual libscratchcpp::IEngine *engine() const = 0;
3033
virtual void setEngine(libscratchcpp::IEngine *newEngine) = 0;
3134

src/penlayer.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PenLayer::PenLayer(QNanoQuickItem *parent) :
1212
IPenLayer(parent)
1313
{
1414
m_fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
15-
m_fboFormat.setSamples(4);
15+
m_fboFormat.setSamples(m_antialiasingEnabled ? 4 : 0);
1616
}
1717

1818
PenLayer::~PenLayer()
@@ -24,6 +24,17 @@ PenLayer::~PenLayer()
2424
m_painter->end();
2525
}
2626

27+
bool PenLayer::antialiasingEnabled() const
28+
{
29+
return m_antialiasingEnabled;
30+
}
31+
32+
void PenLayer::setAntialiasingEnabled(bool enabled)
33+
{
34+
m_antialiasingEnabled = enabled;
35+
m_fboFormat.setSamples(enabled ? 4 : 0);
36+
}
37+
2738
libscratchcpp::IEngine *PenLayer::engine() const
2839
{
2940
return m_engine;
@@ -81,7 +92,7 @@ void scratchcpprender::PenLayer::drawLine(const PenAttributes &penAttributes, do
8192
// Begin painting
8293
m_fbo->bind();
8394
m_painter->beginNativePainting();
84-
m_painter->setRenderHint(QPainter::Antialiasing);
95+
m_painter->setRenderHint(QPainter::Antialiasing, m_antialiasingEnabled);
8596
m_painter->setRenderHint(QPainter::SmoothPixmapTransform, false);
8697

8798
// Translate to Scratch coordinate system

src/penlayer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class PenLayer : public IPenLayer
2020
PenLayer(QNanoQuickItem *parent = nullptr);
2121
~PenLayer();
2222

23+
bool antialiasingEnabled() const override;
24+
void setAntialiasingEnabled(bool enabled) override;
25+
2326
libscratchcpp::IEngine *engine() const override;
2427
void setEngine(libscratchcpp::IEngine *newEngine) override;
2528

@@ -39,6 +42,7 @@ class PenLayer : public IPenLayer
3942

4043
private:
4144
static std::unordered_map<libscratchcpp::IEngine *, IPenLayer *> m_projectPenLayers;
45+
bool m_antialiasingEnabled = true;
4246
libscratchcpp::IEngine *m_engine = nullptr;
4347
std::unique_ptr<QOpenGLFramebufferObject> m_fbo;
4448
std::unique_ptr<QOpenGLPaintDevice> m_paintDevice;

test/lines.png

-4.86 KB
Loading

test/lines_old.png

2.07 KB
Loading

test/mocks/penlayermock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace scratchcpprender
1212
class PenLayerMock : public IPenLayer
1313
{
1414
public:
15+
MOCK_METHOD(bool, antialiasingEnabled, (), (const, override));
16+
MOCK_METHOD(void, setAntialiasingEnabled, (bool), (override));
17+
1518
MOCK_METHOD(libscratchcpp::IEngine *, engine, (), (const, override));
1619
MOCK_METHOD(void, setEngine, (libscratchcpp::IEngine *), (override));
1720

test/penlayer/penlayer_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ TEST_F(PenLayerTest, Engine)
6969
TEST_F(PenLayerTest, FramebufferObject)
7070
{
7171
PenLayer penLayer;
72+
ASSERT_TRUE(penLayer.antialiasingEnabled());
73+
7274
EngineMock engine1, engine2;
7375
EXPECT_CALL(engine1, stageWidth()).WillOnce(Return(480));
7476
EXPECT_CALL(engine1, stageHeight()).WillOnce(Return(360));
@@ -80,6 +82,9 @@ TEST_F(PenLayerTest, FramebufferObject)
8082
ASSERT_EQ(fbo->format().attachment(), QOpenGLFramebufferObject::CombinedDepthStencil);
8183
ASSERT_EQ(fbo->format().samples(), 4);
8284

85+
penLayer.setAntialiasingEnabled(false);
86+
ASSERT_FALSE(penLayer.antialiasingEnabled());
87+
8388
EXPECT_CALL(engine2, stageWidth()).WillOnce(Return(500));
8489
EXPECT_CALL(engine2, stageHeight()).WillOnce(Return(400));
8590
penLayer.setEngine(&engine2);
@@ -88,7 +93,7 @@ TEST_F(PenLayerTest, FramebufferObject)
8893
ASSERT_EQ(fbo->width(), 500);
8994
ASSERT_EQ(fbo->height(), 400);
9095
ASSERT_EQ(fbo->format().attachment(), QOpenGLFramebufferObject::CombinedDepthStencil);
91-
ASSERT_EQ(fbo->format().samples(), 4);
96+
ASSERT_EQ(fbo->format().samples(), 0);
9297
}
9398

9499
TEST_F(PenLayerTest, GetProjectPenLayer)
@@ -161,6 +166,7 @@ TEST_F(PenLayerTest, Clear)
161166
TEST_F(PenLayerTest, DrawPoint)
162167
{
163168
PenLayer penLayer;
169+
penLayer.setAntialiasingEnabled(false);
164170
EngineMock engine;
165171
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
166172
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
@@ -207,6 +213,7 @@ TEST_F(PenLayerTest, DrawPoint)
207213
TEST_F(PenLayerTest, DrawLine)
208214
{
209215
PenLayer penLayer;
216+
penLayer.setAntialiasingEnabled(false);
210217
EngineMock engine;
211218
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
212219
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
@@ -238,7 +245,7 @@ TEST_F(PenLayerTest, DrawLine)
238245
penLayer.drawLine(attr, -54, 21, 88, -6);
239246

240247
QOpenGLFramebufferObject *fbo = penLayer.framebufferObject();
241-
QImage image = fbo->toImage();
248+
QImage image = fbo->toImage().scaled(240, 180);
242249
QBuffer buffer;
243250
image.save(&buffer, "png");
244251
QFile ref("lines.png");

test/points.png

-599 Bytes
Loading

0 commit comments

Comments
 (0)