1 #include "hwengine.h" |
|
2 |
|
3 #include <QDebug> |
1 #include <QDebug> |
4 #include <QLibrary> |
2 #include <QLibrary> |
5 #include <QQmlEngine> |
3 #include <QQmlEngine> |
6 #include <QUuid> |
4 #include <QUuid> |
7 |
5 |
8 #include "gameview.h" |
6 #include "engine_instance.h" |
9 #include "previewimageprovider.h" |
7 #include "engine_interface.h" |
10 #include "runqueue.h" |
8 #include "game_view.h" |
|
9 #include "preview_image_provider.h" |
11 |
10 |
12 extern "C" { |
11 #include "hwengine.h" |
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 |
12 |
26 HWEngine::HWEngine(QQmlEngine* engine, QObject* parent) |
13 HWEngine::HWEngine(QQmlEngine* engine, QObject* parent) |
27 : QObject(parent), |
14 : QObject(parent), |
28 m_engine(engine), |
15 m_engine(engine), |
29 m_previewProvider(new PreviewImageProvider()), |
16 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); |
17 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 } |
18 } |
68 |
19 |
69 HWEngine::~HWEngine() { flibFree(); } |
20 HWEngine::~HWEngine() {} |
70 |
21 |
71 static QObject* hwengine_singletontype_provider(QQmlEngine* engine, |
22 static QObject* hwengine_singletontype_provider(QQmlEngine* engine, |
72 QJSEngine* scriptEngine) { |
23 QJSEngine* scriptEngine) { |
73 Q_UNUSED(scriptEngine) |
24 Q_UNUSED(scriptEngine) |
74 |
25 |
78 |
29 |
79 void HWEngine::exposeToQML() { |
30 void HWEngine::exposeToQML() { |
80 qDebug("HWEngine::exposeToQML"); |
31 qDebug("HWEngine::exposeToQML"); |
81 qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", |
32 qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", |
82 hwengine_singletontype_provider); |
33 hwengine_singletontype_provider); |
83 qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView"); |
34 // 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 } |
35 } |
122 |
36 |
123 void HWEngine::getPreview() { |
37 void HWEngine::getPreview() { |
124 m_seed = QUuid::createUuid().toByteArray(); |
38 m_seed = QUuid::createUuid().toByteArray(); |
125 m_gameConfig.cmdSeed(m_seed); |
39 m_gameConfig.cmdSeed(m_seed); |
126 m_gameConfig.setPreview(true); |
40 m_gameConfig.setPreview(true); |
127 |
41 |
128 m_runQueue->queue(m_gameConfig); |
42 EngineInstance engine; |
|
43 Engine::PreviewInfo preview = engine.generatePreview(); |
|
44 |
|
45 QVector<QRgb> colorTable; |
|
46 colorTable.resize(256); |
|
47 for (int i = 0; i < 256; ++i) colorTable[i] = qRgba(255, 255, 0, i); |
|
48 |
|
49 QImage previewImage(preview.land, preview.width, preview.height, |
|
50 QImage::Format_Indexed8); |
|
51 previewImage.setColorTable(colorTable); |
|
52 previewImage.detach(); |
|
53 |
|
54 m_previewProvider->setImage(previewImage); |
|
55 |
|
56 emit previewImageChanged(); |
|
57 // m_runQueue->queue(m_gameConfig); |
129 } |
58 } |
130 |
59 |
131 void HWEngine::runQuickGame() { |
60 void HWEngine::runQuickGame() { |
132 m_gameConfig.cmdSeed(m_seed); |
61 m_gameConfig.cmdSeed(m_seed); |
133 m_gameConfig.cmdTheme("Nature"); |
62 m_gameConfig.cmdTheme("Nature"); |