qmlfrontend/hwengine.cpp
author unC0Rr
Tue, 06 Nov 2018 17:00:35 +0100
changeset 14164 745c73e0e644
child 14175 8354b390f1a2
permissions -rw-r--r--
Start working on frontend to rust engine rewrite
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     1
#include "hwengine.h"
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     2
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     3
#include <QDebug>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     4
#include <QLibrary>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     5
#include <QQmlEngine>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     6
#include <QUuid>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     7
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     8
#include "gameview.h"
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     9
#include "previewimageprovider.h"
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    10
#include "runqueue.h"
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    11
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    12
extern "C" {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    13
RunEngine_t* flibRunEngine;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    14
GameTick_t* flibGameTick;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    15
ResizeWindow_t* flibResizeWindow;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    16
updateMousePosition_t* flibUpdateMousePosition;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    17
ipcToEngineRaw_t* flibIpcToEngineRaw;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    18
ipcSetEngineBarrier_t* flibIpcSetEngineBarrier;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    19
ipcRemoveBarrierFromEngineQueue_t* flibIpcRemoveBarrierFromEngineQueue;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    20
registerUIMessagesCallback_t* flibRegisterUIMessagesCallback;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    21
flibInit_t* flibInit;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    22
flibFree_t* flibFree;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    23
passFlibEvent_t* flibPassFlibEvent;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    24
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    25
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    26
HWEngine::HWEngine(QQmlEngine* engine, QObject* parent)
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    27
    : QObject(parent),
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    28
      m_engine(engine),
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    29
      m_previewProvider(new PreviewImageProvider()),
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    30
      m_runQueue(new RunQueue(this)) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    31
  qRegisterMetaType<MessageType>("MessageType");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    32
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    33
#ifdef Q_OS_WIN
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    34
  QLibrary hwlib("./libhwengine.dll");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    35
#else
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
  QLibrary hwlib("./libhwengine.so");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
#endif
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
  if (!hwlib.load())
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    40
    qWarning() << "Engine library not found" << hwlib.errorString();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    41
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    42
  flibRunEngine = (RunEngine_t*)hwlib.resolve("RunEngine");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    43
  flibGameTick = (GameTick_t*)hwlib.resolve("GameTick");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    44
  flibResizeWindow = (ResizeWindow_t*)hwlib.resolve("ResizeWindow");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    45
  flibUpdateMousePosition =
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    46
      (updateMousePosition_t*)hwlib.resolve("updateMousePosition");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    47
  flibIpcToEngineRaw = (ipcToEngineRaw_t*)hwlib.resolve("ipcToEngineRaw");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    48
  flibIpcSetEngineBarrier =
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    49
      (ipcSetEngineBarrier_t*)hwlib.resolve("ipcSetEngineBarrier");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    50
  flibIpcRemoveBarrierFromEngineQueue =
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    51
      (ipcRemoveBarrierFromEngineQueue_t*)hwlib.resolve(
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    52
          "ipcRemoveBarrierFromEngineQueue");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    53
  flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*)hwlib.resolve(
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    54
      "registerUIMessagesCallback");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    55
  flibInit = (flibInit_t*)hwlib.resolve("flibInit");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    56
  flibFree = (flibFree_t*)hwlib.resolve("flibFree");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    57
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    58
  flibInit("/usr/home/unC0Rr/Sources/Hedgewars/MainRepo/share/hedgewars/Data",
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    59
           "/usr/home/unC0Rr/.hedgewars");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    60
  flibRegisterUIMessagesCallback(this, &guiMessagesCallback);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    61
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    62
  m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    63
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    64
  connect(m_runQueue, &RunQueue::previewIsRendering, this,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    65
          &HWEngine::previewIsRendering);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    66
  connect(this, &HWEngine::gameFinished, m_runQueue, &RunQueue::onGameFinished);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    67
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    68
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    69
HWEngine::~HWEngine() { flibFree(); }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    70
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    71
static QObject* hwengine_singletontype_provider(QQmlEngine* engine,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    72
                                                QJSEngine* scriptEngine) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    73
  Q_UNUSED(scriptEngine)
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    74
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    75
  HWEngine* hwengine = new HWEngine(engine);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    76
  return hwengine;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    77
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    78
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    79
void HWEngine::exposeToQML() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    80
  qDebug("HWEngine::exposeToQML");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    81
  qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine",
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    82
                                     hwengine_singletontype_provider);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    83
  qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    84
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    85
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    86
void HWEngine::guiMessagesCallback(void* context, MessageType mt,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    87
                                   const char* msg, uint32_t len) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    88
  HWEngine* obj = reinterpret_cast<HWEngine*>(context);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    89
  QByteArray b = QByteArray(msg, len);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    90
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    91
  qDebug() << "FLIPC in" << mt << " size = " << b.size();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    92
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    93
  QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    94
                            Q_ARG(MessageType, mt), Q_ARG(QByteArray, b));
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    95
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    96
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    97
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray& msg) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    98
  switch (mt) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    99
    case MSG_PREVIEW: {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   100
      qDebug("MSG_PREVIEW");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   101
      m_previewProvider->setPixmap(msg);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   102
      emit previewImageChanged();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   103
      break;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   104
    }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   105
    case MSG_PREVIEWHOGCOUNT: {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   106
      qDebug("MSG_PREVIEWHOGCOUNT");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   107
      m_previewHedgehogsCount = static_cast<quint8>(msg.data()[0]);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   108
      emit previewHogCountChanged(m_previewHedgehogsCount);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   109
      break;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   110
    }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   111
    case MSG_TONET: {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   112
      qDebug("MSG_TONET");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   113
      break;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   114
    }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   115
    case MSG_GAMEFINISHED: {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   116
      qDebug("MSG_GAMEFINISHED");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   117
      emit gameFinished();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   118
      break;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   119
    }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   120
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   121
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   122
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   123
void HWEngine::getPreview() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   124
  m_seed = QUuid::createUuid().toByteArray();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   125
  m_gameConfig.cmdSeed(m_seed);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   126
  m_gameConfig.setPreview(true);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   127
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   128
  m_runQueue->queue(m_gameConfig);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   129
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   130
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   131
void HWEngine::runQuickGame() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   132
  m_gameConfig.cmdSeed(m_seed);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   133
  m_gameConfig.cmdTheme("Nature");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   134
  Team team1;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   135
  team1.name = "team1";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   136
  Team team2;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   137
  team2.name = "team2";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   138
  team2.color = "7654321";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   139
  m_gameConfig.cmdTeam(team1);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   140
  m_gameConfig.cmdTeam(team2);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   141
  m_gameConfig.setPreview(false);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   142
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   143
  m_runQueue->queue(m_gameConfig);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   144
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   145
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
   146
int HWEngine::previewHedgehogsCount() const { return m_previewHedgehogsCount; }