diff --git a/include/core/tile.h b/include/core/tile.h index 8d225c50..b97c1775 100644 --- a/include/core/tile.h +++ b/include/core/tile.h @@ -2,12 +2,14 @@ #ifndef TILE_H #define TILE_H +#include class Tile { public: Tile(); Tile(int tileId, bool xflip, bool yflip, int palette); + Tile(uint16_t raw); public: int tileId; @@ -15,6 +17,8 @@ public: bool yflip; int palette; + uint16_t rawValue() const; + static int getIndexInTileset(int); }; diff --git a/src/core/metatileparser.cpp b/src/core/metatileparser.cpp index 7d90ddfc..0bae75e0 100644 --- a/src/core/metatileparser.cpp +++ b/src/core/metatileparser.cpp @@ -72,10 +72,9 @@ QList MetatileParser::parse(QString filepath, bool *error, bool prima QList tiles; for (int j = 0; j < 8; j++) { int metatileOffset = 4 + i * metatileSize + j * 2; - uint16_t word = static_cast( + Tile tile(static_cast( static_cast(in.at(metatileOffset)) | - (static_cast(in.at(metatileOffset + 1)) << 8)); - Tile tile(word & 0x3ff, (word >> 10) & 1, (word >> 11) & 1, (word >> 12) & 0xf); + (static_cast(in.at(metatileOffset + 1)) << 8))); tiles.append(tile); } diff --git a/src/core/tile.cpp b/src/core/tile.cpp index e36ebb37..56465160 100644 --- a/src/core/tile.cpp +++ b/src/core/tile.cpp @@ -15,6 +15,21 @@ palette(palette) { } + Tile::Tile(uint16_t raw) : + tileId(raw & 0x3FF), + xflip((raw >> 10) & 1), + yflip((raw >> 11) & 1), + palette((raw >> 12) & 0xF) + { } + +uint16_t Tile::rawValue() const { + return static_cast( + (this->tileId & 0x3FF) + | ((this->xflip & 1) << 10) + | ((this->yflip & 1) << 11) + | ((this->palette & 0xF) << 12)); +} + int Tile::getIndexInTileset(int tileId) { if (tileId < Project::getNumTilesPrimary()) { return tileId; diff --git a/src/project.cpp b/src/project.cpp index bb9a603f..aa965499 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1054,13 +1054,9 @@ 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); - uint16_t value = static_cast((tile.tileId & 0x3ff) - | ((tile.xflip & 1) << 10) - | ((tile.yflip & 1) << 11) - | ((tile.palette & 0xf) << 12)); - data.append(static_cast(value & 0xff)); - data.append(static_cast((value >> 8) & 0xff)); + uint16_t tile = metatile->tiles.at(i).rawValue(); + data.append(static_cast(tile)); + data.append(static_cast(tile >> 8)); } } metatiles_file.write(data); @@ -1562,13 +1558,8 @@ void Project::loadTilesetMetatiles(Tileset* tileset) { Metatile *metatile = new Metatile; int index = i * (2 * 4 * num_layers); for (int j = 0; j < 4 * num_layers; j++) { - uint16_t word = data[index++] & 0xff; - word += (data[index++] & 0xff) << 8; - Tile tile; - tile.tileId = word & 0x3ff; - tile.xflip = (word >> 10) & 1; - tile.yflip = (word >> 11) & 1; - tile.palette = (word >> 12) & 0xf; + Tile tile(static_cast(data[index++]) + | (static_cast(data[index++]) << 8)); metatile->tiles.append(tile); } metatiles.append(metatile);