author | sheepluva |
Sat, 06 Jan 2018 03:05:14 +0100 | |
branch | qmlfrontend |
changeset 12881 | 07a267d72fce |
parent 12876 | b544bbbd0696 |
child 12882 | bf58f2403953 |
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 |
||
12881 | 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"); |
|
12857 | 52 |
flibRegisterUIMessagesCallback(this, &guiMessagesCallback); |
12861 | 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); |
|
12857 | 58 |
} |
59 |
||
60 |
HWEngine::~HWEngine() |
|
61 |
{ |
|
62 |
flibFree(); |
|
63 |
} |
|
64 |
||
12860 | 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); |
|
12876
b544bbbd0696
Break engine completely and make it render in frontend window (no actual rendering yet, just white screen)
unc0rr
parents:
12863
diff
changeset
|
77 |
qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView"); |
12860 | 78 |
} |
79 |
||
12857 | 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: { |
|
12860 | 94 |
qDebug("MSG_PREVIEW"); |
12861 | 95 |
m_previewProvider->setPixmap(msg); |
12857 | 96 |
emit previewImageChanged(); |
97 |
break; |
|
98 |
} |
|
99 |
case MSG_PREVIEWHOGCOUNT: { |
|
12860 | 100 |
qDebug("MSG_PREVIEWHOGCOUNT"); |
12857 | 101 |
emit previewHogCountChanged((quint8)msg.data()[0]); |
102 |
break; |
|
103 |
} |
|
12860 | 104 |
case MSG_TONET: { |
105 |
qDebug("MSG_TONET"); |
|
106 |
break; |
|
107 |
} |
|
108 |
case MSG_GAMEFINISHED: { |
|
109 |
qDebug("MSG_GAMEFINISHED"); |
|
12861 | 110 |
emit gameFinished(); |
12860 | 111 |
break; |
112 |
} |
|
12857 | 113 |
} |
114 |
} |
|
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 |
void HWEngine::getPreview() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
117 |
{ |
12876
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_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
|
119 |
m_gameConfig.cmdSeed(m_seed); |
12863 | 120 |
m_gameConfig.setPreview(true); |
12861 | 121 |
|
12863 | 122 |
m_runQueue->queue(m_gameConfig); |
12858
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 |
|
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
125 |
void HWEngine::runQuickGame() |
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
126 |
{ |
12876
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.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
|
128 |
m_gameConfig.cmdTheme("Nature"); |
12863 | 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); |
|
12858
0c6fb706f747
More refactoring in attempt to move away from frontlib
unc0rr
parents:
12857
diff
changeset
|
139 |
} |