qmlfrontend/hwengine.cpp
author unC0Rr
Tue, 10 Sep 2024 13:56:51 +0200
branchtransitional_engine
changeset 16032 31cc1e450273
parent 15891 d52f5d8e75e6
permissions -rw-r--r--
Add maze land generator ported from pascal engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14371
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
     1
#include "hwengine.h"
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
     2
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     3
#include <QDebug>
14371
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
     4
#include <QImage>
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     5
#include <QUuid>
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     6
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
     7
#include "engine_instance.h"
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
     8
#include "engine_interface.h"
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14273
diff changeset
     9
#include "game_view.h"
14371
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    10
#include "preview_acceptor.h"
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    11
15891
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    12
HWEngine::HWEngine(QObject* parent) : QObject(parent), m_dataPath{QStringLiteral("Data")} {}
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    13
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
    14
HWEngine::~HWEngine() {}
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    15
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    16
void HWEngine::getPreview() {
14273
645e4591361f Send seed to engine to receive random map previews
unC0Rr
parents: 14271
diff changeset
    17
  emit previewIsRendering();
645e4591361f Send seed to engine to receive random map previews
unC0Rr
parents: 14271
diff changeset
    18
645e4591361f Send seed to engine to receive random map previews
unC0Rr
parents: 14271
diff changeset
    19
  m_gameConfig = GameConfig();
645e4591361f Send seed to engine to receive random map previews
unC0Rr
parents: 14271
diff changeset
    20
  m_gameConfig.cmdSeed(QUuid::createUuid().toByteArray());
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    21
15891
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    22
  EngineInstance engine(m_engineLibrary, m_dataPath);
14372
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    23
  if (!engine.isValid())  // TODO: error notification
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    24
    return;
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    25
14271
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14154
diff changeset
    26
  engine.sendConfig(m_gameConfig);
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14154
diff changeset
    27
14373
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14372
diff changeset
    28
  QImage previewImage = engine.generatePreview();
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
    29
14371
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    30
  if (m_previewAcceptor) m_previewAcceptor->setImage(previewImage);
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
    31
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
    32
  emit previewImageChanged();
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
    33
  // m_runQueue->queue(m_gameConfig);
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    34
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    35
14298
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14294
diff changeset
    36
EngineInstance* HWEngine::runQuickGame() {
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
  m_gameConfig.cmdTheme("Nature");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
  Team team1;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
  team1.name = "team1";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    40
  Team team2;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    41
  team2.name = "team2";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    42
  team2.color = "7654321";
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    43
  m_gameConfig.cmdTeam(team1);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    44
  m_gameConfig.cmdTeam(team2);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    45
15891
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    46
  EngineInstance* engine = new EngineInstance(m_engineLibrary, m_dataPath, this);
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    47
  engine->sendConfig(m_gameConfig);
14372
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    48
14298
00b56ec8b7df Pass opengl context to engine
unC0Rr
parents: 14294
diff changeset
    49
  return engine;
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
    50
  // m_runQueue->queue(m_gameConfig);
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    51
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    52
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    53
int HWEngine::previewHedgehogsCount() const { return m_previewHedgehogsCount; }
14371
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    54
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    55
PreviewAcceptor* HWEngine::previewAcceptor() const { return m_previewAcceptor; }
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    56
14372
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    57
QString HWEngine::engineLibrary() const { return m_engineLibrary; }
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    58
14371
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    59
void HWEngine::setPreviewAcceptor(PreviewAcceptor* previewAcceptor) {
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    60
  if (m_previewAcceptor == previewAcceptor) return;
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    61
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    62
  m_previewAcceptor = previewAcceptor;
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    63
  emit previewAcceptorChanged(m_previewAcceptor);
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14298
diff changeset
    64
}
14372
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    65
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    66
void HWEngine::setEngineLibrary(const QString& engineLibrary) {
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    67
  if (m_engineLibrary == engineLibrary) return;
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    68
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    69
  m_engineLibrary = engineLibrary;
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    70
  emit engineLibraryChanged(m_engineLibrary);
b6824a53d4b1 Allow to instantiate HWEngine with different library binaries
unC0Rr
parents: 14371
diff changeset
    71
}
15891
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    72
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    73
const QString &HWEngine::dataPath() const
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    74
{
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    75
  return m_dataPath;
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    76
}
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    77
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    78
void HWEngine::setDataPath(const QString &newDataPath)
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    79
{
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    80
  if (m_dataPath == newDataPath)
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    81
    return;
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    82
  m_dataPath = newDataPath;
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    83
  emit dataPathChanged();
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 14373
diff changeset
    84
}