author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 07 Apr 2019 23:08:33 +0200 | |
changeset 14763 | bc0e86b0347b |
parent 14718 | cc6ab1e3f7d5 |
child 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 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
9 |
GameView::GameView() : m_delta(0), m_windowChanged(true) { |
14148 | 10 |
connect(this, &QQuickItem::windowChanged, this, |
11 |
&GameView::handleWindowChanged); |
|
12 |
} |
|
13 |
||
14 |
void GameView::tick(quint32 delta) { |
|
15 |
m_delta = delta; |
|
16 |
||
17 |
if (window()) { |
|
18 |
QTimer* timer = new QTimer(this); |
|
19 |
connect(timer, &QTimer::timeout, window(), &QQuickWindow::update); |
|
20 |
timer->start(100); |
|
21 |
||
22 |
// window()->update(); |
|
23 |
} |
|
24 |
} |
|
25 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
26 |
EngineInstance* GameView::engineInstance() const { return m_engineInstance; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
27 |
|
14148 | 28 |
void GameView::handleWindowChanged(QQuickWindow* win) { |
29 |
if (win) { |
|
30 |
connect(win, &QQuickWindow::beforeSynchronizing, this, &GameView::sync, |
|
31 |
Qt::DirectConnection); |
|
32 |
connect(win, &QQuickWindow::sceneGraphInvalidated, this, &GameView::cleanup, |
|
33 |
Qt::DirectConnection); |
|
34 |
||
35 |
win->setClearBeforeRendering(false); |
|
36 |
||
37 |
m_windowChanged = true; |
|
38 |
} |
|
39 |
} |
|
40 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
41 |
void GameView::cleanup() { m_renderer.reset(); } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
42 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
43 |
void GameView::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
44 |
if (m_engineInstance == engineInstance) return; |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
45 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
46 |
cleanup(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
47 |
m_engineInstance = engineInstance; |
14303 | 48 |
|
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
49 |
emit engineInstanceChanged(m_engineInstance); |
14148 | 50 |
} |
51 |
||
52 |
void GameView::sync() { |
|
14303 | 53 |
if (!m_renderer && m_engineInstance) { |
54 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
55 |
m_renderer.reset(new GameViewRenderer()); |
14303 | 56 |
m_renderer->setEngineInstance(m_engineInstance); |
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
57 |
connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
14148 | 58 |
&GameViewRenderer::paint, Qt::DirectConnection); |
59 |
} |
|
60 |
||
14717 | 61 |
if (m_windowChanged || (m_viewportSize != window()->size())) { |
62 |
m_windowChanged = false; |
|
63 |
||
64 |
if (m_engineInstance) |
|
65 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
66 |
||
67 |
m_viewportSize = window()->size(); |
|
14718 | 68 |
m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2; |
14148 | 69 |
} |
70 |
||
14718 | 71 |
if (m_engineInstance) { |
72 |
QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint(); |
|
73 |
m_engineInstance->moveCamera(mousePos - m_centerPoint); |
|
74 |
QCursor::setPos(mapToGlobal(m_centerPoint).toPoint()); |
|
75 |
} |
|
14148 | 76 |
|
14303 | 77 |
if (m_renderer) m_renderer->tick(m_delta); |
14148 | 78 |
} |
79 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
80 |
GameViewRenderer::GameViewRenderer() |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
81 |
: QObject(), m_delta(0), m_engineInstance(nullptr) {} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
82 |
|
14148 | 83 |
GameViewRenderer::~GameViewRenderer() {} |
84 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
85 |
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
86 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
87 |
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
88 |
m_engineInstance = engineInstance; |
14148 | 89 |
} |
90 |
||
91 |
void GameViewRenderer::paint() { |
|
92 |
if (m_delta == 0) return; |
|
93 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
94 |
if (m_engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
95 |
m_engineInstance->advance(m_delta); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
96 |
m_engineInstance->renderFrame(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
97 |
} |
14148 | 98 |
|
99 |
// m_window->resetOpenGLState(); |
|
100 |
} |
|
14717 | 101 |
|
102 |
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) { |
|
103 |
if (m_engineInstance) |
|
104 |
m_engineInstance->setOpenGLContext(window->openglContext()); |
|
105 |
} |