qmlfrontend/engine_instance.cpp
author nemo
Wed, 05 Dec 2018 09:50:25 -0500
branch0.9.25
changeset 14373 c2a3d15df7d3
parent 14303 00b56ec8b7df
child 14377 b6824a53d4b1
permissions -rw-r--r--
Add log note just in case pas2c does anything odd.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14159
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
14303
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
     3
#include <QDebug>
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
     4
#include <QOpenGLFunctions>
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
     5
#include <QSurface>
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
     6
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
     7
static QOpenGLContext* currentOpenglContext = nullptr;
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
     8
extern "C" void (*getProcAddress(const char* fn))() {
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
     9
  if (!currentOpenglContext)
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    10
    return nullptr;
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    11
  else
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    12
    return currentOpenglContext->getProcAddress(fn);
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    13
}
14299
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14295
diff changeset
    14
14295
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14276
diff changeset
    15
EngineInstance::EngineInstance(QObject* parent)
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    16
    : QObject(parent), m_instance(Engine::start_engine()) {}
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    17
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    18
EngineInstance::~EngineInstance() { Engine::cleanup(m_instance); }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    19
14295
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14276
diff changeset
    20
void EngineInstance::sendConfig(const GameConfig& config) {
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    21
  for (auto b : config.config()) {
14295
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14276
diff changeset
    22
    Engine::send_ipc(m_instance, reinterpret_cast<uint8_t*>(b.data()),
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    23
                     static_cast<size_t>(b.size()));
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    24
  }
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    25
}
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    26
14303
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    27
void EngineInstance::advance(quint32 ticks) {
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    28
  Engine::advance_simulation(m_instance, ticks);
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    29
}
14295
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14276
diff changeset
    30
14303
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    31
void EngineInstance::renderFrame() { Engine::render_frame(m_instance); }
14295
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14276
diff changeset
    32
14299
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14295
diff changeset
    33
void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
14303
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    34
  currentOpenglContext = context;
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    35
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    36
  auto size = context->surface()->size();
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    37
  Engine::setup_current_gl_context(
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    38
      m_instance, static_cast<quint16>(size.width()),
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14299
diff changeset
    39
      static_cast<quint16>(size.height()), &getProcAddress);
14299
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14295
diff changeset
    40
}
14295
92e5682810d4 Prepare to have possibility to pass opengl context to engine
unc0rr
parents: 14276
diff changeset
    41
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    42
Engine::PreviewInfo EngineInstance::generatePreview() {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    43
  Engine::PreviewInfo pinfo;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    44
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    45
  Engine::generate_preview(m_instance, &pinfo);
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    46
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    47
  return pinfo;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    48
}