Skip to content

Commit

Permalink
ProjectLoader: Add renderFps property
Browse files Browse the repository at this point in the history
  • Loading branch information
adazem009 committed Oct 25, 2024
1 parent a9268ed commit b940173
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/projectloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ProjectLoader::ProjectLoader(QObject *parent) :
connect(qApp, &QCoreApplication::aboutToQuit, this, &ProjectLoader::clear);

initTimer();
m_renderTimer.start();

// Register pen blocks
ScratchConfiguration::registerExtension(std::make_shared<PenBlocks>());
Expand Down Expand Up @@ -97,6 +98,11 @@ bool ProjectLoader::running() const
return m_running;
}

int ProjectLoader::renderFps() const
{
return m_renderFps;
}

IEngine *ProjectLoader::engine() const
{
if (m_loadThread.isRunning())
Expand Down Expand Up @@ -202,6 +208,15 @@ void ProjectLoader::timerEvent(QTimerEvent *event)
m_running = !m_running;
emit runningChanged();
}

// FPS counter
if (m_renderTimer.elapsed() >= 1000) {
m_renderFps = m_renderFpsCounter;
m_renderFpsCounter = 0;
emit renderFpsChanged();
m_renderTimer.restart();
} else
m_renderFpsCounter++;
}

event->accept();
Expand Down
7 changes: 7 additions & 0 deletions src/projectloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ProjectLoader : public QObject
Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
Q_PROPERTY(bool loadStatus READ loadStatus NOTIFY loadStatusChanged)
Q_PROPERTY(bool running READ running NOTIFY runningChanged)
Q_PROPERTY(int renderFps READ renderFps NOTIFY renderFpsChanged FINAL)
Q_PROPERTY(libscratchcpp::IEngine *engine READ engine NOTIFY engineChanged)
Q_PROPERTY(StageModel *stage READ stage NOTIFY stageChanged)
Q_PROPERTY(QQmlListProperty<SpriteModel> sprites READ sprites NOTIFY spritesChanged)
Expand Down Expand Up @@ -98,11 +99,14 @@ class ProjectLoader : public QObject

unsigned int assetCount() const;

int renderFps() const;

signals:
void fileNameChanged();
void loadStatusChanged();
void loadingFinished();
void runningChanged();
void renderFpsChanged();
void engineChanged();
void stageChanged();
void spritesChanged();
Expand Down Expand Up @@ -145,6 +149,9 @@ class ProjectLoader : public QObject
QFuture<void> m_loadThread;
libscratchcpp::Project m_project;
bool m_running = false;
QElapsedTimer m_renderTimer;
int m_renderFps = 0;
int m_renderFpsCounter = 0;
libscratchcpp::IEngine *m_engine = nullptr;
libscratchcpp::IEngine *m_oldEngine = nullptr;
QMutex m_engineMutex;
Expand Down

0 comments on commit b940173

Please sign in to comment.