author | unC0Rr |
Tue, 15 Nov 2022 15:25:37 +0100 | |
changeset 15922 | ebc50f21e849 |
parent 14863 | ef9630519491 |
child 15923 | 9b8d4f34e0f3 |
permissions | -rw-r--r-- |
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
1 |
#include "game_view.h" |
14164 | 2 |
|
3 |
#include <QtQuick/qquickwindow.h> |
|
4 |
#include <QCursor> |
|
5 |
#include <QTimer> |
|
6 |
#include <QtGui/QOpenGLContext> |
|
7 |
#include <QtGui/QOpenGLShaderProgram> |
|
8 |
||
14863 | 9 |
GameView::GameView(QQuickItem* parent) |
10 |
: QQuickItem(parent), m_delta(0), m_windowChanged(true) { |
|
14164 | 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 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
27 |
EngineInstance* GameView::engineInstance() const { return m_engineInstance; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
28 |
|
14164 | 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 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
42 |
void GameView::cleanup() { m_renderer.reset(); } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
43 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
44 |
void GameView::setEngineInstance(EngineInstance* engineInstance) { |
15922 | 45 |
if (m_engineInstance == engineInstance) { |
46 |
return; |
|
47 |
} |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
48 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
49 |
cleanup(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
50 |
m_engineInstance = engineInstance; |
14319 | 51 |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
52 |
emit engineInstanceChanged(m_engineInstance); |
14164 | 53 |
} |
54 |
||
55 |
void GameView::sync() { |
|
14319 | 56 |
if (!m_renderer && m_engineInstance) { |
57 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
58 |
m_renderer.reset(new GameViewRenderer()); |
14319 | 59 |
m_renderer->setEngineInstance(m_engineInstance); |
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
60 |
connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
14164 | 61 |
&GameViewRenderer::paint, Qt::DirectConnection); |
62 |
} |
|
63 |
||
15922 | 64 |
if (m_windowChanged || (m_viewportSize != size())) { |
14733 | 65 |
m_windowChanged = false; |
66 |
||
67 |
if (m_engineInstance) |
|
68 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
69 |
||
15922 | 70 |
m_viewportSize = size().toSize(); |
14734 | 71 |
m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2; |
14164 | 72 |
} |
73 |
||
14734 | 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 |
} |
|
14164 | 79 |
|
15922 | 80 |
if (m_renderer) { |
81 |
m_renderer->tick(m_delta); |
|
82 |
} |
|
14164 | 83 |
} |
84 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
85 |
GameViewRenderer::GameViewRenderer() |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
86 |
: QObject(), m_delta(0), m_engineInstance(nullptr) {} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
87 |
|
14164 | 88 |
GameViewRenderer::~GameViewRenderer() {} |
89 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
90 |
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
91 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
92 |
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
93 |
m_engineInstance = engineInstance; |
14164 | 94 |
} |
95 |
||
96 |
void GameViewRenderer::paint() { |
|
15922 | 97 |
if (m_delta == 0) { |
98 |
return; |
|
99 |
} |
|
14164 | 100 |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
101 |
if (m_engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
102 |
m_engineInstance->advance(m_delta); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
103 |
m_engineInstance->renderFrame(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
104 |
} |
14164 | 105 |
|
106 |
// m_window->resetOpenGLState(); |
|
107 |
} |
|
14733 | 108 |
|
109 |
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) { |
|
110 |
if (m_engineInstance) |
|
111 |
m_engineInstance->setOpenGLContext(window->openglContext()); |
|
112 |
} |