Skip to content

Commit 894d9ce

Browse files
authored
Merge pull request #163 from scratchcpp/hq_pen_fixes
Fix HQ pen issues
2 parents 47e7aad + a734c95 commit 894d9ce

File tree

8 files changed

+66
-7
lines changed

8 files changed

+66
-7
lines changed

src/penlayer.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
6868

6969
m_engine = newEngine;
7070

71-
if (m_engine && QOpenGLContext::currentContext()) {
71+
if (!m_glCtx) {
72+
m_glCtx = QOpenGLContext::currentContext();
73+
74+
if (m_glCtx)
75+
m_surface = m_glCtx->surface();
76+
}
77+
78+
if (m_engine && m_glCtx) {
7279
m_projectPenLayers[m_engine] = this;
73-
createFbo();
7480

7581
if (!m_painter)
7682
m_painter = std::make_unique<QNanoPainter>();
@@ -80,6 +86,8 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
8086
m_glF->initializeOpenGLFunctions();
8187
}
8288

89+
createFbo();
90+
8391
if (m_vao == 0) {
8492
// Set up VBO and VAO
8593
float vertices[] = { -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f };
@@ -124,8 +132,8 @@ void PenLayer::setHqPen(bool newHqPen)
124132
return;
125133

126134
m_hqPen = newHqPen;
127-
createFbo();
128135
emit hqPenChanged();
136+
createFbo();
129137
}
130138

131139
void scratchcpprender::PenLayer::clear()
@@ -427,6 +435,10 @@ void PenLayer::addPenLayer(libscratchcpp::IEngine *engine, IPenLayer *penLayer)
427435

428436
QNanoQuickItemPainter *PenLayer::createItemPainter() const
429437
{
438+
m_glCtx = QOpenGLContext::currentContext();
439+
Q_ASSERT(m_glCtx);
440+
m_surface = m_glCtx->surface();
441+
Q_ASSERT(m_surface);
430442
return new PenLayerPainter;
431443
}
432444

@@ -440,9 +452,17 @@ void PenLayer::geometryChange(const QRectF &newGeometry, const QRectF &oldGeomet
440452

441453
void PenLayer::createFbo()
442454
{
443-
if (!QOpenGLContext::currentContext() || !m_engine)
455+
if (!m_glCtx || !m_surface || !m_engine || !m_glF)
444456
return;
445457

458+
QOpenGLContext *oldCtx = QOpenGLContext::currentContext();
459+
QSurface *oldSurface = oldCtx->surface();
460+
461+
if (oldCtx != m_glCtx) {
462+
oldCtx->doneCurrent();
463+
m_glCtx->makeCurrent(m_surface);
464+
}
465+
446466
QOpenGLFramebufferObjectFormat fboFormat;
447467
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
448468

@@ -455,6 +475,11 @@ void PenLayer::createFbo()
455475
m_fbo.reset(newFbo);
456476
m_texture = Texture(m_fbo->texture(), m_fbo->size());
457477
m_scale = width() / m_engine->stageWidth();
478+
479+
if (oldCtx != m_glCtx) {
480+
m_glCtx->doneCurrent();
481+
oldCtx->makeCurrent(oldSurface);
482+
}
458483
}
459484

460485
void PenLayer::updateTexture()

src/penlayer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class PenLayer : public IPenLayer
6565
libscratchcpp::IEngine *m_engine = nullptr;
6666
bool m_hqPen = false;
6767
std::unique_ptr<QOpenGLFramebufferObject> m_fbo;
68+
mutable QOpenGLContext *m_glCtx = nullptr;
69+
mutable QSurface *m_surface = nullptr;
6870
double m_scale = 1;
6971
std::unique_ptr<QNanoPainter> m_painter;
7072
std::unique_ptr<QOpenGLExtraFunctions> m_glF;

src/projectloader.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,24 @@ void ProjectLoader::timerEvent(QTimerEvent *event)
210210
return;
211211

212212
if (m_engine) {
213+
QOpenGLContext *oldCtx = QOpenGLContext::currentContext();
214+
QSurface *oldSurface = nullptr;
215+
216+
if (!m_glCtx)
217+
m_glCtx = oldCtx;
218+
219+
if (m_glCtx) {
220+
if (!m_surface)
221+
m_surface = m_glCtx->surface();
222+
223+
oldSurface = oldCtx->surface();
224+
225+
if (oldCtx != m_glCtx) {
226+
oldCtx->doneCurrent();
227+
m_glCtx->makeCurrent(m_surface);
228+
}
229+
}
230+
213231
for (Monitor *monitor : m_unpositionedMonitors)
214232
monitor->autoPosition(m_engine->monitors());
215233

@@ -230,6 +248,13 @@ void ProjectLoader::timerEvent(QTimerEvent *event)
230248
m_renderTimer.restart();
231249
} else
232250
m_renderFpsCounter++;
251+
252+
if (m_glCtx) {
253+
if (oldCtx != m_glCtx) {
254+
m_glCtx->doneCurrent();
255+
oldCtx->makeCurrent(oldSurface);
256+
}
257+
}
233258
}
234259

235260
event->accept();

src/projectloader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
Q_MOC_INCLUDE("spritemodel.h");
1313
Q_MOC_INCLUDE("monitormodel.h");
1414

15+
class QSurface;
16+
class QOpenGLContext;
17+
1518
namespace scratchcpprender
1619
{
1720

@@ -185,6 +188,8 @@ class ProjectLoader : public QObject
185188
std::atomic<unsigned int> m_downloadedAssets = 0;
186189
std::atomic<unsigned int> m_assetCount = 0;
187190
std::atomic<bool> m_stopLoading = false;
191+
QOpenGLContext *m_glCtx = nullptr;
192+
QSurface *m_surface = nullptr;
188193
};
189194

190195
} // namespace scratchcpprender

test/lines.png

-208 Bytes
Loading

test/lines_hq.png

-998 Bytes
Loading

test/penlayer/penlayer_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,11 @@ TEST_F(PenLayerTest, DrawLine)
315315
penLayer.drawLine(attr, 130, 77, 125, -22);
316316

317317
attr.color = QNanoColor(0, 128, 0, 128);
318-
attr.diameter = 10;
318+
attr.diameter = 225;
319+
320+
penLayer.drawLine(attr, -225, 25, -175, -25);
319321

320-
penLayer.drawLine(attr, 152, -158, -228, 145);
322+
attr.diameter = 10;
321323
penLayer.drawLine(attr, -100, 139, 20, 72);
322324

323325
attr.color = QNanoColor(255, 50, 200, 185);

thirdparty/libqnanopainter/nanovg/nanovg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@ void nvgStroke(NVGcontext* ctx)
23452345
{
23462346
NVGstate* state = nvg__getState(ctx);
23472347
float scale = nvg__getAverageScale(state->xform);
2348-
float strokeWidth = nvg__clampf(state->strokeWidth * scale, 0.0f, 200.0f);
2348+
float strokeWidth = state->strokeWidth * scale;
23492349
NVGpaint strokePaint = state->stroke;
23502350
const NVGpath* path;
23512351
int i;

0 commit comments

Comments
 (0)