all repos — mgba @ 229d138dacd0df43d14a498ad190200bde17df9c

mGBA Game Boy Advance Emulator

Qt: Add map export
Vicki Pfau vi@endrift.com
Sat, 23 Sep 2017 12:33:56 -0700
commit

229d138dacd0df43d14a498ad190200bde17df9c

parent

697c1cfa9d67413138349a28380f65be8c6b464d

3 files changed, 49 insertions(+), 9 deletions(-)

jump to
M src/platform/qt/MapView.cppsrc/platform/qt/MapView.cpp

@@ -7,6 +7,9 @@ #include "MapView.h"

#include "CoreController.h" #include "GBAApp.h" +#include "LogController.h" + +#include <mgba-util/png-io.h> #include <QButtonGroup> #include <QFontDatabase>

@@ -59,6 +62,11 @@ selectMap(i);

}); group->addButton(button); } +#ifdef USE_PNG + connect(m_ui.exportButton, &QAbstractButton::clicked, this, &MapView::exportMap); +#else + m_ui.exportButton->setVisible(false); +#endif } void MapView::selectMap(int map) {

@@ -73,14 +81,13 @@ updateTiles(true);

} void MapView::updateTilesGBA(bool force) { - QImage bg; { CoreController::Interrupter interrupter(m_controller); mMapCache* mapCache = mMapCacheSetGetPointer(&m_cacheSet->maps, m_map); int tilesW = 1 << mMapCacheSystemInfoGetTilesWide(mapCache->sysConfig); int tilesH = 1 << mMapCacheSystemInfoGetTilesHigh(mapCache->sysConfig); - bg = QImage(QSize(tilesW * 8, tilesH * 8), QImage::Format_ARGB32); - uchar* bgBits = bg.bits(); + m_rawMap = QImage(QSize(tilesW * 8, tilesH * 8), QImage::Format_ARGB32); + uchar* bgBits = m_rawMap.bits(); for (int j = 0; j < tilesH; ++j) { for (int i = 0; i < tilesW; ++i) { mMapCacheCleanTile(mapCache, &m_mapStatus[i + j * tilesW], i, j);

@@ -90,8 +97,8 @@ memcpy(static_cast<void*>(&bgBits[tilesW * 32 * (i + j * 8)]), mMapCacheGetRow(mapCache, i + j * 8), tilesW * 32);

} } } - bg = bg.convertToFormat(QImage::Format_RGB32).rgbSwapped(); - QPixmap map = QPixmap::fromImage(bg); + m_rawMap = m_rawMap.rgbSwapped(); + QPixmap map = QPixmap::fromImage(m_rawMap.convertToFormat(QImage::Format_RGB32)); if (m_ui.magnification->value() > 1) { map = map.scaled(map.size() * m_ui.magnification->value()); }

@@ -104,3 +111,23 @@ updateTilesGBA(force);

} #endif +#ifdef USE_PNG +void MapView::exportMap() { + QString filename = GBAApp::app()->getSaveFileName(this, tr("Export map"), + tr("Portable Network Graphics (*.png)")); + VFile* vf = VFileDevice::open(filename, O_WRONLY | O_CREAT | O_TRUNC); + if (!vf) { + LOG(QT, ERROR) << tr("Failed to open output PNG file: %1").arg(filename); + return; + } + + CoreController::Interrupter interrupter(m_controller); + png_structp png = PNGWriteOpen(vf); + png_infop info = PNGWriteHeaderA(png, m_rawMap.width(), m_rawMap.height()); + + mMapCache* mapCache = mMapCacheSetGetPointer(&m_cacheSet->maps, m_map); + QImage map = m_rawMap.rgbSwapped(); + PNGWritePixelsA(png, map.width(), map.height(), map.bytesPerLine() / 4, static_cast<const void*>(map.constBits())); + PNGWriteClose(png, info); +} +#endif
M src/platform/qt/MapView.hsrc/platform/qt/MapView.h

@@ -21,6 +21,11 @@

public: MapView(std::shared_ptr<CoreController> controller, QWidget* parent = nullptr); +#ifdef USE_PNG +public slots: + void exportMap(); +#endif + private slots: void selectMap(int);

@@ -37,6 +42,7 @@

std::shared_ptr<CoreController> m_controller; mMapCacheEntry m_mapStatus[1024 * 1024] = {}; // TODO: Correct size int m_map = 0; + QImage m_rawMap; }; }
M src/platform/qt/MapView.uisrc/platform/qt/MapView.ui

@@ -14,7 +14,10 @@ <property name="windowTitle">

<string>Maps</string> </property> <layout class="QGridLayout" name="gridLayout"> - <item row="0" column="1" rowspan="4"> + <item row="0" column="0"> + <layout class="QVBoxLayout" name="bgLayout"/> + </item> + <item row="0" column="1" rowspan="5"> <widget class="QScrollArea" name="scrollArea"> <property name="widgetResizable"> <bool>true</bool>

@@ -71,7 +74,7 @@ </item>

<item row="2" column="0"> <widget class="QGBA::AssetTile" name="tile"/> </item> - <item row="3" column="0"> + <item row="4" column="0"> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum>

@@ -114,8 +117,12 @@ </widget>

</item> </layout> </item> - <item row="0" column="0"> - <layout class="QVBoxLayout" name="bgLayout"/> + <item row="3" column="0"> + <widget class="QPushButton" name="exportButton"> + <property name="text"> + <string>Export</string> + </property> + </widget> </item> </layout> </widget>