Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ Button {
}
```

Please note that the ScratchCPP renderer only works with the basic scene graph render loop.
Qt 6 uses the threaded render loop by default, so you'll have to disable it by calling this
before constructing your application object:
```cpp
qputenv("QSG_RENDER_LOOP", "basic");
```

<p align="right">(<a href="#readme-top">back to top</a>)</p>


Expand Down
1 change: 1 addition & 0 deletions src/ProjectPlayer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ProjectScene {
else
failedToLoad();
}
onStageChanged: stage.loadCostume();
}

function start() {
Expand Down
23 changes: 12 additions & 11 deletions src/irenderedtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
#pragma once

#include <qnanoquickitem.h>
#include <scratchcpp/sprite.h>

class QBuffer;
class QNanoPainter;

namespace libscratchcpp
{

class Costume;
class IEngine;
class Target;

} // namespace libscratchcpp
class QOpenGLContext;

namespace scratchcpprender
{
Expand All @@ -33,9 +26,17 @@ class IRenderedTarget : public QNanoQuickItem

virtual ~IRenderedTarget() { }

virtual void loadProperties() = 0;
virtual void updateVisibility(bool visible) = 0;
virtual void updateX(double x) = 0;
virtual void updateY(double y) = 0;
virtual void updateSize(double size) = 0;
virtual void updateDirection(double direction) = 0;
virtual void updateRotationStyle(libscratchcpp::Sprite::RotationStyle style) = 0;
virtual void updateLayerOrder(int layerOrder) = 0;

virtual void loadCostume(libscratchcpp::Costume *costume) = 0;
virtual void updateProperties() = 0;

virtual void beforeRedraw() = 0;

virtual libscratchcpp::IEngine *engine() const = 0;
virtual void setEngine(libscratchcpp::IEngine *newEngine) = 0;
Expand Down
86 changes: 12 additions & 74 deletions src/projectloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
using namespace scratchcpprender;
using namespace libscratchcpp;

void runEventLoop(IEngine *engine)
{
engine->runEventLoop();
}

ProjectLoader::ProjectLoader(QObject *parent) :
QObject(parent)
{
Expand All @@ -33,12 +28,6 @@ ProjectLoader::ProjectLoader(QObject *parent) :
});

initTimer();

// Update refresh rate when primary screen changes
connect(qApp, &QApplication::primaryScreenChanged, this, [this]() {
killTimer(m_timerId);
initTimer();
});
}

ProjectLoader::~ProjectLoader()
Expand All @@ -48,11 +37,6 @@ ProjectLoader::~ProjectLoader()
if (m_loadThread.isRunning())
m_loadThread.waitForFinished();

if (m_engine && m_eventLoopEnabled) {
m_engine->stopEventLoop();
m_eventLoop.waitForFinished();
}

for (SpriteModel *sprite : m_sprites)
sprite->deleteLater();
}
Expand All @@ -72,11 +56,6 @@ void ProjectLoader::setFileName(const QString &newFileName)

m_fileName = newFileName;

if (m_engine) {
m_engine->stopEventLoop();
m_eventLoop.waitForFinished();
}

m_project.setScratchVersion(ScratchVersion::Scratch3);
m_project.setFileName(m_fileName.toStdString());
m_loadStatus = false;
Expand Down Expand Up @@ -164,17 +143,8 @@ void ProjectLoader::timerEvent(QTimerEvent *event)
if (m_loadThread.isRunning())
return;

auto stageRenderedTarget = m_stage.renderedTarget();

if (stageRenderedTarget)
stageRenderedTarget->updateProperties();

for (auto sprite : m_sprites) {
auto renderedTarget = sprite->renderedTarget();

if (renderedTarget)
renderedTarget->updateProperties();
}
if (m_engine)
m_engine->step();

event->accept();
}
Expand Down Expand Up @@ -213,7 +183,7 @@ void ProjectLoader::load()
m_engine->setCloneLimit(m_cloneLimit);
m_engine->setSpriteFencingEnabled(m_spriteFencing);

auto handler = std::bind(&ProjectLoader::emitTick, this);
auto handler = std::bind(&ProjectLoader::redraw, this);
m_engine->setRedrawHandler(std::function<void()>(handler));

// Load targets
Expand All @@ -240,12 +210,6 @@ void ProjectLoader::load()
return;
}

// Run event loop
m_engine->setSpriteFencingEnabled(false);

if (m_eventLoopEnabled)
m_eventLoop = QtConcurrent::run(&runEventLoop, m_engine);

m_engineMutex.unlock();

emit loadStatusChanged();
Expand All @@ -257,27 +221,24 @@ void ProjectLoader::load()

void ProjectLoader::initTimer()
{
QScreen *screen = qApp->primaryScreen();

if (screen)
m_timerId = startTimer(1000 / screen->refreshRate());
m_timerId = startTimer(1000 / m_fps);
}

void ProjectLoader::emitTick()
void ProjectLoader::redraw()
{
if (m_loadThread.isRunning())
m_loadThread.waitForFinished();

auto stageRenderedTarget = m_stage.renderedTarget();
auto stage = m_stage.renderedTarget();

if (stageRenderedTarget)
stageRenderedTarget->loadProperties();
if (stage)
stage->beforeRedraw();

for (auto sprite : m_sprites) {
auto renderedTarget = sprite->renderedTarget();

if (renderedTarget)
renderedTarget->loadProperties();
renderedTarget->beforeRedraw();
}
}

Expand All @@ -299,6 +260,9 @@ void ProjectLoader::setFps(double newFps)
} else
m_fps = newFps;

killTimer(m_timerId);
initTimer();

m_engineMutex.unlock();
emit fpsChanged();
}
Expand Down Expand Up @@ -405,32 +369,6 @@ void ProjectLoader::setSpriteFencing(bool newSpriteFencing)
emit spriteFencingChanged();
}

bool ProjectLoader::eventLoopEnabled() const
{
return m_eventLoopEnabled;
}

void ProjectLoader::setEventLoopEnabled(bool newEventLoopEnabled)
{
if (m_eventLoopEnabled == newEventLoopEnabled)
return;

m_eventLoopEnabled = newEventLoopEnabled;
m_engineMutex.lock();

if (m_engine) {
if (m_eventLoopEnabled)
m_eventLoop = QtConcurrent::run(&runEventLoop, m_engine);
else {
m_engine->stopEventLoop();
m_eventLoop.waitForFinished();
}
}

m_engineMutex.unlock();
emit eventLoopEnabledChanged();
}

unsigned int ProjectLoader::downloadedAssets() const
{
return m_downloadedAssets;
Expand Down
9 changes: 1 addition & 8 deletions src/projectloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class ProjectLoader : public QObject
Q_PROPERTY(unsigned int stageHeight READ stageHeight WRITE setStageHeight NOTIFY stageHeightChanged)
Q_PROPERTY(int cloneLimit READ cloneLimit WRITE setCloneLimit NOTIFY cloneLimitChanged)
Q_PROPERTY(bool spriteFencing READ spriteFencing WRITE setSpriteFencing NOTIFY spriteFencingChanged)
Q_PROPERTY(bool eventLoopEnabled READ eventLoopEnabled WRITE setEventLoopEnabled NOTIFY eventLoopEnabledChanged)
Q_PROPERTY(unsigned int downloadedAssets READ downloadedAssets NOTIFY downloadedAssetsChanged)
Q_PROPERTY(unsigned int assetCount READ assetCount NOTIFY assetCountChanged)

Expand Down Expand Up @@ -71,9 +70,6 @@ class ProjectLoader : public QObject
bool spriteFencing() const;
void setSpriteFencing(bool newSpriteFencing);

bool eventLoopEnabled() const;
void setEventLoopEnabled(bool newEventLoopEnabled);

unsigned int downloadedAssets() const;

unsigned int assetCount() const;
Expand All @@ -91,7 +87,6 @@ class ProjectLoader : public QObject
void stageHeightChanged();
void cloneLimitChanged();
void spriteFencingChanged();
void eventLoopEnabledChanged();
void downloadedAssetsChanged();
void assetCountChanged();

Expand All @@ -102,7 +97,7 @@ class ProjectLoader : public QObject
static void callLoad(ProjectLoader *loader);
void load();
void initTimer();
void emitTick();
void redraw();

int m_timerId = -1;
QString m_fileName;
Expand All @@ -113,14 +108,12 @@ class ProjectLoader : public QObject
bool m_loadStatus = false;
StageModel m_stage;
QList<SpriteModel *> m_sprites;
QFuture<void> m_eventLoop;
double m_fps = 30;
bool m_turboMode = false;
unsigned int m_stageWidth = 480;
unsigned int m_stageHeight = 360;
int m_cloneLimit = 300;
bool m_spriteFencing = true;
bool m_eventLoopEnabled = true;
std::atomic<unsigned int> m_downloadedAssets = 0;
std::atomic<unsigned int> m_assetCount = 0;
std::atomic<bool> m_stopLoading = false;
Expand Down
Loading