qmlfrontend/engine_instance.cpp
changeset 15919 d52f5d8e75e6
parent 15238 b32c52c76977
child 15921 5b3beb90e1a6
equal deleted inserted replaced
15918:8a6a2d931bae 15919:d52f5d8e75e6
    11     return nullptr;
    11     return nullptr;
    12   else
    12   else
    13     return currentOpenglContext->getProcAddress(fn);
    13     return currentOpenglContext->getProcAddress(fn);
    14 }
    14 }
    15 
    15 
    16 EngineInstance::EngineInstance(const QString& libraryPath, QObject* parent)
    16 EngineInstance::EngineInstance(const QString& libraryPath, const QString&dataPath, QObject* parent)
    17     : QObject(parent) {
    17     : QObject(parent), m_instance{nullptr, nullptr} {
    18   QLibrary hwlib(libraryPath);
    18   QLibrary hwlib(libraryPath);
    19 
    19 
    20   if (!hwlib.load())
    20   if (!hwlib.load())
    21     qWarning() << "Engine library not found" << hwlib.errorString();
    21     qWarning() << "Engine library not found" << hwlib.errorString();
    22 
    22 
    60 
    60 
    61   if (isValid()) {
    61   if (isValid()) {
    62     qDebug() << "Loaded engine library with protocol version"
    62     qDebug() << "Loaded engine library with protocol version"
    63              << hedgewars_engine_protocol_version();
    63              << hedgewars_engine_protocol_version();
    64 
    64 
    65     m_instance = start_engine();
    65     m_instance = std::unique_ptr<Engine::EngineInstance, Engine::cleanup_t*>(start_engine(dataPath.toUtf8().data()), cleanup);
    66   } else {
    66   } else {
    67     qDebug("Engine library load failed");
    67     qDebug("Engine library load failed");
    68   }
    68   }
    69 }
    69 }
    70 
    70 
    71 EngineInstance::~EngineInstance() {
    71 EngineInstance::~EngineInstance() = default;
    72   if (m_isValid) cleanup(m_instance);
       
    73 }
       
    74 
    72 
    75 void EngineInstance::sendConfig(const GameConfig& config) {
    73 void EngineInstance::sendConfig(const GameConfig& config) {
    76   for (auto b : config.config()) {
    74   for (auto b : config.config()) {
    77     send_ipc(m_instance, reinterpret_cast<uint8_t*>(b.data()),
    75     send_ipc(m_instance.get(), reinterpret_cast<uint8_t*>(b.data()),
    78              static_cast<size_t>(b.size()));
    76              static_cast<size_t>(b.size()));
    79   }
    77   }
    80 }
    78 }
    81 
    79 
    82 void EngineInstance::advance(quint32 ticks) {
    80 void EngineInstance::advance(quint32 ticks) {
    83   advance_simulation(m_instance, ticks);
    81   advance_simulation(m_instance.get(), ticks);
    84 }
    82 }
    85 
    83 
    86 void EngineInstance::moveCamera(const QPoint& delta) {
    84 void EngineInstance::moveCamera(const QPoint& delta) {
    87   move_camera(m_instance, delta.x(), delta.y());
    85   move_camera(m_instance.get(), delta.x(), delta.y());
    88 }
    86 }
    89 
    87 
    90 void EngineInstance::simpleEvent(Engine::SimpleEventType event_type) {
    88 void EngineInstance::simpleEvent(Engine::SimpleEventType event_type) {
    91   simple_event(m_instance, event_type);
    89   simple_event(m_instance.get(), event_type);
    92 }
    90 }
    93 
    91 
    94 void EngineInstance::longEvent(Engine::LongEventType event_type,
    92 void EngineInstance::longEvent(Engine::LongEventType event_type,
    95                                Engine::LongEventState state) {
    93                                Engine::LongEventState state) {
    96   long_event(m_instance, event_type, state);
    94   long_event(m_instance.get(), event_type, state);
    97 }
    95 }
    98 
    96 
    99 void EngineInstance::positionedEvent(Engine::PositionedEventType event_type,
    97 void EngineInstance::positionedEvent(Engine::PositionedEventType event_type,
   100                                      qint32 x, qint32 y) {
    98                                      qint32 x, qint32 y) {
   101   positioned_event(m_instance, event_type, x, y);
    99   positioned_event(m_instance.get(), event_type, x, y);
   102 }
   100 }
   103 
   101 
   104 void EngineInstance::renderFrame() { render_frame(m_instance); }
   102 void EngineInstance::renderFrame() { render_frame(m_instance.get()); }
   105 
   103 
   106 void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
   104 void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
   107   currentOpenglContext = context;
   105   currentOpenglContext = context;
   108 
   106 
   109   auto size = context->surface()->size();
   107   auto size = context->surface()->size();
   110   setup_current_gl_context(m_instance, static_cast<quint16>(size.width()),
   108   setup_current_gl_context(m_instance.get(), static_cast<quint16>(size.width()),
   111                            static_cast<quint16>(size.height()),
   109                            static_cast<quint16>(size.height()),
   112                            &getProcAddress);
   110                            &getProcAddress);
   113 }
   111 }
   114 
   112 
   115 QImage EngineInstance::generatePreview() {
   113 QImage EngineInstance::generatePreview() {
   116   Engine::PreviewInfo pinfo;
   114   Engine::PreviewInfo pinfo;
   117 
   115 
   118   generate_preview(m_instance, &pinfo);
   116   generate_preview(m_instance.get(), &pinfo);
   119 
   117 
   120   QVector<QRgb> colorTable;
   118   QVector<QRgb> colorTable;
   121   colorTable.resize(256);
   119   colorTable.resize(256);
   122   for (int i = 0; i < 256; ++i) colorTable[i] = qRgba(255, 255, 0, i);
   120   for (int i = 0; i < 256; ++i) colorTable[i] = qRgba(255, 255, 0, i);
   123 
   121 
   124   QImage previewImage(pinfo.land, static_cast<int>(pinfo.width),
   122   QImage previewImage(pinfo.land, static_cast<int>(pinfo.width),
   125                       static_cast<int>(pinfo.height), QImage::Format_Indexed8);
   123                       static_cast<int>(pinfo.height), QImage::Format_Indexed8);
   126   previewImage.setColorTable(colorTable);
   124   previewImage.setColorTable(colorTable);
   127 
   125 
   128   // Cannot use it here, since QImage refers to original bytes
   126   // Cannot use it here, since QImage refers to original bytes
   129   // dispose_preview(m_instance);
   127   // dispose_preview(m_instance.get());
   130 
   128 
   131   return previewImage;
   129   return previewImage;
   132 }
   130 }
   133 
   131 
   134 bool EngineInstance::isValid() const { return m_isValid; }
   132 bool EngineInstance::isValid() const { return m_isValid; }