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