qmlFrontend/hwengine.cpp
branchqmlfrontend
changeset 10420 02c573d19224
parent 10418 091d2c0216c3
child 10424 4be6cd55f1cf
--- a/qmlFrontend/hwengine.cpp	Sun Sep 21 00:37:50 2014 +0400
+++ b/qmlFrontend/hwengine.cpp	Mon Sep 22 02:10:39 2014 +0400
@@ -1,8 +1,10 @@
 #include <QLibrary>
 #include <QtQml>
 #include <QDebug>
+#include <QPainter>
 
 #include "hwengine.h"
+#include "previewimageprovider.h"
 
 extern "C" {
     RunEngine_t *RunEngine;
@@ -10,8 +12,10 @@
     ipcToEngine_t *ipcToEngine;
     flibInit_t *flibInit;
 }
-HWEngine::HWEngine(QObject *parent) :
-    QObject(parent)
+
+HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) :
+    QObject(parent),
+    m_engine(engine)
 {
     QLibrary hwlib("./libhwengine.so");
 
@@ -48,15 +52,16 @@
         m_args[i] = m_argsList[i].constData();
 
     RunEngine(m_args.size(), m_args.data());
+    sendIPC("eseed helloworld");
+    sendIPC("e$mapgen 0");
     sendIPC("!");
 }
 
 static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
 {
-    Q_UNUSED(engine)
     Q_UNUSED(scriptEngine)
 
-    HWEngine *hwengine = new HWEngine();
+    HWEngine *hwengine = new HWEngine(engine);
     return hwengine;
 }
 
@@ -71,12 +76,41 @@
     quint8 len = b.size() > 255 ? 255 : b.size();
     qDebug() << "sendIPC: len = " << len;
 
-    ipcToEngine(len, b.constData());
+    ipcToEngine(b.constData(), len);
+}
+
+void HWEngine::engineMessageCallback(void *context, const char * msg, quint32 len)
+{
+    HWEngine * obj = (HWEngine *)context;
+    QByteArray b = QByteArray::fromRawData(msg, len);
+
+    qDebug() << "FLIPC in" << b.size() << b;
+
+    QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(QByteArray, b));
 }
 
-void HWEngine::engineMessageCallback(void *context, quint8 len, const char *msg)
+void HWEngine::engineMessageHandler(const QByteArray &msg)
 {
-    QByteArray b = QByteArray::fromRawData(msg, len);
+    if(msg.size() == 128 * 256)
+    {
+        QVector<QRgb> colorTable;
+        colorTable.resize(256);
+        for(int i = 0; i < 256; ++i)
+            colorTable[i] = qRgba(255, 255, 0, i);
+
+        const quint8 *buf = (const quint8*) msg.constData();
+        QImage im(buf, 256, 128, QImage::Format_Indexed8);
+        im.setColorTable(colorTable);
 
-    qDebug() << "FLIPC in" << b;
+        QPixmap px = QPixmap::fromImage(im, Qt::ColorOnly);
+        //QPixmap pxres(px.size());
+        //QPainter p(&pxres);
+
+        //p.fillRect(pxres.rect(), linearGrad);
+        //p.drawPixmap(0, 0, px);
+
+        PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview"));
+        preview->setPixmap(px);
+        emit previewImageChanged();
+    }
 }