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