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
#define TILE_H
#include <QObject>
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);
};

View file

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

View file

@ -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<uint16_t>(
(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;

View file

@ -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<uint16_t>((tile.tileId & 0x3ff)
| ((tile.xflip & 1) << 10)
| ((tile.yflip & 1) << 11)
| ((tile.palette & 0xf) << 12));
data.append(static_cast<char>(value & 0xff));
data.append(static_cast<char>((value >> 8) & 0xff));
uint16_t tile = metatile->tiles.at(i).rawValue();
data.append(static_cast<char>(tile));
data.append(static_cast<char>(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<unsigned char>(data[index++])
| (static_cast<unsigned char>(data[index++]) << 8));
metatile->tiles.append(tile);
}
metatiles.append(metatile);