author | Wuzzy <Wuzzy2@mail.ru> |
Mon, 26 Nov 2018 01:13:10 +0100 | |
changeset 14301 | 0855344f54a7 |
parent 14295 | 92e5682810d4 |
child 14303 | 00b56ec8b7df |
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; |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
48 |
engineInstance->setOpenGLContext(window()->openglContext()); |
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() { |
|
53 |
if (!m_renderer) { |
|
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
54 |
m_renderer.reset(new GameViewRenderer()); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
55 |
connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
14148 | 56 |
&GameViewRenderer::paint, Qt::DirectConnection); |
57 |
} |
|
58 |
||
59 |
if (m_windowChanged) { |
|
60 |
QSize windowSize = window()->size(); |
|
61 |
m_renderer->setViewportSize(windowSize * window()->devicePixelRatio()); |
|
62 |
m_centerX = windowSize.width() / 2; |
|
63 |
m_centerY = windowSize.height() / 2; |
|
64 |
} |
|
65 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
66 |
// QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
67 |
// if (flibUpdateMousePosition(m_centerX, m_centerY, mousePos.x(), |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
68 |
// mousePos.y())) |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
69 |
// QCursor::setPos(mapToGlobal(QPointF(m_centerX, m_centerY)).toPoint()); |
14148 | 70 |
|
71 |
m_renderer->tick(m_delta); |
|
72 |
} |
|
73 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
74 |
GameViewRenderer::GameViewRenderer() |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
75 |
: QObject(), m_delta(0), m_engineInstance(nullptr) {} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
76 |
|
14148 | 77 |
GameViewRenderer::~GameViewRenderer() {} |
78 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
79 |
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
80 |
|
14148 | 81 |
void GameViewRenderer::setViewportSize(const QSize& size) { |
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
82 |
// flibResizeWindow(size.width(), size.height()); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
83 |
} |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
84 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
85 |
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
86 |
m_engineInstance = engineInstance; |
14148 | 87 |
} |
88 |
||
89 |
void GameViewRenderer::paint() { |
|
90 |
if (m_delta == 0) return; |
|
91 |
||
14295
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
92 |
if (m_engineInstance) { |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
93 |
m_engineInstance->advance(m_delta); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
94 |
m_engineInstance->renderFrame(); |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14159
diff
changeset
|
95 |
} |
14148 | 96 |
|
97 |
// m_window->resetOpenGLState(); |
|
98 |
} |