author | nemo |
Thu, 25 Jul 2019 11:58:40 -0400 | |
changeset 15268 | 07836d6bac21 |
parent 14842 | ef9630519491 |
child 15894 | ebc50f21e849 |
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) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
45 |
if (m_engineInstance == engineInstance) return; |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
46 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
47 |
cleanup(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
48 |
m_engineInstance = engineInstance; |
14298 | 49 |
|
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
50 |
emit engineInstanceChanged(m_engineInstance); |
14143 | 51 |
} |
52 |
||
53 |
void GameView::sync() { |
|
14298 | 54 |
if (!m_renderer && m_engineInstance) { |
55 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
56 |
m_renderer.reset(new GameViewRenderer()); |
14298 | 57 |
m_renderer->setEngineInstance(m_engineInstance); |
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
58 |
connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
14143 | 59 |
&GameViewRenderer::paint, Qt::DirectConnection); |
60 |
} |
|
61 |
||
14712 | 62 |
if (m_windowChanged || (m_viewportSize != window()->size())) { |
63 |
m_windowChanged = false; |
|
64 |
||
65 |
if (m_engineInstance) |
|
66 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
67 |
||
68 |
m_viewportSize = window()->size(); |
|
14713 | 69 |
m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2; |
14143 | 70 |
} |
71 |
||
14713 | 72 |
if (m_engineInstance) { |
73 |
QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint(); |
|
74 |
m_engineInstance->moveCamera(mousePos - m_centerPoint); |
|
75 |
QCursor::setPos(mapToGlobal(m_centerPoint).toPoint()); |
|
76 |
} |
|
14143 | 77 |
|
14298 | 78 |
if (m_renderer) m_renderer->tick(m_delta); |
14143 | 79 |
} |
80 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
81 |
GameViewRenderer::GameViewRenderer() |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
82 |
: QObject(), m_delta(0), m_engineInstance(nullptr) {} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
83 |
|
14143 | 84 |
GameViewRenderer::~GameViewRenderer() {} |
85 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
86 |
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
87 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
88 |
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
89 |
m_engineInstance = engineInstance; |
14143 | 90 |
} |
91 |
||
92 |
void GameViewRenderer::paint() { |
|
93 |
if (m_delta == 0) return; |
|
94 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
95 |
if (m_engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
96 |
m_engineInstance->advance(m_delta); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
97 |
m_engineInstance->renderFrame(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
98 |
} |
14143 | 99 |
|
100 |
// m_window->resetOpenGLState(); |
|
101 |
} |
|
14712 | 102 |
|
103 |
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) { |
|
104 |
if (m_engineInstance) |
|
105 |
m_engineInstance->setOpenGLContext(window->openglContext()); |
|
106 |
} |