author | sheepluva |
Sat, 06 Jan 2018 03:05:14 +0100 | |
branch | qmlfrontend |
changeset 12886 | 07a267d72fce |
parent 12881 | b544bbbd0696 |
child 12887 | bf58f2403953 |
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 |
||
12886 | 50 |
flibInit("/home/r/data/hwrepos/.hg-qml_installed/hedgewars/share/hedgewars/Data", "/home/r/.hedgewars"); |
51 |
//flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars"); |
|
12862 | 52 |
flibRegisterUIMessagesCallback(this, &guiMessagesCallback); |
12866 | 53 |
|
54 |
m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider); |
|
55 |
||
56 |
connect(m_runQueue, &RunQueue::previewIsRendering, this, &HWEngine::previewIsRendering); |
|
57 |
connect(this, &HWEngine::gameFinished, m_runQueue, &RunQueue::onGameFinished); |
|
12862 | 58 |
} |
59 |
||
60 |
HWEngine::~HWEngine() |
|
61 |
{ |
|
62 |
flibFree(); |
|
63 |
} |
|
64 |
||
12865 | 65 |
static QObject* hwengine_singletontype_provider(QQmlEngine* engine, QJSEngine* scriptEngine) |
66 |
{ |
|
67 |
Q_UNUSED(scriptEngine) |
|
68 |
||
69 |
HWEngine* hwengine = new HWEngine(engine); |
|
70 |
return hwengine; |
|
71 |
} |
|
72 |
||
73 |
void HWEngine::exposeToQML() |
|
74 |
{ |
|
75 |
qDebug("HWEngine::exposeToQML"); |
|
76 |
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
|
77 |
qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView"); |
12865 | 78 |
} |
79 |
||
12862 | 80 |
void HWEngine::guiMessagesCallback(void* context, MessageType mt, const char* msg, uint32_t len) |
81 |
{ |
|
82 |
HWEngine* obj = reinterpret_cast<HWEngine*>(context); |
|
83 |
QByteArray b = QByteArray(msg, len); |
|
84 |
||
85 |
qDebug() << "FLIPC in" << mt << " size = " << b.size(); |
|
86 |
||
87 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b)); |
|
88 |
} |
|
89 |
||
90 |
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray& msg) |
|
91 |
{ |
|
92 |
switch (mt) { |
|
93 |
case MSG_PREVIEW: { |
|
12865 | 94 |
qDebug("MSG_PREVIEW"); |
12866 | 95 |
m_previewProvider->setPixmap(msg); |
12862 | 96 |
emit previewImageChanged(); |
97 |
break; |
|
98 |
} |
|
99 |
case MSG_PREVIEWHOGCOUNT: { |
|
12865 | 100 |
qDebug("MSG_PREVIEWHOGCOUNT"); |
12862 | 101 |
emit previewHogCountChanged((quint8)msg.data()[0]); |
102 |
break; |
|
103 |
} |
|
12865 | 104 |
case MSG_TONET: { |
105 |
qDebug("MSG_TONET"); |
|
106 |
break; |
|
107 |
} |
|
108 |
case MSG_GAMEFINISHED: { |
|
109 |
qDebug("MSG_GAMEFINISHED"); |
|
12866 | 110 |
emit gameFinished(); |
12865 | 111 |
break; |
112 |
} |
|
12862 | 113 |
} |
114 |
} |
|
12863
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
115 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
116 |
void HWEngine::getPreview() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
117 |
{ |
12881
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_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
|
119 |
m_gameConfig.cmdSeed(m_seed); |
12868 | 120 |
m_gameConfig.setPreview(true); |
12866 | 121 |
|
12868 | 122 |
m_runQueue->queue(m_gameConfig); |
12863
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 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
125 |
void HWEngine::runQuickGame() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
126 |
{ |
12881
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.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
|
128 |
m_gameConfig.cmdTheme("Nature"); |
12868 | 129 |
Team team1; |
130 |
team1.name = "team1"; |
|
131 |
Team team2; |
|
132 |
team2.name = "team2"; |
|
133 |
team2.color = "7654321"; |
|
134 |
m_gameConfig.cmdTeam(team1); |
|
135 |
m_gameConfig.cmdTeam(team2); |
|
136 |
m_gameConfig.setPreview(false); |
|
137 |
||
138 |
m_runQueue->queue(m_gameConfig); |
|
12863
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12862
diff
changeset
|
139 |
} |