qmlfrontend/game_view.cpp
author unC0Rr
Tue, 03 Jan 2023 12:05:59 +0100
branchtransitional_engine
changeset 15930 f39f0f614dbf
parent 15923 9b8d4f34e0f3
child 16039 a73b9770467a
permissions -rw-r--r--
Use LandPixels array allocate in hwengine-future library
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>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     4
#include <QCursor>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     5
#include <QTimer>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     6
#include <QtGui/QOpenGLContext>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     7
#include <QtGui/QOpenGLShaderProgram>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     8
14863
ef9630519491 Fix some minor problems with new frontend code
unc0rr
parents: 14734
diff changeset
     9
GameView::GameView(QQuickItem* parent)
ef9630519491 Fix some minor problems with new frontend code
unc0rr
parents: 14734
diff changeset
    10
    : QQuickItem(parent), m_delta(0), m_windowChanged(true) {
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    11
  connect(this, &QQuickItem::windowChanged, this,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    12
          &GameView::handleWindowChanged);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    13
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    14
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    15
void GameView::tick(quint32 delta) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    16
  m_delta = delta;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    17
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    18
  if (window()) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    19
    QTimer* timer = new QTimer(this);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    20
    connect(timer, &QTimer::timeout, window(), &QQuickWindow::update);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    21
    timer->start(100);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    22
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    23
    // window()->update();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    24
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    25
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    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
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    29
void GameView::handleWindowChanged(QQuickWindow* win) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    30
  if (win) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    31
    connect(win, &QQuickWindow::beforeSynchronizing, this, &GameView::sync,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    32
            Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    33
    connect(win, &QQuickWindow::sceneGraphInvalidated, this, &GameView::cleanup,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    34
            Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    35
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
    win->setClearBeforeRendering(false);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
    m_windowChanged = true;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    40
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    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
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    45
  if (m_engineInstance == engineInstance) {
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    46
    return;
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    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
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14311
diff changeset
    51
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14175
diff changeset
    52
  emit engineInstanceChanged(m_engineInstance);
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    53
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    54
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    55
void GameView::sync() {
14319
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14311
diff changeset
    56
  if (!m_renderer && m_engineInstance) {
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14311
diff changeset
    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
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14311
diff changeset
    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
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    61
            &GameViewRenderer::paint, Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    62
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    63
15922
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    64
  if (m_windowChanged || (m_viewportSize != size())) {
14733
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
    65
    m_windowChanged = false;
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
    66
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
    67
    if (m_engineInstance)
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
    68
      m_engineInstance->setOpenGLContext(window()->openglContext());
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
    69
15922
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    70
    m_viewportSize = size().toSize();
14734
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14733
diff changeset
    71
    m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2;
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    72
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    73
14734
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14733
diff changeset
    74
  if (m_engineInstance) {
15923
9b8d4f34e0f3 Fix camera move control in qmlfrontend
unC0Rr
parents: 15922
diff changeset
    75
    const auto delta = mapFromGlobal(QCursor::pos()).toPoint() - m_centerPoint;
9b8d4f34e0f3 Fix camera move control in qmlfrontend
unC0Rr
parents: 15922
diff changeset
    76
9b8d4f34e0f3 Fix camera move control in qmlfrontend
unC0Rr
parents: 15922
diff changeset
    77
    m_engineInstance->moveCamera(delta);
9b8d4f34e0f3 Fix camera move control in qmlfrontend
unC0Rr
parents: 15922
diff changeset
    78
9b8d4f34e0f3 Fix camera move control in qmlfrontend
unC0Rr
parents: 15922
diff changeset
    79
    QCursor::setPos(window()->screen(), mapToGlobal(m_centerPoint).toPoint());
14734
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14733
diff changeset
    80
  }
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    81
15922
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    82
  if (m_renderer) {
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    83
    m_renderer->tick(m_delta);
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    84
  }
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    85
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    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
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    90
GameViewRenderer::~GameViewRenderer() {}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    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
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    96
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    97
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    98
void GameViewRenderer::paint() {
15922
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
    99
  if (m_delta == 0) {
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
   100
    return;
ebc50f21e849 Code formatting, enable c++17 for qmlfrontend
unC0Rr
parents: 14863
diff changeset
   101
  }
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   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
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   107
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   108
  // m_window->resetOpenGLState();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   109
}
14733
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
   110
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
   111
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) {
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
   112
  if (m_engineInstance)
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
   113
    m_engineInstance->setOpenGLContext(window->openglContext());
57293f34ce59 Detect and handle resize
unc0rr
parents: 14319
diff changeset
   114
}