porymap/tileset.cpp

117 lines
4 KiB
C++
Raw Normal View History

2016-09-07 04:50:47 +01:00
#include "tileset.h"
2018-09-19 18:35:45 +01:00
#include "project.h"
2016-09-07 04:50:47 +01:00
2018-07-07 23:32:54 +01:00
#include <QPainter>
#include <QImage>
2018-07-08 17:17:43 +01:00
#include <QDebug>
2018-07-07 23:32:54 +01:00
2016-09-07 04:50:47 +01:00
Tileset::Tileset()
{
}
2018-07-07 23:32:54 +01:00
Metatile::Metatile()
{
tiles = new QList<Tile>;
}
QImage Metatile::getMetatileImage(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
QImage metatile_image(16, 16, QImage::Format_RGBA8888);
Metatile* metatile = Metatile::getMetatile(tile, primaryTileset, secondaryTileset);
if (!metatile || !metatile->tiles) {
metatile_image.fill(0xffffffff);
return metatile_image;
}
Tileset* blockTileset = Metatile::getBlockTileset(tile, primaryTileset, secondaryTileset);
if (!blockTileset) {
metatile_image.fill(0xffffffff);
return metatile_image;
}
QList<QList<QRgb>> palettes = Metatile::getBlockPalettes(primaryTileset, secondaryTileset);
QPainter metatile_painter(&metatile_image);
for (int layer = 0; layer < 2; layer++)
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++) {
Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4));
QImage tile_image = Metatile::getMetatileTile(tile_.tile, primaryTileset, secondaryTileset);
if (tile_image.isNull()) {
// Some metatiles specify tiles that are outside the valid range.
// These are treated as completely transparent, so they can be skipped without
// being drawn.
continue;
}
// Colorize the metatile tiles with its palette.
2018-07-08 17:17:43 +01:00
if (tile_.palette < palettes.length()) {
QList<QRgb> palette = palettes.value(tile_.palette);
for (int j = 0; j < palette.length(); j++) {
tile_image.setColor(j, palette.value(j));
}
} else {
qDebug() << "Tile is referring to invalid palette number: " << tile_.palette;
2018-07-07 23:32:54 +01:00
}
2018-09-16 17:57:16 +01:00
// The top layer of the metatile has its first color displayed at transparent.
2018-07-07 23:32:54 +01:00
if (layer > 0) {
QColor color(tile_image.color(0));
2018-07-07 23:32:54 +01:00
color.setAlpha(0);
tile_image.setColor(0, color.rgba());
2018-07-07 23:32:54 +01:00
}
QPoint origin = QPoint(x*8, y*8);
metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1));
}
metatile_painter.end();
return metatile_image;
}
Metatile* Metatile::getMetatile(int index, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Metatile::getBlockTileset(index, primaryTileset, secondaryTileset);
int local_index = Metatile::getBlockIndex(index);
if (!tileset || !tileset->metatiles) {
2018-09-15 00:37:36 +01:00
return nullptr;
2018-07-07 23:32:54 +01:00
}
2018-09-15 00:37:36 +01:00
Metatile *metatile = tileset->metatiles->value(local_index, nullptr);
2018-07-07 23:32:54 +01:00
return metatile;
}
QImage Metatile::getMetatileTile(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Metatile::getBlockTileset(tile, primaryTileset, secondaryTileset);
int local_index = Metatile::getBlockIndex(tile);
if (!tileset || !tileset->tiles) {
return QImage();
}
return tileset->tiles->value(local_index, QImage());
}
Tileset* Metatile::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) {
2018-09-19 18:35:45 +01:00
if (metatile_index < Project::getNumMetatilesPrimary()) {
2018-07-07 23:32:54 +01:00
return primaryTileset;
} else {
return secondaryTileset;
}
}
int Metatile::getBlockIndex(int index) {
2018-09-19 18:35:45 +01:00
if (index < Project::getNumMetatilesPrimary()) {
2018-07-07 23:32:54 +01:00
return index;
} else {
2018-09-19 18:35:45 +01:00
return index - Project::getNumMetatilesPrimary();
2018-07-07 23:32:54 +01:00
}
}
QList<QList<QRgb>> Metatile::getBlockPalettes(Tileset *primaryTileset, Tileset *secondaryTileset) {
QList<QList<QRgb>> palettes;
2018-09-19 18:35:45 +01:00
for (int i = 0; i < Project::getNumPalettesPrimary(); i++) {
2018-07-07 23:32:54 +01:00
palettes.append(primaryTileset->palettes->at(i));
}
2018-09-19 18:35:45 +01:00
for (int i = Project::getNumPalettesPrimary(); i < Project::getNumPalettesTotal(); i++) {
2018-07-07 23:32:54 +01:00
palettes.append(secondaryTileset->palettes->at(i));
}
return palettes;
}