Heart/src/public/asyncrenderloop.cpp

61 lines
1.3 KiB
C++
Raw Normal View History

2025-01-20 10:04:23 +01:00
/*
* 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) {
}
2025-01-20 18:22:33 +01:00
AsyncRenderLoop::~AsyncRenderLoop() {
2025-01-26 22:04:13 +01:00
AsyncRenderLoop::stop();
2025-01-20 18:22:33 +01:00
}
2025-01-20 10:04:23 +01:00
void QH::AsyncRenderLoop::run() {
2025-01-28 15:46:52 +01:00
if (auto && thrd = thread()) {
m_run = true;
thrd->start();
asyncLauncher([this](){
renderLoopPrivate();
return true;
});
}
2025-01-20 10:04:23 +01:00
}
void QH::AsyncRenderLoop::stop() {
m_run = false;
2025-01-26 22:04:13 +01:00
thread()->quit();
2025-01-20 10:04:23 +01:00
thread()->wait();
}
bool AsyncRenderLoop::isRun() const {
2025-01-28 15:46:52 +01:00
return m_run && (thread() && thread()->isRunning());
2025-01-20 10:04:23 +01:00
}
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