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