author | unc0rr |
Mon, 22 Sep 2014 02:10:39 +0400 | |
branch | qmlfrontend |
changeset 10420 | 02c573d19224 |
parent 10418 | 091d2c0216c3 |
child 10424 | 4be6cd55f1cf |
permissions | -rw-r--r-- |
10402 | 1 |
#include <QLibrary> |
2 |
#include <QtQml> |
|
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
3 |
#include <QDebug> |
10420 | 4 |
#include <QPainter> |
10402 | 5 |
|
6 |
#include "hwengine.h" |
|
10420 | 7 |
#include "previewimageprovider.h" |
10402 | 8 |
|
9 |
extern "C" { |
|
10416 | 10 |
RunEngine_t *RunEngine; |
11 |
registerIPCCallback_t *registerIPCCallback; |
|
12 |
ipcToEngine_t *ipcToEngine; |
|
13 |
flibInit_t *flibInit; |
|
10402 | 14 |
} |
10420 | 15 |
|
16 |
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) : |
|
17 |
QObject(parent), |
|
18 |
m_engine(engine) |
|
10402 | 19 |
{ |
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
20 |
QLibrary hwlib("./libhwengine.so"); |
10402 | 21 |
|
22 |
if(!hwlib.load()) |
|
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
23 |
qWarning() << "Engine library not found" << hwlib.errorString(); |
10402 | 24 |
|
10416 | 25 |
RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine"); |
26 |
registerIPCCallback = (registerIPCCallback_t*) hwlib.resolve("registerIPCCallback"); |
|
27 |
ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine"); |
|
28 |
flibInit = (flibInit_t*) hwlib.resolve("flibInit"); |
|
29 |
||
30 |
flibInit(); |
|
31 |
registerIPCCallback(this, &engineMessageCallback); |
|
10402 | 32 |
} |
33 |
||
34 |
HWEngine::~HWEngine() |
|
35 |
{ |
|
36 |
||
37 |
} |
|
38 |
||
39 |
void HWEngine::run() |
|
40 |
{ |
|
10416 | 41 |
m_argsList.clear(); |
42 |
m_argsList << ""; |
|
43 |
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"; |
|
49 |
||
50 |
m_args.resize(m_argsList.size()); |
|
51 |
for(int i = m_argsList.size() - 1; i >=0; --i) |
|
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
52 |
m_args[i] = m_argsList[i].constData(); |
10416 | 53 |
|
54 |
RunEngine(m_args.size(), m_args.data()); |
|
10420 | 55 |
sendIPC("eseed helloworld"); |
56 |
sendIPC("e$mapgen 0"); |
|
10416 | 57 |
sendIPC("!"); |
10402 | 58 |
} |
59 |
||
60 |
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
|
61 |
{ |
|
62 |
Q_UNUSED(scriptEngine) |
|
63 |
||
10420 | 64 |
HWEngine *hwengine = new HWEngine(engine); |
10402 | 65 |
return hwengine; |
66 |
} |
|
67 |
||
68 |
void HWEngine::exposeToQML() |
|
69 |
{ |
|
70 |
qDebug("HWEngine::exposeToQML"); |
|
71 |
qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider); |
|
72 |
} |
|
10416 | 73 |
|
74 |
void HWEngine::sendIPC(const QByteArray & b) |
|
75 |
{ |
|
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
76 |
quint8 len = b.size() > 255 ? 255 : b.size(); |
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
77 |
qDebug() << "sendIPC: len = " << len; |
10416 | 78 |
|
10420 | 79 |
ipcToEngine(b.constData(), len); |
80 |
} |
|
81 |
||
82 |
void HWEngine::engineMessageCallback(void *context, const char * msg, quint32 len) |
|
83 |
{ |
|
84 |
HWEngine * obj = (HWEngine *)context; |
|
85 |
QByteArray b = QByteArray::fromRawData(msg, len); |
|
86 |
||
87 |
qDebug() << "FLIPC in" << b.size() << b; |
|
88 |
||
89 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(QByteArray, b)); |
|
10416 | 90 |
} |
91 |
||
10420 | 92 |
void HWEngine::engineMessageHandler(const QByteArray &msg) |
10416 | 93 |
{ |
10420 | 94 |
if(msg.size() == 128 * 256) |
95 |
{ |
|
96 |
QVector<QRgb> colorTable; |
|
97 |
colorTable.resize(256); |
|
98 |
for(int i = 0; i < 256; ++i) |
|
99 |
colorTable[i] = qRgba(255, 255, 0, i); |
|
100 |
||
101 |
const quint8 *buf = (const quint8*) msg.constData(); |
|
102 |
QImage im(buf, 256, 128, QImage::Format_Indexed8); |
|
103 |
im.setColorTable(colorTable); |
|
10416 | 104 |
|
10420 | 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 |
} |
|
10416 | 116 |
} |