asyncrenderloop added into library

This commit is contained in:
Andrei Yankovich 2025-01-20 10:04:23 +01:00
parent aa44c26eaa
commit 3dd8c6cc88
3 changed files with 111 additions and 1 deletions

View File

@ -10,7 +10,6 @@
#include <QObject>
#include <functional>
#include "config.h"
#include "atomicmetatypes.h"
#include "heart_global.h"
namespace QH {

View File

@ -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 <QDateTime>
#include <QThread>
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

View File

@ -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