Heart/src/public/asyncrenderloop.cpp

63 lines
1.4 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 <QThread>
2025-02-18 18:30:36 +01:00
#include <qdebug.h>
2025-01-20 10:04:23 +01:00
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-02-18 18:30:36 +01:00
delete thread();
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();
2025-02-18 18:30:36 +01:00
2025-01-20 10:04:23 +01:00
}
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() {
auto&& currentTime = std::chrono::high_resolution_clock::now();
2025-01-20 10:04:23 +01:00
_lastIterationTime = currentTime;
int iterationTime = 0;
while (m_run) {
renderIteration(iterationTime);
currentTime = std::chrono::high_resolution_clock::now();
iterationTime = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - _lastIterationTime).count();
2025-01-20 10:04:23 +01:00
_lastIterationTime = currentTime;
}
}
} // namespace QH