author | unC0Rr |
Tue, 15 Nov 2022 15:25:37 +0100 | |
changeset 15894 | ebc50f21e849 |
parent 14842 | ef9630519491 |
child 15895 | 9b8d4f34e0f3 |
permissions | -rw-r--r-- |
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
1 |
#include "game_view.h" |
14143 | 2 |
|
3 |
#include <QtQuick/qquickwindow.h> |
|
4 |
#include <QCursor> |
|
5 |
#include <QTimer> |
|
6 |
#include <QtGui/QOpenGLContext> |
|
7 |
#include <QtGui/QOpenGLShaderProgram> |
|
8 |
||
14842 | 9 |
GameView::GameView(QQuickItem* parent) |
10 |
: QQuickItem(parent), m_delta(0), m_windowChanged(true) { |
|
14143 | 11 |
connect(this, &QQuickItem::windowChanged, this, |
12 |
&GameView::handleWindowChanged); |
|
13 |
} |
|
14 |
||
15 |
void GameView::tick(quint32 delta) { |
|
16 |
m_delta = delta; |
|
17 |
||
18 |
if (window()) { |
|
19 |
QTimer* timer = new QTimer(this); |
|
20 |
connect(timer, &QTimer::timeout, window(), &QQuickWindow::update); |
|
21 |
timer->start(100); |
|
22 |
||
23 |
// window()->update(); |
|
24 |
} |
|
25 |
} |
|
26 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
27 |
EngineInstance* GameView::engineInstance() const { return m_engineInstance; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
28 |
|
14143 | 29 |
void GameView::handleWindowChanged(QQuickWindow* win) { |
30 |
if (win) { |
|
31 |
connect(win, &QQuickWindow::beforeSynchronizing, this, &GameView::sync, |
|
32 |
Qt::DirectConnection); |
|
33 |
connect(win, &QQuickWindow::sceneGraphInvalidated, this, &GameView::cleanup, |
|
34 |
Qt::DirectConnection); |
|
35 |
||
36 |
win->setClearBeforeRendering(false); |
|
37 |
||
38 |
m_windowChanged = true; |
|
39 |
} |
|
40 |
} |
|
41 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
42 |
void GameView::cleanup() { m_renderer.reset(); } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
43 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
44 |
void GameView::setEngineInstance(EngineInstance* engineInstance) { |
15894 | 45 |
if (m_engineInstance == engineInstance) { |
46 |
return; |
|
47 |
} |
|
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
48 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
49 |
cleanup(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
50 |
m_engineInstance = engineInstance; |
14298 | 51 |
|
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
52 |
emit engineInstanceChanged(m_engineInstance); |
14143 | 53 |
} |
54 |
||
55 |
void GameView::sync() { |
|
14298 | 56 |
if (!m_renderer && m_engineInstance) { |
57 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
58 |
m_renderer.reset(new GameViewRenderer()); |
14298 | 59 |
m_renderer->setEngineInstance(m_engineInstance); |
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
60 |
connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
14143 | 61 |
&GameViewRenderer::paint, Qt::DirectConnection); |
62 |
} |
|
63 |
||
15894 | 64 |
if (m_windowChanged || (m_viewportSize != size())) { |
14712 | 65 |
m_windowChanged = false; |
66 |
||
67 |
if (m_engineInstance) |
|
68 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
69 |
||
15894 | 70 |
m_viewportSize = size().toSize(); |
14713 | 71 |
m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2; |
14143 | 72 |
} |
73 |
||
14713 | 74 |
if (m_engineInstance) { |
75 |
QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint(); |
|
76 |
m_engineInstance->moveCamera(mousePos - m_centerPoint); |
|
77 |
QCursor::setPos(mapToGlobal(m_centerPoint).toPoint()); |
|
78 |
} |
|
14143 | 79 |
|
15894 | 80 |
if (m_renderer) { |
81 |
m_renderer->tick(m_delta); |
|
82 |
} |
|
14143 | 83 |
} |
84 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
85 |
GameViewRenderer::GameViewRenderer() |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
86 |
: QObject(), m_delta(0), m_engineInstance(nullptr) {} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
87 |
|
14143 | 88 |
GameViewRenderer::~GameViewRenderer() {} |
89 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
90 |
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
91 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
92 |
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
93 |
m_engineInstance = engineInstance; |
14143 | 94 |
} |
95 |
||
96 |
void GameViewRenderer::paint() { |
|
15894 | 97 |
if (m_delta == 0) { |
98 |
return; |
|
99 |
} |
|
14143 | 100 |
|
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
101 |
if (m_engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
102 |
m_engineInstance->advance(m_delta); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
103 |
m_engineInstance->renderFrame(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
104 |
} |
14143 | 105 |
|
106 |
// m_window->resetOpenGLState(); |
|
107 |
} |
|
14712 | 108 |
|
109 |
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) { |
|
110 |
if (m_engineInstance) |
|
111 |
m_engineInstance->setOpenGLContext(window->openglContext()); |
|
112 |
} |