author | unc0rr |
Mon, 20 Oct 2014 22:55:12 +0400 | |
branch | qmlfrontend |
changeset 10442 | c58db813240b |
parent 10436 | 084e046f6bd5 |
child 10444 | 47a6231f1fc1 |
permissions | -rw-r--r-- |
10402 | 1 |
#include <QLibrary> |
2 |
#include <QtQml> |
|
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
3 |
#include <QDebug> |
10420 | 4 |
#include <QPainter> |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
5 |
#include <QUuid> |
10402 | 6 |
|
7 |
#include "hwengine.h" |
|
10420 | 8 |
#include "previewimageprovider.h" |
10436 | 9 |
#include "themeiconprovider.h" |
10402 | 10 |
|
11 |
extern "C" { |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
12 |
RunEngine_t *flibRunEngine; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
13 |
registerGUIMessagesCallback_t *flibRegisterGUIMessagesCallback; |
10430 | 14 |
setSeed_t *flibSetSeed; |
15 |
getSeed_t *flibGetSeed; |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
16 |
getPreview_t *flibGetPreview; |
10432 | 17 |
runQuickGame_t *flibRunQuickGame; |
10416 | 18 |
flibInit_t *flibInit; |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
19 |
flibFree_t *flibFree; |
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
20 |
getThemesList_t *flibGetThemesList; |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
21 |
freeThemesList_t *flibFreeThemesList; |
10436 | 22 |
getThemeIcon_t *flibGetThemeIcon; |
10442 | 23 |
getTeamsList_t *flibGetTeamsList; |
10402 | 24 |
} |
10420 | 25 |
|
10430 | 26 |
Q_DECLARE_METATYPE(MessageType); |
27 |
||
10420 | 28 |
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) : |
29 |
QObject(parent), |
|
30 |
m_engine(engine) |
|
10402 | 31 |
{ |
10430 | 32 |
qRegisterMetaType<MessageType>("MessageType"); |
33 |
||
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
34 |
QLibrary hwlib("./libhwengine.so"); |
10402 | 35 |
|
36 |
if(!hwlib.load()) |
|
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
37 |
qWarning() << "Engine library not found" << hwlib.errorString(); |
10402 | 38 |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
39 |
flibRunEngine = (RunEngine_t*) hwlib.resolve("RunEngine"); |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
40 |
flibRegisterGUIMessagesCallback = (registerGUIMessagesCallback_t*) hwlib.resolve("registerGUIMessagesCallback"); |
10430 | 41 |
flibSetSeed = (setSeed_t*) hwlib.resolve("setSeed"); |
42 |
flibGetSeed = (getSeed_t*) hwlib.resolve("getSeed"); |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
43 |
flibGetPreview = (getPreview_t*) hwlib.resolve("getPreview"); |
10432 | 44 |
flibRunQuickGame = (runQuickGame_t*) hwlib.resolve("runQuickGame"); |
10416 | 45 |
flibInit = (flibInit_t*) hwlib.resolve("flibInit"); |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
46 |
flibFree = (flibFree_t*) hwlib.resolve("flibFree"); |
10416 | 47 |
|
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
48 |
flibGetThemesList = (getThemesList_t*) hwlib.resolve("getThemesList"); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
49 |
flibFreeThemesList = (freeThemesList_t*) hwlib.resolve("freeThemesList"); |
10436 | 50 |
flibGetThemeIcon = (getThemeIcon_t*) hwlib.resolve("getThemeIcon"); |
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
51 |
|
10442 | 52 |
flibGetTeamsList = (getTeamsList_t*) hwlib.resolve("getTeamsList"); |
53 |
||
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
54 |
flibInit("/usr/home/unC0Rr/Sources/Hedgewars/Hedgewars-GC/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars"); |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
55 |
flibRegisterGUIMessagesCallback(this, &guiMessagesCallback); |
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
56 |
|
10436 | 57 |
ThemeIconProvider * themeIcon = (ThemeIconProvider *)m_engine->imageProvider(QLatin1String("theme")); |
58 |
themeIcon->setFileContentsFunction(flibGetThemeIcon); |
|
59 |
||
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
60 |
fillModels(); |
10402 | 61 |
} |
62 |
||
63 |
HWEngine::~HWEngine() |
|
64 |
{ |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
65 |
flibFree(); |
10402 | 66 |
} |
67 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
68 |
void HWEngine::getPreview() |
10402 | 69 |
{ |
10430 | 70 |
flibSetSeed(QUuid::createUuid().toString().toLatin1()); |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
71 |
flibGetPreview(); |
10402 | 72 |
} |
73 |
||
10432 | 74 |
void HWEngine::runQuickGame() |
75 |
{ |
|
76 |
flibSetSeed(QUuid::createUuid().toString().toLatin1()); |
|
77 |
flibRunQuickGame(); |
|
78 |
} |
|
79 |
||
10402 | 80 |
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
81 |
{ |
|
82 |
Q_UNUSED(scriptEngine) |
|
83 |
||
10420 | 84 |
HWEngine *hwengine = new HWEngine(engine); |
10402 | 85 |
return hwengine; |
86 |
} |
|
87 |
||
88 |
void HWEngine::exposeToQML() |
|
89 |
{ |
|
90 |
qDebug("HWEngine::exposeToQML"); |
|
91 |
qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider); |
|
92 |
} |
|
10416 | 93 |
|
94 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
95 |
void HWEngine::guiMessagesCallback(void *context, MessageType mt, const char * msg, uint32_t len) |
10420 | 96 |
{ |
97 |
HWEngine * obj = (HWEngine *)context; |
|
98 |
QByteArray b = QByteArray::fromRawData(msg, len); |
|
99 |
||
100 |
qDebug() << "FLIPC in" << b.size() << b; |
|
101 |
||
10430 | 102 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b)); |
10416 | 103 |
} |
104 |
||
10430 | 105 |
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray &msg) |
10416 | 106 |
{ |
10430 | 107 |
switch(mt) |
10426 | 108 |
{ |
10430 | 109 |
case MSG_PREVIEW: |
10426 | 110 |
PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview")); |
111 |
preview->setPixmap(msg); |
|
112 |
emit previewImageChanged(); |
|
10430 | 113 |
break; |
10426 | 114 |
} |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
115 |
} |
10416 | 116 |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
117 |
QString HWEngine::currentSeed() |
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
118 |
{ |
10430 | 119 |
return QString::fromLatin1(flibGetSeed()); |
10416 | 120 |
} |
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
121 |
|
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
122 |
void HWEngine::fillModels() |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
123 |
{ |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
124 |
QStringList resultModel; |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
125 |
|
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
126 |
char ** themes = flibGetThemesList(); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
127 |
for (char **i = themes; *i != NULL; i++) { |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
128 |
QString theme = QString::fromUtf8(*i); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
129 |
|
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
130 |
resultModel << theme; |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
131 |
} |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
132 |
flibFreeThemesList(themes); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
133 |
|
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
134 |
m_engine->rootContext()->setContextProperty("themesModel", QVariant::fromValue(resultModel)); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
135 |
} |
10442 | 136 |
|
137 |
void HWEngine::getTeamsList() |
|
138 |
{ |
|
139 |
char ** teams = flibGetTeamsList(); |
|
140 |
for (char **i = teams; *i != NULL; i++) { |
|
141 |
QString team = QString::fromUtf8(*i); |
|
142 |
||
143 |
emit localTeamAdded(team, 0); |
|
144 |
} |
|
145 |
} |