qmlfrontend/hwengine.cpp
author alfadur
Mon, 19 Nov 2018 09:37:53 +0300
changeset 14242 81fba21495a5
parent 14159 8354b390f1a2
child 14276 1aac8a62be6f
permissions -rw-r--r--
was a wrong copy apparently
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     1
#include <QDebug>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     2
#include <QLibrary>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     3
#include <QQmlEngine>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     4
#include <QUuid>
745c73e0e644 Start working on frontend to rust engine rewrite
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_instance.h"
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
     7
#include "engine_interface.h"
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
     8
#include "game_view.h"
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
     9
#include "preview_image_provider.h"
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    10
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    11
#include "hwengine.h"
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    12
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    13
HWEngine::HWEngine(QQmlEngine* engine, QObject* parent)
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    14
    : QObject(parent),
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    15
      m_engine(engine),
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    16
      m_previewProvider(new PreviewImageProvider()) {
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    17
  m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    18
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    19
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    20
HWEngine::~HWEngine() {}
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    21
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    22
static QObject* hwengine_singletontype_provider(QQmlEngine* engine,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    23
                                                QJSEngine* scriptEngine) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    24
  Q_UNUSED(scriptEngine)
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    25
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    26
  HWEngine* hwengine = new HWEngine(engine);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    27
  return hwengine;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    28
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    29
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    30
void HWEngine::exposeToQML() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    31
  qDebug("HWEngine::exposeToQML");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    32
  qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine",
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    33
                                     hwengine_singletontype_provider);
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    34
  // qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    35
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
void HWEngine::getPreview() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
  m_seed = QUuid::createUuid().toByteArray();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
  m_gameConfig.cmdSeed(m_seed);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    40
  m_gameConfig.setPreview(true);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    41
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    42
  EngineInstance engine;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    43
  Engine::PreviewInfo preview = engine.generatePreview();
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    44
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    45
  QVector<QRgb> colorTable;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    46
  colorTable.resize(256);
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    47
  for (int i = 0; i < 256; ++i) colorTable[i] = qRgba(255, 255, 0, i);
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    48
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    49
  QImage previewImage(preview.land, preview.width, preview.height,
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    50
                      QImage::Format_Indexed8);
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    51
  previewImage.setColorTable(colorTable);
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    52
  previewImage.detach();
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    53
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    54
  m_previewProvider->setImage(previewImage);
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    55
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    56
  emit previewImageChanged();
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    57
  // m_runQueue->queue(m_gameConfig);
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    58
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    59
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    60
void HWEngine::runQuickGame() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    61
  m_gameConfig.cmdSeed(m_seed);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    62
  m_gameConfig.cmdTheme("Nature");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    63
  Team team1;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    64
  team1.name = "team1";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    65
  Team team2;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    66
  team2.name = "team2";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    67
  team2.color = "7654321";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    68
  m_gameConfig.cmdTeam(team1);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    69
  m_gameConfig.cmdTeam(team2);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    70
  m_gameConfig.setPreview(false);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    71
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14148
diff changeset
    72
  // m_runQueue->queue(m_gameConfig);
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    73
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    74
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    75
int HWEngine::previewHedgehogsCount() const { return m_previewHedgehogsCount; }