1 #include <QLibrary> |
1 #include <QLibrary> |
2 #include <QtQml> |
2 #include <QtQml> |
3 #include <QDebug> |
3 #include <QDebug> |
4 #include <QPainter> |
4 #include <QPainter> |
|
5 #include <QUuid> |
5 |
6 |
6 #include "hwengine.h" |
7 #include "hwengine.h" |
7 #include "previewimageprovider.h" |
8 #include "previewimageprovider.h" |
8 |
9 |
9 extern "C" { |
10 extern "C" { |
10 RunEngine_t *RunEngine; |
11 RunEngine_t *RunEngine; |
11 registerIPCCallback_t *registerIPCCallback; |
12 registerIPCCallback_t *registerIPCCallback; |
12 ipcToEngine_t *ipcToEngine; |
13 ipcToEngine_t *ipcToEngine; |
13 flibInit_t *flibInit; |
14 flibInit_t *flibInit; |
|
15 flibFree_t *flibFree; |
14 } |
16 } |
15 |
17 |
16 HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) : |
18 HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) : |
17 QObject(parent), |
19 QObject(parent), |
18 m_engine(engine) |
20 m_engine(engine) |
24 |
26 |
25 RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine"); |
27 RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine"); |
26 registerIPCCallback = (registerIPCCallback_t*) hwlib.resolve("registerIPCCallback"); |
28 registerIPCCallback = (registerIPCCallback_t*) hwlib.resolve("registerIPCCallback"); |
27 ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine"); |
29 ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine"); |
28 flibInit = (flibInit_t*) hwlib.resolve("flibInit"); |
30 flibInit = (flibInit_t*) hwlib.resolve("flibInit"); |
|
31 flibFree = (flibFree_t*) hwlib.resolve("flibFree"); |
29 |
32 |
30 flibInit(); |
33 flibInit(".", "~/.hedgewars"); |
31 registerIPCCallback(this, &engineMessageCallback); |
34 registerIPCCallback(this, &engineMessageCallback); |
32 } |
35 } |
33 |
36 |
34 HWEngine::~HWEngine() |
37 HWEngine::~HWEngine() |
35 { |
38 { |
36 |
39 flibFree(); |
37 } |
40 } |
38 |
41 |
39 void HWEngine::run() |
42 void HWEngine::run() |
40 { |
43 { |
41 m_argsList.clear(); |
44 m_argsList.clear(); |
42 m_argsList << ""; |
45 m_argsList << ""; |
43 m_argsList << "--internal"; |
46 m_argsList << "--internal"; |
44 //m_argsList << "--user-prefix"; |
|
45 //m_argsList << cfgdir->absolutePath(); |
|
46 //m_argsList << "--prefix"; |
|
47 //m_argsList << datadir->absolutePath(); |
|
48 m_argsList << "--landpreview"; |
47 m_argsList << "--landpreview"; |
49 |
48 |
50 m_args.resize(m_argsList.size()); |
49 m_args.resize(m_argsList.size()); |
51 for(int i = m_argsList.size() - 1; i >=0; --i) |
50 for(int i = m_argsList.size() - 1; i >=0; --i) |
52 m_args[i] = m_argsList[i].constData(); |
51 m_args[i] = m_argsList[i].constData(); |
53 |
52 |
|
53 m_seed = QUuid::createUuid().toString(); |
|
54 |
54 RunEngine(m_args.size(), m_args.data()); |
55 RunEngine(m_args.size(), m_args.data()); |
55 sendIPC("eseed helloworld"); |
56 sendIPC("eseed " + m_seed.toLatin1()); |
56 sendIPC("e$mapgen 0"); |
57 sendIPC("e$mapgen 0"); |
57 sendIPC("!"); |
58 sendIPC("!"); |
58 } |
59 } |
59 |
60 |
60 static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
61 static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
89 QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(QByteArray, b)); |
90 QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(QByteArray, b)); |
90 } |
91 } |
91 |
92 |
92 void HWEngine::engineMessageHandler(const QByteArray &msg) |
93 void HWEngine::engineMessageHandler(const QByteArray &msg) |
93 { |
94 { |
94 if(msg.size() == 128 * 256) |
95 PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview")); |
95 { |
96 preview->setPixmap(msg); |
96 QVector<QRgb> colorTable; |
97 emit previewImageChanged(); |
97 colorTable.resize(256); |
98 } |
98 for(int i = 0; i < 256; ++i) |
|
99 colorTable[i] = qRgba(255, 255, 0, i); |
|
100 |
99 |
101 const quint8 *buf = (const quint8*) msg.constData(); |
100 QString HWEngine::currentSeed() |
102 QImage im(buf, 256, 128, QImage::Format_Indexed8); |
101 { |
103 im.setColorTable(colorTable); |
102 return m_seed; |
104 |
|
105 QPixmap px = QPixmap::fromImage(im, Qt::ColorOnly); |
|
106 //QPixmap pxres(px.size()); |
|
107 //QPainter p(&pxres); |
|
108 |
|
109 //p.fillRect(pxres.rect(), linearGrad); |
|
110 //p.drawPixmap(0, 0, px); |
|
111 |
|
112 PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview")); |
|
113 preview->setPixmap(px); |
|
114 emit previewImageChanged(); |
|
115 } |
|
116 } |
103 } |