Pass opengl context to engine
authorunC0Rr
Mon, 26 Nov 2018 14:48:38 +0100
changeset 14303 00b56ec8b7df
parent 14302 a6c5ce147530
child 14304 5cff42e1dd92
Pass opengl context to engine
qmlfrontend/Page1.qml
qmlfrontend/Page1Form.ui.qml
qmlfrontend/engine_instance.cpp
qmlfrontend/engine_interface.h
qmlfrontend/game_view.cpp
qmlfrontend/hwengine.cpp
qmlfrontend/hwengine.h
--- a/qmlfrontend/Page1.qml	Mon Nov 26 14:31:36 2018 +0100
+++ b/qmlfrontend/Page1.qml	Mon Nov 26 14:48:38 2018 +0100
@@ -6,7 +6,8 @@
     gameView.tick(100)
   }
   gameButton.onClicked: {
-    HWEngine.runQuickGame()
+    var engineInstance = HWEngine.runQuickGame()
+    gameView.engineInstance = engineInstance
   }
   button1.onClicked: {
     HWEngine.getPreview()
--- a/qmlfrontend/Page1Form.ui.qml	Mon Nov 26 14:31:36 2018 +0100
+++ b/qmlfrontend/Page1Form.ui.qml	Mon Nov 26 14:48:38 2018 +0100
@@ -40,8 +40,8 @@
       border.width: 5
       radius: 5
 
+      Layout.minimumHeight: 256
       Layout.fillWidth: true
-      Layout.fillHeight: true
 
       gradient: Gradient {
         GradientStop {
@@ -72,12 +72,12 @@
         cache: false
       }
     }
-  }
-  GameView {
-    id: gameView
-    x: 8
-    y: 154
-    width: 1008
-    height: 638
+
+    GameView {
+      id: gameView
+
+      Layout.fillWidth: true
+      Layout.fillHeight: true
+    }
   }
 }
--- a/qmlfrontend/engine_instance.cpp	Mon Nov 26 14:31:36 2018 +0100
+++ b/qmlfrontend/engine_instance.cpp	Mon Nov 26 14:48:38 2018 +0100
@@ -1,6 +1,16 @@
 #include "engine_instance.h"
 
-extern "C" void (*getProcAddress())(const char* fn) { return nullptr; }
+#include <QDebug>
+#include <QOpenGLFunctions>
+#include <QSurface>
+
+static QOpenGLContext* currentOpenglContext = nullptr;
+extern "C" void (*getProcAddress(const char* fn))() {
+  if (!currentOpenglContext)
+    return nullptr;
+  else
+    return currentOpenglContext->getProcAddress(fn);
+}
 
 EngineInstance::EngineInstance(QObject* parent)
     : QObject(parent), m_instance(Engine::start_engine()) {}
@@ -14,12 +24,19 @@
   }
 }
 
-void EngineInstance::advance(quint32 ticks) {}
+void EngineInstance::advance(quint32 ticks) {
+  Engine::advance_simulation(m_instance, ticks);
+}
 
-void EngineInstance::renderFrame() {}
+void EngineInstance::renderFrame() { Engine::render_frame(m_instance); }
 
 void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
-  Engine::setup_current_gl_context(m_instance, 0, 0, &getProcAddress);
+  currentOpenglContext = context;
+
+  auto size = context->surface()->size();
+  Engine::setup_current_gl_context(
+      m_instance, static_cast<quint16>(size.width()),
+      static_cast<quint16>(size.height()), &getProcAddress);
 }
 
 Engine::PreviewInfo EngineInstance::generatePreview() {
--- a/qmlfrontend/engine_interface.h	Mon Nov 26 14:31:36 2018 +0100
+++ b/qmlfrontend/engine_interface.h	Mon Nov 26 14:48:38 2018 +0100
@@ -31,7 +31,7 @@
 
 typedef void setup_current_gl_context_t(EngineInstance* engine_state,
                                         uint16_t width, uint16_t height,
-                                        void (*())(const char*));
+                                        void (*(const char*))());
 typedef void render_frame_t(EngineInstance* engine_state);
 
 typedef bool advance_simulation_t(EngineInstance* engine_state, uint32_t ticks);
--- a/qmlfrontend/game_view.cpp	Mon Nov 26 14:31:36 2018 +0100
+++ b/qmlfrontend/game_view.cpp	Mon Nov 26 14:48:38 2018 +0100
@@ -45,13 +45,15 @@
 
   cleanup();
   m_engineInstance = engineInstance;
-  engineInstance->setOpenGLContext(window()->openglContext());
+
   emit engineInstanceChanged(m_engineInstance);
 }
 
 void GameView::sync() {
-  if (!m_renderer) {
+  if (!m_renderer && m_engineInstance) {
+    m_engineInstance->setOpenGLContext(window()->openglContext());
     m_renderer.reset(new GameViewRenderer());
+    m_renderer->setEngineInstance(m_engineInstance);
     connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(),
             &GameViewRenderer::paint, Qt::DirectConnection);
   }
@@ -68,7 +70,7 @@
   // mousePos.y()))
   //  QCursor::setPos(mapToGlobal(QPointF(m_centerX, m_centerY)).toPoint());
 
-  m_renderer->tick(m_delta);
+  if (m_renderer) m_renderer->tick(m_delta);
 }
 
 GameViewRenderer::GameViewRenderer()
--- a/qmlfrontend/hwengine.cpp	Mon Nov 26 14:31:36 2018 +0100
+++ b/qmlfrontend/hwengine.cpp	Mon Nov 26 14:48:38 2018 +0100
@@ -32,6 +32,9 @@
   qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine",
                                      hwengine_singletontype_provider);
   qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
+  qmlRegisterUncreatableType<EngineInstance>("Hedgewars.Engine", 1, 0,
+                                             "EngineInstance",
+                                             "Create by HWEngine run methods");
 }
 
 void HWEngine::getPreview() {
@@ -61,7 +64,7 @@
   // m_runQueue->queue(m_gameConfig);
 }
 
-void HWEngine::runQuickGame() {
+EngineInstance* HWEngine::runQuickGame() {
   m_gameConfig.cmdTheme("Nature");
   Team team1;
   team1.name = "team1";
@@ -71,6 +74,8 @@
   m_gameConfig.cmdTeam(team1);
   m_gameConfig.cmdTeam(team2);
 
+  EngineInstance* engine = new EngineInstance(this);
+  return engine;
   // m_runQueue->queue(m_gameConfig);
 }
 
--- a/qmlfrontend/hwengine.h	Mon Nov 26 14:31:36 2018 +0100
+++ b/qmlfrontend/hwengine.h	Mon Nov 26 14:48:38 2018 +0100
@@ -9,6 +9,7 @@
 
 class QQmlEngine;
 class PreviewImageProvider;
+class EngineInstance;
 
 class HWEngine : public QObject {
   Q_OBJECT
@@ -23,7 +24,7 @@
   static void exposeToQML();
 
   Q_INVOKABLE void getPreview();
-  Q_INVOKABLE void runQuickGame();
+  Q_INVOKABLE EngineInstance* runQuickGame();
 
   int previewHedgehogsCount() const;