qmlFrontend/hwengine.cpp
author unc0rr
Tue, 30 Sep 2014 00:54:04 +0400
branchqmlfrontend
changeset 10434 1614b13ad35e
parent 10432 b0abef0ee78c
child 10436 084e046f6bd5
permissions -rw-r--r--
Themes model, also add some files I forgot to add previously
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     1
#include <QLibrary>
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     2
#include <QtQml>
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
     3
#include <QDebug>
10420
unc0rr
parents: 10418
diff changeset
     4
#include <QPainter>
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
     5
#include <QUuid>
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     6
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     7
#include "hwengine.h"
10420
unc0rr
parents: 10418
diff changeset
     8
#include "previewimageprovider.h"
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     9
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    10
extern "C" {
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    11
    RunEngine_t *flibRunEngine;
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    12
    registerGUIMessagesCallback_t *flibRegisterGUIMessagesCallback;
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    13
    setSeed_t *flibSetSeed;
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    14
    getSeed_t *flibGetSeed;
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    15
    getPreview_t *flibGetPreview;
10432
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    16
    runQuickGame_t *flibRunQuickGame;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    17
    flibInit_t *flibInit;
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    18
    flibFree_t *flibFree;
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    19
    getThemesList_t *flibGetThemesList;
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    20
    freeThemesList_t *flibFreeThemesList;
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    21
}
10420
unc0rr
parents: 10418
diff changeset
    22
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    23
Q_DECLARE_METATYPE(MessageType);
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    24
10420
unc0rr
parents: 10418
diff changeset
    25
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) :
unc0rr
parents: 10418
diff changeset
    26
    QObject(parent),
unc0rr
parents: 10418
diff changeset
    27
    m_engine(engine)
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    28
{
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    29
    qRegisterMetaType<MessageType>("MessageType");
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    30
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    31
    QLibrary hwlib("./libhwengine.so");
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    32
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    33
    if(!hwlib.load())
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    34
        qWarning() << "Engine library not found" << hwlib.errorString();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    35
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    36
    flibRunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    37
    flibRegisterGUIMessagesCallback = (registerGUIMessagesCallback_t*) hwlib.resolve("registerGUIMessagesCallback");
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    38
    flibSetSeed = (setSeed_t*) hwlib.resolve("setSeed");
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    39
    flibGetSeed = (getSeed_t*) hwlib.resolve("getSeed");
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    40
    flibGetPreview = (getPreview_t*) hwlib.resolve("getPreview");
10432
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    41
    flibRunQuickGame = (runQuickGame_t*) hwlib.resolve("runQuickGame");
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    42
    flibInit = (flibInit_t*) hwlib.resolve("flibInit");
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    43
    flibFree = (flibFree_t*) hwlib.resolve("flibFree");
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    44
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    45
    flibGetThemesList = (getThemesList_t*) hwlib.resolve("getThemesList");
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    46
    flibFreeThemesList = (freeThemesList_t*) hwlib.resolve("freeThemesList");
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    47
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    48
    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
    49
    flibRegisterGUIMessagesCallback(this, &guiMessagesCallback);
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    50
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    51
    fillModels();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    52
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    53
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    54
HWEngine::~HWEngine()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    55
{
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    56
    flibFree();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    57
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    58
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    59
void HWEngine::getPreview()
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    60
{
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    61
    flibSetSeed(QUuid::createUuid().toString().toLatin1());
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    62
    flibGetPreview();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    63
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    64
10432
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    65
void HWEngine::runQuickGame()
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    66
{
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    67
    flibSetSeed(QUuid::createUuid().toString().toLatin1());
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    68
    flibRunQuickGame();
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    69
}
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    70
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    71
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    72
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    73
    Q_UNUSED(scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    74
10420
unc0rr
parents: 10418
diff changeset
    75
    HWEngine *hwengine = new HWEngine(engine);
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    76
    return hwengine;
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    77
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    78
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    79
void HWEngine::exposeToQML()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    80
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    81
    qDebug("HWEngine::exposeToQML");
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    82
    qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider);
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    83
}
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    84
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    85
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    86
void HWEngine::guiMessagesCallback(void *context, MessageType mt, const char * msg, uint32_t len)
10420
unc0rr
parents: 10418
diff changeset
    87
{
unc0rr
parents: 10418
diff changeset
    88
    HWEngine * obj = (HWEngine *)context;
unc0rr
parents: 10418
diff changeset
    89
    QByteArray b = QByteArray::fromRawData(msg, len);
unc0rr
parents: 10418
diff changeset
    90
unc0rr
parents: 10418
diff changeset
    91
    qDebug() << "FLIPC in" << b.size() << b;
unc0rr
parents: 10418
diff changeset
    92
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    93
    QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b));
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    94
}
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    95
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    96
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray &msg)
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    97
{
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    98
    switch(mt)
10426
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
    99
    {
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
   100
    case MSG_PREVIEW:
10426
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   101
        PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview"));
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   102
        preview->setPixmap(msg);
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   103
        emit previewImageChanged();
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
   104
        break;
10426
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   105
    }
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   106
}
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   107
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   108
QString HWEngine::currentSeed()
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   109
{
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
   110
    return QString::fromLatin1(flibGetSeed());
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   111
}
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   112
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   113
void HWEngine::fillModels()
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   114
{
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   115
    QStringList resultModel;
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   116
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   117
    char ** themes = flibGetThemesList();
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   118
    for (char **i = themes; *i != NULL; i++) {
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   119
        QString theme = QString::fromUtf8(*i);
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   120
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   121
        resultModel << theme;
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   122
    }
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   123
    flibFreeThemesList(themes);
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   124
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   125
    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
   126
}