qmlfrontend/main.cpp
author unC0Rr
Fri, 07 Dec 2018 13:35:08 +0100
changeset 14376 90bd2c331703
parent 14375 ef2fc0210362
child 14377 b6824a53d4b1
permissions -rw-r--r--
Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
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: 14148
diff changeset
     1
#include <QDebug>
12859
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
     2
#include <QGuiApplication>
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
     3
#include <QLibrary>
12859
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
     4
#include <QQmlApplicationEngine>
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
     5
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
     6
#include "engine_interface.h"
14376
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
     7
#include "game_view.h"
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents: 12859
diff changeset
     8
#include "hwengine.h"
14376
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
     9
#include "preview_acceptor.h"
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents: 12859
diff changeset
    10
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    11
namespace Engine {
14375
ef2fc0210362 Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents: 14299
diff changeset
    12
hedgewars_engine_protocol_version_t* hedgewars_engine_protocol_version;
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    13
start_engine_t* start_engine;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    14
generate_preview_t* generate_preview;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    15
cleanup_t* cleanup;
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    16
send_ipc_t* send_ipc;
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    17
read_ipc_t* read_ipc;
14299
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    18
setup_current_gl_context_t* setup_current_gl_context;
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    19
render_frame_t* render_frame;
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    20
advance_simulation_t* advance_simulation;
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    21
};  // namespace Engine
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    22
14376
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    23
static QObject* previewacceptor_singletontype_provider(
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    24
    QQmlEngine* engine, QJSEngine* scriptEngine) {
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    25
  Q_UNUSED(scriptEngine)
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    26
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    27
  PreviewAcceptor* acceptor = new PreviewAcceptor(engine);
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    28
  return acceptor;
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    29
}
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    30
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    31
void loadEngineLibrary() {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    32
#ifdef Q_OS_WIN
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    33
  QLibrary hwlib("./libhedgewars_engine.dll");
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    34
#else
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    35
  QLibrary hwlib("./libhedgewars_engine.so");
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    36
#endif
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    37
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    38
  if (!hwlib.load())
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    39
    qWarning() << "Engine library not found" << hwlib.errorString();
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    40
14375
ef2fc0210362 Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents: 14299
diff changeset
    41
  Engine::hedgewars_engine_protocol_version =
ef2fc0210362 Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents: 14299
diff changeset
    42
      reinterpret_cast<Engine::hedgewars_engine_protocol_version_t*>(
ef2fc0210362 Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents: 14299
diff changeset
    43
          hwlib.resolve("hedgewars_engine_protocol_version"));
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    44
  Engine::start_engine =
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    45
      reinterpret_cast<Engine::start_engine_t*>(hwlib.resolve("start_engine"));
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    46
  Engine::generate_preview = reinterpret_cast<Engine::generate_preview_t*>(
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    47
      hwlib.resolve("generate_preview"));
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    48
  Engine::cleanup =
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    49
      reinterpret_cast<Engine::cleanup_t*>(hwlib.resolve("cleanup"));
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    50
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    51
  Engine::send_ipc =
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    52
      reinterpret_cast<Engine::send_ipc_t*>(hwlib.resolve("send_ipc"));
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    53
  Engine::read_ipc =
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    54
      reinterpret_cast<Engine::read_ipc_t*>(hwlib.resolve("read_ipc"));
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14159
diff changeset
    55
14299
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    56
  Engine::setup_current_gl_context =
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    57
      reinterpret_cast<Engine::setup_current_gl_context_t*>(
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    58
          hwlib.resolve("setup_current_gl_context"));
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    59
  Engine::render_frame =
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    60
      reinterpret_cast<Engine::render_frame_t*>(hwlib.resolve("render_frame"));
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    61
  Engine::advance_simulation = reinterpret_cast<Engine::advance_simulation_t*>(
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    62
      hwlib.resolve("advance_simulation"));
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14276
diff changeset
    63
14375
ef2fc0210362 Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents: 14299
diff changeset
    64
  if (Engine::hedgewars_engine_protocol_version)
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    65
    qDebug() << "Loaded engine library with protocol version"
14375
ef2fc0210362 Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents: 14299
diff changeset
    66
             << Engine::hedgewars_engine_protocol_version();
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    67
}
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    68
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents: 12859
diff changeset
    69
int main(int argc, char* argv[]) {
12859
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
    70
  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
    71
  QGuiApplication app(argc, argv);
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
    72
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    73
  loadEngineLibrary();
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    74
12859
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
    75
  QQmlApplicationEngine engine;
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents: 12859
diff changeset
    76
14376
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    77
  qmlRegisterSingletonType<PreviewAcceptor>(
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    78
      "Hedgewars.Engine", 1, 0, "PreviewAcceptor",
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    79
      previewacceptor_singletontype_provider);
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    80
  qmlRegisterType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine");
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    81
  qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    82
  qmlRegisterUncreatableType<EngineInstance>("Hedgewars.Engine", 1, 0,
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    83
                                             "EngineInstance",
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14375
diff changeset
    84
                                             "Create by HWEngine run methods");
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents: 12859
diff changeset
    85
12859
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
    86
  engine.load(QUrl(QLatin1String("qrc:/main.qml")));
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents: 12859
diff changeset
    87
  if (engine.rootObjects().isEmpty()) return -1;
12859
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
    88
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
    89
  return app.exec();
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
    90
}