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