From 4eaa6c091f97ca3455b2ae48a5f68867c0f7768e Mon Sep 17 00:00:00 2001 From: XaviDCR92 Date: Tue, 26 Dec 2017 22:18:37 +0100 Subject: * Slight optimization: all comparations against true are now compared against != false. + Added a provisional background during gameplay (still WIP). * Map editor now allows creating a map. --- Source/MapEditor/MapEditor.pro | 6 +- Source/MapEditor/MapEditor.pro.user | 6 +- Source/MapEditor/mainwindow.cpp | 193 +++++++++++++++++++++++++--- Source/MapEditor/mainwindow.h | 5 +- Source/MapEditor/mainwindow.ui | 39 +++--- Source/MapEditor/mygraphicsscene.cpp | 20 +++ Source/MapEditor/mygraphicsscene.h | 17 +++ Source/MapEditor/release/moc_mainwindow.cpp | 111 ---------------- Source/MapEditor/ui_mainwindow.h | 128 ------------------ 9 files changed, 243 insertions(+), 282 deletions(-) create mode 100644 Source/MapEditor/mygraphicsscene.cpp create mode 100644 Source/MapEditor/mygraphicsscene.h delete mode 100644 Source/MapEditor/release/moc_mainwindow.cpp delete mode 100644 Source/MapEditor/ui_mainwindow.h (limited to 'Source/MapEditor') diff --git a/Source/MapEditor/MapEditor.pro b/Source/MapEditor/MapEditor.pro index 8f56728..cce1ab2 100644 --- a/Source/MapEditor/MapEditor.pro +++ b/Source/MapEditor/MapEditor.pro @@ -13,9 +13,11 @@ TEMPLATE = app SOURCES += main.cpp\ - mainwindow.cpp + mainwindow.cpp \ + mygraphicsscene.cpp -HEADERS += mainwindow.h +HEADERS += mainwindow.h \ + mygraphicsscene.h FORMS += mainwindow.ui diff --git a/Source/MapEditor/MapEditor.pro.user b/Source/MapEditor/MapEditor.pro.user index c9d463f..0b066e2 100644 --- a/Source/MapEditor/MapEditor.pro.user +++ b/Source/MapEditor/MapEditor.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -66,7 +66,7 @@ 0 0 - C:/cygwin/home/Xavier/Airport/Source/MapEditor + C:/cygwin/home/Xavier/Airport/Source/build-MapEditor-Desktop_Qt_5_7_0_MinGW_32bit-Debug true @@ -292,7 +292,7 @@ MapEditor.pro false - C:/cygwin/home/Xavier/Airport/Source/MapEditor + C:/cygwin/home/Xavier/Airport/Source/build-MapEditor-Desktop_Qt_5_7_0_MinGW_32bit-Debug 3768 false true diff --git a/Source/MapEditor/mainwindow.cpp b/Source/MapEditor/mainwindow.cpp index 2632429..ef0dfc6 100644 --- a/Source/MapEditor/mainwindow.cpp +++ b/Source/MapEditor/mainwindow.cpp @@ -1,14 +1,18 @@ #include "mainwindow.h" #include #include +#include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) + ui(new Ui::MainWindow), + gscene(new MyGraphicsScene) { ui->setupUi(this); - connect(ui->LoadMap_Btn, SIGNAL(released()), this, SLOT(onLoadMap())); + connect(ui->LoadMap_Btn, SIGNAL(released()), this, SLOT(onLoadMap())); + connect(ui->CreateMap_Btn, SIGNAL(released()), this, SLOT(onCreateMap())); appSettings(); } @@ -16,6 +20,72 @@ MainWindow::MainWindow(QWidget *parent) : MainWindow::~MainWindow() { delete ui; + delete gscene; +} + +void MainWindow::onCreateMap(void) +{ + bool ok; + QStringList items; + + items << "8"; + items << "16"; + items << "24"; + + QString size = QInputDialog::getItem(this, tr("Create new map"), + tr("Select map size:"), items, 0, false, &ok); + + if ( (ok == false) || (size.isEmpty() == true) ) + { + return; + } + + QByteArray data; + + data.append("ATC"); + + if (size == "8") + { + data.append((char)0x08); + } + else if (size == "16") + { + data.append((char)0x10); + } + else if (size == "24") + { + data.append((char)0x18); + } + + data.append("Default airport"); + + for (int i = (data.count() - 1); i < 0x3F; i++) + { + data.append(0xFF); + } + + qDebug() << data.count(); + + int size_int = size.toInt(&ok, 10); + + if (ok == false) + { + qDebug() << "Invalid map size."; + return; + } + + 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 + } + } + + qDebug() << "Created default map. Bytes: " + QString::number(data.count()); + + onProcessMapFile(data); } void MainWindow::onLoadMap(void) @@ -32,8 +102,110 @@ void MainWindow::onLoadMap(void) return; } - QDataStream txt(&f); + QByteArray data = f.readAll(); + onProcessMapFile(data); +} + +void MainWindow::onProcessMapFile(QByteArray data) +{ + QDataStream ds(&data, QIODevice::ReadWrite); + + char header[3]; + + ds.readRawData(header, 3); + + if (strncmp(header, "ATC", 3) != 0) + { + qDebug() << "Incorrect header"; + return; + } + + char ch; + + ds.readRawData(&ch, sizeof(char)); + + int level_size = ch; + qDebug() << level_size; + qDebug() << data.count(); + + QPixmap tile1("..\\..\\Sprites\\TILESET1.bmp"); + + int expected_filesize = (0x3F + (level_size * level_size)); + + if (data.count() < expected_filesize) + { + qDebug() << "Invalid count. Expected " + QString::number(expected_filesize, 10); + return; + } + + ds.skipRawData(0x3B); + + gscene->clear(); + gscene->clearFocus(); + + for (int j = 0; j < level_size; j++) + { + for (int i = 0; i < level_size; i++) + { + int u; + int v; + char byte[2]; + ds.readRawData(byte, 2); + quint8 CurrentTile = byte[1]; + + if (CurrentTile & 0x80) + { + u = (int)((CurrentTile & 0x7F) % 4) * 64; + v = (int)((CurrentTile & 0x7F) / 4) * 48; + } + else + { + u = (int)(CurrentTile % 4) * 64; + v = (int)(CurrentTile / 4) * 48; + } + + QImage cropped = tile1.copy(u, v, 64, 48).toImage(); + + if (CurrentTile & 0x80) + { + cropped = cropped.mirrored(true, false); + } + + qDebug() << CurrentTile; + + cropped = cropped.convertToFormat(QImage::Format_ARGB32); // or maybe other format + + for (int i = 0; i < cropped.width(); i++) + { + for (int j = 0; j < cropped.height(); j++) + { + QColor rgb = cropped.pixel(i, j); + + if (rgb == QColor(Qt::magenta)) + { + cropped.setPixel(i, j, qRgba(0,0,0,0)); + } + } + } + + QGraphicsPixmapItem* it = gscene->addPixmap(QPixmap::fromImage(cropped)); + int x; + int y; + + x = (i * 64) - (i * 32); + x -= (j * 32); + + y = (j * 16); + y += i * 16; + + it->setX(x); + it->setY(y); + } + } + + ui->graphicsView->setScene(gscene); + ui->graphicsView->show(); } bool MainWindow::checkFile(QFile& f) @@ -54,21 +226,6 @@ bool MainWindow::checkFile(QFile& f) _last_dir = d.absolutePath(); - QPixmap pix; - - if(pix.load("C:\\cygwin\\home\\Xavier\\Airport\\Sprites\\Tileset1.bmp") == false) - { - qDebug() << "Error loading bitmap."; - } - - QPainter p; - - p.begin(&pix); - - p.setWindow(ui->widget->geometry()); - - p.drawPixmap(ui->widget->x(), ui->widget->y(), pix); - return true; } diff --git a/Source/MapEditor/mainwindow.h b/Source/MapEditor/mainwindow.h index b05c6f5..d9c04d8 100644 --- a/Source/MapEditor/mainwindow.h +++ b/Source/MapEditor/mainwindow.h @@ -6,6 +6,7 @@ #include #include +#include "mygraphicsscene.h" #include "ui_mainwindow.h" namespace Ui { @@ -26,10 +27,12 @@ private: bool checkFile(QFile &f); void appSettings(void); QString _last_dir; + MyGraphicsScene *gscene; protected slots: void onLoadMap(void); - + void onCreateMap(void); + void onProcessMapFile(QByteArray data); }; #endif // MAINWINDOW_H diff --git a/Source/MapEditor/mainwindow.ui b/Source/MapEditor/mainwindow.ui index a23ee45..f715053 100644 --- a/Source/MapEditor/mainwindow.ui +++ b/Source/MapEditor/mainwindow.ui @@ -24,41 +24,42 @@ true - + - - - - 0 - 0 - + + + Create map + + + + Load map - - - - 0 - 0 - - + - Save Map + PushButton - + + + + 0 + 0 + + - 320 + 16777215 16777215 @@ -69,8 +70,8 @@ - - + + diff --git a/Source/MapEditor/mygraphicsscene.cpp b/Source/MapEditor/mygraphicsscene.cpp new file mode 100644 index 0000000..391931e --- /dev/null +++ b/Source/MapEditor/mygraphicsscene.cpp @@ -0,0 +1,20 @@ +#include "mygraphicsscene.h" +#include + +MyGraphicsScene::MyGraphicsScene() +{ + +} + +void MyGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ + QGraphicsItem *it = this->itemAt(mouseEvent->scenePos(), QTransform()); + if (it != NULL) + { + + } + else + { + // No items selected + } +} diff --git a/Source/MapEditor/mygraphicsscene.h b/Source/MapEditor/mygraphicsscene.h new file mode 100644 index 0000000..300cc9c --- /dev/null +++ b/Source/MapEditor/mygraphicsscene.h @@ -0,0 +1,17 @@ +#ifndef MYGRAPHICSSCENE_H +#define MYGRAPHICSSCENE_H + +#include +#include + +class MyGraphicsScene : public QGraphicsScene +{ +public: + MyGraphicsScene(); + +private: + void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent); + +}; + +#endif // MYGRAPHICSSCENE_H diff --git a/Source/MapEditor/release/moc_mainwindow.cpp b/Source/MapEditor/release/moc_mainwindow.cpp deleted file mode 100644 index 59ffe7e..0000000 --- a/Source/MapEditor/release/moc_mainwindow.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************** -** Meta object code from reading C++ file 'mainwindow.h' -** -** Created by: The Qt Meta Object Compiler version 67 (Qt 5.7.0) -** -** WARNING! All changes made in this file will be lost! -*****************************************************************************/ - -#include "../mainwindow.h" -#include -#include -#if !defined(Q_MOC_OUTPUT_REVISION) -#error "The header file 'mainwindow.h' doesn't include ." -#elif Q_MOC_OUTPUT_REVISION != 67 -#error "This file was generated using the moc from 5.7.0. It" -#error "cannot be used with the include files from this version of Qt." -#error "(The moc has changed too much.)" -#endif - -QT_BEGIN_MOC_NAMESPACE -struct qt_meta_stringdata_MainWindow_t { - QByteArrayData data[3]; - char stringdata0[22]; -}; -#define QT_MOC_LITERAL(idx, ofs, len) \ - Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \ - qptrdiff(offsetof(qt_meta_stringdata_MainWindow_t, stringdata0) + ofs \ - - idx * sizeof(QByteArrayData)) \ - ) -static const qt_meta_stringdata_MainWindow_t qt_meta_stringdata_MainWindow = { - { -QT_MOC_LITERAL(0, 0, 10), // "MainWindow" -QT_MOC_LITERAL(1, 11, 9), // "onLoadMap" -QT_MOC_LITERAL(2, 21, 0) // "" - - }, - "MainWindow\0onLoadMap\0" -}; -#undef QT_MOC_LITERAL - -static const uint qt_meta_data_MainWindow[] = { - - // content: - 7, // revision - 0, // classname - 0, 0, // classinfo - 1, 14, // methods - 0, 0, // properties - 0, 0, // enums/sets - 0, 0, // constructors - 0, // flags - 0, // signalCount - - // slots: name, argc, parameters, tag, flags - 1, 0, 19, 2, 0x09 /* Protected */, - - // slots: parameters - QMetaType::Void, - - 0 // eod -}; - -void MainWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a) -{ - if (_c == QMetaObject::InvokeMetaMethod) { - MainWindow *_t = static_cast(_o); - Q_UNUSED(_t) - switch (_id) { - case 0: _t->onLoadMap(); break; - default: ; - } - } - Q_UNUSED(_a); -} - -const QMetaObject MainWindow::staticMetaObject = { - { &QMainWindow::staticMetaObject, qt_meta_stringdata_MainWindow.data, - qt_meta_data_MainWindow, qt_static_metacall, Q_NULLPTR, Q_NULLPTR} -}; - - -const QMetaObject *MainWindow::metaObject() const -{ - return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject; -} - -void *MainWindow::qt_metacast(const char *_clname) -{ - if (!_clname) return Q_NULLPTR; - if (!strcmp(_clname, qt_meta_stringdata_MainWindow.stringdata0)) - return static_cast(const_cast< MainWindow*>(this)); - return QMainWindow::qt_metacast(_clname); -} - -int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a) -{ - _id = QMainWindow::qt_metacall(_c, _id, _a); - if (_id < 0) - return _id; - if (_c == QMetaObject::InvokeMetaMethod) { - if (_id < 1) - qt_static_metacall(this, _c, _id, _a); - _id -= 1; - } else if (_c == QMetaObject::RegisterMethodArgumentMetaType) { - if (_id < 1) - *reinterpret_cast(_a[0]) = -1; - _id -= 1; - } - return _id; -} -QT_END_MOC_NAMESPACE diff --git a/Source/MapEditor/ui_mainwindow.h b/Source/MapEditor/ui_mainwindow.h deleted file mode 100644 index 47ec5ac..0000000 --- a/Source/MapEditor/ui_mainwindow.h +++ /dev/null @@ -1,128 +0,0 @@ -/******************************************************************************** -** Form generated from reading UI file 'mainwindow.ui' -** -** Created by: Qt User Interface Compiler version 5.7.0 -** -** WARNING! All changes made in this file will be lost when recompiling UI file! -********************************************************************************/ - -#ifndef UI_MAINWINDOW_H -#define UI_MAINWINDOW_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class Ui_MainWindow -{ -public: - QWidget *centralWidget; - QGridLayout *gridLayout; - QHBoxLayout *horizontalLayout; - QPushButton *LoadMap_Btn; - QPushButton *SaveMap_Btn; - QTreeWidget *treeWidget; - QWidget *widget; - QMenuBar *menuBar; - QToolBar *mainToolBar; - QStatusBar *statusBar; - - void setupUi(QMainWindow *MainWindow) - { - if (MainWindow->objectName().isEmpty()) - MainWindow->setObjectName(QStringLiteral("MainWindow")); - MainWindow->resize(501, 380); - centralWidget = new QWidget(MainWindow); - centralWidget->setObjectName(QStringLiteral("centralWidget")); - QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - sizePolicy.setHorizontalStretch(0); - sizePolicy.setVerticalStretch(0); - sizePolicy.setHeightForWidth(centralWidget->sizePolicy().hasHeightForWidth()); - centralWidget->setSizePolicy(sizePolicy); - centralWidget->setAutoFillBackground(true); - gridLayout = new QGridLayout(centralWidget); - gridLayout->setSpacing(6); - gridLayout->setContentsMargins(11, 11, 11, 11); - gridLayout->setObjectName(QStringLiteral("gridLayout")); - horizontalLayout = new QHBoxLayout(); - horizontalLayout->setSpacing(6); - horizontalLayout->setObjectName(QStringLiteral("horizontalLayout")); - LoadMap_Btn = new QPushButton(centralWidget); - LoadMap_Btn->setObjectName(QStringLiteral("LoadMap_Btn")); - QSizePolicy sizePolicy1(QSizePolicy::Fixed, QSizePolicy::Fixed); - sizePolicy1.setHorizontalStretch(0); - sizePolicy1.setVerticalStretch(0); - sizePolicy1.setHeightForWidth(LoadMap_Btn->sizePolicy().hasHeightForWidth()); - LoadMap_Btn->setSizePolicy(sizePolicy1); - - horizontalLayout->addWidget(LoadMap_Btn); - - SaveMap_Btn = new QPushButton(centralWidget); - SaveMap_Btn->setObjectName(QStringLiteral("SaveMap_Btn")); - sizePolicy1.setHeightForWidth(SaveMap_Btn->sizePolicy().hasHeightForWidth()); - SaveMap_Btn->setSizePolicy(sizePolicy1); - - horizontalLayout->addWidget(SaveMap_Btn); - - - gridLayout->addLayout(horizontalLayout, 0, 0, 1, 1); - - treeWidget = new QTreeWidget(centralWidget); - treeWidget->setObjectName(QStringLiteral("treeWidget")); - treeWidget->setMaximumSize(QSize(320, 16777215)); - - gridLayout->addWidget(treeWidget, 2, 0, 1, 1); - - widget = new QWidget(centralWidget); - widget->setObjectName(QStringLiteral("widget")); - - gridLayout->addWidget(widget, 2, 1, 1, 1); - - MainWindow->setCentralWidget(centralWidget); - menuBar = new QMenuBar(MainWindow); - menuBar->setObjectName(QStringLiteral("menuBar")); - menuBar->setGeometry(QRect(0, 0, 501, 21)); - MainWindow->setMenuBar(menuBar); - mainToolBar = new QToolBar(MainWindow); - mainToolBar->setObjectName(QStringLiteral("mainToolBar")); - MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar); - statusBar = new QStatusBar(MainWindow); - statusBar->setObjectName(QStringLiteral("statusBar")); - MainWindow->setStatusBar(statusBar); - - retranslateUi(MainWindow); - - QMetaObject::connectSlotsByName(MainWindow); - } // setupUi - - void retranslateUi(QMainWindow *MainWindow) - { - MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0)); - LoadMap_Btn->setText(QApplication::translate("MainWindow", "Load map", 0)); - SaveMap_Btn->setText(QApplication::translate("MainWindow", "Save Map", 0)); - QTreeWidgetItem *___qtreewidgetitem = treeWidget->headerItem(); - ___qtreewidgetitem->setText(0, QApplication::translate("MainWindow", "Items", 0)); - } // retranslateUi - -}; - -namespace Ui { - class MainWindow: public Ui_MainWindow {}; -} // namespace Ui - -QT_END_NAMESPACE - -#endif // UI_MAINWINDOW_H -- cgit v1.2.3