1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40
41#include "qlogicaspect.h"
42#include "qlogicaspect_p.h"
43
44#include <Qt3DLogic/qframeaction.h>
45#include <Qt3DCore/qnode.h>
46#include <QtCore/QThread>
47#include <QtGui/QWindow>
48
49#include <Qt3DLogic/private/executor_p.h>
50#include <Qt3DLogic/private/handler_p.h>
51#include <Qt3DLogic/private/manager_p.h>
52#include <Qt3DCore/private/qchangearbiter_p.h>
53#include <Qt3DCore/private/qscene_p.h>
54#include <Qt3DCore/private/qservicelocator_p.h>
55
56QT_BEGIN_NAMESPACE
57
58using namespace Qt3DCore;
59
60namespace Qt3DLogic {
61
62/*!
63 \class Qt3DLogic::QLogicAspect
64 \inherits Qt3DCore::QAbstractAspect
65 \inmodule Qt3DLogic
66 \brief Responsible for handling frame synchronization jobs.
67 \since 5.7
68*/
69
70QLogicAspectPrivate::QLogicAspectPrivate()
71 : QAbstractAspectPrivate()
72 , m_time(0)
73 , m_initialized(false)
74 , m_manager(new Logic::Manager)
75 , m_executor(new Logic::Executor)
76 , m_callbackJob(new Logic::CallbackJob)
77{
78 m_callbackJob->setManager(m_manager.data());
79 m_manager->setExecutor(m_executor.data());
80}
81
82void QLogicAspectPrivate::onEngineAboutToShutdown()
83{
84 m_executor->setScene(nullptr);
85}
86
87void QLogicAspectPrivate::registerBackendTypes()
88{
89 Q_Q(QLogicAspect);
90 q->registerBackendType<QFrameAction>(functor: QBackendNodeMapperPtr(new Logic::HandlerFunctor(m_manager.data())));
91}
92
93/*!
94 Constructs a new Qt3DLogic::QLogicAspect instance with \a parent.
95*/
96QLogicAspect::QLogicAspect(QObject *parent)
97 : QLogicAspect(*new QLogicAspectPrivate(), parent) {}
98
99/*! \internal */
100QLogicAspect::QLogicAspect(QLogicAspectPrivate &dd, QObject *parent)
101 : QAbstractAspect(dd, parent)
102{
103 Q_D(QLogicAspect);
104 setObjectName(QStringLiteral("Logic Aspect"));
105 d->registerBackendTypes();
106 d_func()->m_manager->setLogicAspect(this);
107}
108
109/*! \internal */
110QLogicAspect::~QLogicAspect()
111{
112}
113
114/*! \internal */
115QVector<QAspectJobPtr> QLogicAspect::jobsToExecute(qint64 time)
116{
117 Q_D(QLogicAspect);
118 const qint64 deltaTime = time - d->m_time;
119 const float dt = static_cast<float>(deltaTime) / 1.0e9;
120 d->m_manager->setDeltaTime(dt);
121 d->m_time = time;
122
123 // Create jobs that will get executed by the threadpool
124 QVector<QAspectJobPtr> jobs;
125
126 if (d->m_manager->hasFrameActions())
127 jobs.append(t: d->m_callbackJob);
128
129 return jobs;
130}
131
132/*! \internal */
133void QLogicAspect::onRegistered()
134{
135}
136
137/*! \internal */
138void QLogicAspect::onEngineStartup()
139{
140 Q_D(QLogicAspect);
141 d->m_executor->setScene(d->m_arbiter->scene());
142}
143
144} // namespace Qt3DLogic
145
146QT_END_NAMESPACE
147
148QT3D_REGISTER_NAMESPACED_ASPECT("logic", QT_PREPEND_NAMESPACE(Qt3DLogic), QLogicAspect)
149
150

source code of qt3d/src/logic/qlogicaspect.cpp