From 3dd8c6cc8881134083bc61fa27c9013bb00ef76b Mon Sep 17 00:00:00 2001 From: EndrII Date: Mon, 20 Jan 2025 10:04:23 +0100 Subject: [PATCH] asyncrenderloop added into library --- src/public/async.h | 1 - src/public/asyncrenderloop.cpp | 51 +++++++++++++++++++++++++++++ src/public/asyncrenderloop.h | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/public/asyncrenderloop.cpp create mode 100644 src/public/asyncrenderloop.h diff --git a/src/public/async.h b/src/public/async.h index 5aa7d30..a306589 100644 --- a/src/public/async.h +++ b/src/public/async.h @@ -10,7 +10,6 @@ #include #include #include "config.h" -#include "atomicmetatypes.h" #include "heart_global.h" namespace QH { diff --git a/src/public/asyncrenderloop.cpp b/src/public/asyncrenderloop.cpp new file mode 100644 index 0000000..d8dd2b9 --- /dev/null +++ b/src/public/asyncrenderloop.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2025-2025 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#include "asyncrenderloop.h" +#include +#include + +namespace QH { + +AsyncRenderLoop::AsyncRenderLoop(QThread *thread, QObject *ptr): Async(thread, ptr) { + +} + +void QH::AsyncRenderLoop::run() { + m_run = true; + asyncLauncher([this](){ + renderLoopPrivate(); + return true; + }); +} + +void QH::AsyncRenderLoop::stop() { + m_run = false; + thread()->exit(); + thread()->wait(); +} + +bool AsyncRenderLoop::isRun() const { + return m_run || (thread() && thread()->isRunning()); +} + +void QH::AsyncRenderLoop::renderLoopPrivate() { + quint64 currentTime = QDateTime::currentMSecsSinceEpoch(); + + _lastIterationTime = currentTime; + int iterationTime = 0; + + while (m_run) { + renderIteration(iterationTime); + + currentTime = QDateTime::currentMSecsSinceEpoch(); + iterationTime = currentTime - _lastIterationTime; + _lastIterationTime = currentTime; + } +} + +} // namespace QH diff --git a/src/public/asyncrenderloop.h b/src/public/asyncrenderloop.h new file mode 100644 index 0000000..d2e3616 --- /dev/null +++ b/src/public/asyncrenderloop.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2025-2025 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#ifndef ASYNCRENDERLOOP_H +#define ASYNCRENDERLOOP_H + +#include "async.h" + +namespace QH { + +/** + * @brief The AsyncRenderLoop class is a class for rendering the world. + */ +class HEARTSHARED_EXPORT AsyncRenderLoop: public Async +{ + Q_OBJECT +public: + AsyncRenderLoop(QThread* thread, QObject* ptr = nullptr); + + /** + * @brief run This method starts the render loop. + */ + virtual void run(); + + /** + * @brief stop This method stops the render loop. + */ + virtual void stop(); + + /** + * @brief isRun This method returns the state of the render loop. + * @return true if the render loop is running, else false. + */ + bool isRun() const; + +protected: + + /** + * @brief renderIteration This method is called in each iteration of the render loop. + * This method must be implemented in the derived class. + * @param msec time in milliseconds from the last iteration. + * @see stop + * @see run + */ + virtual void renderIteration(int msec) = 0; + +private slots: + void renderLoopPrivate(); + +private: + bool m_run = false; + quint64 _lastIterationTime = 0; + +}; +} +#endif // ASYNCRENDERLOOP_H