author | unc0rr |
Fri, 05 Jan 2018 22:32:11 +0100 | |
branch | qmlfrontend |
changeset 12880 | 590c7f324292 |
parent 12876 | b544bbbd0696 |
child 12881 | 07a267d72fce |
permissions | -rw-r--r-- |
12857 | 1 |
#include "hwengine.h" |
2 |
||
3 |
#include <QDebug> |
|
4 |
#include <QLibrary> |
|
12860 | 5 |
#include <QQmlEngine> |
12861 | 6 |
#include <QUuid> |
7 |
||
12876
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12863
diff
changeset
|
8 |
#include "gameview.h" |
12861 | 9 |
#include "previewimageprovider.h" |
10 |
#include "runqueue.h" |
|
12857 | 11 |
|
12 |
extern "C" { |
|
13 |
RunEngine_t* flibRunEngine; |
|
12876
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12863
diff
changeset
|
14 |
GameTick_t* flibGameTick; |
12859 | 15 |
ipcToEngineRaw_t* flibIpcToEngineRaw; |
16 |
ipcSetEngineBarrier_t* flibIpcSetEngineBarrier; |
|
17 |
ipcRemoveBarrierFromEngineQueue_t* flibIpcRemoveBarrierFromEngineQueue; |
|
12857 | 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) |
|
12861 | 27 |
, m_previewProvider(new PreviewImageProvider()) |
28 |
, m_runQueue(new RunQueue(this)) |
|
12857 | 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"); |
|
12876
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12863
diff
changeset
|
42 |
flibGameTick = (GameTick_t*)hwlib.resolve("GameTick"); |
12859 | 43 |
flibIpcToEngineRaw = (ipcToEngineRaw_t*)hwlib.resolve("ipcToEngineRaw"); |
44 |
flibIpcSetEngineBarrier = (ipcSetEngineBarrier_t*)hwlib.resolve("ipcSetEngineBarrier"); |
|
45 |
flibIpcRemoveBarrierFromEngineQueue = (ipcRemoveBarrierFromEngineQueue_t*)hwlib.resolve("ipcRemoveBarrierFromEngineQueue"); |
|
12857 | 46 |
flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*)hwlib.resolve("registerUIMessagesCallback"); |
47 |
flibInit = (flibInit_t*)hwlib.resolve("flibInit"); |
|
48 |
flibFree = (flibFree_t*)hwlib.resolve("flibFree"); |
|
49 |
||
50 |
flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars"); |
|
51 |
flibRegisterUIMessagesCallback(this, &guiMessagesCallback); |
|
12861 | 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); |
|
12857 | 57 |
} |
58 |
||
59 |
HWEngine::~HWEngine() |
|
60 |
{ |
|
61 |
flibFree(); |
|
62 |
} |
|
63 |
||
12860 | 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); |
|
12876
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12863
diff
changeset
|
76 |
qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView"); |
12860 | 77 |
} |
78 |
||
12857 | 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: { |
|
12860 | 93 |
qDebug("MSG_PREVIEW"); |
12861 | 94 |
m_previewProvider->setPixmap(msg); |
12857 | 95 |
emit previewImageChanged(); |
96 |
break; |
|
97 |
} |
|
98 |
case MSG_PREVIEWHOGCOUNT: { |
|
12860 | 99 |
qDebug("MSG_PREVIEWHOGCOUNT"); |
12857 | 100 |
emit previewHogCountChanged((quint8)msg.data()[0]); |
101 |
break; |
|
102 |
} |
|
12860 | 103 |
case MSG_TONET: { |
104 |
qDebug("MSG_TONET"); |
|
105 |
break; |
|
106 |
} |
|
107 |
case MSG_GAMEFINISHED: { |
|
108 |
qDebug("MSG_GAMEFINISHED"); |
|
12861 | 109 |
emit gameFinished(); |
12860 | 110 |
break; |
111 |
} |
|
12857 | 112 |
} |
113 |
} |
|
12858
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
114 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
115 |
void HWEngine::getPreview() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
116 |
{ |
12876
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12863
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:
12863
diff
changeset
|
118 |
m_gameConfig.cmdSeed(m_seed); |
12863 | 119 |
m_gameConfig.setPreview(true); |
12861 | 120 |
|
12863 | 121 |
m_runQueue->queue(m_gameConfig); |
12858
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
122 |
} |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
123 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
124 |
void HWEngine::runQuickGame() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
125 |
{ |
12876
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12863
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:
12863
diff
changeset
|
127 |
m_gameConfig.cmdTheme("Nature"); |
12863 | 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); |
|
12858
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
138 |
} |