author | unc0rr |
Tue, 26 Dec 2017 01:36:58 +0100 | |
branch | qmlfrontend |
changeset 12866 | 488782d9aba9 |
parent 12865 | e33bcb9d5e9c |
child 12868 | fe16fa088b69 |
permissions | -rw-r--r-- |
12862 | 1 |
#include "hwengine.h" |
2 |
||
3 |
#include <QDebug> |
|
4 |
#include <QLibrary> |
|
12865 | 5 |
#include <QQmlEngine> |
12866 | 6 |
#include <QUuid> |
7 |
||
8 |
#include "previewimageprovider.h" |
|
9 |
#include "runqueue.h" |
|
12862 | 10 |
|
11 |
extern "C" { |
|
12 |
RunEngine_t* flibRunEngine; |
|
12864 | 13 |
ipcToEngineRaw_t* flibIpcToEngineRaw; |
14 |
ipcSetEngineBarrier_t* flibIpcSetEngineBarrier; |
|
15 |
ipcRemoveBarrierFromEngineQueue_t* flibIpcRemoveBarrierFromEngineQueue; |
|
12862 | 16 |
registerUIMessagesCallback_t* flibRegisterUIMessagesCallback; |
17 |
flibInit_t* flibInit; |
|
18 |
flibFree_t* flibFree; |
|
19 |
passFlibEvent_t* flibPassFlibEvent; |
|
20 |
} |
|
21 |
||
22 |
HWEngine::HWEngine(QQmlEngine* engine, QObject* parent) |
|
23 |
: QObject(parent) |
|
24 |
, m_engine(engine) |
|
12866 | 25 |
, m_previewProvider(new PreviewImageProvider()) |
26 |
, m_runQueue(new RunQueue(this)) |
|
12862 | 27 |
{ |
28 |
qRegisterMetaType<MessageType>("MessageType"); |
|
29 |
||
30 |
#ifdef Q_OS_WIN |
|
31 |
QLibrary hwlib("./libhwengine.dll"); |
|
32 |
#else |
|
33 |
QLibrary hwlib("./libhwengine.so"); |
|
34 |
#endif |
|
35 |
||
36 |
if (!hwlib.load()) |
|
37 |
qWarning() << "Engine library not found" << hwlib.errorString(); |
|
38 |
||
39 |
flibRunEngine = (RunEngine_t*)hwlib.resolve("RunEngine"); |
|
12864 | 40 |
flibIpcToEngineRaw = (ipcToEngineRaw_t*)hwlib.resolve("ipcToEngineRaw"); |
41 |
flibIpcSetEngineBarrier = (ipcSetEngineBarrier_t*)hwlib.resolve("ipcSetEngineBarrier"); |
|
42 |
flibIpcRemoveBarrierFromEngineQueue = (ipcRemoveBarrierFromEngineQueue_t*)hwlib.resolve("ipcRemoveBarrierFromEngineQueue"); |
|
12862 | 43 |
flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*)hwlib.resolve("registerUIMessagesCallback"); |
44 |
flibInit = (flibInit_t*)hwlib.resolve("flibInit"); |
|
45 |
flibFree = (flibFree_t*)hwlib.resolve("flibFree"); |
|
46 |
||
47 |
flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars"); |
|
48 |
flibRegisterUIMessagesCallback(this, &guiMessagesCallback); |
|
12866 | 49 |
|
50 |
m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider); |
|
51 |
||
52 |
connect(m_runQueue, &RunQueue::previewIsRendering, this, &HWEngine::previewIsRendering); |
|
53 |
connect(this, &HWEngine::gameFinished, m_runQueue, &RunQueue::onGameFinished); |
|
12862 | 54 |
} |
55 |
||
56 |
HWEngine::~HWEngine() |
|
57 |
{ |
|
58 |
flibFree(); |
|
59 |
} |
|
60 |
||
12865 | 61 |
static QObject* hwengine_singletontype_provider(QQmlEngine* engine, QJSEngine* scriptEngine) |
62 |
{ |
|
63 |
Q_UNUSED(scriptEngine) |
|
64 |
||
65 |
HWEngine* hwengine = new HWEngine(engine); |
|
66 |
return hwengine; |
|
67 |
} |
|
68 |
||
69 |
void HWEngine::exposeToQML() |
|
70 |
{ |
|
71 |
qDebug("HWEngine::exposeToQML"); |
|
72 |
qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider); |
|
73 |
} |
|
74 |
||
12862 | 75 |
void HWEngine::guiMessagesCallback(void* context, MessageType mt, const char* msg, uint32_t len) |
76 |
{ |
|
77 |
HWEngine* obj = reinterpret_cast<HWEngine*>(context); |
|
78 |
QByteArray b = QByteArray(msg, len); |
|
79 |
||
80 |
qDebug() << "FLIPC in" << mt << " size = " << b.size(); |
|
81 |
||
82 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b)); |
|
83 |
} |
|
84 |
||
85 |
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray& msg) |
|
86 |
{ |
|
87 |
switch (mt) { |
|
88 |
case MSG_PREVIEW: { |
|
12865 | 89 |
qDebug("MSG_PREVIEW"); |
12866 | 90 |
m_previewProvider->setPixmap(msg); |
12862 | 91 |
emit previewImageChanged(); |
92 |
break; |
|
93 |
} |
|
94 |
case MSG_PREVIEWHOGCOUNT: { |
|
12865 | 95 |
qDebug("MSG_PREVIEWHOGCOUNT"); |
12862 | 96 |
emit previewHogCountChanged((quint8)msg.data()[0]); |
97 |
break; |
|
98 |
} |
|
12865 | 99 |
case MSG_TONET: { |
100 |
qDebug("MSG_TONET"); |
|
101 |
break; |
|
102 |
} |
|
103 |
case MSG_GAMEFINISHED: { |
|
104 |
qDebug("MSG_GAMEFINISHED"); |
|
12866 | 105 |
emit gameFinished(); |
12865 | 106 |
break; |
107 |
} |
|
12862 | 108 |
} |
109 |
} |
|
12863
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
110 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
111 |
void HWEngine::getPreview() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
112 |
{ |
12865 | 113 |
GameConfig cfg; |
12866 | 114 |
cfg.cmdSeed(QUuid::createUuid().toByteArray()); |
115 |
||
116 |
m_runQueue->queue(cfg); |
|
12863
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
117 |
} |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
118 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
119 |
void HWEngine::runQuickGame() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
120 |
{ |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
121 |
} |