author | unc0rr |
Sun, 28 Sep 2014 01:17:55 +0400 | |
branch | qmlfrontend |
changeset 10430 | 899a30018ede |
parent 10428 | 7c25297720f1 |
child 10432 | b0abef0ee78c |
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> |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
5 |
#include <QUuid> |
10402 | 6 |
|
7 |
#include "hwengine.h" |
|
10420 | 8 |
#include "previewimageprovider.h" |
10402 | 9 |
|
10 |
extern "C" { |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
11 |
RunEngine_t *flibRunEngine; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
12 |
registerGUIMessagesCallback_t *flibRegisterGUIMessagesCallback; |
10430 | 13 |
setSeed_t *flibSetSeed; |
14 |
getSeed_t *flibGetSeed; |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
15 |
getPreview_t *flibGetPreview; |
10416 | 16 |
flibInit_t *flibInit; |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
17 |
flibFree_t *flibFree; |
10402 | 18 |
} |
10420 | 19 |
|
10430 | 20 |
Q_DECLARE_METATYPE(MessageType); |
21 |
||
10420 | 22 |
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) : |
23 |
QObject(parent), |
|
24 |
m_engine(engine) |
|
10402 | 25 |
{ |
10430 | 26 |
qRegisterMetaType<MessageType>("MessageType"); |
27 |
||
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
28 |
QLibrary hwlib("./libhwengine.so"); |
10402 | 29 |
|
30 |
if(!hwlib.load()) |
|
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
31 |
qWarning() << "Engine library not found" << hwlib.errorString(); |
10402 | 32 |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
33 |
flibRunEngine = (RunEngine_t*) hwlib.resolve("RunEngine"); |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
34 |
flibRegisterGUIMessagesCallback = (registerGUIMessagesCallback_t*) hwlib.resolve("registerGUIMessagesCallback"); |
10430 | 35 |
flibSetSeed = (setSeed_t*) hwlib.resolve("setSeed"); |
36 |
flibGetSeed = (getSeed_t*) hwlib.resolve("getSeed"); |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
37 |
flibGetPreview = (getPreview_t*) hwlib.resolve("getPreview"); |
10416 | 38 |
flibInit = (flibInit_t*) hwlib.resolve("flibInit"); |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
39 |
flibFree = (flibFree_t*) hwlib.resolve("flibFree"); |
10416 | 40 |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
41 |
flibInit(".", "~/.hedgewars"); |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
42 |
flibRegisterGUIMessagesCallback(this, &guiMessagesCallback); |
10402 | 43 |
} |
44 |
||
45 |
HWEngine::~HWEngine() |
|
46 |
{ |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
47 |
flibFree(); |
10402 | 48 |
} |
49 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
50 |
void HWEngine::getPreview() |
10402 | 51 |
{ |
10430 | 52 |
flibSetSeed(QUuid::createUuid().toString().toLatin1()); |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
53 |
flibGetPreview(); |
10402 | 54 |
} |
55 |
||
56 |
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
|
57 |
{ |
|
58 |
Q_UNUSED(scriptEngine) |
|
59 |
||
10420 | 60 |
HWEngine *hwengine = new HWEngine(engine); |
10402 | 61 |
return hwengine; |
62 |
} |
|
63 |
||
64 |
void HWEngine::exposeToQML() |
|
65 |
{ |
|
66 |
qDebug("HWEngine::exposeToQML"); |
|
67 |
qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider); |
|
68 |
} |
|
10416 | 69 |
|
70 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
71 |
void HWEngine::guiMessagesCallback(void *context, MessageType mt, const char * msg, uint32_t len) |
10420 | 72 |
{ |
73 |
HWEngine * obj = (HWEngine *)context; |
|
74 |
QByteArray b = QByteArray::fromRawData(msg, len); |
|
75 |
||
76 |
qDebug() << "FLIPC in" << b.size() << b; |
|
77 |
||
10430 | 78 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b)); |
10416 | 79 |
} |
80 |
||
10430 | 81 |
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray &msg) |
10416 | 82 |
{ |
10430 | 83 |
switch(mt) |
10426 | 84 |
{ |
10430 | 85 |
case MSG_PREVIEW: |
10426 | 86 |
PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview")); |
87 |
preview->setPixmap(msg); |
|
88 |
emit previewImageChanged(); |
|
10430 | 89 |
break; |
10426 | 90 |
} |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
91 |
} |
10416 | 92 |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
93 |
QString HWEngine::currentSeed() |
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
94 |
{ |
10430 | 95 |
return QString::fromLatin1(flibGetSeed()); |
10416 | 96 |
} |