|
1 #include "game_view.h" |
|
2 |
|
3 #include <QtQuick/qquickwindow.h> |
|
4 #include <QCursor> |
|
5 #include <QTimer> |
|
6 #include <QtGui/QOpenGLContext> |
|
7 #include <QtGui/QOpenGLShaderProgram> |
|
8 |
|
9 GameView::GameView(QQuickItem* parent) |
|
10 : QQuickItem(parent), m_delta(0), m_windowChanged(true) { |
|
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 |
|
27 EngineInstance* GameView::engineInstance() const { return m_engineInstance; } |
|
28 |
|
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 |
|
42 void GameView::cleanup() { m_renderer.reset(); } |
|
43 |
|
44 void GameView::setEngineInstance(EngineInstance* engineInstance) { |
|
45 if (m_engineInstance == engineInstance) return; |
|
46 |
|
47 cleanup(); |
|
48 m_engineInstance = engineInstance; |
|
49 |
|
50 emit engineInstanceChanged(m_engineInstance); |
|
51 } |
|
52 |
|
53 void GameView::sync() { |
|
54 if (!m_renderer && m_engineInstance) { |
|
55 m_engineInstance->setOpenGLContext(window()->openglContext()); |
|
56 m_renderer.reset(new GameViewRenderer()); |
|
57 m_renderer->setEngineInstance(m_engineInstance); |
|
58 connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(), |
|
59 &GameViewRenderer::paint, Qt::DirectConnection); |
|
60 } |
|
61 |
|
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(); |
|
69 m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2; |
|
70 } |
|
71 |
|
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 } |
|
77 |
|
78 if (m_renderer) m_renderer->tick(m_delta); |
|
79 } |
|
80 |
|
81 GameViewRenderer::GameViewRenderer() |
|
82 : QObject(), m_delta(0), m_engineInstance(nullptr) {} |
|
83 |
|
84 GameViewRenderer::~GameViewRenderer() {} |
|
85 |
|
86 void GameViewRenderer::tick(quint32 delta) { m_delta = delta; } |
|
87 |
|
88 void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) { |
|
89 m_engineInstance = engineInstance; |
|
90 } |
|
91 |
|
92 void GameViewRenderer::paint() { |
|
93 if (m_delta == 0) return; |
|
94 |
|
95 if (m_engineInstance) { |
|
96 m_engineInstance->advance(m_delta); |
|
97 m_engineInstance->renderFrame(); |
|
98 } |
|
99 |
|
100 // m_window->resetOpenGLState(); |
|
101 } |
|
102 |
|
103 void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) { |
|
104 if (m_engineInstance) |
|
105 m_engineInstance->setOpenGLContext(window->openglContext()); |
|
106 } |