qmlfrontend/engine_instance.cpp
changeset 14372 b6824a53d4b1
parent 14298 00b56ec8b7df
child 14373 4409344db447
equal deleted inserted replaced
14371:90bd2c331703 14372:b6824a53d4b1
     1 #include "engine_instance.h"
     1 #include "engine_instance.h"
     2 
     2 
     3 #include <QDebug>
     3 #include <QDebug>
       
     4 #include <QLibrary>
     4 #include <QOpenGLFunctions>
     5 #include <QOpenGLFunctions>
     5 #include <QSurface>
     6 #include <QSurface>
     6 
     7 
     7 static QOpenGLContext* currentOpenglContext = nullptr;
     8 static QOpenGLContext* currentOpenglContext = nullptr;
     8 extern "C" void (*getProcAddress(const char* fn))() {
     9 extern "C" void (*getProcAddress(const char* fn))() {
    10     return nullptr;
    11     return nullptr;
    11   else
    12   else
    12     return currentOpenglContext->getProcAddress(fn);
    13     return currentOpenglContext->getProcAddress(fn);
    13 }
    14 }
    14 
    15 
    15 EngineInstance::EngineInstance(QObject* parent)
    16 EngineInstance::EngineInstance(const QString& libraryPath, QObject* parent)
    16     : QObject(parent), m_instance(Engine::start_engine()) {}
    17     : QObject(parent) {
       
    18   QLibrary hwlib(libraryPath);
    17 
    19 
    18 EngineInstance::~EngineInstance() { Engine::cleanup(m_instance); }
    20   if (!hwlib.load())
       
    21     qWarning() << "Engine library not found" << hwlib.errorString();
       
    22 
       
    23   hedgewars_engine_protocol_version =
       
    24       reinterpret_cast<Engine::hedgewars_engine_protocol_version_t*>(
       
    25           hwlib.resolve("hedgewars_engine_protocol_version"));
       
    26   start_engine =
       
    27       reinterpret_cast<Engine::start_engine_t*>(hwlib.resolve("start_engine"));
       
    28   generate_preview = reinterpret_cast<Engine::generate_preview_t*>(
       
    29       hwlib.resolve("generate_preview"));
       
    30   cleanup = reinterpret_cast<Engine::cleanup_t*>(hwlib.resolve("cleanup"));
       
    31 
       
    32   send_ipc = reinterpret_cast<Engine::send_ipc_t*>(hwlib.resolve("send_ipc"));
       
    33   read_ipc = reinterpret_cast<Engine::read_ipc_t*>(hwlib.resolve("read_ipc"));
       
    34 
       
    35   setup_current_gl_context =
       
    36       reinterpret_cast<Engine::setup_current_gl_context_t*>(
       
    37           hwlib.resolve("setup_current_gl_context"));
       
    38   render_frame =
       
    39       reinterpret_cast<Engine::render_frame_t*>(hwlib.resolve("render_frame"));
       
    40   advance_simulation = reinterpret_cast<Engine::advance_simulation_t*>(
       
    41       hwlib.resolve("advance_simulation"));
       
    42 
       
    43   m_isValid = hedgewars_engine_protocol_version && start_engine &&
       
    44               generate_preview && cleanup && send_ipc && read_ipc &&
       
    45               setup_current_gl_context && render_frame && advance_simulation;
       
    46   emit isValidChanged(m_isValid);
       
    47 
       
    48   if (isValid()) {
       
    49     qDebug() << "Loaded engine library with protocol version"
       
    50              << hedgewars_engine_protocol_version();
       
    51 
       
    52     m_instance = start_engine();
       
    53   }
       
    54 }
       
    55 
       
    56 EngineInstance::~EngineInstance() {
       
    57   if (m_isValid) cleanup(m_instance);
       
    58 }
    19 
    59 
    20 void EngineInstance::sendConfig(const GameConfig& config) {
    60 void EngineInstance::sendConfig(const GameConfig& config) {
    21   for (auto b : config.config()) {
    61   for (auto b : config.config()) {
    22     Engine::send_ipc(m_instance, reinterpret_cast<uint8_t*>(b.data()),
    62     send_ipc(m_instance, reinterpret_cast<uint8_t*>(b.data()),
    23                      static_cast<size_t>(b.size()));
    63              static_cast<size_t>(b.size()));
    24   }
    64   }
    25 }
    65 }
    26 
    66 
    27 void EngineInstance::advance(quint32 ticks) {
    67 void EngineInstance::advance(quint32 ticks) {
    28   Engine::advance_simulation(m_instance, ticks);
    68   advance_simulation(m_instance, ticks);
    29 }
    69 }
    30 
    70 
    31 void EngineInstance::renderFrame() { Engine::render_frame(m_instance); }
    71 void EngineInstance::renderFrame() { render_frame(m_instance); }
    32 
    72 
    33 void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
    73 void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
    34   currentOpenglContext = context;
    74   currentOpenglContext = context;
    35 
    75 
    36   auto size = context->surface()->size();
    76   auto size = context->surface()->size();
    37   Engine::setup_current_gl_context(
    77   setup_current_gl_context(m_instance, static_cast<quint16>(size.width()),
    38       m_instance, static_cast<quint16>(size.width()),
    78                            static_cast<quint16>(size.height()),
    39       static_cast<quint16>(size.height()), &getProcAddress);
    79                            &getProcAddress);
    40 }
    80 }
    41 
    81 
    42 Engine::PreviewInfo EngineInstance::generatePreview() {
    82 Engine::PreviewInfo EngineInstance::generatePreview() {
    43   Engine::PreviewInfo pinfo;
    83   Engine::PreviewInfo pinfo;
    44 
    84 
    45   Engine::generate_preview(m_instance, &pinfo);
    85   generate_preview(m_instance, &pinfo);
    46 
    86 
    47   return pinfo;
    87   return pinfo;
    48 }
    88 }
       
    89 
       
    90 bool EngineInstance::isValid() const { return m_isValid; }