author | unC0Rr |
Tue, 03 Jan 2023 12:05:59 +0100 | |
branch | transitional_engine |
changeset 15930 | f39f0f614dbf |
parent 15923 | 9b8d4f34e0f3 |
child 16039 | a73b9770467a |
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) { |
15922 | 45 |
if (m_engineInstance == engineInstance) { |
46 |
return; |
|
47 |
} |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
48 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
49 |
cleanup(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
50 |
m_engineInstance = engineInstance; |
14319 | 51 |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
52 |
emit engineInstanceChanged(m_engineInstance); |
14164 | 53 |
} |
54 |
||
55 |
void GameView::sync() { |
|
14319 | 56 |
if (!m_renderer && m_engineInstance) { |
57 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
58 |
m_renderer.reset(new GameViewRenderer()); |
14319 | 59 |
m_renderer->setEngineInstance(m_engineInstance); |
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
60 |
connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
14164 | 61 |
&GameViewRenderer::paint, Qt::DirectConnection); |
62 |
} |
|
63 |
||
15922 | 64 |
if (m_windowChanged || (m_viewportSize != size())) { |
14733 | 65 |
m_windowChanged = false; |
66 |
||
67 |
if (m_engineInstance) |
|
68 |
m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
69 |
||
15922 | 70 |
m_viewportSize = size().toSize(); |
14734 | 71 |
m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2; |
14164 | 72 |
} |
73 |
||
14734 | 74 |
if (m_engineInstance) { |
15923 | 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()); |
|
14734 | 80 |
} |
14164 | 81 |
|
15922 | 82 |
if (m_renderer) { |
83 |
m_renderer->tick(m_delta); |
|
84 |
} |
|
14164 | 85 |
} |
86 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
87 |
GameViewRenderer::GameViewRenderer() |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
88 |
: QObject(), m_delta(0), m_engineInstance(nullptr) {} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
89 |
|
14164 | 90 |
GameViewRenderer::~GameViewRenderer() {} |
91 |
||
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
92 |
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
93 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
94 |
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
95 |
m_engineInstance = engineInstance; |
14164 | 96 |
} |
97 |
||
98 |
void GameViewRenderer::paint() { |
|
15922 | 99 |
if (m_delta == 0) { |
100 |
return; |
|
101 |
} |
|
14164 | 102 |
|
14311
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
103 |
if (m_engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
104 |
m_engineInstance->advance(m_delta); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
105 |
m_engineInstance->renderFrame(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14175
diff
changeset
|
106 |
} |
14164 | 107 |
|
108 |
// m_window->resetOpenGLState(); |
|
109 |
} |
|
14733 | 110 |
|
111 |
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) { |
|
112 |
if (m_engineInstance) |
|
113 |
m_engineInstance->setOpenGLContext(window->openglContext()); |
|
114 |
} |