@@ -11,8 +11,6 @@ std::unordered_map<libscratchcpp::IEngine *, IPenLayer *> PenLayer::m_projectPen
1111PenLayer::PenLayer (QNanoQuickItem *parent) :
1212 IPenLayer(parent)
1313{
14- m_fboFormat.setAttachment (QOpenGLFramebufferObject::CombinedDepthStencil);
15- m_fboFormat.setSamples (m_antialiasingEnabled ? 4 : 0 );
1614 setSmooth (false );
1715}
1816
@@ -30,7 +28,6 @@ bool PenLayer::antialiasingEnabled() const
3028void PenLayer::setAntialiasingEnabled (bool enabled)
3129{
3230 m_antialiasingEnabled = enabled;
33- m_fboFormat.setSamples (enabled ? 4 : 0 );
3431}
3532
3633libscratchcpp::IEngine *PenLayer::engine () const
@@ -50,10 +47,15 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
5047
5148 if (m_engine && QOpenGLContext::currentContext ()) {
5249 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);
5453 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>();
5558
56- m_paintDevice = std::make_unique<QOpenGLPaintDevice>(m_fbo->size ());
5759 clear ();
5860 }
5961
@@ -89,15 +91,13 @@ void scratchcpprender::PenLayer::drawPoint(const PenAttributes &penAttributes, d
8991
9092void scratchcpprender::PenLayer::drawLine (const PenAttributes &penAttributes, double x0, double y0, double x1, double y1)
9193{
92- if (!m_fbo || !m_paintDevice || !m_engine)
94+ if (!m_fbo || !m_painter || !m_engine)
9395 return ;
9496
9597 // Begin painting
9698 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 ());
101101
102102 // Translate to Scratch coordinate system
103103 double stageWidthHalf = m_engine->stageWidth () / 2 ;
@@ -108,20 +108,29 @@ void scratchcpprender::PenLayer::drawLine(const PenAttributes &penAttributes, do
108108 y1 = stageHeightHalf - y1;
109109
110110 // 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 ;
115121
116122 // 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+ }
121131
122132 // End painting
123- painter.endNativePainting ();
124- painter.end ();
133+ m_painter->endFrame ();
125134 m_fbo->release ();
126135
127136 m_textureDirty = true ;
@@ -239,13 +248,4 @@ void PenLayer::updateTexture()
239248
240249 m_textureDirty = false ;
241250 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 ());
251251}
0 commit comments