qmlfrontend/game_view.cpp
author unc0rr
Sat, 24 Nov 2018 23:51:45 +0100
changeset 14290 92e5682810d4
parent 14154 8354b390f1a2
child 14298 00b56ec8b7df
permissions -rw-r--r--
Prepare to have possibility to pass opengl context to engine
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
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
     9
GameView::GameView() : m_delta(0), m_windowChanged(true) {
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    10
  connect(this, &QQuickItem::windowChanged, this,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    11
          &GameView::handleWindowChanged);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    12
}
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
void GameView::tick(quint32 delta) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    15
  m_delta = delta;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    16
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    17
  if (window()) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    18
    QTimer* timer = new QTimer(this);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    19
    connect(timer, &QTimer::timeout, window(), &QQuickWindow::update);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    20
    timer->start(100);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    21
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    22
    // window()->update();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    23
  }
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
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    26
EngineInstance* GameView::engineInstance() const { return m_engineInstance; }
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    27
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    28
void GameView::handleWindowChanged(QQuickWindow* win) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    29
  if (win) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    30
    connect(win, &QQuickWindow::beforeSynchronizing, this, &GameView::sync,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    31
            Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    32
    connect(win, &QQuickWindow::sceneGraphInvalidated, this, &GameView::cleanup,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    33
            Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    34
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    35
    win->setClearBeforeRendering(false);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
    m_windowChanged = true;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
  }
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
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    41
void GameView::cleanup() { m_renderer.reset(); }
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    42
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    43
void GameView::setEngineInstance(EngineInstance* engineInstance) {
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    44
  if (m_engineInstance == engineInstance) return;
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    45
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    46
  cleanup();
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    47
  m_engineInstance = engineInstance;
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    48
  engineInstance->setOpenGLContext(window()->openglContext());
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    49
  emit engineInstanceChanged(m_engineInstance);
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    50
}
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
void GameView::sync() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    53
  if (!m_renderer) {
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    54
    m_renderer.reset(new GameViewRenderer());
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    55
    connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(),
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    56
            &GameViewRenderer::paint, Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    57
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    58
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    59
  if (m_windowChanged) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    60
    QSize windowSize = window()->size();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    61
    m_renderer->setViewportSize(windowSize * window()->devicePixelRatio());
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    62
    m_centerX = windowSize.width() / 2;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    63
    m_centerY = windowSize.height() / 2;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    64
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    65
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    66
  // QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    67
  // if (flibUpdateMousePosition(m_centerX, m_centerY, mousePos.x(),
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    68
  // mousePos.y()))
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    69
  //  QCursor::setPos(mapToGlobal(QPointF(m_centerX, m_centerY)).toPoint());
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
  m_renderer->tick(m_delta);
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
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    74
GameViewRenderer::GameViewRenderer()
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    75
    : QObject(), m_delta(0), m_engineInstance(nullptr) {}
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    76
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    77
GameViewRenderer::~GameViewRenderer() {}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    78
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    79
void GameViewRenderer::tick(quint32 delta) { m_delta = delta; }
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    80
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    81
void GameViewRenderer::setViewportSize(const QSize& size) {
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    82
  // flibResizeWindow(size.width(), size.height());
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    83
}
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    84
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    85
void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) {
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    86
  m_engineInstance = engineInstance;
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    87
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    88
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    89
void GameViewRenderer::paint() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    90
  if (m_delta == 0) return;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    91
14290
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    92
  if (m_engineInstance) {
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    93
    m_engineInstance->advance(m_delta);
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    94
    m_engineInstance->renderFrame();
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14154
diff changeset
    95
  }
14143
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
  // m_window->resetOpenGLState();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    98
}