qmlfrontend/gameview.cpp
changeset 14175 8354b390f1a2
parent 14174 b273b43b16d2
child 14176 8f82d87d223f
equal deleted inserted replaced
14174:b273b43b16d2 14175:8354b390f1a2
     1 #include "gameview.h"
       
     2 
       
     3 #include <QtQuick/qquickwindow.h>
       
     4 #include <QCursor>
       
     5 #include <QTimer>
       
     6 #include <QtGui/QOpenGLContext>
       
     7 #include <QtGui/QOpenGLShaderProgram>
       
     8 
       
     9 #include "flib.h"
       
    10 
       
    11 extern "C" {
       
    12 extern GameTick_t* flibGameTick;
       
    13 extern ResizeWindow_t* flibResizeWindow;
       
    14 extern updateMousePosition_t* flibUpdateMousePosition;
       
    15 }
       
    16 
       
    17 GameView::GameView() : m_delta(0), m_renderer(nullptr), m_windowChanged(true) {
       
    18   connect(this, &QQuickItem::windowChanged, this,
       
    19           &GameView::handleWindowChanged);
       
    20 }
       
    21 
       
    22 void GameView::tick(quint32 delta) {
       
    23   m_delta = delta;
       
    24 
       
    25   if (window()) {
       
    26     QTimer* timer = new QTimer(this);
       
    27     connect(timer, &QTimer::timeout, window(), &QQuickWindow::update);
       
    28     timer->start(100);
       
    29 
       
    30     // window()->update();
       
    31   }
       
    32 }
       
    33 
       
    34 void GameView::handleWindowChanged(QQuickWindow* win) {
       
    35   if (win) {
       
    36     connect(win, &QQuickWindow::beforeSynchronizing, this, &GameView::sync,
       
    37             Qt::DirectConnection);
       
    38     connect(win, &QQuickWindow::sceneGraphInvalidated, this, &GameView::cleanup,
       
    39             Qt::DirectConnection);
       
    40 
       
    41     win->setClearBeforeRendering(false);
       
    42 
       
    43     m_windowChanged = true;
       
    44   }
       
    45 }
       
    46 
       
    47 void GameView::cleanup() {
       
    48   if (m_renderer) {
       
    49     delete m_renderer;
       
    50     m_renderer = 0;
       
    51   }
       
    52 }
       
    53 
       
    54 void GameView::sync() {
       
    55   if (!m_renderer) {
       
    56     m_renderer = new GameViewRenderer();
       
    57     connect(window(), &QQuickWindow::beforeRendering, m_renderer,
       
    58             &GameViewRenderer::paint, Qt::DirectConnection);
       
    59   }
       
    60 
       
    61   if (m_windowChanged) {
       
    62     QSize windowSize = window()->size();
       
    63     m_renderer->setViewportSize(windowSize * window()->devicePixelRatio());
       
    64     m_centerX = windowSize.width() / 2;
       
    65     m_centerY = windowSize.height() / 2;
       
    66   }
       
    67 
       
    68   QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
       
    69   if (flibUpdateMousePosition(m_centerX, m_centerY, mousePos.x(), mousePos.y()))
       
    70     QCursor::setPos(mapToGlobal(QPointF(m_centerX, m_centerY)).toPoint());
       
    71 
       
    72   m_renderer->tick(m_delta);
       
    73 }
       
    74 
       
    75 GameViewRenderer::~GameViewRenderer() {}
       
    76 
       
    77 void GameViewRenderer::setViewportSize(const QSize& size) {
       
    78   flibResizeWindow(size.width(), size.height());
       
    79 }
       
    80 
       
    81 void GameViewRenderer::paint() {
       
    82   if (m_delta == 0) return;
       
    83 
       
    84   flibGameTick(m_delta);
       
    85 
       
    86   // m_window->resetOpenGLState();
       
    87 }