qmlfrontend/game_view.cpp
author Wuzzy <Wuzzy2@mail.ru>
Wed, 07 Nov 2018 17:45:20 +0100
changeset 14179 ddc77c3b1823
parent 14175 8354b390f1a2
child 14311 92e5682810d4
permissions -rw-r--r--
Add TODO in ru.txt for reviewing all 04:XX strings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     1
#include "gameview.h"
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
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     9
#include "flib.h"
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    10
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    11
extern "C" {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    12
extern GameTick_t* flibGameTick;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    13
extern ResizeWindow_t* flibResizeWindow;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    14
extern updateMousePosition_t* flibUpdateMousePosition;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    15
}
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
GameView::GameView() : m_delta(0), m_renderer(nullptr), m_windowChanged(true) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    18
  connect(this, &QQuickItem::windowChanged, this,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    19
          &GameView::handleWindowChanged);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    20
}
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
void GameView::tick(quint32 delta) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    23
  m_delta = delta;
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
  if (window()) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    26
    QTimer* timer = new QTimer(this);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    27
    connect(timer, &QTimer::timeout, window(), &QQuickWindow::update);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    28
    timer->start(100);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    29
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    30
    // window()->update();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    31
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    32
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    33
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    34
void GameView::handleWindowChanged(QQuickWindow* win) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    35
  if (win) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
    connect(win, &QQuickWindow::beforeSynchronizing, this, &GameView::sync,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
            Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
    connect(win, &QQuickWindow::sceneGraphInvalidated, this, &GameView::cleanup,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
            Qt::DirectConnection);
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
    win->setClearBeforeRendering(false);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    42
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    43
    m_windowChanged = true;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    44
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    45
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    46
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    47
void GameView::cleanup() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    48
  if (m_renderer) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    49
    delete m_renderer;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    50
    m_renderer = 0;
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
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    54
void GameView::sync() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    55
  if (!m_renderer) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    56
    m_renderer = new GameViewRenderer();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    57
    connect(window(), &QQuickWindow::beforeRendering, m_renderer,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    58
            &GameViewRenderer::paint, Qt::DirectConnection);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    59
  }
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
  if (m_windowChanged) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    62
    QSize windowSize = window()->size();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    63
    m_renderer->setViewportSize(windowSize * window()->devicePixelRatio());
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    64
    m_centerX = windowSize.width() / 2;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    65
    m_centerY = windowSize.height() / 2;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    66
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    67
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    68
  QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    69
  if (flibUpdateMousePosition(m_centerX, m_centerY, mousePos.x(), mousePos.y()))
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    70
    QCursor::setPos(mapToGlobal(QPointF(m_centerX, m_centerY)).toPoint());
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    71
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    72
  m_renderer->tick(m_delta);
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
GameViewRenderer::~GameViewRenderer() {}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    76
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    77
void GameViewRenderer::setViewportSize(const QSize& size) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    78
  flibResizeWindow(size.width(), size.height());
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
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    81
void GameViewRenderer::paint() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    82
  if (m_delta == 0) return;
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
  flibGameTick(m_delta);
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
  // m_window->resetOpenGLState();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    87
}