author | Wuzzy <Wuzzy2@mail.ru> |
Fri, 24 May 2019 19:44:49 +0200 | |
changeset 15045 | 0dbd06bf90f4 |
parent 14847 | ef9630519491 |
permissions | -rw-r--r-- |
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
1 |
#include "game_view.h" |
14148 | 2 |
|
3 |
#include <QtQuick/qquickwindow.h> |
|
4 |
#include <QCursor> |
|
5 |
#include <QTimer> |
|
6 |
#include <QtGui/QOpenGLContext> |
|
7 |
#include <QtGui/QOpenGLShaderProgram> |
|
8 |
||
14847 | 9 |
GameView::GameView(QQuickItem* parent) |
10 |
: QQuickItem(parent), m_delta(0), m_windowChanged(true) { |
|
14148 | 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 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
27 |
EngineInstance* GameView::engineInstance() const { return m_engineInstance; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
28 |
|
14148 | 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 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
42 |
void GameView::cleanup() { m_renderer.reset(); } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
43 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
44 |
void GameView::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
45 |
if (m_engineInstance == engineInstance) return; |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
46 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
47 |
cleanup(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
48 |
m_engineInstance = engineInstance; |
14303 | 49 |
|
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
50 |
emit engineInstanceChanged(m_engineInstance); |
14148 | 51 |
} |
52 |
||
53 |
void GameView::sync() { |
|
14303 | 54 |
if (!m_renderer && m_engineInstance) { |
55 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
56 |
m_renderer.reset(new GameViewRenderer()); |
14303 | 57 |
m_renderer->setEngineInstance(m_engineInstance); |
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
58 |
connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
14148 | 59 |
&GameViewRenderer::paint, Qt::DirectConnection); |
60 |
} |
|
61 |
||
14717 | 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(); |
|
14718 | 69 |
m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2; |
14148 | 70 |
} |
71 |
||
14718 | 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 |
} |
|
14148 | 77 |
|
14303 | 78 |
if (m_renderer) m_renderer->tick(m_delta); |
14148 | 79 |
} |
80 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
81 |
GameViewRenderer::GameViewRenderer() |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
82 |
: QObject(), m_delta(0), m_engineInstance(nullptr) {} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
83 |
|
14148 | 84 |
GameViewRenderer::~GameViewRenderer() {} |
85 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
86 |
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
87 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
88 |
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
89 |
m_engineInstance = engineInstance; |
14148 | 90 |
} |
91 |
||
92 |
void GameViewRenderer::paint() { |
|
93 |
if (m_delta == 0) return; |
|
94 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
95 |
if (m_engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
96 |
m_engineInstance->advance(m_delta); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
97 |
m_engineInstance->renderFrame(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
98 |
} |
14148 | 99 |
|
100 |
// m_window->resetOpenGLState(); |
|
101 |
} |
|
14717 | 102 |
|
103 |
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) { |
|
104 |
if (m_engineInstance) |
|
105 |
m_engineInstance->setOpenGLContext(window->openglContext()); |
|
106 |
} |