@@ -11,8 +11,6 @@ std::unordered_map<libscratchcpp::IEngine *, IPenLayer *> PenLayer::m_projectPen
11
11
PenLayer::PenLayer (QNanoQuickItem *parent) :
12
12
IPenLayer(parent)
13
13
{
14
- m_fboFormat.setAttachment (QOpenGLFramebufferObject::CombinedDepthStencil);
15
- m_fboFormat.setSamples (m_antialiasingEnabled ? 4 : 0 );
16
14
setSmooth (false );
17
15
}
18
16
@@ -30,7 +28,6 @@ bool PenLayer::antialiasingEnabled() const
30
28
void PenLayer::setAntialiasingEnabled (bool enabled)
31
29
{
32
30
m_antialiasingEnabled = enabled;
33
- m_fboFormat.setSamples (enabled ? 4 : 0 );
34
31
}
35
32
36
33
libscratchcpp::IEngine *PenLayer::engine () const
@@ -50,10 +47,15 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
50
47
51
48
if (m_engine && QOpenGLContext::currentContext ()) {
52
49
m_projectPenLayers[m_engine] = this ;
53
- m_fbo = std::make_unique<QOpenGLFramebufferObject>(m_engine->stageWidth (), m_engine->stageHeight (), m_fboFormat);
50
+ QOpenGLFramebufferObjectFormat fboFormat;
51
+ fboFormat.setAttachment (QOpenGLFramebufferObject::CombinedDepthStencil);
52
+ m_fbo = std::make_unique<QOpenGLFramebufferObject>(m_engine->stageWidth (), m_engine->stageHeight (), fboFormat);
54
53
Q_ASSERT (m_fbo->isValid ());
54
+ m_texture = Texture (m_fbo->texture (), m_fbo->size ());
55
+
56
+ if (!m_painter)
57
+ m_painter = std::make_unique<QNanoPainter>();
55
58
56
- m_paintDevice = std::make_unique<QOpenGLPaintDevice>(m_fbo->size ());
57
59
clear ();
58
60
}
59
61
@@ -89,15 +91,13 @@ void scratchcpprender::PenLayer::drawPoint(const PenAttributes &penAttributes, d
89
91
90
92
void scratchcpprender::PenLayer::drawLine (const PenAttributes &penAttributes, double x0, double y0, double x1, double y1)
91
93
{
92
- if (!m_fbo || !m_paintDevice || !m_engine)
94
+ if (!m_fbo || !m_painter || !m_engine)
93
95
return ;
94
96
95
97
// Begin painting
96
98
m_fbo->bind ();
97
- QPainter painter (m_paintDevice.get ());
98
- painter.beginNativePainting ();
99
- painter.setRenderHint (QPainter::Antialiasing, m_antialiasingEnabled);
100
- painter.setRenderHint (QPainter::SmoothPixmapTransform, false );
99
+
100
+ m_painter->beginFrame (m_fbo->width (), m_fbo->height ());
101
101
102
102
// Translate to Scratch coordinate system
103
103
double stageWidthHalf = m_engine->stageWidth () / 2 ;
@@ -108,20 +108,29 @@ void scratchcpprender::PenLayer::drawLine(const PenAttributes &penAttributes, do
108
108
y1 = stageHeightHalf - y1 ;
109
109
110
110
// Set pen attributes
111
- QPen pen (penAttributes.color );
112
- pen.setWidthF (penAttributes.diameter );
113
- pen.setCapStyle (Qt::RoundCap);
114
- painter.setPen (pen);
111
+ m_painter->setLineWidth (penAttributes.diameter );
112
+ m_painter->setStrokeStyle (penAttributes.color );
113
+ m_painter->setFillStyle (penAttributes.color );
114
+ m_painter->setLineJoin (QNanoPainter::JOIN_ROUND);
115
+ m_painter->setLineCap (QNanoPainter::CAP_ROUND);
116
+ m_painter->setAntialias (m_antialiasingEnabled ? 1 .0f : 0 .0f );
117
+ m_painter->beginPath ();
118
+
119
+ // Width 1 and 3 lines need to be offset by 0.5
120
+ const double offset = (std::fmod (std::max (4 - penAttributes.diameter , 0.0 ), 2 )) / 2 ;
115
121
116
122
// If the start and end coordinates are the same, draw a point, otherwise draw a line
117
- if (x0 == x1 && y0 == y1 )
118
- painter.drawPoint (x0, y0 );
119
- else
120
- painter.drawLine (x0, y0 , x1, y1 );
123
+ if (x0 == x1 && y0 == y1 ) {
124
+ m_painter->circle (x0 + offset, y0 + offset, penAttributes.diameter / 2 );
125
+ m_painter->fill ();
126
+ } else {
127
+ m_painter->moveTo (x0 + offset, y0 + offset);
128
+ m_painter->lineTo (x1 + offset, y1 + offset);
129
+ m_painter->stroke ();
130
+ }
121
131
122
132
// End painting
123
- painter.endNativePainting ();
124
- painter.end ();
133
+ m_painter->endFrame ();
125
134
m_fbo->release ();
126
135
127
136
m_textureDirty = true ;
@@ -239,13 +248,4 @@ void PenLayer::updateTexture()
239
248
240
249
m_textureDirty = false ;
241
250
m_textureManager.removeTexture (m_texture);
242
-
243
- if (!m_resolvedFbo || m_resolvedFbo->size () != m_fbo->size ()) {
244
- QOpenGLFramebufferObjectFormat format;
245
- format.setAttachment (QOpenGLFramebufferObject::CombinedDepthStencil);
246
- m_resolvedFbo = std::make_unique<QOpenGLFramebufferObject>(m_fbo->size (), format);
247
- }
248
-
249
- QOpenGLFramebufferObject::blitFramebuffer (m_resolvedFbo.get (), m_fbo.get ());
250
- m_texture = Texture (m_resolvedFbo->texture (), m_resolvedFbo->size ());
251
251
}
0 commit comments