Detect and handle resize
authorunc0rr
Fri, 22 Mar 2019 21:06:12 +0100
changeset 14733 57293f34ce59
parent 14732 40809bfd44af
child 14734 cc6ab1e3f7d5
Detect and handle resize
qmlfrontend/game_view.cpp
qmlfrontend/game_view.h
--- a/qmlfrontend/game_view.cpp	Fri Mar 22 22:26:41 2019 +0300
+++ b/qmlfrontend/game_view.cpp	Fri Mar 22 21:06:12 2019 +0100
@@ -58,11 +58,13 @@
             &GameViewRenderer::paint, Qt::DirectConnection);
   }
 
-  if (m_windowChanged) {
-    QSize windowSize = window()->size();
-    m_renderer->setViewportSize(windowSize * window()->devicePixelRatio());
-    m_centerX = windowSize.width() / 2;
-    m_centerY = windowSize.height() / 2;
+  if (m_windowChanged || (m_viewportSize != window()->size())) {
+    m_windowChanged = false;
+
+    if (m_engineInstance)
+      m_engineInstance->setOpenGLContext(window()->openglContext());
+
+    m_viewportSize = window()->size();
   }
 
   // QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
@@ -80,10 +82,6 @@
 
 void GameViewRenderer::tick(quint32 delta) { m_delta = delta; }
 
-void GameViewRenderer::setViewportSize(const QSize& size) {
-  // flibResizeWindow(size.width(), size.height());
-}
-
 void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) {
   m_engineInstance = engineInstance;
 }
@@ -98,3 +96,8 @@
 
   // m_window->resetOpenGLState();
 }
+
+void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) {
+  if (m_engineInstance)
+    m_engineInstance->setOpenGLContext(window->openglContext());
+}
--- a/qmlfrontend/game_view.h	Fri Mar 22 22:26:41 2019 +0300
+++ b/qmlfrontend/game_view.h	Fri Mar 22 21:06:12 2019 +0100
@@ -13,15 +13,15 @@
 class GameViewRenderer : public QObject, protected QOpenGLFunctions {
   Q_OBJECT
  public:
-  GameViewRenderer();
-  ~GameViewRenderer();
+  explicit GameViewRenderer();
+  ~GameViewRenderer() override;
 
   void tick(quint32 delta);
-  void setViewportSize(const QSize& size);
   void setEngineInstance(EngineInstance* engineInstance);
 
  public slots:
   void paint();
+  void onViewportSizeChanged(QQuickWindow* window);
 
  private:
   quint32 m_delta;
@@ -35,7 +35,7 @@
                  setEngineInstance NOTIFY engineInstanceChanged)
 
  public:
-  GameView();
+  explicit GameView();
 
   Q_INVOKABLE void tick(quint32 delta);
 
@@ -56,9 +56,8 @@
   quint32 m_delta;
   QScopedPointer<GameViewRenderer> m_renderer;
   bool m_windowChanged;
-  qint32 m_centerX;
-  qint32 m_centerY;
   QPointer<EngineInstance> m_engineInstance;
+  QSize m_viewportSize;
 };
 
 #endif  // GAMEVIEW_H