Skip to content

Commit

Permalink
Combine tile image export functions
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Dec 16, 2024
1 parent 81b6cfa commit 8f1e112
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
1 change: 1 addition & 0 deletions include/ui/tileseteditortileselector.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class TilesetEditorTileSelector: public SelectablePixmapItem {
QPoint getTileCoords(uint16_t);
QList<QRgb> getCurPaletteTable();
QList<Tile> buildSelectedTiles(int, int, QList<Tile>);
QImage buildImage(int tileIdStart, int numTiles);

void drawUnused();

Expand Down
46 changes: 12 additions & 34 deletions src/ui/tileseteditortileselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,54 +219,32 @@ QPoint TilesetEditorTileSelector::getTileCoordsOnWidget(uint16_t tile) {
}

QImage TilesetEditorTileSelector::buildPrimaryTilesIndexedImage() {
if (!this->primaryTileset || !this->secondaryTileset) {
if (!this->primaryTileset)
return QImage();
}

int primaryLength = this->primaryTileset->tiles.length();
int height = qCeil(primaryLength / static_cast<double>(this->numTilesWide));
QImage image(this->numTilesWide * 8, height * 8, QImage::Format_RGBA8888);
image.fill(0);

QPainter painter(&image);
for (uint16_t tile = 0; tile < primaryLength; tile++) {
QImage tileImage = getGreyscaleTileImage(tile, this->primaryTileset, this->secondaryTileset);
int y = tile / this->numTilesWide;
int x = tile % this->numTilesWide;
QPoint origin = QPoint(x * 8, y * 8);
painter.drawImage(origin, tileImage);
}

painter.end();

// Image is first converted using greyscale so that palettes with duplicate colors
// are properly represented in the final image.
QImage indexedImage = image.convertToFormat(QImage::Format::Format_Indexed8, greyscalePalette.toVector());
QList<QRgb> palette = Tileset::getPalette(this->paletteId, this->primaryTileset, this->secondaryTileset, true);
indexedImage.setColorTable(palette.toVector());
return indexedImage;
return buildImage(0, this->primaryTileset->tiles.length());
}

QImage TilesetEditorTileSelector::buildSecondaryTilesIndexedImage() {
if (!this->primaryTileset || !this->secondaryTileset) {
if (!this->secondaryTileset)
return QImage();
}

int secondaryLength = this->secondaryTileset->tiles.length();
int height = qCeil(secondaryLength / static_cast<double>(this->numTilesWide));
return buildImage(Project::getNumTilesPrimary(), this->secondaryTileset->tiles.length());
}

QImage TilesetEditorTileSelector::buildImage(int tileIdStart, int numTiles) {
int height = qCeil(numTiles / static_cast<double>(this->numTilesWide));
QImage image(this->numTilesWide * 8, height * 8, QImage::Format_RGBA8888);
image.fill(0);

QPainter painter(&image);
uint16_t primaryLength = static_cast<uint16_t>(Project::getNumTilesPrimary());
for (uint16_t tile = 0; tile < secondaryLength; tile++) {
QImage tileImage = getGreyscaleTileImage(tile + primaryLength, this->primaryTileset, this->secondaryTileset);
int y = tile / this->numTilesWide;
int x = tile % this->numTilesWide;
for (int i = 0; i < numTiles; i++) {
QImage tileImage = getGreyscaleTileImage(tileIdStart + i, this->primaryTileset, this->secondaryTileset);
int y = i / this->numTilesWide;
int x = i % this->numTilesWide;
QPoint origin = QPoint(x * 8, y * 8);
painter.drawImage(origin, tileImage);
}

painter.end();

// Image is first converted using greyscale so that palettes with duplicate colors
Expand Down

0 comments on commit 8f1e112

Please sign in to comment.