Use project values for tilesets

This commit is contained in:
Diegoisawesome 2018-09-18 13:18:12 -05:00
parent 3b61b1af0b
commit a6fc81ae1a
6 changed files with 88 additions and 14 deletions

View file

@ -377,6 +377,7 @@ void MainWindow::loadDataStructures() {
project->readSecretBaseIds();
project->readBgEventFacingDirections();
project->readMapsWithConnections();
project->readTilesetProperties();
}
void MainWindow::populateMapList() {

View file

@ -68,7 +68,7 @@ uint16_t Map::getSelectedBlockIndex(int index) {
if (index < layout->tileset_primary->metatiles->length()) {
return static_cast<uint16_t>(index);
} else {
return 0x200 + static_cast<uint16_t>(index - layout->tileset_primary->metatiles->length());
return static_cast<uint16_t>(Tileset::num_metatiles_primary + index - layout->tileset_primary->metatiles->length());
}
}
@ -76,7 +76,7 @@ int Map::getDisplayedBlockIndex(int index) {
if (index < layout->tileset_primary->metatiles->length()) {
return index;
} else {
return index - 0x200 + layout->tileset_primary->metatiles->length();
return index - Tileset::num_metatiles_primary + layout->tileset_primary->metatiles->length();
}
}
@ -337,7 +337,7 @@ QPixmap Map::renderMetatiles() {
for (int i = 0; i < length_; i++) {
int tile = i;
if (i >= primary_length) {
tile += 0x200 - primary_length;
tile += Tileset::num_metatiles_primary - primary_length;
}
QImage metatile_image = Metatile::getMetatileImage(tile, layout->tileset_primary, layout->tileset_secondary);
int map_y = i / width_;

View file

@ -1121,6 +1121,70 @@ QMap<QString, QStringList> Project::getTilesets() {
return allTilesets;
}
void Project::readTilesetProperties() {
QStringList defines;
QString text = readTextFile(root + "/include/fieldmap.h");
if (!text.isNull()) {
bool error = false;
QStringList definePrefixes;
definePrefixes << "NUM_";
QMap<QString, int> defines = readCDefines(text, definePrefixes);
auto it = defines.find("NUM_TILES_IN_PRIMARY");
if (it != defines.end()) {
Tileset::num_tiles_primary = it.value();
}
else {
Tileset::num_tiles_primary = 512;
error = true;
}
it = defines.find("NUM_TILES_TOTAL");
if (it != defines.end()) {
Tileset::num_tiles_total = it.value();
}
else {
Tileset::num_tiles_total = 1024;
error = true;
}
it = defines.find("NUM_METATILES_IN_PRIMARY");
if (it != defines.end()) {
Tileset::num_metatiles_primary = it.value();
}
else {
Tileset::num_metatiles_primary = 512;
error = true;
}
it = defines.find("NUM_METATILES_TOTAL");
if (it != defines.end()) {
Tileset::num_metatiles_total = it.value();
}
else {
Tileset::num_metatiles_total = 1024;
error = true;
}
it = defines.find("NUM_PALS_IN_PRIMARY");
if (it != defines.end()) {
Tileset::num_pals_primary = it.value();
}
else {
Tileset::num_pals_primary = 6;
error = true;
}
it = defines.find("NUM_PALS_TOTAL");
if (it != defines.end()) {
Tileset::num_pals_total = it.value();
}
else {
Tileset::num_pals_total = 13;
error = true;
}
if (error)
{
qDebug() << "Some global tileset values could not be loaded. Using default values";
}
}
}
void Project::readRegionMapSections() {
QString filepath = root + "/include/constants/region_map_sections.h";
QStringList prefixes = (QStringList() << "MAPSEC_");

View file

@ -88,6 +88,7 @@ public:
QStringList getSongNames();
QStringList getVisibilities();
QMap<QString, QStringList> getTilesets();
void readTilesetProperties();
void readRegionMapSections();
void readItemNames();
void readFlagNames();

View file

@ -9,6 +9,13 @@ Tileset::Tileset()
}
int Tileset::num_tiles_primary;
int Tileset::num_tiles_total;
int Tileset::num_metatiles_primary;
int Tileset::num_metatiles_total;
int Tileset::num_pals_primary;
int Tileset::num_pals_total;
Metatile::Metatile()
{
tiles = new QList<Tile>;
@ -88,8 +95,7 @@ QImage Metatile::getMetatileTile(int tile, Tileset *primaryTileset, Tileset *sec
}
Tileset* Metatile::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) {
int primary_size = 0x200;
if (metatile_index < primary_size) {
if (metatile_index < Tileset::num_metatiles_primary) {
return primaryTileset;
} else {
return secondaryTileset;
@ -97,24 +103,19 @@ Tileset* Metatile::getBlockTileset(int metatile_index, Tileset *primaryTileset,
}
int Metatile::getBlockIndex(int index) {
int primary_size = 0x200;
if (index < primary_size) {
if (index < Tileset::num_metatiles_primary) {
return index;
} else {
return index - primary_size;
return index - Tileset::num_metatiles_primary;
}
}
QList<QList<QRgb>> Metatile::getBlockPalettes(Tileset *primaryTileset, Tileset *secondaryTileset) {
QList<QList<QRgb>> palettes;
for (int i = 0; i < 6; i++) {
for (int i = 0; i < Tileset::num_pals_primary; i++) {
palettes.append(primaryTileset->palettes->at(i));
}
// TODO: Find a reliable way to detect Ruby vs. Emerald
// Ruby's secondary tilesets only use palettes 6-11, whereas
// Emerald uses 6-12.
for (int i = 6; i < 13; i++) {
for (int i = Tileset::num_pals_primary; i < Tileset::num_pals_total; i++) {
palettes.append(secondaryTileset->palettes->at(i));
}
return palettes;

View file

@ -24,6 +24,13 @@ public:
QList<QImage> *tiles = nullptr;
QList<Metatile*> *metatiles = nullptr;
QList<QList<QRgb>> *palettes = nullptr;
static int num_tiles_primary;
static int num_tiles_total;
static int num_metatiles_primary;
static int num_metatiles_total;
static int num_pals_primary;
static int num_pals_total;
};
class Metatile