qmlfrontend/hwengine.cpp
author unc0rr
Sun, 24 Dec 2017 00:44:16 +0100
branchqmlfrontend
changeset 12858 0c6fb706f747
parent 12857 90f927b4b9e1
child 12859 a03f245243b0
permissions -rw-r--r--
More refactoring in attempt to move away from frontlib
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>
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     5
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     6
extern "C" {
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     7
RunEngine_t* flibRunEngine;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     8
registerUIMessagesCallback_t* flibRegisterUIMessagesCallback;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
     9
flibInit_t* flibInit;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    10
flibFree_t* flibFree;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    11
passFlibEvent_t* flibPassFlibEvent;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    12
}
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    13
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    14
HWEngine::HWEngine(QQmlEngine* engine, QObject* parent)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    15
    : QObject(parent)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    16
    , m_engine(engine)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    17
{
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    18
    qRegisterMetaType<MessageType>("MessageType");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    19
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    20
#ifdef Q_OS_WIN
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    21
    QLibrary hwlib("./libhwengine.dll");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    22
#else
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    23
    QLibrary hwlib("./libhwengine.so");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    24
#endif
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    25
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    26
    if (!hwlib.load())
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    27
        qWarning() << "Engine library not found" << hwlib.errorString();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    28
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    29
    flibRunEngine = (RunEngine_t*)hwlib.resolve("RunEngine");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    30
    flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*)hwlib.resolve("registerUIMessagesCallback");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    31
    flibInit = (flibInit_t*)hwlib.resolve("flibInit");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    32
    flibFree = (flibFree_t*)hwlib.resolve("flibFree");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    33
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    34
    flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars");
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    35
    flibRegisterUIMessagesCallback(this, &guiMessagesCallback);
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    36
}
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    37
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    38
HWEngine::~HWEngine()
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    39
{
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    40
    flibFree();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    41
}
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    42
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    43
void HWEngine::guiMessagesCallback(void* context, MessageType mt, const char* msg, uint32_t len)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    44
{
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    45
    HWEngine* obj = reinterpret_cast<HWEngine*>(context);
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    46
    QByteArray b = QByteArray(msg, len);
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    47
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    48
    qDebug() << "FLIPC in" << mt << " size = " << b.size();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    49
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    50
    QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b));
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    51
}
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    52
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    53
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray& msg)
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    54
{
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    55
    switch (mt) {
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    56
    case MSG_RENDERINGPREVIEW: {
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    57
        emit previewIsRendering();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    58
        break;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    59
    }
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    60
    case MSG_PREVIEW: {
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    61
        emit previewImageChanged();
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    62
        break;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    63
    }
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    64
    case MSG_PREVIEWHOGCOUNT: {
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    65
        emit previewHogCountChanged((quint8)msg.data()[0]);
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    66
        break;
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    67
    }
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    68
    }
90f927b4b9e1 Start hacking new frontend
unc0rr
parents:
diff changeset
    69
}
12858
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    70
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    71
void HWEngine::getPreview()
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    72
{
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    73
    int argc;
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    74
    const char** argv;
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    75
    flibRunEngine(argc, argv);
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    76
}
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    77
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    78
void HWEngine::runQuickGame()
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    79
{
0c6fb706f747 More refactoring in attempt to move away from frontlib
unc0rr
parents: 12857
diff changeset
    80
}