author | sheepluva |
Sat, 06 Jan 2018 03:17:12 +0100 | |
branch | qmlfrontend |
changeset 12887 | bf58f2403953 |
parent 12886 | 07a267d72fce |
child 12888 | adb1fccc706a |
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 |
||
12881
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12868
diff
changeset
|
8 |
#include "gameview.h" |
12866 | 9 |
#include "previewimageprovider.h" |
10 |
#include "runqueue.h" |
|
12862 | 11 |
|
12 |
extern "C" { |
|
13 |
RunEngine_t* flibRunEngine; |
|
12881
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12868
diff
changeset
|
14 |
GameTick_t* flibGameTick; |
12864 | 15 |
ipcToEngineRaw_t* flibIpcToEngineRaw; |
16 |
ipcSetEngineBarrier_t* flibIpcSetEngineBarrier; |
|
17 |
ipcRemoveBarrierFromEngineQueue_t* flibIpcRemoveBarrierFromEngineQueue; |
|
12862 | 18 |
registerUIMessagesCallback_t* flibRegisterUIMessagesCallback; |
19 |
flibInit_t* flibInit; |
|
20 |
flibFree_t* flibFree; |
|
21 |
passFlibEvent_t* flibPassFlibEvent; |
|
22 |
} |
|
23 |
||
24 |
HWEngine::HWEngine(QQmlEngine* engine, QObject* parent) |
|
25 |
: QObject(parent) |
|
26 |
, m_engine(engine) |
|
12866 | 27 |
, m_previewProvider(new PreviewImageProvider()) |
28 |
, m_runQueue(new RunQueue(this)) |
|
12862 | 29 |
{ |
30 |
qRegisterMetaType<MessageType>("MessageType"); |
|
31 |
||
32 |
#ifdef Q_OS_WIN |
|
33 |
QLibrary hwlib("./libhwengine.dll"); |
|
34 |
#else |
|
35 |
QLibrary hwlib("./libhwengine.so"); |
|
36 |
#endif |
|
37 |
||
38 |
if (!hwlib.load()) |
|
39 |
qWarning() << "Engine library not found" << hwlib.errorString(); |
|
40 |
||
41 |
flibRunEngine = (RunEngine_t*)hwlib.resolve("RunEngine"); |
|
12881
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12868
diff
changeset
|
42 |
flibGameTick = (GameTick_t*)hwlib.resolve("GameTick"); |
12864 | 43 |
flibIpcToEngineRaw = (ipcToEngineRaw_t*)hwlib.resolve("ipcToEngineRaw"); |
44 |
flibIpcSetEngineBarrier = (ipcSetEngineBarrier_t*)hwlib.resolve("ipcSetEngineBarrier"); |
|
45 |
flibIpcRemoveBarrierFromEngineQueue = (ipcRemoveBarrierFromEngineQueue_t*)hwlib.resolve("ipcRemoveBarrierFromEngineQueue"); |
|
12862 | 46 |
flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*)hwlib.resolve("registerUIMessagesCallback"); |
47 |
flibInit = (flibInit_t*)hwlib.resolve("flibInit"); |
|
48 |
flibFree = (flibFree_t*)hwlib.resolve("flibFree"); |
|
49 |
||
12887 | 50 |
flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars"); |
12862 | 51 |
flibRegisterUIMessagesCallback(this, &guiMessagesCallback); |
12866 | 52 |
|
53 |
m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider); |
|
54 |
||
55 |
connect(m_runQueue, &RunQueue::previewIsRendering, this, &HWEngine::previewIsRendering); |
|
56 |
connect(this, &HWEngine::gameFinished, m_runQueue, &RunQueue::onGameFinished); |
|
12862 | 57 |
} |
58 |
||
59 |
HWEngine::~HWEngine() |
|
60 |
{ |
|
61 |
flibFree(); |
|
62 |
} |
|
63 |
||
12865 | 64 |
static QObject* hwengine_singletontype_provider(QQmlEngine* engine, QJSEngine* scriptEngine) |
65 |
{ |
|
66 |
Q_UNUSED(scriptEngine) |
|
67 |
||
68 |
HWEngine* hwengine = new HWEngine(engine); |
|
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); |
|
12881
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12868
diff
changeset
|
76 |
qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView"); |
12865 | 77 |
} |
78 |
||
12862 | 79 |
void HWEngine::guiMessagesCallback(void* context, MessageType mt, const char* msg, uint32_t len) |
80 |
{ |
|
81 |
HWEngine* obj = reinterpret_cast<HWEngine*>(context); |
|
82 |
QByteArray b = QByteArray(msg, len); |
|
83 |
||
84 |
qDebug() << "FLIPC in" << mt << " size = " << b.size(); |
|
85 |
||
86 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b)); |
|
87 |
} |
|
88 |
||
89 |
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray& msg) |
|
90 |
{ |
|
91 |
switch (mt) { |
|
92 |
case MSG_PREVIEW: { |
|
12865 | 93 |
qDebug("MSG_PREVIEW"); |
12866 | 94 |
m_previewProvider->setPixmap(msg); |
12862 | 95 |
emit previewImageChanged(); |
96 |
break; |
|
97 |
} |
|
98 |
case MSG_PREVIEWHOGCOUNT: { |
|
12865 | 99 |
qDebug("MSG_PREVIEWHOGCOUNT"); |
12862 | 100 |
emit previewHogCountChanged((quint8)msg.data()[0]); |
101 |
break; |
|
102 |
} |
|
12865 | 103 |
case MSG_TONET: { |
104 |
qDebug("MSG_TONET"); |
|
105 |
break; |
|
106 |
} |
|
107 |
case MSG_GAMEFINISHED: { |
|
108 |
qDebug("MSG_GAMEFINISHED"); |
|
12866 | 109 |
emit gameFinished(); |
12865 | 110 |
break; |
111 |
} |
|
12862 | 112 |
} |
113 |
} |
|
12863
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
114 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
115 |
void HWEngine::getPreview() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
116 |
{ |
12881
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12868
diff
changeset
|
117 |
m_seed = QUuid::createUuid().toByteArray(); |
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12868
diff
changeset
|
118 |
m_gameConfig.cmdSeed(m_seed); |
12868 | 119 |
m_gameConfig.setPreview(true); |
12866 | 120 |
|
12868 | 121 |
m_runQueue->queue(m_gameConfig); |
12863
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
122 |
} |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
123 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
124 |
void HWEngine::runQuickGame() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
125 |
{ |
12881
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12868
diff
changeset
|
126 |
m_gameConfig.cmdSeed(m_seed); |
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12868
diff
changeset
|
127 |
m_gameConfig.cmdTheme("Nature"); |
12868 | 128 |
Team team1; |
129 |
team1.name = "team1"; |
|
130 |
Team team2; |
|
131 |
team2.name = "team2"; |
|
132 |
team2.color = "7654321"; |
|
133 |
m_gameConfig.cmdTeam(team1); |
|
134 |
m_gameConfig.cmdTeam(team2); |
|
135 |
m_gameConfig.setPreview(false); |
|
136 |
||
137 |
m_runQueue->queue(m_gameConfig); |
|
12863
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
138 |
} |