qmlfrontend/game_view.cpp
author unC0Rr
Tue, 21 May 2024 14:38:30 +0200
changeset 16046 e8afb1bf2779
parent 16044 0e90bdfc2642
permissions -rw-r--r--
Implement triggering advancement of simulation in qmlfrontend
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14175
diff changeset
     1
#include "game_view.h"
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     2
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     3
#include <QtQuick/qquickwindow.h>
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
     4
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     5
#include <QCursor>
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
     6
#include <QOpenGLFramebufferObjectFormat>
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
     7
#include <QQuickOpenGLUtils>
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     8
#include <QTimer>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     9
#include <QtGui/QOpenGLContext>
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    10
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    11
class GameViewRenderer : public QQuickFramebufferObject::Renderer {
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    12
 public:
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    13
  explicit GameViewRenderer() = default;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    14
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    15
  GameViewRenderer(const GameViewRenderer&) = delete;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    16
  GameViewRenderer(GameViewRenderer&&) = delete;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    17
  GameViewRenderer& operator=(const GameViewRenderer&) = delete;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    18
  GameViewRenderer& operator=(GameViewRenderer&&) = delete;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    19
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    20
  void render() override;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    21
  QOpenGLFramebufferObject* createFramebufferObject(const QSize& size) override;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    22
  void synchronize(QQuickFramebufferObject* fbo) override;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    23
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    24
  QPointer<GameView> m_gameView;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    25
  QPointer<QQuickWindow> m_window;
16044
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    26
  bool m_dirty{true};
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    27
  QSizeF m_gameViewSize;
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    28
};
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    29
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    30
void GameViewRenderer::render() {
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    31
  const auto engine = m_gameView->engineInstance();
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    32
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    33
  if (!engine) {
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    34
    return;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    35
  }
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    36
16044
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    37
  if (m_dirty) {
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    38
    m_dirty = false;
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    39
    engine->setOpenGLContext(QOpenGLContext::currentContext());
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    40
  }
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    41
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    42
  engine->renderFrame();
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    43
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    44
  QQuickOpenGLUtils::resetOpenGLState();
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    45
}
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    46
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    47
QOpenGLFramebufferObject* GameViewRenderer::createFramebufferObject(
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    48
    const QSize& size) {
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    49
  QOpenGLFramebufferObjectFormat format;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    50
  format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    51
  format.setSamples(8);
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    52
  auto fbo = new QOpenGLFramebufferObject(size, format);
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    53
  return fbo;
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    54
}
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    55
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    56
void GameViewRenderer::synchronize(QQuickFramebufferObject* fbo) {
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    57
  if (!m_gameView) {
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    58
    m_gameView = qobject_cast<GameView*>(fbo);
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    59
    m_window = fbo->window();
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    60
  }
16044
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    61
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    62
  if (const auto currentSize = m_gameView->size();
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    63
      currentSize != m_gameViewSize) {
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    64
    m_gameViewSize = currentSize;
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    65
    m_dirty = true;
0e90bdfc2642 Handle game view resize
unC0Rr
parents: 16039
diff changeset
    66
  }
16046
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    67
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    68
  m_gameView->executeActions();
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    69
}
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    70
16046
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    71
GameView::GameView(QQuickItem* parent) : QQuickFramebufferObject(parent) {
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    72
  setMirrorVertically(true);
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    73
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    74
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    75
void GameView::tick(quint32 delta) {
16046
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    76
  addAction([delta](auto engine) { engine->advance(delta); });
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    77
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    78
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14175
diff changeset
    79
EngineInstance* GameView::engineInstance() const { return m_engineInstance; }
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14175
diff changeset
    80
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    81
QQuickFramebufferObject::Renderer* GameView::createRenderer() const {
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
    82
  return new GameViewRenderer{};
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    83
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    84
16046
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    85
void GameView::executeActions() {
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    86
  if (!m_engineInstance) {
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    87
    return;
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    88
  }
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    89
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    90
  for (const auto& action : m_actions) {
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    91
    action(m_engineInstance);
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    92
  }
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    93
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    94
  m_actions.clear();
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    95
}
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
    96
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14175
diff changeset
    97
void GameView::setEngineInstance(EngineInstance* engineInstance) {
15922
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    98
  if (m_engineInstance == engineInstance) {
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    99
    return;
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
   100
  }
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14175
diff changeset
   101
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14175
diff changeset
   102
  m_engineInstance = engineInstance;
14319
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14311
diff changeset
   103
16039
a73b9770467a Port QML frontend to Qt6, use rendering to framebuffer object
unC0Rr
parents: 15923
diff changeset
   104
  Q_EMIT engineInstanceChanged(m_engineInstance);
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   105
}
16046
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
   106
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
   107
void GameView::addAction(std::function<void(EngineInstance*)>&& action) {
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
   108
  m_actions.append(std::move(action));
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16044
diff changeset
   109
}