Move tile value masks to tile.cpp

This commit is contained in:
GriffinR 2022-02-03 21:32:27 -05:00 committed by huderlem
parent cf973710c8
commit f92950c46d
4 changed files with 26 additions and 17 deletions

View file

@ -2,12 +2,14 @@
#ifndef TILE_H #ifndef TILE_H
#define TILE_H #define TILE_H
#include <QObject>
class Tile class Tile
{ {
public: public:
Tile(); Tile();
Tile(int tileId, bool xflip, bool yflip, int palette); Tile(int tileId, bool xflip, bool yflip, int palette);
Tile(uint16_t raw);
public: public:
int tileId; int tileId;
@ -15,6 +17,8 @@ public:
bool yflip; bool yflip;
int palette; int palette;
uint16_t rawValue() const;
static int getIndexInTileset(int); static int getIndexInTileset(int);
}; };

View file

@ -72,10 +72,9 @@ QList<Metatile*> MetatileParser::parse(QString filepath, bool *error, bool prima
QList<Tile> tiles; QList<Tile> tiles;
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
int metatileOffset = 4 + i * metatileSize + j * 2; int metatileOffset = 4 + i * metatileSize + j * 2;
uint16_t word = static_cast<uint16_t>( Tile tile(static_cast<uint16_t>(
static_cast<unsigned char>(in.at(metatileOffset)) | static_cast<unsigned char>(in.at(metatileOffset)) |
(static_cast<unsigned char>(in.at(metatileOffset + 1)) << 8)); (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);
} }

View file

@ -15,6 +15,21 @@
palette(palette) 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<uint16_t>(
(this->tileId & 0x3FF)
| ((this->xflip & 1) << 10)
| ((this->yflip & 1) << 11)
| ((this->palette & 0xF) << 12));
}
int Tile::getIndexInTileset(int tileId) { int Tile::getIndexInTileset(int tileId) {
if (tileId < Project::getNumTilesPrimary()) { if (tileId < Project::getNumTilesPrimary()) {
return tileId; return tileId;

View file

@ -1054,13 +1054,9 @@ void Project::saveTilesetMetatiles(Tileset *tileset) {
for (Metatile *metatile : tileset->metatiles) { for (Metatile *metatile : tileset->metatiles) {
int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8;
for (int i = 0; i < numTiles; i++) { for (int i = 0; i < numTiles; i++) {
Tile tile = metatile->tiles.at(i); uint16_t tile = metatile->tiles.at(i).rawValue();
uint16_t value = static_cast<uint16_t>((tile.tileId & 0x3ff) data.append(static_cast<char>(tile));
| ((tile.xflip & 1) << 10) data.append(static_cast<char>(tile >> 8));
| ((tile.yflip & 1) << 11)
| ((tile.palette & 0xf) << 12));
data.append(static_cast<char>(value & 0xff));
data.append(static_cast<char>((value >> 8) & 0xff));
} }
} }
metatiles_file.write(data); metatiles_file.write(data);
@ -1562,13 +1558,8 @@ void Project::loadTilesetMetatiles(Tileset* tileset) {
Metatile *metatile = new Metatile; Metatile *metatile = new Metatile;
int index = i * (2 * 4 * num_layers); int index = i * (2 * 4 * num_layers);
for (int j = 0; j < 4 * num_layers; j++) { for (int j = 0; j < 4 * num_layers; j++) {
uint16_t word = data[index++] & 0xff; Tile tile(static_cast<unsigned char>(data[index++])
word += (data[index++] & 0xff) << 8; | (static_cast<unsigned char>(data[index++]) << 8));
Tile tile;
tile.tileId = word & 0x3ff;
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); metatiles.append(metatile);