Skip to content

Commit

Permalink
Add costume mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
adazem009 committed Nov 27, 2023
1 parent 302cd32 commit 9f937b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ScratchCPPGui/renderedtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void RenderedTarget::loadCostume(Costume *costume)
if (!costume)
return;

m_costumeMutex.lock();
Target *target = scratchTarget();
m_costume = costume;
m_imageChanged = true;
Expand All @@ -104,6 +105,8 @@ void RenderedTarget::loadCostume(Costume *costume)
QSize size = reader.size();
calculateSize(target, size.width(), size.height());
}

m_costumeMutex.unlock();
}

void RenderedTarget::updateProperties()
Expand Down Expand Up @@ -223,6 +226,16 @@ const QString &RenderedTarget::bitmapUniqueKey() const
return m_bitmapUniqueKey;
}

void RenderedTarget::lockCostume()
{
m_costumeMutex.lock();
}

void RenderedTarget::unlockCostume()
{
m_costumeMutex.unlock();
}

bool RenderedTarget::mirrorHorizontally() const
{
return m_mirrorHorizontally;
Expand Down
4 changes: 4 additions & 0 deletions ScratchCPPGui/renderedtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class RenderedTarget : public QNanoQuickItem
QBuffer *bitmapBuffer();
const QString &bitmapUniqueKey() const;

void lockCostume();
void unlockCostume();

bool mirrorHorizontally() const;

signals:
Expand All @@ -77,6 +80,7 @@ class RenderedTarget : public QNanoQuickItem
unsigned char *m_svgBitmap = nullptr;
QBuffer m_bitmapBuffer;
QString m_bitmapUniqueKey;
QMutex m_costumeMutex;
QMutex mutex;
bool m_imageChanged = false;
bool m_visible = true;
Expand Down
3 changes: 3 additions & 0 deletions ScratchCPPGui/targetpainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ TargetPainter::~TargetPainter()

void TargetPainter::paint(QNanoPainter *painter)
{
m_target->lockCostume();
unsigned char *svgBitmap = m_target->svgBitmap();
double width = m_target->width();
double height = m_target->height();
Expand All @@ -43,6 +44,8 @@ void TargetPainter::paint(QNanoPainter *painter)
QNanoImage image = QNanoImage::fromCache(painter, m_target->bitmapBuffer(), m_target->bitmapUniqueKey());
painter->drawImage(image, 0, 0, width, height);
}

m_target->unlockCostume();
}

void TargetPainter::synchronize(QNanoQuickItem *item)
Expand Down

0 comments on commit 9f937b7

Please sign in to comment.