qmlfrontend/engine_instance.cpp
author unC0Rr
Fri, 07 Dec 2018 13:56:45 +0100
changeset 14393 b6824a53d4b1
parent 14319 00b56ec8b7df
child 14394 4409344db447
permissions -rw-r--r--
Allow to instantiate HWEngine with different library binaries
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14175
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     1
#include "engine_instance.h"
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     2
14319
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
     3
#include <QDebug>
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
     4
#include <QLibrary>
14319
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
     5
#include <QOpenGLFunctions>
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
     6
#include <QSurface>
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
     7
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
     8
static QOpenGLContext* currentOpenglContext = nullptr;
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
     9
extern "C" void (*getProcAddress(const char* fn))() {
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    10
  if (!currentOpenglContext)
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    11
    return nullptr;
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    12
  else
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    13
    return currentOpenglContext->getProcAddress(fn);
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    14
}
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14311
diff changeset
    15
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    16
EngineInstance::EngineInstance(const QString& libraryPath, QObject* parent)
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    17
    : QObject(parent) {
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    18
  QLibrary hwlib(libraryPath);
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    19
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    20
  if (!hwlib.load())
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    21
    qWarning() << "Engine library not found" << hwlib.errorString();
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    22
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    23
  hedgewars_engine_protocol_version =
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    24
      reinterpret_cast<Engine::hedgewars_engine_protocol_version_t*>(
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    25
          hwlib.resolve("hedgewars_engine_protocol_version"));
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    26
  start_engine =
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    27
      reinterpret_cast<Engine::start_engine_t*>(hwlib.resolve("start_engine"));
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    28
  generate_preview = reinterpret_cast<Engine::generate_preview_t*>(
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    29
      hwlib.resolve("generate_preview"));
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    30
  cleanup = reinterpret_cast<Engine::cleanup_t*>(hwlib.resolve("cleanup"));
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    31
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    32
  send_ipc = reinterpret_cast<Engine::send_ipc_t*>(hwlib.resolve("send_ipc"));
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    33
  read_ipc = reinterpret_cast<Engine::read_ipc_t*>(hwlib.resolve("read_ipc"));
14175
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    34
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    35
  setup_current_gl_context =
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    36
      reinterpret_cast<Engine::setup_current_gl_context_t*>(
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    37
          hwlib.resolve("setup_current_gl_context"));
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    38
  render_frame =
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    39
      reinterpret_cast<Engine::render_frame_t*>(hwlib.resolve("render_frame"));
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    40
  advance_simulation = reinterpret_cast<Engine::advance_simulation_t*>(
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    41
      hwlib.resolve("advance_simulation"));
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    42
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    43
  m_isValid = hedgewars_engine_protocol_version && start_engine &&
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    44
              generate_preview && cleanup && send_ipc && read_ipc &&
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    45
              setup_current_gl_context && render_frame && advance_simulation;
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    46
  emit isValidChanged(m_isValid);
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    47
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    48
  if (isValid()) {
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    49
    qDebug() << "Loaded engine library with protocol version"
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    50
             << hedgewars_engine_protocol_version();
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    51
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    52
    m_instance = start_engine();
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    53
  }
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    54
}
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    55
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    56
EngineInstance::~EngineInstance() {
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    57
  if (m_isValid) cleanup(m_instance);
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    58
}
14175
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    59
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14292
diff changeset
    60
void EngineInstance::sendConfig(const GameConfig& config) {
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14175
diff changeset
    61
  for (auto b : config.config()) {
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    62
    send_ipc(m_instance, reinterpret_cast<uint8_t*>(b.data()),
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    63
             static_cast<size_t>(b.size()));
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14175
diff changeset
    64
  }
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14175
diff changeset
    65
}
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14175
diff changeset
    66
14319
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    67
void EngineInstance::advance(quint32 ticks) {
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    68
  advance_simulation(m_instance, ticks);
14319
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    69
}
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14292
diff changeset
    70
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    71
void EngineInstance::renderFrame() { render_frame(m_instance); }
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14292
diff changeset
    72
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14311
diff changeset
    73
void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
14319
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    74
  currentOpenglContext = context;
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    75
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14315
diff changeset
    76
  auto size = context->surface()->size();
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    77
  setup_current_gl_context(m_instance, static_cast<quint16>(size.width()),
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    78
                           static_cast<quint16>(size.height()),
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    79
                           &getProcAddress);
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14311
diff changeset
    80
}
14311
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14292
diff changeset
    81
14175
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    82
Engine::PreviewInfo EngineInstance::generatePreview() {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    83
  Engine::PreviewInfo pinfo;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    84
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    85
  generate_preview(m_instance, &pinfo);
14175
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    86
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    87
  return pinfo;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    88
}
14393
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    89
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14319
diff changeset
    90
bool EngineInstance::isValid() const { return m_isValid; }