14164
|
1 |
#include "hwengine.h"
|
|
2 |
|
|
3 |
#include <QDebug>
|
|
4 |
#include <QLibrary>
|
|
5 |
#include <QQmlEngine>
|
|
6 |
#include <QUuid>
|
|
7 |
|
|
8 |
#include "gameview.h"
|
|
9 |
#include "previewimageprovider.h"
|
|
10 |
#include "runqueue.h"
|
|
11 |
|
|
12 |
extern "C" {
|
|
13 |
RunEngine_t* flibRunEngine;
|
|
14 |
GameTick_t* flibGameTick;
|
|
15 |
ResizeWindow_t* flibResizeWindow;
|
|
16 |
updateMousePosition_t* flibUpdateMousePosition;
|
|
17 |
ipcToEngineRaw_t* flibIpcToEngineRaw;
|
|
18 |
ipcSetEngineBarrier_t* flibIpcSetEngineBarrier;
|
|
19 |
ipcRemoveBarrierFromEngineQueue_t* flibIpcRemoveBarrierFromEngineQueue;
|
|
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),
|
|
29 |
m_previewProvider(new PreviewImageProvider()),
|
|
30 |
m_runQueue(new RunQueue(this)) {
|
|
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");
|
|
43 |
flibGameTick = (GameTick_t*)hwlib.resolve("GameTick");
|
|
44 |
flibResizeWindow = (ResizeWindow_t*)hwlib.resolve("ResizeWindow");
|
|
45 |
flibUpdateMousePosition =
|
|
46 |
(updateMousePosition_t*)hwlib.resolve("updateMousePosition");
|
|
47 |
flibIpcToEngineRaw = (ipcToEngineRaw_t*)hwlib.resolve("ipcToEngineRaw");
|
|
48 |
flibIpcSetEngineBarrier =
|
|
49 |
(ipcSetEngineBarrier_t*)hwlib.resolve("ipcSetEngineBarrier");
|
|
50 |
flibIpcRemoveBarrierFromEngineQueue =
|
|
51 |
(ipcRemoveBarrierFromEngineQueue_t*)hwlib.resolve(
|
|
52 |
"ipcRemoveBarrierFromEngineQueue");
|
|
53 |
flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*)hwlib.resolve(
|
|
54 |
"registerUIMessagesCallback");
|
|
55 |
flibInit = (flibInit_t*)hwlib.resolve("flibInit");
|
|
56 |
flibFree = (flibFree_t*)hwlib.resolve("flibFree");
|
|
57 |
|
|
58 |
flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data",
|
|
59 |
"/usr/home/unC0Rr/.hedgewars");
|
|
60 |
flibRegisterUIMessagesCallback(this, &guiMessagesCallback);
|
|
61 |
|
|
62 |
m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider);
|
|
63 |
|
|
64 |
connect(m_runQueue, &RunQueue::previewIsRendering, this,
|
|
65 |
&HWEngine::previewIsRendering);
|
|
66 |
connect(this, &HWEngine::gameFinished, m_runQueue, &RunQueue::onGameFinished);
|
|
67 |
}
|
|
68 |
|
|
69 |
HWEngine::~HWEngine() { flibFree(); }
|
|
70 |
|
|
71 |
static QObject* hwengine_singletontype_provider(QQmlEngine* engine,
|
|
72 |
QJSEngine* scriptEngine) {
|
|
73 |
Q_UNUSED(scriptEngine)
|
|
74 |
|
|
75 |
HWEngine* hwengine = new HWEngine(engine);
|
|
76 |
return hwengine;
|
|
77 |
}
|
|
78 |
|
|
79 |
void HWEngine::exposeToQML() {
|
|
80 |
qDebug("HWEngine::exposeToQML");
|
|
81 |
qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine",
|
|
82 |
hwengine_singletontype_provider);
|
|
83 |
qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
|
|
84 |
}
|
|
85 |
|
|
86 |
void HWEngine::guiMessagesCallback(void* context, MessageType mt,
|
|
87 |
const char* msg, uint32_t len) {
|
|
88 |
HWEngine* obj = reinterpret_cast<HWEngine*>(context);
|
|
89 |
QByteArray b = QByteArray(msg, len);
|
|
90 |
|
|
91 |
qDebug() << "FLIPC in" << mt << " size = " << b.size();
|
|
92 |
|
|
93 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection,
|
|
94 |
Q_ARG(MessageType, mt), Q_ARG(QByteArray, b));
|
|
95 |
}
|
|
96 |
|
|
97 |
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray& msg) {
|
|
98 |
switch (mt) {
|
|
99 |
case MSG_PREVIEW: {
|
|
100 |
qDebug("MSG_PREVIEW");
|
|
101 |
m_previewProvider->setPixmap(msg);
|
|
102 |
emit previewImageChanged();
|
|
103 |
break;
|
|
104 |
}
|
|
105 |
case MSG_PREVIEWHOGCOUNT: {
|
|
106 |
qDebug("MSG_PREVIEWHOGCOUNT");
|
|
107 |
m_previewHedgehogsCount = static_cast<quint8>(msg.data()[0]);
|
|
108 |
emit previewHogCountChanged(m_previewHedgehogsCount);
|
|
109 |
break;
|
|
110 |
}
|
|
111 |
case MSG_TONET: {
|
|
112 |
qDebug("MSG_TONET");
|
|
113 |
break;
|
|
114 |
}
|
|
115 |
case MSG_GAMEFINISHED: {
|
|
116 |
qDebug("MSG_GAMEFINISHED");
|
|
117 |
emit gameFinished();
|
|
118 |
break;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
void HWEngine::getPreview() {
|
|
124 |
m_seed = QUuid::createUuid().toByteArray();
|
|
125 |
m_gameConfig.cmdSeed(m_seed);
|
|
126 |
m_gameConfig.setPreview(true);
|
|
127 |
|
|
128 |
m_runQueue->queue(m_gameConfig);
|
|
129 |
}
|
|
130 |
|
|
131 |
void HWEngine::runQuickGame() {
|
|
132 |
m_gameConfig.cmdSeed(m_seed);
|
|
133 |
m_gameConfig.cmdTheme("Nature");
|
|
134 |
Team team1;
|
|
135 |
team1.name = "team1";
|
|
136 |
Team team2;
|
|
137 |
team2.name = "team2";
|
|
138 |
team2.color = "7654321";
|
|
139 |
m_gameConfig.cmdTeam(team1);
|
|
140 |
m_gameConfig.cmdTeam(team2);
|
|
141 |
m_gameConfig.setPreview(false);
|
|
142 |
|
|
143 |
m_runQueue->queue(m_gameConfig);
|
|
144 |
}
|
|
145 |
|
|
146 |
int HWEngine::previewHedgehogsCount() const { return m_previewHedgehogsCount; }
|