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