Prepare to have possibility to pass opengl context to engine
authorunc0rr
Sat, 24 Nov 2018 23:51:45 +0100
changeset 14295 92e5682810d4
parent 14294 12618cd83af8
child 14296 f1a5b7baa87f
Prepare to have possibility to pass opengl context to engine
qmlfrontend/CMakeLists.txt
qmlfrontend/engine_instance.cpp
qmlfrontend/engine_instance.h
qmlfrontend/game_view.cpp
qmlfrontend/game_view.h
--- a/qmlfrontend/CMakeLists.txt	Sat Nov 24 21:57:01 2018 +0100
+++ b/qmlfrontend/CMakeLists.txt	Sat Nov 24 23:51:45 2018 +0100
@@ -11,6 +11,7 @@
 add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc"
     "hwengine.cpp" "hwengine.h"
     "game_config.cpp" "game_config.h"
+    "game_view.cpp" "game_view.h"
     "team.cpp" "team.h"
     "engine_instance.cpp" "engine_instance.h"
     "preview_image_provider.cpp" "preview_image_provider.h"
--- a/qmlfrontend/engine_instance.cpp	Sat Nov 24 21:57:01 2018 +0100
+++ b/qmlfrontend/engine_instance.cpp	Sat Nov 24 23:51:45 2018 +0100
@@ -1,17 +1,23 @@
 #include "engine_instance.h"
 
-EngineInstance::EngineInstance(QObject *parent)
+EngineInstance::EngineInstance(QObject* parent)
     : QObject(parent), m_instance(Engine::start_engine()) {}
 
 EngineInstance::~EngineInstance() { Engine::cleanup(m_instance); }
 
-void EngineInstance::sendConfig(const GameConfig &config) {
+void EngineInstance::sendConfig(const GameConfig& config) {
   for (auto b : config.config()) {
-    Engine::send_ipc(m_instance, reinterpret_cast<uint8_t *>(b.data()),
+    Engine::send_ipc(m_instance, reinterpret_cast<uint8_t*>(b.data()),
                      static_cast<size_t>(b.size()));
   }
 }
 
+void EngineInstance::advance(quint32 ticks) {}
+
+void EngineInstance::renderFrame() {}
+
+void EngineInstance::setOpenGLContext(QOpenGLContext* context) {}
+
 Engine::PreviewInfo EngineInstance::generatePreview() {
   Engine::PreviewInfo pinfo;
 
--- a/qmlfrontend/engine_instance.h	Sat Nov 24 21:57:01 2018 +0100
+++ b/qmlfrontend/engine_instance.h	Sat Nov 24 23:51:45 2018 +0100
@@ -4,16 +4,20 @@
 #include "engine_interface.h"
 
 #include <QObject>
+#include <QOpenGLContext>
 
 #include "game_config.h"
 
 class EngineInstance : public QObject {
   Q_OBJECT
  public:
-  explicit EngineInstance(QObject *parent = nullptr);
+  explicit EngineInstance(QObject* parent = nullptr);
   ~EngineInstance();
 
-  void sendConfig(const GameConfig &config);
+  void sendConfig(const GameConfig& config);
+  void advance(quint32 ticks);
+  void renderFrame();
+  void setOpenGLContext(QOpenGLContext* context);
   Engine::PreviewInfo generatePreview();
 
  signals:
@@ -21,7 +25,7 @@
  public slots:
 
  private:
-  Engine::EngineInstance *m_instance;
+  Engine::EngineInstance* m_instance;
 };
 
 #endif  // ENGINEINSTANCE_H
--- a/qmlfrontend/game_view.cpp	Sat Nov 24 21:57:01 2018 +0100
+++ b/qmlfrontend/game_view.cpp	Sat Nov 24 23:51:45 2018 +0100
@@ -1,4 +1,4 @@
-#include "gameview.h"
+#include "game_view.h"
 
 #include <QtQuick/qquickwindow.h>
 #include <QCursor>
@@ -6,15 +6,7 @@
 #include <QtGui/QOpenGLContext>
 #include <QtGui/QOpenGLShaderProgram>
 
-#include "flib.h"
-
-extern "C" {
-extern GameTick_t* flibGameTick;
-extern ResizeWindow_t* flibResizeWindow;
-extern updateMousePosition_t* flibUpdateMousePosition;
-}
-
-GameView::GameView() : m_delta(0), m_renderer(nullptr), m_windowChanged(true) {
+GameView::GameView() : m_delta(0), m_windowChanged(true) {
   connect(this, &QQuickItem::windowChanged, this,
           &GameView::handleWindowChanged);
 }
@@ -31,6 +23,8 @@
   }
 }
 
+EngineInstance* GameView::engineInstance() const { return m_engineInstance; }
+
 void GameView::handleWindowChanged(QQuickWindow* win) {
   if (win) {
     connect(win, &QQuickWindow::beforeSynchronizing, this, &GameView::sync,
@@ -44,17 +38,21 @@
   }
 }
 
-void GameView::cleanup() {
-  if (m_renderer) {
-    delete m_renderer;
-    m_renderer = 0;
-  }
+void GameView::cleanup() { m_renderer.reset(); }
+
+void GameView::setEngineInstance(EngineInstance* engineInstance) {
+  if (m_engineInstance == engineInstance) return;
+
+  cleanup();
+  m_engineInstance = engineInstance;
+  engineInstance->setOpenGLContext(window()->openglContext());
+  emit engineInstanceChanged(m_engineInstance);
 }
 
 void GameView::sync() {
   if (!m_renderer) {
-    m_renderer = new GameViewRenderer();
-    connect(window(), &QQuickWindow::beforeRendering, m_renderer,
+    m_renderer.reset(new GameViewRenderer());
+    connect(window(), &QQuickWindow::beforeRendering, m_renderer.data(),
             &GameViewRenderer::paint, Qt::DirectConnection);
   }
 
@@ -65,23 +63,36 @@
     m_centerY = windowSize.height() / 2;
   }
 
-  QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
-  if (flibUpdateMousePosition(m_centerX, m_centerY, mousePos.x(), mousePos.y()))
-    QCursor::setPos(mapToGlobal(QPointF(m_centerX, m_centerY)).toPoint());
+  // QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
+  // if (flibUpdateMousePosition(m_centerX, m_centerY, mousePos.x(),
+  // mousePos.y()))
+  //  QCursor::setPos(mapToGlobal(QPointF(m_centerX, m_centerY)).toPoint());
 
   m_renderer->tick(m_delta);
 }
 
+GameViewRenderer::GameViewRenderer()
+    : QObject(), m_delta(0), m_engineInstance(nullptr) {}
+
 GameViewRenderer::~GameViewRenderer() {}
 
+void GameViewRenderer::tick(quint32 delta) { m_delta = delta; }
+
 void GameViewRenderer::setViewportSize(const QSize& size) {
-  flibResizeWindow(size.width(), size.height());
+  // flibResizeWindow(size.width(), size.height());
+}
+
+void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) {
+  m_engineInstance = engineInstance;
 }
 
 void GameViewRenderer::paint() {
   if (m_delta == 0) return;
 
-  flibGameTick(m_delta);
+  if (m_engineInstance) {
+    m_engineInstance->advance(m_delta);
+    m_engineInstance->renderFrame();
+  }
 
   // m_window->resetOpenGLState();
 }
--- a/qmlfrontend/game_view.h	Sat Nov 24 21:57:01 2018 +0100
+++ b/qmlfrontend/game_view.h	Sat Nov 24 23:51:45 2018 +0100
@@ -3,49 +3,62 @@
 
 #include <QQuickItem>
 
+#include <QPointer>
+#include <QScopedPointer>
 #include <QtGui/QOpenGLFunctions>
 #include <QtGui/QOpenGLShaderProgram>
 
+#include "engine_instance.h"
+
 class GameViewRenderer : public QObject, protected QOpenGLFunctions {
   Q_OBJECT
  public:
-  GameViewRenderer() : m_delta(0) {}
+  GameViewRenderer();
   ~GameViewRenderer();
 
-  void tick(quint32 delta) { m_delta = delta; }
+  void tick(quint32 delta);
   void setViewportSize(const QSize& size);
+  void setEngineInstance(EngineInstance* engineInstance);
 
  public slots:
   void paint();
 
  private:
   quint32 m_delta;
+  QPointer<EngineInstance> m_engineInstance;
 };
 
 class GameView : public QQuickItem {
   Q_OBJECT
 
+  Q_PROPERTY(EngineInstance* engineInstance READ engineInstance WRITE
+                 setEngineInstance NOTIFY engineInstanceChanged)
+
  public:
   GameView();
 
   Q_INVOKABLE void tick(quint32 delta);
 
+  EngineInstance* engineInstance() const;
+
  signals:
-  void tChanged();
+  void engineInstanceChanged(EngineInstance* engineInstance);
 
  public slots:
   void sync();
   void cleanup();
+  void setEngineInstance(EngineInstance* engineInstance);
 
  private slots:
   void handleWindowChanged(QQuickWindow* win);
 
  private:
   quint32 m_delta;
-  GameViewRenderer* m_renderer;
+  QScopedPointer<GameViewRenderer> m_renderer;
   bool m_windowChanged;
   qint32 m_centerX;
   qint32 m_centerY;
+  QPointer<EngineInstance> m_engineInstance;
 };
 
 #endif  // GAMEVIEW_H