Plug leaks in Metatile

This commit is contained in:
BigBahss 2021-02-16 12:14:27 -05:00
parent 8e2388cf62
commit 8187b2d4bb
9 changed files with 46 additions and 36 deletions

View file

@ -11,8 +11,9 @@ class Metatile
{
public:
Metatile();
Metatile(const Metatile &other);
public:
QList<Tile> *tiles = nullptr;
QList<Tile> tiles;
uint16_t behavior; // 8 bits RSE, 9 bits FRLG
uint8_t layerType;
uint8_t encounterType; // FRLG only

View file

@ -2,10 +2,21 @@
#include "tileset.h"
#include "project.h"
Metatile::Metatile()
{
tiles = new QList<Tile>;
}
Metatile::Metatile() :
behavior(0),
layerType(0),
encounterType(0),
terrainType(0)
{ }
Metatile::Metatile(const Metatile &other) :
tiles(other.tiles),
behavior(other.behavior),
layerType(other.layerType),
encounterType(other.encounterType),
terrainType(other.terrainType),
label(other.label)
{ }
Metatile* Metatile::copy() {
Metatile *copy = new Metatile;
@ -13,10 +24,9 @@ Metatile* Metatile::copy() {
copy->layerType = this->layerType;
copy->encounterType = this->encounterType;
copy->terrainType = this->terrainType;
copy->tiles = new QList<Tile>;
copy->label = this->label;
for (Tile tile : *this->tiles) {
copy->tiles->append(tile);
for (const Tile &tile : this->tiles) {
copy->tiles.append(tile);
}
return copy;
}
@ -27,8 +37,8 @@ void Metatile::copyInPlace(Metatile *other) {
this->encounterType = other->encounterType;
this->terrainType = other->terrainType;
this->label = other->label;
for (int i = 0; i < this->tiles->length(); i++) {
(*this->tiles)[i] = other->tiles->at(i);
for (int i = 0; i < this->tiles.length(); i++) {
this->tiles[i] = other->tiles.at(i);
}
}

View file

@ -75,14 +75,14 @@ QList<Metatile*> *MetatileParser::parse(QString filepath, bool *error, bool prim
QList<Metatile*> *metatiles = new QList<Metatile*>();
for (int i = 0; i < numMetatiles; i++) {
Metatile *metatile = new Metatile();
QList<Tile> *tiles = new QList<Tile>();
QList<Tile> tiles;
for (int j = 0; j < 8; j++) {
int metatileOffset = 4 + i * metatileSize + j * 2;
uint16_t word = static_cast<uint16_t>(
static_cast<unsigned char>(in.at(metatileOffset)) |
(static_cast<unsigned char>(in.at(metatileOffset + 1)) << 8));
Tile tile(word & 0x3ff, (word >> 10) & 1, (word >> 11) & 1, (word >> 12) & 0xf);
tiles->append(tile);
tiles.append(tile);
}
int attrOffset = 4 + (numMetatiles * metatileSize) + (i * attrSize);

View file

@ -71,8 +71,7 @@ Metatile* Tileset::getMetatile(int index, Tileset *primaryTileset, Tileset *seco
if (!tileset || !tileset->metatiles) {
return nullptr;
}
Metatile *metatile = tileset->metatiles->value(local_index, nullptr);
return metatile;
return tileset->metatiles->value(local_index, nullptr);
}
bool Tileset::metatileIsValid(uint16_t metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) {

View file

@ -1219,16 +1219,16 @@ void MainWindow::on_actionNew_Tileset_triggered() {
int tilesPerMetatile = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8;
Metatile *mt = new Metatile();
for(int j = 0; j < tilesPerMetatile; ++j){
Tile *tile = new Tile();
Tile tile;
//Create a checkerboard-style dummy tileset
if(((i / 8) % 2) == 0)
tile->tile = ((i % 2) == 0) ? 1 : 2;
tile.tile = ((i % 2) == 0) ? 1 : 2;
else
tile->tile = ((i % 2) == 1) ? 1 : 2;
tile->xflip = false;
tile->yflip = false;
tile->palette = 0;
mt->tiles->append(*tile);
tile.tile = ((i % 2) == 1) ? 1 : 2;
tile.xflip = false;
tile.yflip = false;
tile.palette = 0;
mt->tiles.append(tile);
}
mt->behavior = 0;
mt->layerType = 0;
@ -1239,7 +1239,7 @@ void MainWindow::on_actionNew_Tileset_triggered() {
}
newSet->palettes = new QList<QList<QRgb>>();
newSet->palettePreviews = new QList<QList<QRgb>>();
newSet->palettePaths = *new QList<QString>();
newSet->palettePaths.clear();
for(int i = 0; i < 16; ++i) {
QList<QRgb> *currentPal = new QList<QRgb>();
for(int i = 0; i < 16;++i) {

View file

@ -1088,7 +1088,7 @@ void Project::saveTilesetMetatiles(Tileset *tileset) {
for (Metatile *metatile : *tileset->metatiles) {
int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8;
for (int i = 0; i < numTiles; i++) {
Tile tile = metatile->tiles->at(i);
Tile tile = metatile->tiles.at(i);
uint16_t value = static_cast<uint16_t>((tile.tile & 0x3ff)
| ((tile.xflip & 1) << 10)
| ((tile.yflip & 1) << 11)
@ -1626,7 +1626,7 @@ void Project::loadTilesetMetatiles(Tileset* tileset) {
tile.xflip = (word >> 10) & 1;
tile.yflip = (word >> 11) & 1;
tile.palette = (word >> 12) & 0xf;
metatile->tiles->append(tile);
metatile->tiles.append(tile);
}
metatiles->append(metatile);
}

View file

@ -26,7 +26,7 @@ QImage getMetatileImage(
metatile_image.fill(Qt::black);
Metatile* metatile = Tileset::getMetatile(tile, primaryTileset, secondaryTileset);
if (!metatile || !metatile->tiles) {
if (!metatile) {
metatile_image.fill(Qt::magenta);
return metatile_image;
}
@ -46,7 +46,7 @@ QImage getMetatileImage(
for (int x = 0; x < 2; x++) {
int l = layerOrder.size() >= numLayers ? layerOrder[layer] : layer;
int bottomLayer = layerOrder.size() >= numLayers ? layerOrder[0] : 0;
Tile tile_ = metatile->tiles->value((y * 2) + x + (l * 4));
Tile tile_ = metatile->tiles.value((y * 2) + x + (l * 4));
QImage tile_image = getTileImage(tile_.tile, primaryTileset, secondaryTileset);
if (tile_image.isNull()) {
// Some metatiles specify tiles that are outside the valid range.

View file

@ -25,7 +25,7 @@ void MetatileLayersItem::draw() {
QPainter painter(&pixmap);
int numTiles = isTripleLayerMetatile ? 12 : 8;
for (int i = 0; i < numTiles; i++) {
Tile tile = this->metatile->tiles->at(i);
Tile tile = this->metatile->tiles.at(i);
QImage tileImage = getPalettedTileImage(tile.tile, this->primaryTileset, this->secondaryTileset, tile.palette, true)
.mirrored(tile.xflip, tile.yflip)
.scaled(16, 16);

View file

@ -361,7 +361,7 @@ void TilesetEditor::onSelectedTilesChanged() {
}
void TilesetEditor::onMetatileLayerTileChanged(int x, int y) {
const QList<QPoint> tileCoords = QList<QPoint>{
static const QList<QPoint> tileCoords = QList<QPoint>{
QPoint(0, 0),
QPoint(1, 0),
QPoint(0, 1),
@ -387,11 +387,11 @@ void TilesetEditor::onMetatileLayerTileChanged(int x, int y) {
if (tileIndex < maxTileIndex
&& tileCoords.at(tileIndex).x() >= x
&& tileCoords.at(tileIndex).y() >= y){
Tile *tile = &(*this->metatile->tiles)[tileIndex];
tile->tile = tiles.at(selectedTileIndex).tile;
tile->xflip = tiles.at(selectedTileIndex).xflip;
tile->yflip = tiles.at(selectedTileIndex).yflip;
tile->palette = tiles.at(selectedTileIndex).palette;
Tile &tile = this->metatile->tiles[tileIndex];
tile.tile = tiles.at(selectedTileIndex).tile;
tile.xflip = tiles.at(selectedTileIndex).xflip;
tile.yflip = tiles.at(selectedTileIndex).yflip;
tile.palette = tiles.at(selectedTileIndex).palette;
}
selectedTileIndex++;
}
@ -416,7 +416,7 @@ void TilesetEditor::onMetatileLayerSelectionChanged(QPoint selectionOrigin, int
for (int i = 0; i < width; i++) {
int tileIndex = ((x + i) / 2 * 4) + ((y + j) * 2) + ((x + i) % 2);
if (tileIndex < maxTileIndex) {
tiles.append(this->metatile->tiles->at(tileIndex));
tiles.append(this->metatile->tiles.at(tileIndex));
tileIdxs.append(tileIndex);
}
}
@ -740,7 +740,7 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered()
metatile->encounterType = 0;
metatile->terrainType = 0;
for (int i = 0; i < numTiles; i++) {
metatile->tiles->append(tile);
metatile->tiles.append(tile);
}
this->primaryTileset->metatiles->append(metatile);
}
@ -760,7 +760,7 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered()
metatile->encounterType = 0;
metatile->terrainType = 0;
for (int i = 0; i < numTiles; i++) {
metatile->tiles->append(tile);
metatile->tiles.append(tile);
}
this->secondaryTileset->metatiles->append(metatile);
}