author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 15 Sep 2019 16:07:49 +0200 | |
changeset 15426 | b99834f70847 |
parent 14863 | ef9630519491 |
child 15922 | ebc50f21e849 |
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) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
45 |
if (m_engineInstance == engineInstance) return; |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
46 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
47 |
cleanup(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
48 |
m_engineInstance = engineInstance; |
14319 | 49 |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
50 |
emit engineInstanceChanged(m_engineInstance); |
14164 | 51 |
} |
52 |
||
53 |
void GameView::sync() { |
|
14319 | 54 |
if (!m_renderer && m_engineInstance) { |
55 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
56 |
m_renderer.reset(new GameViewRenderer()); |
14319 | 57 |
m_renderer->setEngineInstance(m_engineInstance); |
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
58 |
connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
14164 | 59 |
&GameViewRenderer::paint, Qt::DirectConnection); |
60 |
} |
|
61 |
||
14733 | 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(); |
|
14734 | 69 |
m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2; |
14164 | 70 |
} |
71 |
||
14734 | 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 |
} |
|
14164 | 77 |
|
14319 | 78 |
if (m_renderer) m_renderer->tick(m_delta); |
14164 | 79 |
} |
80 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
81 |
GameViewRenderer::GameViewRenderer() |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
82 |
: QObject(), m_delta(0), m_engineInstance(nullptr) {} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
83 |
|
14164 | 84 |
GameViewRenderer::~GameViewRenderer() {} |
85 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
86 |
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
87 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
88 |
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
89 |
m_engineInstance = engineInstance; |
14164 | 90 |
} |
91 |
||
92 |
void GameViewRenderer::paint() { |
|
93 |
if (m_delta == 0) return; |
|
94 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
95 |
if (m_engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
96 |
m_engineInstance->advance(m_delta); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
97 |
m_engineInstance->renderFrame(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
98 |
} |
14164 | 99 |
|
100 |
// m_window->resetOpenGLState(); |
|
101 |
} |
|
14733 | 102 |
|
103 |
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) { |
|
104 |
if (m_engineInstance) |
|
105 |
m_engineInstance->setOpenGLContext(window->openglContext()); |
|
106 |
} |