qmlfrontend/hwengine.cpp
author unc0rr
Tue, 26 Dec 2017 01:36:58 +0100
branchqmlfrontend
changeset 12866 488782d9aba9
parent 12865 e33bcb9d5e9c
child 12868 fe16fa088b69
permissions -rw-r--r--
Recreate uFLRunQueue in Qt, render previews
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     1
#include "hwengine.h"
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     2
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     3
#include <QDebug>
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     4
#include <QLibrary>
12865
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
     5
#include <QQmlEngine>
12866
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
     6
#include <QUuid>
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
     7
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
     8
#include "previewimageprovider.h"
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
     9
#include "runqueue.h"
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    10
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    11
extern "C" {
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    12
RunEngine_t* flibRunEngine;
12864
a03f245243b0 And more refactoring...
unc0rr
parents: 12863
diff changeset
    13
ipcToEngineRaw_t* flibIpcToEngineRaw;
a03f245243b0 And more refactoring...
unc0rr
parents: 12863
diff changeset
    14
ipcSetEngineBarrier_t* flibIpcSetEngineBarrier;
a03f245243b0 And more refactoring...
unc0rr
parents: 12863
diff changeset
    15
ipcRemoveBarrierFromEngineQueue_t* flibIpcRemoveBarrierFromEngineQueue;
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    16
registerUIMessagesCallback_t* flibRegisterUIMessagesCallback;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    17
flibInit_t* flibInit;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    18
flibFree_t* flibFree;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    19
passFlibEvent_t* flibPassFlibEvent;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    20
}
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    21
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    22
HWEngine::HWEngine(QQmlEngine* engine, QObject* parent)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    23
    : QObject(parent)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    24
    , m_engine(engine)
12866
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
    25
    , m_previewProvider(new PreviewImageProvider())
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
    26
    , m_runQueue(new RunQueue(this))
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    27
{
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    28
    qRegisterMetaType<MessageType>("MessageType");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    29
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    30
#ifdef Q_OS_WIN
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    31
    QLibrary hwlib("./libhwengine.dll");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    32
#else
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    33
    QLibrary hwlib("./libhwengine.so");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    34
#endif
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    35
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    36
    if (!hwlib.load())
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    37
        qWarning() << "Engine library not found" << hwlib.errorString();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    38
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    39
    flibRunEngine = (RunEngine_t*)hwlib.resolve("RunEngine");
12864
a03f245243b0 And more refactoring...
unc0rr
parents: 12863
diff changeset
    40
    flibIpcToEngineRaw = (ipcToEngineRaw_t*)hwlib.resolve("ipcToEngineRaw");
a03f245243b0 And more refactoring...
unc0rr
parents: 12863
diff changeset
    41
    flibIpcSetEngineBarrier = (ipcSetEngineBarrier_t*)hwlib.resolve("ipcSetEngineBarrier");
a03f245243b0 And more refactoring...
unc0rr
parents: 12863
diff changeset
    42
    flibIpcRemoveBarrierFromEngineQueue = (ipcRemoveBarrierFromEngineQueue_t*)hwlib.resolve("ipcRemoveBarrierFromEngineQueue");
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    43
    flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*)hwlib.resolve("registerUIMessagesCallback");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    44
    flibInit = (flibInit_t*)hwlib.resolve("flibInit");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    45
    flibFree = (flibFree_t*)hwlib.resolve("flibFree");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    46
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    47
    flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    48
    flibRegisterUIMessagesCallback(this, &guiMessagesCallback);
12866
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
    49
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
    50
    m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider);
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
    51
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
    52
    connect(m_runQueue, &RunQueue::previewIsRendering, this, &HWEngine::previewIsRendering);
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
    53
    connect(this, &HWEngine::gameFinished, m_runQueue, &RunQueue::onGameFinished);
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    54
}
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    55
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    56
HWEngine::~HWEngine()
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    57
{
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    58
    flibFree();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    59
}
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    60
12865
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    61
static QObject* hwengine_singletontype_provider(QQmlEngine* engine, QJSEngine* scriptEngine)
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    62
{
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    63
    Q_UNUSED(scriptEngine)
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    64
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    65
    HWEngine* hwengine = new HWEngine(engine);
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    66
    return hwengine;
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    67
}
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    68
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    69
void HWEngine::exposeToQML()
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    70
{
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    71
    qDebug("HWEngine::exposeToQML");
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    72
    qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider);
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    73
}
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    74
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    75
void HWEngine::guiMessagesCallback(void* context, MessageType mt, const char* msg, uint32_t len)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    76
{
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    77
    HWEngine* obj = reinterpret_cast<HWEngine*>(context);
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    78
    QByteArray b = QByteArray(msg, len);
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    79
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    80
    qDebug() << "FLIPC in" << mt << " size = " << b.size();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    81
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    82
    QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b));
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    83
}
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    84
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    85
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray& msg)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    86
{
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    87
    switch (mt) {
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    88
    case MSG_PREVIEW: {
12865
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    89
        qDebug("MSG_PREVIEW");
12866
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
    90
        m_previewProvider->setPixmap(msg);
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    91
        emit previewImageChanged();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    92
        break;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    93
    }
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    94
    case MSG_PREVIEWHOGCOUNT: {
12865
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    95
        qDebug("MSG_PREVIEWHOGCOUNT");
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    96
        emit previewHogCountChanged((quint8)msg.data()[0]);
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    97
        break;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    98
    }
12865
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
    99
    case MSG_TONET: {
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
   100
        qDebug("MSG_TONET");
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
   101
        break;
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
   102
    }
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
   103
    case MSG_GAMEFINISHED: {
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
   104
        qDebug("MSG_GAMEFINISHED");
12866
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
   105
        emit gameFinished();
12865
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
   106
        break;
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
   107
    }
12862
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
   108
    }
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
   109
}
12863
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12862
diff changeset
   110
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12862
diff changeset
   111
void HWEngine::getPreview()
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12862
diff changeset
   112
{
12865
e33bcb9d5e9c Preview rendering seems to work now
unc0rr
parents: 12864
diff changeset
   113
    GameConfig cfg;
12866
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
   114
    cfg.cmdSeed(QUuid::createUuid().toByteArray());
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
   115
488782d9aba9 Recreate uFLRunQueue in Qt, render previews
unc0rr
parents: 12865
diff changeset
   116
    m_runQueue->queue(cfg);
12863
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12862
diff changeset
   117
}
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::runQuickGame()
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12862
diff changeset
   120
{
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12862
diff changeset
   121
}