mirror of
https://github.com/QuasarApp/Heart.git
synced 2025-04-27 18:24:38 +00:00
asyncrenderloop added into library
This commit is contained in:
parent
aa44c26eaa
commit
3dd8c6cc88
@ -10,7 +10,6 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "atomicmetatypes.h"
|
|
||||||
#include "heart_global.h"
|
#include "heart_global.h"
|
||||||
|
|
||||||
namespace QH {
|
namespace QH {
|
||||||
|
51
src/public/asyncrenderloop.cpp
Normal file
51
src/public/asyncrenderloop.cpp
Normal 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
|
60
src/public/asyncrenderloop.h
Normal file
60
src/public/asyncrenderloop.h
Normal 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
|
Loading…
x
Reference in New Issue
Block a user