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