author | unC0Rr |
Fri, 19 Apr 2024 17:44:55 +0200 | |
changeset 16010 | a73b9770467a |
parent 15895 | 9b8d4f34e0f3 |
child 16015 | 0e90bdfc2642 |
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> |
|
16010
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
4 |
|
14143 | 5 |
#include <QCursor> |
16010
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
6 |
#include <QOpenGLFramebufferObjectFormat> |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
7 |
#include <QQuickOpenGLUtils> |
14143 | 8 |
#include <QTimer> |
9 |
#include <QtGui/QOpenGLContext> |
|
16010
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
10 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
11 |
class GameViewRenderer : public QQuickFramebufferObject::Renderer { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
12 |
public: |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
13 |
explicit GameViewRenderer() = default; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
14 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
15 |
GameViewRenderer(const GameViewRenderer&) = delete; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
16 |
GameViewRenderer(GameViewRenderer&&) = delete; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
17 |
GameViewRenderer& operator=(const GameViewRenderer&) = delete; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
18 |
GameViewRenderer& operator=(GameViewRenderer&&) = delete; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
19 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
20 |
void render() override; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
21 |
QOpenGLFramebufferObject* createFramebufferObject(const QSize& size) override; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
22 |
void synchronize(QQuickFramebufferObject* fbo) override; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
23 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
24 |
QPointer<GameView> m_gameView; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
25 |
QPointer<QQuickWindow> m_window; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
26 |
bool m_inited{false}; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
27 |
}; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
28 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
29 |
void GameViewRenderer::render() { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
30 |
const auto engine = m_gameView->engineInstance(); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
31 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
32 |
if (!engine) { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
33 |
return; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
34 |
} |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
35 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
36 |
if (!m_inited) { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
37 |
m_inited = true; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
38 |
engine->setOpenGLContext(QOpenGLContext::currentContext()); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
39 |
} |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
40 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
41 |
engine->renderFrame(); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
42 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
43 |
QQuickOpenGLUtils::resetOpenGLState(); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
44 |
} |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
45 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
46 |
QOpenGLFramebufferObject* GameViewRenderer::createFramebufferObject( |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
47 |
const QSize& size) { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
48 |
QOpenGLFramebufferObjectFormat format; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
49 |
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
50 |
format.setSamples(8); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
51 |
auto fbo = new QOpenGLFramebufferObject(size, format); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
52 |
return fbo; |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
53 |
} |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
54 |
|
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
55 |
void GameViewRenderer::synchronize(QQuickFramebufferObject* fbo) { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
56 |
if (!m_gameView) { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
57 |
m_gameView = qobject_cast<GameView*>(fbo); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
58 |
m_window = fbo->window(); |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
59 |
} |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
60 |
} |
14143 | 61 |
|
14842 | 62 |
GameView::GameView(QQuickItem* parent) |
16010
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
63 |
: QQuickFramebufferObject(parent), m_delta(0) { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
64 |
setMirrorVertically(true); |
14143 | 65 |
} |
66 |
||
67 |
void GameView::tick(quint32 delta) { |
|
68 |
m_delta = delta; |
|
69 |
||
70 |
if (window()) { |
|
71 |
QTimer* timer = new QTimer(this); |
|
16010
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
72 |
connect(timer, &QTimer::timeout, this, &GameView::update); |
14143 | 73 |
timer->start(100); |
74 |
} |
|
75 |
} |
|
76 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
77 |
EngineInstance* GameView::engineInstance() const { return m_engineInstance; } |
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
78 |
|
16010
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
79 |
QQuickFramebufferObject::Renderer* GameView::createRenderer() const { |
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
80 |
return new GameViewRenderer{}; |
14143 | 81 |
} |
82 |
||
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
83 |
void GameView::setEngineInstance(EngineInstance* engineInstance) { |
15894 | 84 |
if (m_engineInstance == engineInstance) { |
85 |
return; |
|
86 |
} |
|
14290
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
87 |
|
92e5682810d4
Prepare to have possibility to pass opengl context to engine
unc0rr
parents:
14154
diff
changeset
|
88 |
m_engineInstance = engineInstance; |
14298 | 89 |
|
16010
a73b9770467a
Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents:
15895
diff
changeset
|
90 |
Q_EMIT engineInstanceChanged(m_engineInstance); |
14143 | 91 |
} |