qmlfrontend/hwengine.cpp
changeset 14159 8354b390f1a2
parent 14148 745c73e0e644
child 14276 1aac8a62be6f
--- a/qmlfrontend/hwengine.cpp	Wed Nov 07 15:55:32 2018 +0100
+++ b/qmlfrontend/hwengine.cpp	Wed Nov 07 15:59:51 2018 +0100
@@ -1,72 +1,23 @@
-#include "hwengine.h"
-
 #include <QDebug>
 #include <QLibrary>
 #include <QQmlEngine>
 #include <QUuid>
 
-#include "gameview.h"
-#include "previewimageprovider.h"
-#include "runqueue.h"
+#include "engine_instance.h"
+#include "engine_interface.h"
+#include "game_view.h"
+#include "preview_image_provider.h"
 
-extern "C" {
-RunEngine_t* flibRunEngine;
-GameTick_t* flibGameTick;
-ResizeWindow_t* flibResizeWindow;
-updateMousePosition_t* flibUpdateMousePosition;
-ipcToEngineRaw_t* flibIpcToEngineRaw;
-ipcSetEngineBarrier_t* flibIpcSetEngineBarrier;
-ipcRemoveBarrierFromEngineQueue_t* flibIpcRemoveBarrierFromEngineQueue;
-registerUIMessagesCallback_t* flibRegisterUIMessagesCallback;
-flibInit_t* flibInit;
-flibFree_t* flibFree;
-passFlibEvent_t* flibPassFlibEvent;
-}
+#include "hwengine.h"
 
 HWEngine::HWEngine(QQmlEngine* engine, QObject* parent)
     : QObject(parent),
       m_engine(engine),
-      m_previewProvider(new PreviewImageProvider()),
-      m_runQueue(new RunQueue(this)) {
-  qRegisterMetaType<MessageType>("MessageType");
-
-#ifdef Q_OS_WIN
-  QLibrary hwlib("./libhwengine.dll");
-#else
-  QLibrary hwlib("./libhwengine.so");
-#endif
-
-  if (!hwlib.load())
-    qWarning() << "Engine library not found" << hwlib.errorString();
-
-  flibRunEngine = (RunEngine_t*)hwlib.resolve("RunEngine");
-  flibGameTick = (GameTick_t*)hwlib.resolve("GameTick");
-  flibResizeWindow = (ResizeWindow_t*)hwlib.resolve("ResizeWindow");
-  flibUpdateMousePosition =
-      (updateMousePosition_t*)hwlib.resolve("updateMousePosition");
-  flibIpcToEngineRaw = (ipcToEngineRaw_t*)hwlib.resolve("ipcToEngineRaw");
-  flibIpcSetEngineBarrier =
-      (ipcSetEngineBarrier_t*)hwlib.resolve("ipcSetEngineBarrier");
-  flibIpcRemoveBarrierFromEngineQueue =
-      (ipcRemoveBarrierFromEngineQueue_t*)hwlib.resolve(
-          "ipcRemoveBarrierFromEngineQueue");
-  flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*)hwlib.resolve(
-      "registerUIMessagesCallback");
-  flibInit = (flibInit_t*)hwlib.resolve("flibInit");
-  flibFree = (flibFree_t*)hwlib.resolve("flibFree");
-
-  flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data",
-           "/usr/home/unC0Rr/.hedgewars");
-  flibRegisterUIMessagesCallback(this, &guiMessagesCallback);
-
+      m_previewProvider(new PreviewImageProvider()) {
   m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider);
-
-  connect(m_runQueue, &RunQueue::previewIsRendering, this,
-          &HWEngine::previewIsRendering);
-  connect(this, &HWEngine::gameFinished, m_runQueue, &RunQueue::onGameFinished);
 }
 
-HWEngine::~HWEngine() { flibFree(); }
+HWEngine::~HWEngine() {}
 
 static QObject* hwengine_singletontype_provider(QQmlEngine* engine,
                                                 QJSEngine* scriptEngine) {
@@ -80,44 +31,7 @@
   qDebug("HWEngine::exposeToQML");
   qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine",
                                      hwengine_singletontype_provider);
-  qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
-}
-
-void HWEngine::guiMessagesCallback(void* context, MessageType mt,
-                                   const char* msg, uint32_t len) {
-  HWEngine* obj = reinterpret_cast<HWEngine*>(context);
-  QByteArray b = QByteArray(msg, len);
-
-  qDebug() << "FLIPC in" << mt << " size = " << b.size();
-
-  QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection,
-                            Q_ARG(MessageType, mt), Q_ARG(QByteArray, b));
-}
-
-void HWEngine::engineMessageHandler(MessageType mt, const QByteArray& msg) {
-  switch (mt) {
-    case MSG_PREVIEW: {
-      qDebug("MSG_PREVIEW");
-      m_previewProvider->setPixmap(msg);
-      emit previewImageChanged();
-      break;
-    }
-    case MSG_PREVIEWHOGCOUNT: {
-      qDebug("MSG_PREVIEWHOGCOUNT");
-      m_previewHedgehogsCount = static_cast<quint8>(msg.data()[0]);
-      emit previewHogCountChanged(m_previewHedgehogsCount);
-      break;
-    }
-    case MSG_TONET: {
-      qDebug("MSG_TONET");
-      break;
-    }
-    case MSG_GAMEFINISHED: {
-      qDebug("MSG_GAMEFINISHED");
-      emit gameFinished();
-      break;
-    }
-  }
+  // qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
 }
 
 void HWEngine::getPreview() {
@@ -125,7 +39,22 @@
   m_gameConfig.cmdSeed(m_seed);
   m_gameConfig.setPreview(true);
 
-  m_runQueue->queue(m_gameConfig);
+  EngineInstance engine;
+  Engine::PreviewInfo preview = engine.generatePreview();
+
+  QVector<QRgb> colorTable;
+  colorTable.resize(256);
+  for (int i = 0; i < 256; ++i) colorTable[i] = qRgba(255, 255, 0, i);
+
+  QImage previewImage(preview.land, preview.width, preview.height,
+                      QImage::Format_Indexed8);
+  previewImage.setColorTable(colorTable);
+  previewImage.detach();
+
+  m_previewProvider->setImage(previewImage);
+
+  emit previewImageChanged();
+  // m_runQueue->queue(m_gameConfig);
 }
 
 void HWEngine::runQuickGame() {
@@ -140,7 +69,7 @@
   m_gameConfig.cmdTeam(team2);
   m_gameConfig.setPreview(false);
 
-  m_runQueue->queue(m_gameConfig);
+  // m_runQueue->queue(m_gameConfig);
 }
 
 int HWEngine::previewHedgehogsCount() const { return m_previewHedgehogsCount; }