qmlfrontend/game_view.cpp
changeset 15922 ebc50f21e849
parent 14863 ef9630519491
child 15923 9b8d4f34e0f3
equal deleted inserted replaced
15921:5b3beb90e1a6 15922:ebc50f21e849
    40 }
    40 }
    41 
    41 
    42 void GameView::cleanup() { m_renderer.reset(); }
    42 void GameView::cleanup() { m_renderer.reset(); }
    43 
    43 
    44 void GameView::setEngineInstance(EngineInstance* engineInstance) {
    44 void GameView::setEngineInstance(EngineInstance* engineInstance) {
    45   if (m_engineInstance == engineInstance) return;
    45   if (m_engineInstance == engineInstance) {
       
    46     return;
       
    47   }
    46 
    48 
    47   cleanup();
    49   cleanup();
    48   m_engineInstance = engineInstance;
    50   m_engineInstance = engineInstance;
    49 
    51 
    50   emit engineInstanceChanged(m_engineInstance);
    52   emit engineInstanceChanged(m_engineInstance);
    57     m_renderer->setEngineInstance(m_engineInstance);
    59     m_renderer->setEngineInstance(m_engineInstance);
    58     connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(),
    60     connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(),
    59             &GameViewRenderer::paint, Qt::DirectConnection);
    61             &GameViewRenderer::paint, Qt::DirectConnection);
    60   }
    62   }
    61 
    63 
    62   if (m_windowChanged || (m_viewportSize != window()->size())) {
    64   if (m_windowChanged || (m_viewportSize != size())) {
    63     m_windowChanged = false;
    65     m_windowChanged = false;
    64 
    66 
    65     if (m_engineInstance)
    67     if (m_engineInstance)
    66       m_engineInstance->setOpenGLContext(window()->openglContext());
    68       m_engineInstance->setOpenGLContext(window()->openglContext());
    67 
    69 
    68     m_viewportSize = window()->size();
    70     m_viewportSize = size().toSize();
    69     m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2;
    71     m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2;
    70   }
    72   }
    71 
    73 
    72   if (m_engineInstance) {
    74   if (m_engineInstance) {
    73     QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
    75     QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
    74     m_engineInstance->moveCamera(mousePos - m_centerPoint);
    76     m_engineInstance->moveCamera(mousePos - m_centerPoint);
    75     QCursor::setPos(mapToGlobal(m_centerPoint).toPoint());
    77     QCursor::setPos(mapToGlobal(m_centerPoint).toPoint());
    76   }
    78   }
    77 
    79 
    78   if (m_renderer) m_renderer->tick(m_delta);
    80   if (m_renderer) {
       
    81     m_renderer->tick(m_delta);
       
    82   }
    79 }
    83 }
    80 
    84 
    81 GameViewRenderer::GameViewRenderer()
    85 GameViewRenderer::GameViewRenderer()
    82     : QObject(), m_delta(0), m_engineInstance(nullptr) {}
    86     : QObject(), m_delta(0), m_engineInstance(nullptr) {}
    83 
    87 
    88 void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) {
    92 void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) {
    89   m_engineInstance = engineInstance;
    93   m_engineInstance = engineInstance;
    90 }
    94 }
    91 
    95 
    92 void GameViewRenderer::paint() {
    96 void GameViewRenderer::paint() {
    93   if (m_delta == 0) return;
    97   if (m_delta == 0) {
       
    98     return;
       
    99   }
    94 
   100 
    95   if (m_engineInstance) {
   101   if (m_engineInstance) {
    96     m_engineInstance->advance(m_delta);
   102     m_engineInstance->advance(m_delta);
    97     m_engineInstance->renderFrame();
   103     m_engineInstance->renderFrame();
    98   }
   104   }