From cc7a5f5819bd9c31c58a6de8d1efe75e28cae53d Mon Sep 17 00:00:00 2001 From: BigBahss Date: Tue, 16 Feb 2021 21:45:54 -0500 Subject: [PATCH] Fix memory leaks in Tileset --- include/core/metatileparser.h | 2 +- include/core/tile.h | 16 +++++- include/core/tileset.h | 17 ++++--- include/mainwindow.h | 4 +- src/core/metatileparser.cpp | 18 +++---- src/core/tile.cpp | 9 ---- src/core/tileset.cpp | 65 +++--------------------- src/mainwindow.cpp | 27 ++++------ src/mainwindow_scriptapi.cpp | 23 ++++----- src/project.cpp | 56 ++++++++++---------- src/ui/imageproviders.cpp | 4 +- src/ui/metatileselector.cpp | 15 +++--- src/ui/paletteeditor.cpp | 24 ++++----- src/ui/tileseteditor.cpp | 48 +++++++---------- src/ui/tileseteditormetatileselector.cpp | 17 +++---- src/ui/tileseteditortileselector.cpp | 17 +++---- 16 files changed, 147 insertions(+), 215 deletions(-) delete mode 100644 src/core/tile.cpp diff --git a/include/core/metatileparser.h b/include/core/metatileparser.h index 7bf0b096..2008d60f 100644 --- a/include/core/metatileparser.h +++ b/include/core/metatileparser.h @@ -10,7 +10,7 @@ class MetatileParser { public: MetatileParser(); - QList *parse(QString filepath, bool *error, bool primaryTileset); + QList parse(QString filepath, bool *error, bool primaryTileset); }; #endif // METATILEPARSER_H diff --git a/include/core/tile.h b/include/core/tile.h index e2b1ec4d..dfcb7117 100644 --- a/include/core/tile.h +++ b/include/core/tile.h @@ -6,8 +6,20 @@ class Tile { public: - Tile() {} - Tile(int tile, bool xflip, bool yflip, int palette); + Tile() : + tile(0), + xflip(false), + yflip(false), + palette(0) + { } + + Tile(int tile, bool xflip, bool yflip, int palette) : + tile(tile), + xflip(xflip), + yflip(yflip), + palette(palette) + { } + public: int tile; bool xflip; diff --git a/include/core/tileset.h b/include/core/tileset.h index c6349cfe..2ffeea85 100644 --- a/include/core/tileset.h +++ b/include/core/tileset.h @@ -9,7 +9,10 @@ class Tileset { public: - Tileset(); + Tileset() = default; + Tileset(const Tileset &other) = default; + Tileset &operator=(const Tileset &other) = default; + public: QString name; QString is_compressed; @@ -24,14 +27,12 @@ public: QString metatile_attrs_path; QString tilesImagePath; QImage tilesImage; - QList palettePaths; + QStringList palettePaths; - QList *tiles = nullptr; - QList *metatiles = nullptr; - QList> *palettes = nullptr; - QList> *palettePreviews = nullptr; - - Tileset* copy(); + QList tiles; + QList metatiles; + QList> palettes; + QList> palettePreviews; static Tileset* getBlockTileset(int, Tileset*, Tileset*); static Metatile* getMetatile(int, Tileset*, Tileset*); diff --git a/include/mainwindow.h b/include/mainwindow.h index 4d333e5c..63d430a7 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -77,8 +77,8 @@ public: Q_INVOKABLE void setPrimaryTilesetPalettes(QList>> palettes); Q_INVOKABLE void setSecondaryTilesetPalette(int paletteIndex, QList> colors); Q_INVOKABLE void setSecondaryTilesetPalettes(QList>> palettes); - QJSValue getTilesetPalette(QList> *palettes, int paletteIndex); - QJSValue getTilesetPalettes(QList> *palettes); + QJSValue getTilesetPalette(const QList> &palettes, int paletteIndex); + QJSValue getTilesetPalettes(const QList> &palettes); Q_INVOKABLE QJSValue getPrimaryTilesetPalette(int paletteIndex); Q_INVOKABLE QJSValue getPrimaryTilesetPalettes(); Q_INVOKABLE QJSValue getSecondaryTilesetPalette(int paletteIndex); diff --git a/src/core/metatileparser.cpp b/src/core/metatileparser.cpp index 2fef6dc0..3bbed668 100644 --- a/src/core/metatileparser.cpp +++ b/src/core/metatileparser.cpp @@ -8,13 +8,13 @@ MetatileParser::MetatileParser() } -QList *MetatileParser::parse(QString filepath, bool *error, bool primaryTileset) +QList MetatileParser::parse(QString filepath, bool *error, bool primaryTileset) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; logError(QString("Could not open Advance Map 1.92 Metatile .bvd file '%1': ").arg(filepath) + file.errorString()); - return nullptr; + return { }; } QByteArray in = file.readAll(); @@ -23,7 +23,7 @@ QList *MetatileParser::parse(QString filepath, bool *error, bool prim if (in.length() < 9 || in.length() % 2 != 0) { *error = true; logError(QString("Advance Map 1.92 Metatile .bvd file '%1' is an unexpected size.").arg(filepath)); - return nullptr; + return { }; } int projIdOffset = in.length() - 4; @@ -46,7 +46,7 @@ QList *MetatileParser::parse(QString filepath, bool *error, bool prim } else { *error = true; logError(QString("Detected unsupported game type from .bvd file. Last 4 bytes of file must be 'RSE ' or 'FRLG'.")); - return nullptr; + return { }; } int maxMetatiles = primaryTileset ? Project::getNumMetatilesPrimary() : Project::getNumMetatilesTotal() - Project::getNumMetatilesPrimary(); @@ -57,22 +57,22 @@ QList *MetatileParser::parse(QString filepath, bool *error, bool prim if (numMetatiles > maxMetatiles) { *error = true; logError(QString(".bvd file contains data for %1 metatiles, but the maximum number of metatiles is %2.").arg(numMetatiles).arg(maxMetatiles)); - return nullptr; + return { }; } if (numMetatiles < 1) { *error = true; logError(QString(".bvd file contains no data for metatiles.")); - return nullptr; + return { }; } int expectedFileSize = 4 + (metatileSize * numMetatiles) + (attrSize * numMetatiles) + 4; if (in.length() != expectedFileSize) { *error = true; logError(QString(".bvd file is an unexpected size. Expected %1 bytes, but it has %2 bytes.").arg(expectedFileSize).arg(in.length())); - return nullptr; + return { }; } - QList *metatiles = new QList(); + QList metatiles; for (int i = 0; i < numMetatiles; i++) { Metatile *metatile = new Metatile(); QList tiles; @@ -104,7 +104,7 @@ QList *MetatileParser::parse(QString filepath, bool *error, bool prim metatile->terrainType = 0; } metatile->tiles = tiles; - metatiles->append(metatile); + metatiles.append(metatile); } return metatiles; diff --git a/src/core/tile.cpp b/src/core/tile.cpp deleted file mode 100644 index 1077f98f..00000000 --- a/src/core/tile.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "tile.h" - -Tile::Tile(int tile, bool xflip, bool yflip, int palette) -{ - this->tile = tile; - this->xflip = xflip; - this->yflip = yflip; - this->palette = palette; -} diff --git a/src/core/tileset.cpp b/src/core/tileset.cpp index 03b0fb7c..f710f165 100644 --- a/src/core/tileset.cpp +++ b/src/core/tileset.cpp @@ -7,55 +7,6 @@ #include #include -Tileset::Tileset() -{ - -} - -Tileset* Tileset::copy() { - Tileset *copy = new Tileset; - copy->name = this->name; - copy->is_compressed = this->is_compressed; - copy->is_secondary = this->is_secondary; - copy->padding = this->padding; - copy->tiles_label = this->tiles_label; - copy->palettes_label = this->palettes_label; - copy->metatiles_label = this->metatiles_label; - copy->metatiles_path = this->metatiles_path; - copy->callback_label = this->callback_label; - copy->metatile_attrs_label = this->metatile_attrs_label; - copy->metatile_attrs_path = this->metatile_attrs_path; - copy->tilesImage = this->tilesImage.copy(); - copy->tilesImagePath = this->tilesImagePath; - for (int i = 0; i < this->palettePaths.length(); i++) { - copy->palettePaths.append(this->palettePaths.at(i)); - } - copy->tiles = new QList; - for (QImage tile : *this->tiles) { - copy->tiles->append(tile.copy()); - } - copy->metatiles = new QList; - for (Metatile *metatile : *this->metatiles) { - copy->metatiles->append(new Metatile(*metatile)); - } - copy->palettes = new QList>; - for (QList palette : *this->palettes) { - QList copyPalette; - for (QRgb color : palette) { - copyPalette.append(color); - } - copy->palettes->append(copyPalette); - } - copy->palettePreviews = new QList>; - for (QList palette : *this->palettePreviews) { - QList copyPalette; - for (QRgb color : palette) { - copyPalette.append(color); - } - copy->palettePreviews->append(copyPalette); - } - return copy; -} Tileset* Tileset::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) { if (metatile_index < Project::getNumMetatilesPrimary()) { @@ -68,20 +19,20 @@ Tileset* Tileset::getBlockTileset(int metatile_index, Tileset *primaryTileset, T Metatile* Tileset::getMetatile(int index, Tileset *primaryTileset, Tileset *secondaryTileset) { Tileset *tileset = Tileset::getBlockTileset(index, primaryTileset, secondaryTileset); int local_index = Metatile::getBlockIndex(index); - if (!tileset || !tileset->metatiles) { + if (!tileset) { return nullptr; } - return tileset->metatiles->value(local_index, nullptr); + return tileset->metatiles.value(local_index, nullptr); } bool Tileset::metatileIsValid(uint16_t metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) { if (metatileId >= Project::getNumMetatilesTotal()) return false; - if (metatileId < Project::getNumMetatilesPrimary() && metatileId >= primaryTileset->metatiles->length()) + if (metatileId < Project::getNumMetatilesPrimary() && metatileId >= primaryTileset->metatiles.length()) return false; - if (metatileId >= Project::getNumMetatilesPrimary() + secondaryTileset->metatiles->length()) + if (metatileId >= Project::getNumMetatilesPrimary() + secondaryTileset->metatiles.length()) return false; return true; @@ -91,11 +42,11 @@ QList> Tileset::getBlockPalettes(Tileset *primaryTileset, Tileset *s QList> palettes; auto primaryPalettes = useTruePalettes ? primaryTileset->palettes : primaryTileset->palettePreviews; for (int i = 0; i < Project::getNumPalettesPrimary(); i++) { - palettes.append(primaryPalettes->at(i)); + palettes.append(primaryPalettes.at(i)); } auto secondaryPalettes = useTruePalettes ? secondaryTileset->palettes : secondaryTileset->palettePreviews; for (int i = Project::getNumPalettesPrimary(); i < Project::getNumPalettesTotal(); i++) { - palettes.append(secondaryPalettes->at(i)); + palettes.append(secondaryPalettes.at(i)); } return palettes; } @@ -106,8 +57,8 @@ QList Tileset::getPalette(int paletteId, Tileset *primaryTileset, Tileset ? primaryTileset : secondaryTileset; auto palettes = useTruePalettes ? tileset->palettes : tileset->palettePreviews; - for (int i = 0; i < palettes->at(paletteId).length(); i++) { - paletteTable.append(palettes->at(paletteId).at(i)); + for (int i = 0; i < palettes.at(paletteId).length(); i++) { + paletteTable.append(palettes.at(paletteId).at(i)); } return paletteTable; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 5d1740fc..4d3d5757 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1212,22 +1212,18 @@ void MainWindow::on_actionNew_Tileset_triggered() { newSet->metatile_attrs_path = fullDirectoryPath + "/metatile_attributes.bin"; newSet->is_secondary = createTilesetDialog->isSecondary ? "TRUE" : "FALSE"; int numMetaTiles = createTilesetDialog->isSecondary ? (Project::getNumTilesTotal() - Project::getNumTilesPrimary()) : Project::getNumTilesPrimary(); - QImage *tilesImage = new QImage(":/images/blank_tileset.png"); - editor->project->loadTilesetTiles(newSet, *tilesImage); - newSet->metatiles = new QList(); + QImage tilesImage(":/images/blank_tileset.png"); + editor->project->loadTilesetTiles(newSet, tilesImage); for(int i = 0; i < numMetaTiles; ++i) { int tilesPerMetatile = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; Metatile *mt = new Metatile(); for(int j = 0; j < tilesPerMetatile; ++j){ - Tile tile; + Tile tile(0, false, false, 0); //Create a checkerboard-style dummy tileset if(((i / 8) % 2) == 0) 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); } mt->behavior = 0; @@ -1235,23 +1231,20 @@ void MainWindow::on_actionNew_Tileset_triggered() { mt->encounterType = 0; mt->terrainType = 0; - newSet->metatiles->append(mt); + newSet->metatiles.append(mt); } - newSet->palettes = new QList>(); - newSet->palettePreviews = new QList>(); - newSet->palettePaths.clear(); for(int i = 0; i < 16; ++i) { - QList *currentPal = new QList(); + QList currentPal; for(int i = 0; i < 16;++i) { - currentPal->append(qRgb(0,0,0)); + currentPal.append(qRgb(0,0,0)); } - newSet->palettes->append(*currentPal); - newSet->palettePreviews->append(*currentPal); + newSet->palettes.append(currentPal); + newSet->palettePreviews.append(currentPal); QString fileName = QString("%1.pal").arg(i, 2, 10, QLatin1Char('0')); newSet->palettePaths.append(fullDirectoryPath+"/palettes/" + fileName); } - (*newSet->palettes)[0][1] = qRgb(255,0,255); - (*newSet->palettePreviews)[0][1] = qRgb(255,0,255); + newSet->palettes[0][1] = qRgb(255,0,255); + newSet->palettePreviews[0][1] = qRgb(255,0,255); newSet->is_compressed = "TRUE"; newSet->padding = "0"; editor->project->saveTilesetTilesImage(newSet); diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 494cb779..7a425e07 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -260,7 +260,7 @@ void MainWindow::refreshAfterPaletteChange(Tileset *tileset) { void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QList> colors) { if (!this->editor || !this->editor->map || !this->editor->map->layout) return; - if (paletteIndex >= tileset->palettes->size()) + if (paletteIndex >= tileset->palettes.size()) return; if (colors.size() != 16) return; @@ -268,8 +268,8 @@ void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QListpalettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); - (*tileset->palettePreviews)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); + tileset->palettes[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); + tileset->palettePreviews[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); } } @@ -305,22 +305,22 @@ void MainWindow::setSecondaryTilesetPalettes(QList>> palettes) this->refreshAfterPaletteChange(this->editor->map->layout->tileset_secondary); } -QJSValue MainWindow::getTilesetPalette(QList> *palettes, int paletteIndex) { - if (paletteIndex >= palettes->size()) +QJSValue MainWindow::getTilesetPalette(const QList> &palettes, int paletteIndex) { + if (paletteIndex >= palettes.size()) return QJSValue(); QList> palette; - for (auto color : palettes->value(paletteIndex)) { + for (auto color : palettes.value(paletteIndex)) { palette.append(QList({qRed(color), qGreen(color), qBlue(color)})); } return Scripting::getEngine()->toScriptValue(palette); } -QJSValue MainWindow::getTilesetPalettes(QList> *palettes) { +QJSValue MainWindow::getTilesetPalettes(const QList> &palettes) { QList>> outPalettes; - for (int i = 0; i < palettes->size(); i++) { + for (int i = 0; i < palettes.size(); i++) { QList> colors; - for (auto color : palettes->value(i)) { + for (auto color : palettes.value(i)) { colors.append(QList({qRed(color), qGreen(color), qBlue(color)})); } outPalettes.append(colors); @@ -363,7 +363,7 @@ void MainWindow::refreshAfterPalettePreviewChange() { void MainWindow::setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QList> colors) { if (!this->editor || !this->editor->map || !this->editor->map->layout) return; - if (paletteIndex >= tileset->palettePreviews->size()) + if (paletteIndex >= tileset->palettePreviews.size()) return; if (colors.size() != 16) return; @@ -371,8 +371,7 @@ void MainWindow::setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QL for (int i = 0; i < 16; i++) { if (colors[i].size() != 3) continue; - auto palettes = tileset->palettePreviews; - (*palettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); + tileset->palettePreviews[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); } } diff --git a/src/project.cpp b/src/project.cpp index 710e4edc..021f3d49 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -995,16 +995,16 @@ void Project::saveTilesetMetatileLabels(Tileset *primaryTileset, Tileset *second } // Add the new labels. - for (int i = 0; i < primaryTileset->metatiles->size(); i++) { - Metatile *metatile = primaryTileset->metatiles->at(i); + for (int i = 0; i < primaryTileset->metatiles.size(); i++) { + Metatile *metatile = primaryTileset->metatiles.at(i); if (metatile->label.size() != 0) { QString defineName = QString("%1%2").arg(primaryPrefix, metatile->label); defines.insert(defineName, i); definesFileModified = true; } } - for (int i = 0; i < secondaryTileset->metatiles->size(); i++) { - Metatile *metatile = secondaryTileset->metatiles->at(i); + for (int i = 0; i < secondaryTileset->metatiles.size(); i++) { + Metatile *metatile = secondaryTileset->metatiles.at(i); if (metatile->label.size() != 0) { QString defineName = QString("%1%2").arg(secondaryPrefix, metatile->label); defines.insert(defineName, i + Project::num_tiles_primary); @@ -1061,7 +1061,7 @@ void Project::saveTilesetMetatileAttributes(Tileset *tileset) { QByteArray data; if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokefirered) { - for (Metatile *metatile : *tileset->metatiles) { + for (Metatile *metatile : tileset->metatiles) { data.append(static_cast(metatile->behavior)); data.append(static_cast(metatile->behavior >> 8) | static_cast(metatile->terrainType << 1)); @@ -1070,7 +1070,7 @@ void Project::saveTilesetMetatileAttributes(Tileset *tileset) { static_cast(metatile->layerType << 5)); } } else { - for (Metatile *metatile : *tileset->metatiles) { + for (Metatile *metatile : tileset->metatiles) { data.append(static_cast(metatile->behavior)); data.append(static_cast((metatile->layerType << 4) & 0xF0)); } @@ -1085,7 +1085,7 @@ void Project::saveTilesetMetatiles(Tileset *tileset) { QFile metatiles_file(tileset->metatiles_path); if (metatiles_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { QByteArray data; - for (Metatile *metatile : *tileset->metatiles) { + for (Metatile *metatile : tileset->metatiles) { int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; for (int i = 0; i < numTiles; i++) { Tile tile = metatile->tiles.at(i); @@ -1099,7 +1099,7 @@ void Project::saveTilesetMetatiles(Tileset *tileset) { } metatiles_file.write(data); } else { - tileset->metatiles = new QList; + tileset->metatiles.clear(); logError(QString("Could not open tileset metatiles file '%1'").arg(tileset->metatiles_path)); } } @@ -1112,7 +1112,7 @@ void Project::saveTilesetPalettes(Tileset *tileset) { PaletteUtil paletteParser; for (int i = 0; i < Project::getNumPalettesTotal(); i++) { QString filepath = tileset->palettePaths.at(i); - paletteParser.writeJASC(filepath, tileset->palettes->at(i).toVector(), 0, 16); + paletteParser.writeJASC(filepath, tileset->palettes.at(i).toVector(), 0, 16); } } @@ -1552,8 +1552,8 @@ void Project::loadTilesetAssets(Tileset* tileset) { this->loadTilesetMetatileLabels(tileset); // palettes - QList> *palettes = new QList>; - QList> *palettePreviews = new QList>; + QList> palettes; + QList> palettePreviews; for (int i = 0; i < tileset->palettePaths.length(); i++) { QList palette; QString path = tileset->palettePaths.value(i); @@ -1587,21 +1587,21 @@ void Project::loadTilesetAssets(Tileset* tileset) { logError(QString("Could not open tileset palette path '%1'").arg(path)); } - palettes->append(palette); - palettePreviews->append(palette); + palettes.append(palette); + palettePreviews.append(palette); } tileset->palettes = palettes; tileset->palettePreviews = palettePreviews; } void Project::loadTilesetTiles(Tileset *tileset, QImage image) { - QList *tiles = new QList; + QList tiles; int w = 8; int h = 8; for (int y = 0; y < image.height(); y += h) for (int x = 0; x < image.width(); x += w) { QImage tile = image.copy(x, y, w, h); - tiles->append(tile); + tiles.append(tile); } tileset->tilesImage = image; tileset->tiles = tiles; @@ -1614,7 +1614,7 @@ void Project::loadTilesetMetatiles(Tileset* tileset) { int metatile_data_length = projectConfig.getTripleLayerMetatilesEnabled() ? 24 : 16; int num_metatiles = data.length() / metatile_data_length; int num_layers = projectConfig.getTripleLayerMetatilesEnabled() ? 3 : 2; - QList *metatiles = new QList; + QList metatiles; for (int i = 0; i < num_metatiles; i++) { Metatile *metatile = new Metatile; int index = i * (2 * 4 * num_layers); @@ -1628,18 +1628,18 @@ void Project::loadTilesetMetatiles(Tileset* tileset) { tile.palette = (word >> 12) & 0xf; metatile->tiles.append(tile); } - metatiles->append(metatile); + metatiles.append(metatile); } tileset->metatiles = metatiles; } else { - tileset->metatiles = new QList; + tileset->metatiles.clear(); logError(QString("Could not open tileset metatiles file '%1'").arg(tileset->metatiles_path)); } QFile attrs_file(tileset->metatile_attrs_path); if (attrs_file.open(QIODevice::ReadOnly)) { QByteArray data = attrs_file.readAll(); - int num_metatiles = tileset->metatiles->count(); + int num_metatiles = tileset->metatiles.count(); if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokefirered) { int num_metatileAttrs = data.length() / 4; @@ -1654,10 +1654,10 @@ void Project::loadTilesetMetatiles(Tileset* tileset) { (static_cast(data.at(i * 4 + 2)) << 16) | (static_cast(data.at(i * 4 + 1)) << 8) | (static_cast(data.at(i * 4 + 0))); - tileset->metatiles->at(i)->behavior = value & 0x1FF; - tileset->metatiles->at(i)->terrainType = (value & 0x3E00) >> 9; - tileset->metatiles->at(i)->encounterType = (value & 0x7000000) >> 24; - tileset->metatiles->at(i)->layerType = (value & 0x60000000) >> 29; + tileset->metatiles.at(i)->behavior = value & 0x1FF; + tileset->metatiles.at(i)->terrainType = (value & 0x3E00) >> 9; + tileset->metatiles.at(i)->encounterType = (value & 0x7000000) >> 24; + tileset->metatiles.at(i)->layerType = (value & 0x60000000) >> 29; if (value & ~(0x67003FFF)) unusedAttribute = true; } @@ -1672,10 +1672,10 @@ void Project::loadTilesetMetatiles(Tileset* tileset) { } for (int i = 0; i < num_metatileAttrs; i++) { int value = (static_cast(data.at(i * 2 + 1)) << 8) | static_cast(data.at(i * 2)); - tileset->metatiles->at(i)->behavior = value & 0xFF; - tileset->metatiles->at(i)->layerType = (value & 0xF000) >> 12; - tileset->metatiles->at(i)->encounterType = 0; - tileset->metatiles->at(i)->terrainType = 0; + tileset->metatiles.at(i)->behavior = value & 0xFF; + tileset->metatiles.at(i)->layerType = (value & 0xF000) >> 12; + tileset->metatiles.at(i)->encounterType = 0; + tileset->metatiles.at(i)->terrainType = 0; } } } else { @@ -1734,7 +1734,7 @@ Tileset* Project::getTileset(QString label, bool forceLoad) { } if (existingTileset && !forceLoad) { - return tilesetCache->value(label); + return existingTileset; } else { Tileset *tileset = loadTileset(label, existingTileset); return tileset; diff --git a/src/ui/imageproviders.cpp b/src/ui/imageproviders.cpp index 4c37206e..44a7e4f2 100644 --- a/src/ui/imageproviders.cpp +++ b/src/ui/imageproviders.cpp @@ -97,10 +97,10 @@ QImage getMetatileImage( QImage getTileImage(uint16_t tile, Tileset *primaryTileset, Tileset *secondaryTileset) { Tileset *tileset = Tileset::getBlockTileset(tile, primaryTileset, secondaryTileset); int local_index = Metatile::getBlockIndex(tile); - if (!tileset || !tileset->tiles) { + if (!tileset) { return QImage(); } - return tileset->tiles->value(local_index, QImage()); + return tileset->tiles.value(local_index, QImage()); } QImage getColoredTileImage(uint16_t tile, Tileset *primaryTileset, Tileset *secondaryTileset, QList palette) { diff --git a/src/ui/metatileselector.cpp b/src/ui/metatileselector.cpp index c360da5f..80f05c59 100644 --- a/src/ui/metatileselector.cpp +++ b/src/ui/metatileselector.cpp @@ -12,13 +12,12 @@ QPoint MetatileSelector::getSelectionDimensions() { } void MetatileSelector::draw() { - if (!this->primaryTileset || !this->primaryTileset->metatiles - || !this->secondaryTileset || !this->secondaryTileset->metatiles) { + if (!this->primaryTileset || !this->secondaryTileset) { this->setPixmap(QPixmap()); } - int primaryLength = this->primaryTileset->metatiles->length(); - int length_ = primaryLength + this->secondaryTileset->metatiles->length(); + int primaryLength = this->primaryTileset->metatiles.length(); + int length_ = primaryLength + this->secondaryTileset->metatiles.length(); int height_ = length_ / this->numMetatilesWide; if (length_ % this->numMetatilesWide != 0) { height_++; @@ -69,7 +68,7 @@ void MetatileSelector::setTilesets(Tileset *primaryTileset, Tileset *secondaryTi if (this->externalSelection) { this->select(0); } else { - this->select(Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles->length() - 1); + this->select(Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles.length() - 1); } } else if (this->externalSelection) { emit selectedMetatilesChanged(); @@ -181,10 +180,10 @@ bool MetatileSelector::selectionIsValid() { uint16_t MetatileSelector::getMetatileId(int x, int y) { int index = y * this->numMetatilesWide + x; - if (index < this->primaryTileset->metatiles->length()) { + if (index < this->primaryTileset->metatiles.length()) { return static_cast(index); } else { - return static_cast(Project::getNumMetatilesPrimary() + index - this->primaryTileset->metatiles->length()); + return static_cast(Project::getNumMetatilesPrimary() + index - this->primaryTileset->metatiles.length()); } } @@ -197,7 +196,7 @@ QPoint MetatileSelector::getMetatileIdCoords(uint16_t metatileId) { int index = metatileId < Project::getNumMetatilesPrimary() ? metatileId - : metatileId - Project::getNumMetatilesPrimary() + this->primaryTileset->metatiles->length(); + : metatileId - Project::getNumMetatilesPrimary() + this->primaryTileset->metatiles.length(); return QPoint(index % this->numMetatilesWide, index / this->numMetatilesWide); } diff --git a/src/ui/paletteeditor.cpp b/src/ui/paletteeditor.cpp index 0e1366e6..f376dc3b 100644 --- a/src/ui/paletteeditor.cpp +++ b/src/ui/paletteeditor.cpp @@ -151,9 +151,9 @@ void PaletteEditor::refreshColorSliders() { for (int i = 0; i < 16; i++) { QRgb color; if (paletteNum < Project::getNumPalettesPrimary()) { - color = this->primaryTileset->palettes->at(paletteNum).at(i); + color = this->primaryTileset->palettes.at(paletteNum).at(i); } else { - color = this->secondaryTileset->palettes->at(paletteNum).at(i); + color = this->secondaryTileset->palettes.at(paletteNum).at(i); } this->sliders[i][0]->setValue(qRed(color) / 8); @@ -204,8 +204,8 @@ void PaletteEditor::setColor(int colorIndex) { Tileset *tileset = paletteNum < Project::getNumPalettesPrimary() ? this->primaryTileset : this->secondaryTileset; - (*tileset->palettes)[paletteNum][colorIndex] = qRgb(red, green, blue); - (*tileset->palettePreviews)[paletteNum][colorIndex] = qRgb(red, green, blue); + tileset->palettes[paletteNum][colorIndex] = qRgb(red, green, blue); + tileset->palettePreviews[paletteNum][colorIndex] = qRgb(red, green, blue); this->refreshColor(colorIndex); this->commitEditHistory(paletteNum); emit this->changedPaletteColor(); @@ -255,11 +255,11 @@ void PaletteEditor::setColorsFromHistory(PaletteHistoryItem *history, int palett for (int i = 0; i < 16; i++) { if (paletteId < Project::getNumPalettesPrimary()) { - (*this->primaryTileset->palettes)[paletteId][i] = history->colors.at(i); - (*this->primaryTileset->palettePreviews)[paletteId][i] = history->colors.at(i); + this->primaryTileset->palettes[paletteId][i] = history->colors.at(i); + this->primaryTileset->palettePreviews[paletteId][i] = history->colors.at(i); } else { - (*this->secondaryTileset->palettes)[paletteId][i] = history->colors.at(i); - (*this->secondaryTileset->palettePreviews)[paletteId][i] = history->colors.at(i); + this->secondaryTileset->palettes[paletteId][i] = history->colors.at(i); + this->secondaryTileset->palettePreviews[paletteId][i] = history->colors.at(i); } } @@ -307,11 +307,11 @@ void PaletteEditor::on_actionImport_Palette_triggered() int paletteId = this->ui->spinBox_PaletteId->value(); for (int i = 0; i < 16; i++) { if (paletteId < Project::getNumPalettesPrimary()) { - (*this->primaryTileset->palettes)[paletteId][i] = palette.at(i); - (*this->primaryTileset->palettePreviews)[paletteId][i] = palette.at(i); + this->primaryTileset->palettes[paletteId][i] = palette.at(i); + this->primaryTileset->palettePreviews[paletteId][i] = palette.at(i); } else { - (*this->secondaryTileset->palettes)[paletteId][i] = palette.at(i); - (*this->secondaryTileset->palettePreviews)[paletteId][i] = palette.at(i); + this->secondaryTileset->palettes[paletteId][i] = palette.at(i); + this->secondaryTileset->palettePreviews[paletteId][i] = palette.at(i); } } diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 928087b5..4fd61a83 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -81,8 +81,8 @@ void TilesetEditor::setTilesets(QString primaryTilesetLabel, QString secondaryTi Tileset *secondaryTileset = project->getTileset(secondaryTilesetLabel); if (this->primaryTileset) delete this->primaryTileset; if (this->secondaryTileset) delete this->secondaryTileset; - this->primaryTileset = primaryTileset->copy(); - this->secondaryTileset = secondaryTileset->copy(); + this->primaryTileset = new Tileset(*primaryTileset); + this->secondaryTileset = new Tileset(*secondaryTileset); if (paletteEditor) paletteEditor->setTilesets(this->primaryTileset, this->secondaryTileset); } @@ -716,8 +716,8 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered() secondarySpinBox->setMinimum(1); primarySpinBox->setMaximum(Project::getNumMetatilesPrimary()); secondarySpinBox->setMaximum(Project::getNumMetatilesTotal() - Project::getNumMetatilesPrimary()); - primarySpinBox->setValue(this->primaryTileset->metatiles->length()); - secondarySpinBox->setValue(this->secondaryTileset->metatiles->length()); + primarySpinBox->setValue(this->primaryTileset->metatiles.length()); + secondarySpinBox->setValue(this->secondaryTileset->metatiles.length()); form.addRow(new QLabel("Primary Tileset"), primarySpinBox); form.addRow(new QLabel("Secondary Tileset"), secondarySpinBox); @@ -730,16 +730,11 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered() int numPrimaryMetatiles = primarySpinBox->value(); int numSecondaryMetatiles = secondarySpinBox->value(); int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; - while (this->primaryTileset->metatiles->length() > numPrimaryMetatiles) { - Metatile *metatile = this->primaryTileset->metatiles->takeLast(); - delete metatile; + while (this->primaryTileset->metatiles.length() > numPrimaryMetatiles) { + delete this->primaryTileset->metatiles.takeLast(); } - while (this->primaryTileset->metatiles->length() < numPrimaryMetatiles) { - Tile tile; - tile.palette = 0; - tile.tile = 0; - tile.xflip = false; - tile.yflip = false; + while (this->primaryTileset->metatiles.length() < numPrimaryMetatiles) { + Tile tile(0, false, false, 0); Metatile *metatile = new Metatile; metatile->behavior = 0; metatile->layerType = 0; @@ -748,18 +743,13 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered() for (int i = 0; i < numTiles; i++) { metatile->tiles.append(tile); } - this->primaryTileset->metatiles->append(metatile); + this->primaryTileset->metatiles.append(metatile); } - while (this->secondaryTileset->metatiles->length() > numSecondaryMetatiles) { - Metatile *metatile = this->secondaryTileset->metatiles->takeLast(); - delete metatile; + while (this->secondaryTileset->metatiles.length() > numSecondaryMetatiles) { + delete this->secondaryTileset->metatiles.takeLast(); } - while (this->secondaryTileset->metatiles->length() < numSecondaryMetatiles) { - Tile tile; - tile.palette = 0; - tile.tile = 0; - tile.xflip = 0; - tile.yflip = 0; + while (this->secondaryTileset->metatiles.length() < numSecondaryMetatiles) { + Tile tile(0, false, false, 0); Metatile *metatile = new Metatile; metatile->behavior = 0; metatile->layerType = 0; @@ -768,7 +758,7 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered() for (int i = 0; i < numTiles; i++) { metatile->tiles.append(tile); } - this->secondaryTileset->metatiles->append(metatile); + this->secondaryTileset->metatiles.append(metatile); } this->metatileSelector->updateSelectedMetatile(); @@ -891,7 +881,7 @@ void TilesetEditor::importTilesetMetatiles(Tileset *tileset, bool primary) MetatileParser parser; bool error = false; - QList *metatiles = parser.parse(filepath, &error, primary); + QList metatiles = parser.parse(filepath, &error, primary); if (error) { QMessageBox msgBox(this); msgBox.setText("Failed to import metatiles from Advance Map 1.92 .bvd file."); @@ -906,14 +896,14 @@ void TilesetEditor::importTilesetMetatiles(Tileset *tileset, bool primary) // TODO: This is crude because it makes a history entry for every newly-imported metatile. // Revisit this when tiles and num metatiles are added to tileset editory history. int metatileIdBase = primary ? 0 : Project::getNumMetatilesPrimary(); - for (int i = 0; i < metatiles->length(); i++) { - if (i >= tileset->metatiles->length()) { + for (int i = 0; i < metatiles.length(); i++) { + if (i >= tileset->metatiles.length()) { break; } - Metatile *prevMetatile = new Metatile(*tileset->metatiles->at(i)); + Metatile *prevMetatile = new Metatile(*tileset->metatiles.at(i)); MetatileHistoryItem *commit = new MetatileHistoryItem(static_cast(metatileIdBase + i), - prevMetatile, new Metatile(*metatiles->at(i))); + prevMetatile, new Metatile(*metatiles.at(i))); metatileHistory.push(commit); } diff --git a/src/ui/tileseteditormetatileselector.cpp b/src/ui/tileseteditormetatileselector.cpp index dddfdbbd..c7df404b 100644 --- a/src/ui/tileseteditormetatileselector.cpp +++ b/src/ui/tileseteditormetatileselector.cpp @@ -4,13 +4,12 @@ #include void TilesetEditorMetatileSelector::draw() { - if (!this->primaryTileset || !this->primaryTileset->metatiles - || !this->secondaryTileset || !this->secondaryTileset->metatiles) { + if (!this->primaryTileset || !this->secondaryTileset) { this->setPixmap(QPixmap()); } - int primaryLength = this->primaryTileset->metatiles->length(); - int length_ = primaryLength + this->secondaryTileset->metatiles->length(); + int primaryLength = this->primaryTileset->metatiles.length(); + int length_ = primaryLength + this->secondaryTileset->metatiles.length(); int height_ = length_ / this->numMetatilesWide; if (length_ % this->numMetatilesWide != 0) { height_++; @@ -63,7 +62,7 @@ void TilesetEditorMetatileSelector::updateSelectedMetatile() { if (Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) this->selectedMetatile = metatileId; else - this->selectedMetatile = Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles->length() - 1; + this->selectedMetatile = Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles.length() - 1; emit selectedMetatileChanged(this->selectedMetatile); } @@ -73,10 +72,10 @@ uint16_t TilesetEditorMetatileSelector::getSelectedMetatile() { uint16_t TilesetEditorMetatileSelector::getMetatileId(int x, int y) { int index = y * this->numMetatilesWide + x; - if (index < this->primaryTileset->metatiles->length()) { + if (index < this->primaryTileset->metatiles.length()) { return static_cast(index); } else { - return static_cast(Project::getNumMetatilesPrimary() + index - this->primaryTileset->metatiles->length()); + return static_cast(Project::getNumMetatilesPrimary() + index - this->primaryTileset->metatiles.length()); } } @@ -119,10 +118,10 @@ QPoint TilesetEditorMetatileSelector::getMetatileIdCoords(uint16_t metatileId) { { // Invalid metatile id. return QPoint(0, 0); - } + } int index = metatileId < Project::getNumMetatilesPrimary() ? metatileId - : metatileId - Project::getNumMetatilesPrimary() + this->primaryTileset->metatiles->length(); + : metatileId - Project::getNumMetatilesPrimary() + this->primaryTileset->metatiles.length(); return QPoint(index % this->numMetatilesWide, index / this->numMetatilesWide); } diff --git a/src/ui/tileseteditortileselector.cpp b/src/ui/tileseteditortileselector.cpp index 1b68466a..54a2da0e 100644 --- a/src/ui/tileseteditortileselector.cpp +++ b/src/ui/tileseteditortileselector.cpp @@ -13,14 +13,13 @@ QPoint TilesetEditorTileSelector::getSelectionDimensions() { } void TilesetEditorTileSelector::draw() { - if (!this->primaryTileset || !this->primaryTileset->tiles - || !this->secondaryTileset || !this->secondaryTileset->tiles) { + if (!this->primaryTileset || !this->secondaryTileset) { this->setPixmap(QPixmap()); } int totalTiles = Project::getNumTilesTotal(); - int primaryLength = this->primaryTileset->tiles->length(); - int secondaryLength = this->secondaryTileset->tiles->length(); + int primaryLength = this->primaryTileset->tiles.length(); + int secondaryLength = this->secondaryTileset->tiles.length(); int height = totalTiles / this->numTilesWide; QList palette = Tileset::getPalette(this->paletteId, this->primaryTileset, this->secondaryTileset, true); QImage image(this->numTilesWide * 16, height * 16, QImage::Format_RGBA8888); @@ -218,12 +217,11 @@ QPoint TilesetEditorTileSelector::getTileCoordsOnWidget(uint16_t tile) { } QImage TilesetEditorTileSelector::buildPrimaryTilesIndexedImage() { - if (!this->primaryTileset || !this->primaryTileset->tiles - || !this->secondaryTileset || !this->secondaryTileset->tiles) { + if (!this->primaryTileset || !this->secondaryTileset) { return QImage(); } - int primaryLength = this->primaryTileset->tiles->length(); + int primaryLength = this->primaryTileset->tiles.length(); int height = qCeil(primaryLength / static_cast(this->numTilesWide)); QImage image(this->numTilesWide * 8, height * 8, QImage::Format_RGBA8888); @@ -254,12 +252,11 @@ QImage TilesetEditorTileSelector::buildPrimaryTilesIndexedImage() { } QImage TilesetEditorTileSelector::buildSecondaryTilesIndexedImage() { - if (!this->primaryTileset || !this->primaryTileset->tiles - || !this->secondaryTileset || !this->secondaryTileset->tiles) { + if (!this->primaryTileset || !this->secondaryTileset) { return QImage(); } - int secondaryLength = this->secondaryTileset->tiles->length(); + int secondaryLength = this->secondaryTileset->tiles.length(); int height = qCeil(secondaryLength / static_cast(this->numTilesWide)); QImage image(this->numTilesWide * 8, height * 8, QImage::Format_RGBA8888);