From fa27014c9b78d13fe8720341ed73c249782c04d7 Mon Sep 17 00:00:00 2001 From: Xavier ASUS Date: Fri, 23 Nov 2018 12:46:39 +0100 Subject: Minor improvements and optimizations. --- Source/MapEditor/MapEditor.pro | 2 + Source/MapEditor/mainwindow.cpp | 244 ++++++++++++++++++++-------------------- Source/MapEditor/mainwindow.h | 18 +-- Source/MapEditor/mainwindow.ui | 136 ++++++++++++++-------- Source/MapEditor/settings.ini | 2 +- 5 files changed, 224 insertions(+), 178 deletions(-) (limited to 'Source/MapEditor') diff --git a/Source/MapEditor/MapEditor.pro b/Source/MapEditor/MapEditor.pro index 3a7473f..3d53cf5 100644 --- a/Source/MapEditor/MapEditor.pro +++ b/Source/MapEditor/MapEditor.pro @@ -12,6 +12,8 @@ TARGET = MapEditor TEMPLATE = app CONFIG += static +QMAKE_CXXFLAGS += -funsigned-char + SOURCES += main.cpp\ mainwindow.cpp \ mygraphicsscene.cpp diff --git a/Source/MapEditor/mainwindow.cpp b/Source/MapEditor/mainwindow.cpp index fe1cfaa..32a699e 100644 --- a/Source/MapEditor/mainwindow.cpp +++ b/Source/MapEditor/mainwindow.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #define DEFAULT_AIRPORT_NAME QByteArray("Default Airport\0") @@ -13,9 +14,9 @@ MainWindow::MainWindow(QWidget *parent) : selected_item(-1) { ui->setupUi(this); - this->setWindowTitle(APP_NAME + " " + APP_VERSION_STRING); + this->setWindowTitle(APP_FULL_NAME); - connect(ui->LoadMap_Btn, SIGNAL(released()), this, SLOT(onLoadMap())); + connect(ui->LoadMap_Btn, SIGNAL(released()), this, SLOT(loadMap())); connect(ui->CreateMap_Btn, SIGNAL(released()), this, SLOT(onCreateMap())); connect(ui->saveMap_Btn, SIGNAL(released()), this, SLOT(onSaveMap(void))); connect(ui->showNumbers_Checkbox, SIGNAL(stateChanged(int)), this, SLOT(onShowNumbers(int))); @@ -37,7 +38,7 @@ MainWindow::~MainWindow() void MainWindow::onShowNumbers(int) { - onProcessMapFile(map_buffer); + processMapFile(map_buffer); } void MainWindow::onMapItemClicked(QPointF pos) @@ -57,7 +58,7 @@ void MainWindow::onMapItemClicked(QPointF pos) if (tile_no < (level_size * level_size)) { selected_item = tile_no; - onProcessMapFile(map_buffer); + processMapFile(map_buffer); } else { @@ -67,30 +68,29 @@ void MainWindow::onMapItemClicked(QPointF pos) void MainWindow::onListItemSelected(void) { - QList item = ui->listWidget->selectedItems(); - - foreach (QListWidgetItem *it, item) + foreach (const QListWidgetItem* const it, ui->tileList->selectedItems()) { - int row = ui->listWidget->row(it); - - if (selected_item != -1) + if (it != nullptr) { - int map_buffer_pos = (DATA_HEADER_SIZE + 1) + (selected_item * sizeof(quint16)); - //map_buffer_pos++; // MSB: building data, LSB: terrain data. - - qDebug() << "Calculated file offset: 0x" + QString::number(map_buffer_pos, 16); - - if (map_buffer_pos < map_buffer.count()) + if (selected_item != -1) { - qDebug() << "Current data at 0x" + QString::number(map_buffer_pos, 16) + ": " + tilesetData.value(map_buffer[map_buffer_pos]); - map_buffer[map_buffer_pos] = row; + const int map_buffer_pos = static_cast((DATA_HEADER_SIZE + 1) + + (static_cast(selected_item) * sizeof(quint16))); + //map_buffer_pos++; // MSB: building data, LSB: terrain data. - if (ui->mirror_CheckBox->isChecked() == true) + if (map_buffer_pos < map_buffer.count()) { - map_buffer[map_buffer_pos] = map_buffer[map_buffer_pos] | TILE_MIRROR_FLAG; - } + const char row = static_cast(ui->tileList->row(it)); + + map_buffer[map_buffer_pos] = row; + + if (ui->mirror_CheckBox->isChecked() ) + { + map_buffer[map_buffer_pos] = map_buffer[map_buffer_pos] | TILE_MIRROR_FLAG; + } - onProcessMapFile(map_buffer); + processMapFile(map_buffer); + } } } } @@ -98,14 +98,14 @@ void MainWindow::onListItemSelected(void) void MainWindow::onSaveMap(void) { - QString path = QFileDialog::getSaveFileName(this, - "Save map file", - _last_dir, - "Map files (*.LVL)"); + const QString path = QFileDialog::getSaveFileName(this, + "Save map file", + _last_dir, + "Map files (*.LVL)"); QFile f(path); - if(checkFile(f, QFile::WriteOnly) == false) + if (checkFile(f, QFile::WriteOnly) == false) { return; } @@ -118,7 +118,7 @@ void MainWindow::onSaveMap(void) void MainWindow::onNoItemSelected(void) { selected_item = -1; - onProcessMapFile(map_buffer); + processMapFile(map_buffer); } void MainWindow::onCreateMap(void) @@ -130,126 +130,123 @@ void MainWindow::onCreateMap(void) items << "16"; items << "24"; - QString size = QInputDialog::getItem( this, - tr("Create new map"), - tr("Select map size:"), - items, - 0, - false, - &ok ); + const QString strSize = QInputDialog::getItem(this, + tr("Create new map"), + tr("Select map size:"), + items, + 0, + false, + &ok ); - if ( (ok == false) || (size.isEmpty() == true) ) + if (ok && (not strSize.isEmpty())) { - return; - } + QByteArray data; - QByteArray data; + data.append("ATC"); - data.append("ATC"); + const quint8 size = static_cast(strSize.toInt(&ok)); - if (size == "8") - { - level_size = 8; - data.append((char)0x08); - } - else if (size == "16") - { - level_size = 16; - data.append((char)0x10); - } - else if (size == "24") - { - level_size = 24; - data.append((char)0x18); - } + if (ok) + { + switch (size) + { + case 8: + // Fall through. + case 16: + // Fall through. + case 24: + level_size = size; + data.append(static_cast(size)); + break; + + default: + showError(tr("Invalid map size ") + strSize); + break; + } - data.append(DEFAULT_AIRPORT_NAME); + data.append(DEFAULT_AIRPORT_NAME); - for (int i = 0x04 + DEFAULT_AIRPORT_NAME.count(); i < 0x1C; i++) - { - data.append('\0'); - } - - for (int i = (data.count() - 1); i < DATA_HEADER_SIZE; i++) - { - data.append(0xFF); - } + for (int i = 0x04 + DEFAULT_AIRPORT_NAME.count(); i < 0x1C; i++) + { + data.append('\0'); + } - int size_int = size.toInt(&ok, 10); + for (int i = (data.count() - 1); i < DATA_HEADER_SIZE; i++) + { + data.append(static_cast(0xFF)); + } - if (ok == false) - { - qDebug() << "Invalid map size."; - return; - } + for (quint8 i = 0; i < size; i++) + { + for (quint8 j = 0; j < size; j++) + { + data.append(static_cast(0)); // Building data + data.append(static_cast(0)); // Terrain data + } + } - for (int i = 0; i < size_int; i++) - { - for (int j = 0; j < size_int; j++) - { - data.append((char)0); // Building data - data.append((char)0); // Terrain data + processMapFile(data); } } - - qDebug() << "Created default map. Bytes: " + QString::number(data.count()); - - onProcessMapFile(data); } -void MainWindow::onLoadMap(void) +void MainWindow::loadMap(void) { - QString path = QFileDialog::getOpenFileName(this, - "Open map file", - _last_dir, - "Map files (*.LVL)"); + const QString path = QFileDialog::getOpenFileName(this, + "Open map file", + _last_dir, + "Map files (*.LVL)"); QFile f(path); - if(checkFile(f) == false) - { - return; + if (checkFile(f)) + { + processMapFile(f.readAll()); } - - QByteArray data = f.readAll(); - - onProcessMapFile(data); } -void MainWindow::onProcessMapFile(QByteArray data) +void MainWindow::processMapFile(const QByteArray& data) { map_buffer = data; - QDataStream ds(&data, QIODevice::ReadWrite); + QDataStream ds(data); char header[3]; ds.readRawData(header, 3); - if (strncmp(header, "ATC", 3) != 0) + if (strncmp(header, "ATC", 3) == 0) { - qDebug() << "Incorrect header"; - return; - } - - char ch; + char ch; - ds.readRawData(&ch, sizeof(char)); + ds.readRawData(&ch, sizeof(char)); - level_size = ch; + level_size = ch; - const QString filePath = "../../Sprites/TILESET1.bmp"; + const QString filePath = "../../Sprites/TILESET1.bmp"; - QPixmap tile1(filePath); + QPixmap tile1(filePath); - int expected_filesize = (DATA_HEADER_SIZE + (level_size * level_size)); + const int expected_filesize = (DATA_HEADER_SIZE + (level_size * level_size)); - if (data.count() < expected_filesize) + if (data.count() >= expected_filesize) + { + parseMapData(ds, tile1); + } + else + { + showError(tr("Invalid file size. Expected ") + + QString::number(expected_filesize, 10)); + } + } + else { - qDebug() << "Invalid count. Expected " + QString::number(expected_filesize, 10); - return; + showError(tr("Invalid header") + "\"" + header + "\""); } +} +void MainWindow::parseMapData(QDataStream& ds, const QPixmap& tileSet) +{ char airportName[0x1A]; ds.readRawData(airportName, sizeof(airportName) / sizeof(airportName[0])); @@ -269,20 +266,20 @@ void MainWindow::onProcessMapFile(QByteArray data) int v; char byte[2]; ds.readRawData(byte, 2); - quint8 CurrentTile = byte[1]; + quint8 CurrentTile = static_cast(byte[1]); if (CurrentTile & TILE_MIRROR_FLAG) { - u = (int)((CurrentTile & 0x7F) % 4) * 64; - v = (int)((CurrentTile & 0x7F) / 4) * 48; + u = static_cast(((CurrentTile & 0x7F) % 4) * 64); + v = static_cast(((CurrentTile & 0x7F) / 4) * 48); } else { - u = (int)(CurrentTile % 4) * 64; - v = (int)(CurrentTile / 4) * 48; + u = static_cast((CurrentTile % 4) * 64); + v = static_cast((CurrentTile / 4) * 48); } - QImage cropped = tile1.copy(u, v, 64, 48).toImage(); + QImage cropped = tileSet.copy(u, v, 64, 48).toImage(); if (CurrentTile & TILE_MIRROR_FLAG) { @@ -311,7 +308,7 @@ void MainWindow::onProcessMapFile(QByteArray data) { cropped.setPixel(i, j, qRgba(0,0,0,0)); } - else if (selected == true) + else if (selected ) { QColor c = cropped.pixelColor(i, j); @@ -337,7 +334,7 @@ void MainWindow::onProcessMapFile(QByteArray data) it->setX(x); it->setY(y); - if (ui->showNumbers_Checkbox->isChecked() == true) + if (ui->showNumbers_Checkbox->isChecked() ) { QGraphicsTextItem* io = new QGraphicsTextItem(); io->setPos(x + (TILE_SIZE / 4), y); @@ -356,7 +353,7 @@ bool MainWindow::checkFile(QFile& f, QFile::OpenModeFlag flags) { QFileInfo fi(f); - if(f.open(flags) == false) + if (f.open(flags) == false) { return false; } @@ -417,13 +414,13 @@ void MainWindow::loadTilesetData(void) QString tileNumber = "tile" + QString::number(i); QString tileName = tilesetFile.value(tileNumber, "").toString(); - if (tileName.isEmpty() == true) + if (tileName.isEmpty() ) { break; } tilesetData.insert(i++, tileName); - ui->listWidget->addItem(tileName); + ui->tileList->addItem(tileName); } tilesetFile.endGroup(); @@ -433,7 +430,7 @@ void MainWindow::loadTilesetData(void) void MainWindow::onAirportNameModified(QString name) { - if (map_buffer.isEmpty() == true) + if (map_buffer.isEmpty() ) { return; } @@ -450,3 +447,8 @@ void MainWindow::onAirportNameModified(QString name) } } } + +void MainWindow::showError(const QString& error) +{ + QMessageBox::critical(this, APP_FULL_NAME, error); +} diff --git a/Source/MapEditor/mainwindow.h b/Source/MapEditor/mainwindow.h index 705404f..8cc4c8e 100644 --- a/Source/MapEditor/mainwindow.h +++ b/Source/MapEditor/mainwindow.h @@ -5,16 +5,18 @@ #include #include #include +#include #include "mygraphicsscene.h" #include "ui_mainwindow.h" #define APP_NAME QString("Airport Map Editor") -#define APP_VERSION_STRING QString("0.2") +#define APP_VERSION_STRING QString("0.3") +#define APP_FULL_NAME APP_NAME + " " + APP_VERSION_STRING -#define TILE_SIZE 64 -#define DATA_HEADER_SIZE 0x3F -#define TILE_MIRROR_FLAG ((char) 0x80) +#define TILE_SIZE (64) +#define DATA_HEADER_SIZE (0x3F) +#define TILE_MIRROR_FLAG (0x80) namespace Ui { class MainWindow; @@ -25,7 +27,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - explicit MainWindow(QWidget *parent = 0); + explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); void closeEvent(QCloseEvent*); @@ -34,6 +36,7 @@ private: bool checkFile(QFile &f, QFile::OpenModeFlag flags = QFile::ReadOnly); void appSettings(void); void loadTilesetData(void); + void parseMapData(QDataStream &ds, const QPixmap &tileSet); QString _last_dir; MyGraphicsScene *gscene; int level_size; @@ -42,15 +45,16 @@ private: QHash tilesetData; private slots: - void onLoadMap(void); + void loadMap(void); void onCreateMap(void); - void onProcessMapFile(QByteArray); + void processMapFile(const QByteArray &); void onMapItemClicked(QPointF); void onNoItemSelected(void); void onListItemSelected(void); void onSaveMap(void); void onShowNumbers(int); void onAirportNameModified(QString); + void showError(const QString& error); }; #endif // MAINWINDOW_H diff --git a/Source/MapEditor/mainwindow.ui b/Source/MapEditor/mainwindow.ui index f5f3514..95daad6 100644 --- a/Source/MapEditor/mainwindow.ui +++ b/Source/MapEditor/mainwindow.ui @@ -7,7 +7,7 @@ 0 0 920 - 605 + 648 @@ -24,73 +24,100 @@ true - - + + + + + 99 + 99 + + + + + 0 + 0 + + + + + + - + - Show numbers on map - - - true + Tile list: - - - Mirror tile + + + + 256 + 16777215 + - - - - - + - Create map + Building list: - - - Load map - - + - - - Save map - - + + + + + Show numbers on map + + + true + + + + + + + Mirror tile + + + + + + + + + + + Create map + + + + + + + Load map + + + + + + + Save map + + + + - - - - - 640 - 480 - - - - - - - - - 256 - 16777215 - - - - - + @@ -116,7 +143,7 @@ 0 0 920 - 21 + 20 @@ -129,6 +156,17 @@ + + + toolBar + + + TopToolBarArea + + + false + + diff --git a/Source/MapEditor/settings.ini b/Source/MapEditor/settings.ini index c825412..dcef128 100644 --- a/Source/MapEditor/settings.ini +++ b/Source/MapEditor/settings.ini @@ -1,2 +1,2 @@ [app_settings] -last_dir=/home/xavier/Airport/Levels/LEVEL2.LVL +last_dir=/home/xavier/Airport/Levels/LEVEL1.LVL -- cgit v1.2.3