qmlfrontend/game_view.cpp
author Simon McVittie <smcv@debian.org>
Mon, 12 Sep 2022 10:40:53 -0400
branch1.0.0
changeset 15859 7b1d6dfa3173
parent 14842 ef9630519491
child 15894 ebc50f21e849
permissions -rw-r--r--
Remove FindSDL2 find-module, use sdl2-config.cmake instead This requires SDL >= 2.0.4. Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was not itself built using CMake: it installs a sdl2-config.cmake file to ${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and library, analogous to a pkg-config .pc file. As a result, we no longer need to copy/paste a "find-module package" to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2). Find-module packages are now discouraged by the CMake developers, in favour of having upstream projects behave as config-file packages. This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR and SDL2_LIBRARY, but the standard behaviour for config-file packages is to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword to make sure we search in config-file package mode, and will not find a FindSDL2.cmake in some other directory that implements the old interface. In addition to deleting redundant code, this avoids some assumptions in FindSDL2 about the layout of a SDL installation. The current libsdl2-dev package in Debian breaks those assumptions; this is considered a bug and will hopefully be fixed soon, but it illustrates how fragile these assumptions can be. We can be more robust against different installation layouts by relying on SDL's own CMake integration. When linking to a copy of CMake in a non-standard location, users can now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point to it; previously, these users would have used the SDL2DIR environment variable. This continues to be unnecessary if using matching system-wide installations of CMake and SDL2, for example both from Debian.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
     1
#include "game_view.h"
14143
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
14842
ef9630519491 Fix some minor problems with new frontend code
unc0rr
parents: 14713
diff changeset
     9
GameView::GameView(QQuickItem* parent)
ef9630519491 Fix some minor problems with new frontend code
unc0rr
parents: 14713
diff changeset
    10
    : QQuickItem(parent), m_delta(0), m_windowChanged(true) {
14143
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
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    27
EngineInstance* GameView::engineInstance() const { return m_engineInstance; }
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    28
14143
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
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    42
void GameView::cleanup() { m_renderer.reset(); }
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    43
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    44
void GameView::setEngineInstance(EngineInstance* engineInstance) {
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    45
  if (m_engineInstance == engineInstance) return;
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    46
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    47
  cleanup();
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    48
  m_engineInstance = engineInstance;
14298
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14290
diff changeset
    49
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    50
  emit engineInstanceChanged(m_engineInstance);
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    51
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    52
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    53
void GameView::sync() {
14298
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14290
diff changeset
    54
  if (!m_renderer && m_engineInstance) {
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14290
diff changeset
    55
    m_engineInstance->setOpenGLContext(window()->openglContext());
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    56
    m_renderer.reset(new GameViewRenderer());
14298
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14290
diff changeset
    57
    m_renderer->setEngineInstance(m_engineInstance);
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    58
    connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(),
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    59
            &GameViewRenderer::paint, Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    60
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    61
14712
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
    62
  if (m_windowChanged || (m_viewportSize != window()->size())) {
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
    63
    m_windowChanged = false;
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
    64
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
    65
    if (m_engineInstance)
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
    66
      m_engineInstance->setOpenGLContext(window()->openglContext());
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
    67
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
    68
    m_viewportSize = window()->size();
14713
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14712
diff changeset
    69
    m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2;
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    70
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    71
14713
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14712
diff changeset
    72
  if (m_engineInstance) {
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14712
diff changeset
    73
    QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14712
diff changeset
    74
    m_engineInstance->moveCamera(mousePos - m_centerPoint);
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14712
diff changeset
    75
    QCursor::setPos(mapToGlobal(m_centerPoint).toPoint());
cc6ab1e3f7d5 Allow to move camera around
unc0rr
parents: 14712
diff changeset
    76
  }
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    77
14298
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14290
diff changeset
    78
  if (m_renderer) m_renderer->tick(m_delta);
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    79
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    80
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    81
GameViewRenderer::GameViewRenderer()
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    82
    : QObject(), m_delta(0), m_engineInstance(nullptr) {}
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    83
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    84
GameViewRenderer::~GameViewRenderer() {}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    85
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    86
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; }
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
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) {
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    89
  m_engineInstance = engineInstance;
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    90
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    91
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    92
void GameViewRenderer::paint() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    93
  if (m_delta == 0) return;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    94
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    95
  if (m_engineInstance) {
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    96
    m_engineInstance->advance(m_delta);
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    97
    m_engineInstance->renderFrame();
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    98
  }
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    99
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   100
  // m_window->resetOpenGLState();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   101
}
14712
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
   102
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
   103
void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) {
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
   104
  if (m_engineInstance)
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
   105
    m_engineInstance->setOpenGLContext(window->openglContext());
57293f34ce59 Detect and handle resize
unc0rr
parents: 14298
diff changeset
   106
}