diff --git a/include/core/blockdata.h b/include/core/blockdata.h index 5d434f21..a1e45b63 100644 --- a/include/core/blockdata.h +++ b/include/core/blockdata.h @@ -10,7 +10,7 @@ class Blockdata : public QVector { public: - QByteArray serialize(); + QByteArray serialize() const; }; #endif // BLOCKDATA_H diff --git a/include/core/map.h b/include/core/map.h index cedb7c19..ba871317 100644 --- a/include/core/map.h +++ b/include/core/map.h @@ -75,8 +75,8 @@ public: int getBorderHeight(); QPixmap render(bool ignoreCache, MapLayout * fromLayout = nullptr); QPixmap renderCollision(qreal opacity, bool ignoreCache); - bool mapBlockChanged(int i, Blockdata * cache); - bool borderBlockChanged(int i, Blockdata * cache); + bool mapBlockChanged(int i, Blockdata cache); + bool borderBlockChanged(int i, Blockdata cache); void cacheBlockdata(); void cacheCollision(); bool getBlock(int x, int y, Block *out); diff --git a/include/core/maplayout.h b/include/core/maplayout.h index d04fbab5..38bb3f23 100644 --- a/include/core/maplayout.h +++ b/include/core/maplayout.h @@ -24,15 +24,15 @@ public: QString tileset_secondary_label; Tileset *tileset_primary = nullptr; Tileset *tileset_secondary = nullptr; - Blockdata *blockdata = nullptr; + Blockdata blockdata; QImage border_image; QPixmap border_pixmap; - Blockdata *border = nullptr; - Blockdata *cached_blockdata = nullptr; - Blockdata *cached_collision = nullptr; - Blockdata *cached_border = nullptr; + Blockdata border; + Blockdata cached_blockdata; + Blockdata cached_collision; + Blockdata cached_border; struct { - Blockdata *blocks = nullptr; + Blockdata blocks; QSize dimensions; } lastCommitMapBlocks; // to track map changes }; diff --git a/include/project.h b/include/project.h index 085cd6d1..988a8316 100644 --- a/include/project.h +++ b/include/project.h @@ -91,7 +91,7 @@ public: Tileset* getTileset(QString, bool forceLoad = false); QMap tilesetLabels; - Blockdata* readBlockdata(QString); + Blockdata readBlockdata(QString); bool loadBlockdata(Map*); void saveTextFile(QString path, QString text); @@ -129,7 +129,7 @@ public: void saveLayoutBlockdata(Map*); void saveLayoutBorder(Map*); - void writeBlockdata(QString, Blockdata*); + void writeBlockdata(QString, const Blockdata &); void saveAllMaps(); void saveMap(Map*); void saveAllDataStructures(); diff --git a/src/core/blockdata.cpp b/src/core/blockdata.cpp index ab42b888..5fcd4e32 100644 --- a/src/core/blockdata.cpp +++ b/src/core/blockdata.cpp @@ -1,6 +1,6 @@ #include "blockdata.h" -QByteArray Blockdata::serialize() { +QByteArray Blockdata::serialize() const { QByteArray data; for (const auto &block : *this) { uint16_t word = block.rawValue(); diff --git a/src/core/editcommands.cpp b/src/core/editcommands.cpp index efc30d25..e7a3d300 100644 --- a/src/core/editcommands.cpp +++ b/src/core/editcommands.cpp @@ -49,11 +49,9 @@ void PaintMetatile::redo() { if (!map) return; - if (map->layout->blockdata) { - *map->layout->blockdata = newMetatiles; - } + map->layout->blockdata = newMetatiles; - *map->layout->lastCommitMapBlocks.blocks = *map->layout->blockdata; + map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; renderMapBlocks(map); } @@ -61,11 +59,9 @@ void PaintMetatile::redo() { void PaintMetatile::undo() { if (!map) return; - if (map->layout->blockdata) { - *map->layout->blockdata = oldMetatiles; - } + map->layout->blockdata = oldMetatiles; - *map->layout->lastCommitMapBlocks.blocks = *map->layout->blockdata; + map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; renderMapBlocks(map); @@ -107,9 +103,7 @@ void PaintBorder::redo() { if (!map) return; - if (map->layout->border) { - *map->layout->border = newBorder; - } + map->layout->border = newBorder; map->borderItem->draw(); } @@ -117,9 +111,7 @@ void PaintBorder::redo() { void PaintBorder::undo() { if (!map) return; - if (map->layout->border) { - *map->layout->border = oldBorder; - } + map->layout->border = oldBorder; map->borderItem->draw(); @@ -147,11 +139,9 @@ void ShiftMetatiles::redo() { if (!map) return; - if (map->layout->blockdata) { - *map->layout->blockdata = newMetatiles; - } + map->layout->blockdata = newMetatiles; - *map->layout->lastCommitMapBlocks.blocks = *map->layout->blockdata; + map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; renderMapBlocks(map, true); } @@ -159,11 +149,9 @@ void ShiftMetatiles::redo() { void ShiftMetatiles::undo() { if (!map) return; - if (map->layout->blockdata) { - *map->layout->blockdata = oldMetatiles; - } + map->layout->blockdata = oldMetatiles; - *map->layout->lastCommitMapBlocks.blocks = *map->layout->blockdata; + map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; renderMapBlocks(map, true); @@ -221,15 +209,11 @@ void ResizeMap::redo() { if (!map) return; - if (map->layout->blockdata) { - *map->layout->blockdata = newMetatiles; - map->setDimensions(newMapWidth, newMapHeight, false); - } + map->layout->blockdata = newMetatiles; + map->setDimensions(newMapWidth, newMapHeight, false); - if (map->layout->border) { - *map->layout->border = newBorder; - map->setBorderDimensions(newBorderWidth, newBorderHeight, false); - } + map->layout->border = newBorder; + map->setBorderDimensions(newBorderWidth, newBorderHeight, false); map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight()); @@ -239,15 +223,11 @@ void ResizeMap::redo() { void ResizeMap::undo() { if (!map) return; - if (map->layout->blockdata) { - *map->layout->blockdata = oldMetatiles; - map->setDimensions(oldMapWidth, oldMapHeight, false); - } + map->layout->blockdata = oldMetatiles; + map->setDimensions(oldMapWidth, oldMapHeight, false); - if (map->layout->border) { - *map->layout->border = oldBorder; - map->setBorderDimensions(oldBorderWidth, oldBorderHeight, false); - } + map->layout->border = oldBorder; + map->setBorderDimensions(oldBorderWidth, oldBorderHeight, false); map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight()); @@ -508,14 +488,12 @@ void ScriptEditMap::redo() { if (!map) return; - if (map->layout->blockdata) { - *map->layout->blockdata = newMetatiles; - if (newMapWidth != map->getWidth() || newMapHeight != map->getHeight()) { - map->setDimensions(newMapWidth, newMapHeight, false); - } + map->layout->blockdata = newMetatiles; + if (newMapWidth != map->getWidth() || newMapHeight != map->getHeight()) { + map->setDimensions(newMapWidth, newMapHeight, false); } - *map->layout->lastCommitMapBlocks.blocks = newMetatiles; + map->layout->lastCommitMapBlocks.blocks = newMetatiles; map->layout->lastCommitMapBlocks.dimensions = QSize(newMapWidth, newMapHeight); renderMapBlocks(map); @@ -524,14 +502,12 @@ void ScriptEditMap::redo() { void ScriptEditMap::undo() { if (!map) return; - if (map->layout->blockdata) { - *map->layout->blockdata = oldMetatiles; - if (oldMapWidth != map->getWidth() || oldMapHeight != map->getHeight()) { - map->setDimensions(oldMapWidth, oldMapHeight, false); - } + map->layout->blockdata = oldMetatiles; + if (oldMapWidth != map->getWidth() || oldMapHeight != map->getHeight()) { + map->setDimensions(oldMapWidth, oldMapHeight, false); } - *map->layout->lastCommitMapBlocks.blocks = oldMetatiles; + map->layout->lastCommitMapBlocks.blocks = oldMetatiles; map->layout->lastCommitMapBlocks.dimensions = QSize(oldMapWidth, oldMapHeight); renderMapBlocks(map); diff --git a/src/core/map.cpp b/src/core/map.cpp index 85af9a99..0f126fa8 100644 --- a/src/core/map.cpp +++ b/src/core/map.cpp @@ -78,88 +78,61 @@ int Map::getBorderHeight() { return layout->border_height.toInt(nullptr, 0); } -bool Map::mapBlockChanged(int i, Blockdata * cache) { - if (!cache) +bool Map::mapBlockChanged(int i, Blockdata cache) { + if (cache.length() <= i) return true; - if (!layout->blockdata) - return true; - if (cache->length() <= i) - return true; - if (layout->blockdata->length() <= i) + if (layout->blockdata.length() <= i) return true; - return layout->blockdata->value(i) != cache->value(i); + return layout->blockdata.at(i) != cache.at(i); } -bool Map::borderBlockChanged(int i, Blockdata * cache) { - if (!cache) +bool Map::borderBlockChanged(int i, Blockdata cache) { + if (cache.length() <= i) return true; - if (!layout->border) - return true; - if (cache->length() <= i) - return true; - if (layout->border->length() <= i) + if (layout->border.length() <= i) return true; - return layout->border->value(i) != cache->value(i); + return layout->border.at(i) != cache.at(i); } void Map::cacheBorder() { - if (layout->cached_border) delete layout->cached_border; - layout->cached_border = new Blockdata; - if (layout->border) { - for (int i = 0; i < layout->border->length(); i++) { - Block block = layout->border->value(i); - layout->cached_border->append(block); - } - } + layout->cached_border.clear(); + for (const auto &block : layout->border) + layout->cached_border.append(block); } void Map::cacheBlockdata() { - if (layout->cached_blockdata) delete layout->cached_blockdata; - layout->cached_blockdata = new Blockdata; - if (layout->blockdata) { - for (int i = 0; i < layout->blockdata->length(); i++) { - Block block = layout->blockdata->value(i); - layout->cached_blockdata->append(block); - } - } + layout->cached_blockdata.clear(); + for (const auto &block : layout->blockdata) + layout->cached_blockdata.append(block); } void Map::cacheCollision() { - if (layout->cached_collision) delete layout->cached_collision; - layout->cached_collision = new Blockdata; - if (layout->blockdata) { - for (int i = 0; i < layout->blockdata->length(); i++) { - Block block = layout->blockdata->value(i); - layout->cached_collision->append(block); - } - } + layout->cached_collision.clear(); + for (const auto &block : layout->blockdata) + layout->cached_collision.append(block); } QPixmap Map::renderCollision(qreal opacity, bool ignoreCache) { bool changed_any = false; int width_ = getWidth(); int height_ = getHeight(); - if ( - collision_image.isNull() - || collision_image.width() != width_ * 16 - || collision_image.height() != height_ * 16 - ) { + if (collision_image.isNull() || collision_image.width() != width_ * 16 || collision_image.height() != height_ * 16) { collision_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888); changed_any = true; } - if (!(layout->blockdata && width_ && height_)) { + if (layout->blockdata.isEmpty() || !width_ || !height_) { collision_pixmap = collision_pixmap.fromImage(collision_image); return collision_pixmap; } QPainter painter(&collision_image); - for (int i = 0; i < layout->blockdata->length(); i++) { - if (!ignoreCache && layout->cached_collision && !mapBlockChanged(i, layout->cached_collision)) { + for (int i = 0; i < layout->blockdata.length(); i++) { + if (!ignoreCache && !mapBlockChanged(i, layout->cached_collision)) { continue; } changed_any = true; - Block block = layout->blockdata->value(i); + Block block = layout->blockdata.at(i); QImage metatile_image = getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary, metatileLayerOrder, metatileLayerOpacity); QImage collision_metatile_image = getCollisionMetatileImage(block); int map_y = width_ ? i / width_ : 0; @@ -184,26 +157,22 @@ QPixmap Map::render(bool ignoreCache = false, MapLayout * fromLayout) { bool changed_any = false; int width_ = getWidth(); int height_ = getHeight(); - if ( - image.isNull() - || image.width() != width_ * 16 - || image.height() != height_ * 16 - ) { + if (image.isNull() || image.width() != width_ * 16 || image.height() != height_ * 16) { image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888); changed_any = true; } - if (!(layout->blockdata && width_ && height_)) { + if (layout->blockdata.isEmpty() || !width_ || !height_) { pixmap = pixmap.fromImage(image); return pixmap; } QPainter painter(&image); - for (int i = 0; i < layout->blockdata->length(); i++) { + for (int i = 0; i < layout->blockdata.length(); i++) { if (!ignoreCache && !mapBlockChanged(i, layout->cached_blockdata)) { continue; } changed_any = true; - Block block = layout->blockdata->value(i); + Block block = layout->blockdata.at(i); QImage metatile_image = getMetatileImage( block.tile, fromLayout ? fromLayout->tileset_primary : layout->tileset_primary, @@ -237,18 +206,18 @@ QPixmap Map::renderBorder(bool ignoreCache) { layout->border_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888); border_resized = true; } - if (!layout->border) { + if (layout->border.isEmpty()) { layout->border_pixmap = layout->border_pixmap.fromImage(layout->border_image); return layout->border_pixmap; } QPainter painter(&layout->border_image); - for (int i = 0; i < layout->border->length(); i++) { + for (int i = 0; i < layout->border.length(); i++) { if (!ignoreCache && (!border_resized && !borderBlockChanged(i, layout->cached_border))) { continue; } changed_any = true; - Block block = layout->border->value(i); + Block block = layout->border.at(i); uint16_t tile = block.tile; QImage metatile_image = getMetatileImage(tile, layout->tileset_primary, layout->tileset_secondary, metatileLayerOrder, metatileLayerOpacity); int map_y = width_ ? i / width_ : 0; @@ -302,38 +271,34 @@ void Map::setNewDimensionsBlockdata(int newWidth, int newHeight) { int oldWidth = getWidth(); int oldHeight = getHeight(); - Blockdata* newBlockData = new Blockdata; + layout->blockdata.clear(); for (int y = 0; y < newHeight; y++) for (int x = 0; x < newWidth; x++) { if (x < oldWidth && y < oldHeight) { int index = y * oldWidth + x; - newBlockData->append(layout->blockdata->value(index)); + layout->blockdata.append(layout->blockdata.value(index)); } else { - newBlockData->append(0); + layout->blockdata.append(0); } } - - *layout->blockdata = *newBlockData; } void Map::setNewBorderDimensionsBlockdata(int newWidth, int newHeight) { int oldWidth = getBorderWidth(); int oldHeight = getBorderHeight(); - Blockdata* newBlockData = new Blockdata; + layout->border.clear(); for (int y = 0; y < newHeight; y++) for (int x = 0; x < newWidth; x++) { if (x < oldWidth && y < oldHeight) { int index = y * oldWidth + x; - newBlockData->append(layout->border->value(index)); + layout->border.append(layout->border.value(index)); } else { - newBlockData->append(0); + layout->border.append(0); } } - - *layout->border = *newBlockData; } void Map::setDimensions(int newWidth, int newHeight, bool setNewBlockdata) { @@ -360,21 +325,19 @@ void Map::setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata) } bool Map::getBlock(int x, int y, Block *out) { - if (layout->blockdata) { - if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) { - int i = y * getWidth() + x; - *out = layout->blockdata->value(i); - return true; - } + if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) { + int i = y * getWidth() + x; + *out = layout->blockdata.value(i); + return true; } return false; } void Map::setBlock(int x, int y, Block block, bool enableScriptCallback) { int i = y * getWidth() + x; - if (layout->blockdata && i < layout->blockdata->size()) { - Block prevBlock = layout->blockdata->value(i); - layout->blockdata->replace(i, block); + if (i < layout->blockdata.size()) { + Block prevBlock = layout->blockdata.at(i); + layout->blockdata.replace(i, block); if (enableScriptCallback) { Scripting::cb_MetatileChanged(x, y, prevBlock, block); } @@ -385,40 +348,40 @@ void Map::_floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_ QList todo; todo.append(QPoint(x, y)); while (todo.length()) { - QPoint point = todo.takeAt(0); - x = point.x(); - y = point.y(); - Block block; - if (!getBlock(x, y, &block)) { - continue; - } + QPoint point = todo.takeAt(0); + x = point.x(); + y = point.y(); + Block block; + if (!getBlock(x, y, &block)) { + continue; + } - uint old_coll = block.collision; - uint old_elev = block.elevation; - if (old_coll == collision && old_elev == elevation) { - continue; - } + uint old_coll = block.collision; + uint old_elev = block.elevation; + if (old_coll == collision && old_elev == elevation) { + continue; + } - block.collision = collision; - block.elevation = elevation; - setBlock(x, y, block, true); - if (getBlock(x + 1, y, &block) && block.collision == old_coll && block.elevation == old_elev) { - todo.append(QPoint(x + 1, y)); - } - if (getBlock(x - 1, y, &block) && block.collision == old_coll && block.elevation == old_elev) { - todo.append(QPoint(x - 1, y)); - } - if (getBlock(x, y + 1, &block) && block.collision == old_coll && block.elevation == old_elev) { - todo.append(QPoint(x, y + 1)); - } - if (getBlock(x, y - 1, &block) && block.collision == old_coll && block.elevation == old_elev) { - todo.append(QPoint(x, y - 1)); - } + block.collision = collision; + block.elevation = elevation; + setBlock(x, y, block, true); + if (getBlock(x + 1, y, &block) && block.collision == old_coll && block.elevation == old_elev) { + todo.append(QPoint(x + 1, y)); + } + if (getBlock(x - 1, y, &block) && block.collision == old_coll && block.elevation == old_elev) { + todo.append(QPoint(x - 1, y)); + } + if (getBlock(x, y + 1, &block) && block.collision == old_coll && block.elevation == old_elev) { + todo.append(QPoint(x, y + 1)); + } + if (getBlock(x, y - 1, &block) && block.collision == old_coll && block.elevation == old_elev) { + todo.append(QPoint(x, y - 1)); + } } } void Map::floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation) { - Block block;; + Block block; if (getBlock(x, y, &block) && (block.collision != collision || block.elevation != elevation)) { _floodFillCollisionElevation(x, y, collision, elevation); } diff --git a/src/editor.cpp b/src/editor.cpp index 5d7ecd93..f855d9f3 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -959,7 +959,7 @@ void Editor::onHoveredMapMetatileChanged(const QPoint &pos) { if (map_item->paintingMode == MapPixmapItem::PaintMode::Metatiles && pos.x() >= 0 && pos.x() < map->getWidth() && pos.y() >= 0 && pos.y() < map->getHeight()) { int blockIndex = pos.y() * map->getWidth() + pos.x(); - int metatileId = map->layout->blockdata->at(blockIndex).tile; + int metatileId = map->layout->blockdata.at(blockIndex).tile; this->ui->statusBar->showMessage(QString("X: %1, Y: %2, %3, Scale = %4x") .arg(pos.x()) .arg(pos.y()) @@ -989,8 +989,8 @@ void Editor::onHoveredMapMovementPermissionChanged(int x, int y) { if (map_item->paintingMode == MapPixmapItem::PaintMode::Metatiles && x >= 0 && x < map->getWidth() && y >= 0 && y < map->getHeight()) { int blockIndex = y * map->getWidth() + x; - uint16_t collision = map->layout->blockdata->at(blockIndex).collision; - uint16_t elevation = map->layout->blockdata->at(blockIndex).elevation; + uint16_t collision = map->layout->blockdata.at(blockIndex).collision; + uint16_t elevation = map->layout->blockdata.at(blockIndex).elevation; QString message = QString("X: %1, Y: %2, %3") .arg(x) .arg(y) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a878b24d..22df6d71 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2610,8 +2610,8 @@ void MainWindow::on_pushButton_ChangeDimensions_clicked() if (dialog.exec() == QDialog::Accepted) { Map *map = editor->map; - Blockdata *oldMetatiles = new Blockdata(*map->layout->blockdata); - Blockdata *oldBorder = new Blockdata(*map->layout->border); + Blockdata oldMetatiles = map->layout->blockdata; + Blockdata oldBorder = map->layout->border; QSize oldMapDimensions(map->getWidth(), map->getHeight()); QSize oldBorderDimensions(map->getBorderWidth(), map->getBorderHeight()); QSize newMapDimensions(widthSpinBox->value(), heightSpinBox->value()); @@ -2621,9 +2621,9 @@ void MainWindow::on_pushButton_ChangeDimensions_clicked() editor->map->setBorderDimensions(newBorderDimensions.width(), newBorderDimensions.height()); editor->map->editHistory.push(new ResizeMap(map, oldMapDimensions, newMapDimensions, - *oldMetatiles, *map->layout->blockdata, + oldMetatiles, map->layout->blockdata, oldBorderDimensions, newBorderDimensions, - *oldBorder, *map->layout->border + oldBorder, map->layout->border )); } } diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 630978b3..43d0bfc1 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -26,7 +26,7 @@ void MainWindow::tryCommitMapChanges(bool commitChanges) { if (map) { map->editHistory.push(new ScriptEditMap(map, map->layout->lastCommitMapBlocks.dimensions, QSize(map->getWidth(), map->getHeight()), - *map->layout->lastCommitMapBlocks.blocks, *map->layout->blockdata + map->layout->lastCommitMapBlocks.blocks, map->layout->blockdata )); } } diff --git a/src/project.cpp b/src/project.cpp index 0e311c43..ee041654 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1183,30 +1183,27 @@ bool Project::loadBlockdata(Map *map) { QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path); map->layout->blockdata = readBlockdata(path); - if (map->layout->lastCommitMapBlocks.blocks) { - delete map->layout->lastCommitMapBlocks.blocks; - } - *map->layout->lastCommitMapBlocks.blocks = *map->layout->blockdata; + map->layout->lastCommitMapBlocks.blocks.clear(); + map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight()); - if (map->layout->blockdata->count() != map->getWidth() * map->getHeight()) { + if (map->layout->blockdata.count() != map->getWidth() * map->getHeight()) { logWarn(QString("Layout blockdata length %1 does not match dimensions %2x%3 (should be %4). Resizing blockdata.") - .arg(map->layout->blockdata->count()) + .arg(map->layout->blockdata.count()) .arg(map->getWidth()) .arg(map->getHeight()) .arg(map->getWidth() * map->getHeight())); - map->layout->blockdata->resize(map->getWidth() * map->getHeight()); + map->layout->blockdata.resize(map->getWidth() * map->getHeight()); } return true; } void Project::setNewMapBlockdata(Map *map) { - Blockdata *blockdata = new Blockdata; + map->layout->blockdata.clear(); for (int i = 0; i < map->getWidth() * map->getHeight(); i++) { - blockdata->append(qint16(0x3001)); + map->layout->blockdata.append(qint16(0x3001)); } - map->layout->blockdata = blockdata; - *map->layout->lastCommitMapBlocks.blocks = *map->layout->blockdata; + map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight()); } @@ -1218,33 +1215,32 @@ bool Project::loadMapBorder(Map *map) { QString path = QString("%1/%2").arg(root).arg(map->layout->border_path); map->layout->border = readBlockdata(path); int borderLength = map->getBorderWidth() * map->getBorderHeight(); - if (map->layout->border->count() != borderLength) { + if (map->layout->border.count() != borderLength) { logWarn(QString("Layout border blockdata length %1 must be %2. Resizing border blockdata.") - .arg(map->layout->border->count()) + .arg(map->layout->border.count()) .arg(borderLength)); - map->layout->border->resize(borderLength); + map->layout->border.resize(borderLength); } return true; } void Project::setNewMapBorder(Map *map) { - Blockdata *blockdata = new Blockdata; + map->layout->border.clear(); if (map->getBorderWidth() != DEFAULT_BORDER_WIDTH || map->getBorderHeight() != DEFAULT_BORDER_HEIGHT) { for (int i = 0; i < map->getBorderWidth() * map->getBorderHeight(); i++) { - blockdata->append(0); + map->layout->border.append(0); } } else if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokefirered) { - blockdata->append(qint16(0x0014)); - blockdata->append(qint16(0x0015)); - blockdata->append(qint16(0x001C)); - blockdata->append(qint16(0x001D)); + map->layout->border.append(qint16(0x0014)); + map->layout->border.append(qint16(0x0015)); + map->layout->border.append(qint16(0x001C)); + map->layout->border.append(qint16(0x001D)); } else { - blockdata->append(qint16(0x01D4)); - blockdata->append(qint16(0x01D5)); - blockdata->append(qint16(0x01DC)); - blockdata->append(qint16(0x01DD)); + map->layout->border.append(qint16(0x01D4)); + map->layout->border.append(qint16(0x01D5)); + map->layout->border.append(qint16(0x01DC)); + map->layout->border.append(qint16(0x01DD)); } - map->layout->border = blockdata; } void Project::saveLayoutBorder(Map *map) { @@ -1257,10 +1253,10 @@ void Project::saveLayoutBlockdata(Map* map) { writeBlockdata(path, map->layout->blockdata); } -void Project::writeBlockdata(QString path, Blockdata *blockdata) { +void Project::writeBlockdata(QString path, const Blockdata &blockdata) { QFile file(path); if (file.open(QIODevice::WriteOnly)) { - QByteArray data = blockdata->serialize(); + QByteArray data = blockdata.serialize(); file.write(data); } else { logError(QString("Failed to open blockdata file for writing: '%1'").arg(path)); @@ -1700,14 +1696,14 @@ void Project::loadTilesetMetatileLabels(Tileset* tileset) { } } -Blockdata* Project::readBlockdata(QString path) { - Blockdata *blockdata = new Blockdata; +Blockdata Project::readBlockdata(QString path) { + Blockdata blockdata; QFile file(path); if (file.open(QIODevice::ReadOnly)) { QByteArray data = file.readAll(); for (int i = 0; (i + 1) < data.length(); i += 2) { uint16_t word = static_cast((data[i] & 0xff) + ((data[i + 1] & 0xff) << 8)); - blockdata->append(word); + blockdata.append(word); } } else { logError(QString("Failed to open blockdata path '%1'").arg(path)); diff --git a/src/ui/bordermetatilespixmapitem.cpp b/src/ui/bordermetatilespixmapitem.cpp index 3f78a526..89558978 100644 --- a/src/ui/bordermetatilespixmapitem.cpp +++ b/src/ui/bordermetatilespixmapitem.cpp @@ -11,22 +11,19 @@ void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) int width = map->getBorderWidth(); int height = map->getBorderHeight(); - Blockdata *oldBorder = new Blockdata(*map->layout->border); + Blockdata oldBorder = map->layout->border; for (int i = 0; i < selectionDimensions.x() && (i + pos.x()) < width; i++) { for (int j = 0; j < selectionDimensions.y() && (j + pos.y()) < height; j++) { int blockIndex = (j + pos.y()) * width + (i + pos.x()); uint16_t tile = selectedMetatiles->at(j * selectionDimensions.x() + i); - (*map->layout->border)[blockIndex].tile = tile; + map->layout->border[blockIndex].tile = tile; } } - Blockdata *newBorder = new Blockdata(*map->layout->border); - if (*newBorder == *oldBorder) { - delete newBorder; - delete oldBorder; - } else { - map->editHistory.push(new PaintBorder(map, *oldBorder, *newBorder, 0)); + Blockdata newBorder = map->layout->border; + if (newBorder != oldBorder) { + map->editHistory.push(new PaintBorder(map, oldBorder, newBorder, 0)); } emit borderMetatilesChanged(); @@ -46,7 +43,7 @@ void BorderMetatilesPixmapItem::draw() { int y = j * 16; int index = j * width + i; QImage metatile_image = getMetatileImage( - map->layout->border->value(index).tile, + map->layout->border.value(index).tile, map->layout->tileset_primary, map->layout->tileset_secondary, map->metatileLayerOrder, diff --git a/src/ui/collisionpixmapitem.cpp b/src/ui/collisionpixmapitem.cpp index 2e9b3007..b4ef78a5 100644 --- a/src/ui/collisionpixmapitem.cpp +++ b/src/ui/collisionpixmapitem.cpp @@ -45,7 +45,7 @@ void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) { if (event->type() == QEvent::GraphicsSceneMouseRelease) { actionId_++; } else if (map) { - Blockdata *oldCollision = new Blockdata(*map->layout->blockdata); + Blockdata oldCollision = map->layout->blockdata; QPoint pos = Metatile::coordFromPixmapCoord(event->pos()); @@ -65,12 +65,9 @@ void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) { map->setBlock(pos.x(), pos.y(), block, true); } - Blockdata *newCollision = new Blockdata(*map->layout->blockdata); - if (*newCollision == *oldCollision) { - delete newCollision; - delete oldCollision; - } else { - map->editHistory.push(new PaintCollision(map, *oldCollision, *newCollision, actionId_)); + Blockdata newCollision = map->layout->blockdata; + if (newCollision != oldCollision) { + map->editHistory.push(new PaintCollision(map, oldCollision, newCollision, actionId_)); } } } @@ -79,19 +76,16 @@ void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) { if (event->type() == QEvent::GraphicsSceneMouseRelease) { this->actionId_++; } else if (map) { - Blockdata *oldCollision = new Blockdata(*map->layout->blockdata); + Blockdata oldCollision = map->layout->blockdata; QPoint pos = Metatile::coordFromPixmapCoord(event->pos()); uint16_t collision = this->movementPermissionsSelector->getSelectedCollision(); uint16_t elevation = this->movementPermissionsSelector->getSelectedElevation(); map->floodFillCollisionElevation(pos.x(), pos.y(), collision, elevation); - Blockdata *newCollision = new Blockdata(*map->layout->blockdata); - if (*newCollision == *oldCollision) { - delete newCollision; - delete oldCollision; - } else { - map->editHistory.push(new BucketFillCollision(map, *oldCollision, *newCollision)); + Blockdata newCollision = map->layout->blockdata; + if (newCollision != oldCollision) { + map->editHistory.push(new BucketFillCollision(map, oldCollision, newCollision)); } } } @@ -100,18 +94,15 @@ void CollisionPixmapItem::magicFill(QGraphicsSceneMouseEvent *event) { if (event->type() == QEvent::GraphicsSceneMouseRelease) { this->actionId_++; } else if (map) { - Blockdata *oldCollision = new Blockdata(*map->layout->blockdata); + Blockdata oldCollision = map->layout->blockdata; QPoint pos = Metatile::coordFromPixmapCoord(event->pos()); uint16_t collision = this->movementPermissionsSelector->getSelectedCollision(); uint16_t elevation = this->movementPermissionsSelector->getSelectedElevation(); map->magicFillCollisionElevation(pos.x(), pos.y(), collision, elevation); - Blockdata *newCollision = new Blockdata(*map->layout->blockdata); - if (*newCollision == *oldCollision) { - delete newCollision; - delete oldCollision; - } else { - map->editHistory.push(new MagicFillCollision(map, *oldCollision, *newCollision)); + Blockdata newCollision = map->layout->blockdata; + if (newCollision != oldCollision) { + map->editHistory.push(new MagicFillCollision(map, oldCollision, newCollision)); } } } diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index ba2618cb..5bcda7cf 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -76,7 +76,7 @@ void MapPixmapItem::shift(QGraphicsSceneMouseEvent *event) { } void MapPixmapItem::shift(int xDelta, int yDelta, bool fromScriptCall) { - Blockdata *backupBlockdata = new Blockdata(*map->layout->blockdata); + Blockdata backupBlockdata = map->layout->blockdata; for (int i = 0; i < map->getWidth(); i++) for (int j = 0; j < map->getHeight(); j++) { int destX = i + xDelta; @@ -89,20 +89,15 @@ void MapPixmapItem::shift(int xDelta, int yDelta, bool fromScriptCall) { destY %= map->getHeight(); int blockIndex = j * map->getWidth() + i; - Block srcBlock = backupBlockdata->at(blockIndex); + Block srcBlock = backupBlockdata.at(blockIndex); map->setBlock(destX, destY, srcBlock); } if (!fromScriptCall) { - Blockdata *newMetatiles = new Blockdata(*map->layout->blockdata); - if (*newMetatiles == *backupBlockdata) { - delete newMetatiles; - delete backupBlockdata; - } else { - map->editHistory.push(new ShiftMetatiles(map, *backupBlockdata, *newMetatiles, actionId_)); + Blockdata newMetatiles = map->layout->blockdata; + if (newMetatiles != backupBlockdata) { + map->editHistory.push(new ShiftMetatiles(map, backupBlockdata, newMetatiles, actionId_)); } - } else { - delete backupBlockdata; } } @@ -124,8 +119,7 @@ void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) { y = initialY + (yDiff / selectionDimensions.y()) * selectionDimensions.y(); // for edit history - Blockdata *oldMetatiles = nullptr; - if (!fromScriptCall) oldMetatiles = new Blockdata(*map->layout->blockdata); + Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata(); for (int i = 0; i < selectionDimensions.x() && i + x < map->getWidth(); i++) for (int j = 0; j < selectionDimensions.y() && j + y < map->getHeight(); j++) { @@ -144,12 +138,9 @@ void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) { } if (!fromScriptCall) { - Blockdata *newMetatiles = new Blockdata(*map->layout->blockdata); - if (*newMetatiles == *oldMetatiles) { - delete newMetatiles; - delete oldMetatiles; - } else { - map->editHistory.push(new PaintMetatile(map, *oldMetatiles, *newMetatiles, actionId_)); + Blockdata newMetatiles = map->layout->blockdata; + if (newMetatiles != oldMetatiles) { + map->editHistory.push(new PaintMetatile(map, oldMetatiles, newMetatiles, actionId_)); } } } @@ -198,8 +189,7 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) { } // for edit history - Blockdata *oldMetatiles = nullptr; - if (!fromScriptCall) oldMetatiles = new Blockdata(*map->layout->blockdata); + Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata(); // Fill the region with the open tile. for (int i = 0; i <= 1; i++) @@ -264,12 +254,9 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) { } if (!fromScriptCall) { - Blockdata *newMetatiles = new Blockdata(*map->layout->blockdata); - if (*newMetatiles == *oldMetatiles) { - delete newMetatiles; - delete oldMetatiles; - } else { - map->editHistory.push(new PaintMetatile(map, *oldMetatiles, *newMetatiles, actionId_)); + Blockdata newMetatiles = map->layout->blockdata; + if (newMetatiles != oldMetatiles) { + map->editHistory.push(new PaintMetatile(map, oldMetatiles, newMetatiles, actionId_)); } } } @@ -352,7 +339,7 @@ void MapPixmapItem::updateMetatileSelection(QGraphicsSceneMouseEvent *event) { metatiles.append(block.tile); } int blockIndex = y * map->getWidth() + x; - block = map->layout->blockdata->at(blockIndex); + block = map->layout->blockdata.at(blockIndex); auto collision = block.collision; auto elevation = block.elevation; collisions.append(QPair(collision, elevation)); @@ -421,8 +408,7 @@ void MapPixmapItem::magicFill( return; } - Blockdata *oldMetatiles = nullptr; - if (!fromScriptCall) oldMetatiles = new Blockdata(*map->layout->blockdata); + Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata(); bool setCollisions = selectedCollisions && selectedCollisions->length() == selectedMetatiles->length(); uint16_t tile = block.tile; @@ -447,12 +433,9 @@ void MapPixmapItem::magicFill( } if (!fromScriptCall) { - Blockdata *newMetatiles = new Blockdata(*map->layout->blockdata); - if (*newMetatiles == *oldMetatiles) { - delete newMetatiles; - delete oldMetatiles; - } else { - map->editHistory.push(new MagicFillMetatile(map, *oldMetatiles, *newMetatiles, actionId_)); + Blockdata newMetatiles = map->layout->blockdata; + if (newMetatiles != oldMetatiles) { + map->editHistory.push(new MagicFillMetatile(map, oldMetatiles, newMetatiles, actionId_)); } } } @@ -480,10 +463,7 @@ void MapPixmapItem::floodFill( QList> *selectedCollisions, bool fromScriptCall) { bool setCollisions = selectedCollisions && selectedCollisions->length() == selectedMetatiles->length(); - Blockdata *oldMetatiles = nullptr; - if (!fromScriptCall) { - oldMetatiles = new Blockdata(*map->layout->blockdata); - } + Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata(); QSet visited; QList todo; @@ -534,12 +514,9 @@ void MapPixmapItem::floodFill( } if (!fromScriptCall) { - Blockdata *newMetatiles = new Blockdata(*map->layout->blockdata); - if (*newMetatiles == *oldMetatiles) { - delete newMetatiles; - delete oldMetatiles; - } else { - map->editHistory.push(new BucketFillMetatile(map, *oldMetatiles, *newMetatiles, actionId_)); + Blockdata newMetatiles = map->layout->blockdata; + if (newMetatiles != oldMetatiles) { + map->editHistory.push(new BucketFillMetatile(map, oldMetatiles, newMetatiles, actionId_)); } } } @@ -563,8 +540,7 @@ void MapPixmapItem::floodFillSmartPath(int initialX, int initialY, bool fromScri setCollisions = true; } - Blockdata *oldMetatiles = nullptr; - if (!fromScriptCall) oldMetatiles = new Blockdata(*map->layout->blockdata); + Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata(); // Flood fill the region with the open tile. QList todo; @@ -660,12 +636,9 @@ void MapPixmapItem::floodFillSmartPath(int initialX, int initialY, bool fromScri } if (!fromScriptCall) { - Blockdata *newMetatiles = new Blockdata(*map->layout->blockdata); - if (*newMetatiles == *oldMetatiles) { - delete newMetatiles; - delete oldMetatiles; - } else { - map->editHistory.push(new BucketFillMetatile(map, *oldMetatiles, *newMetatiles, actionId_)); + Blockdata newMetatiles = map->layout->blockdata; + if (newMetatiles != oldMetatiles) { + map->editHistory.push(new BucketFillMetatile(map, oldMetatiles, newMetatiles, actionId_)); } } }