Refactor AssembleMap32Tiles to report errors

This commit is contained in:
scawful
2024-08-29 19:10:02 -04:00
parent c774999f08
commit 93d7aa545c
5 changed files with 39 additions and 28 deletions

View File

@@ -970,7 +970,7 @@ void OverworldEditor::DrawOverworldSprites() {
int i = 0;
for (auto &sprite : *overworld_.mutable_sprites(game_state_)) {
if (!sprite.deleted()) {
int map_id = sprite.map_id();
// int map_id = sprite.map_id();
// map x and map y are relative to the map
// So we need to check if the map is large or small then add the offset
@@ -1063,10 +1063,10 @@ absl::Status OverworldEditor::LoadGraphics() {
// Copy the tile16 data into individual tiles.
auto tile16_data = overworld_.tile16_blockset_data();
tile16_individual_.reserve(kNumTile16Individual);
tile16_individual_.reserve(zelda3::overworld::kNumTile16Individual);
// Loop through the tiles and copy their pixel data into separate vectors
for (uint i = 0; i < kNumTile16Individual; i++) {
for (uint i = 0; i < zelda3::overworld::kNumTile16Individual; i++) {
std::vector<uint8_t> tile_data(kTile16Size * kTile16Size, 0x00);
// Copy the pixel data for the current tile into the vector

View File

@@ -34,7 +34,6 @@ constexpr uint kMessageIdSize = 5;
constexpr uint kNumSheetsToLoad = 223;
constexpr uint kTile8DisplayHeight = 64;
constexpr uint kOverworldMapSize = 0x200;
constexpr uint kNumTile16Individual = 4096;
constexpr float kInputFieldSize = 30.f;
constexpr ImVec2 kOverworldCanvasSize(kOverworldMapSize * 8,
kOverworldMapSize * 8);

View File

@@ -117,7 +117,7 @@ absl::Status OverworldEditor::RefreshTile16Blockset() {
std::vector<std::future<void>> futures;
// Loop through the tiles and copy their pixel data into separate vectors
for (uint i = 0; i < kNumTile16Individual; i++) {
for (uint i = 0; i < zelda3::overworld::kNumTile16Individual; i++) {
futures.push_back(std::async(
std::launch::async,
[&](int index) {
@@ -141,7 +141,7 @@ absl::Status OverworldEditor::RefreshTile16Blockset() {
}
// Render the bitmaps of each tile.
for (uint id = 0; id < kNumTile16Individual; id++) {
for (uint id = 0; id < zelda3::overworld::kNumTile16Individual; id++) {
RETURN_IF_ERROR(tile16_individual_[id].ApplyPalette(palette_));
Renderer::GetInstance().UpdateBitmap(&tile16_individual_[id]);
}

View File

@@ -80,7 +80,7 @@ absl::flat_hash_map<int, MapData> parseFile(const std::string &filename) {
absl::Status Overworld::Load(Rom &rom) {
rom_ = rom;
AssembleMap32Tiles();
RETURN_IF_ERROR(AssembleMap32Tiles());
AssembleMap16Tiles();
RETURN_IF_ERROR(DecompressAllMapTiles())
@@ -154,30 +154,37 @@ void Overworld::FetchLargeMaps() {
}
}
void Overworld::AssembleMap32Tiles() {
auto get_tile16_for_tile32 = [this](int index, int quadrant, int dimension) {
const uint32_t map32address[4] = {rom()->version_constants().kMap32TileTL,
rom()->version_constants().kMap32TileTR,
rom()->version_constants().kMap32TileBL,
rom()->version_constants().kMap32TileBR};
return (uint16_t)(rom_[map32address[dimension] + quadrant + (index)] +
(((rom_[map32address[dimension] + (index) +
(quadrant <= 1 ? 4 : 5)] >>
(quadrant % 2 == 0 ? 4 : 0)) &
0x0F) *
256));
};
absl::StatusOr<uint16_t> Overworld::GetTile16ForTile32(int index, int quadrant,
int dimension) {
const uint32_t map32address[4] = {rom_.version_constants().kMap32TileTL,
rom_.version_constants().kMap32TileTR,
rom_.version_constants().kMap32TileBL,
rom_.version_constants().kMap32TileBR};
ASSIGN_OR_RETURN(auto arg1,
rom_.ReadByte(map32address[dimension] + quadrant + (index)));
ASSIGN_OR_RETURN(auto arg2, rom_.ReadWord(map32address[dimension] + (index) +
(quadrant <= 1 ? 4 : 5)));
return (uint16_t)(arg1 +
(((arg2 >> (quadrant % 2 == 0 ? 4 : 0)) & 0x0F) * 256));
}
constexpr int kMap32TilesLength = 0x33F0;
absl::Status Overworld::AssembleMap32Tiles() {
// Loop through each 32x32 pixel tile in the rom
for (int i = 0; i < 0x33F0; i += 6) {
for (int i = 0; i < kMap32TilesLength; i += 6) {
// Loop through each quadrant of the 32x32 pixel tile.
for (int k = 0; k < 4; k++) {
// Generate the 16-bit tile for the current quadrant of the current
// 32x32 pixel tile.
uint16_t tl = get_tile16_for_tile32(i, k, (int)Dimension::map32TilesTL);
uint16_t tr = get_tile16_for_tile32(i, k, (int)Dimension::map32TilesTR);
uint16_t bl = get_tile16_for_tile32(i, k, (int)Dimension::map32TilesBL);
uint16_t br = get_tile16_for_tile32(i, k, (int)Dimension::map32TilesBR);
ASSIGN_OR_RETURN(uint16_t tl,
GetTile16ForTile32(i, k, (int)Dimension::map32TilesTL));
ASSIGN_OR_RETURN(uint16_t tr,
GetTile16ForTile32(i, k, (int)Dimension::map32TilesTR));
ASSIGN_OR_RETURN(uint16_t bl,
GetTile16ForTile32(i, k, (int)Dimension::map32TilesBL));
ASSIGN_OR_RETURN(uint16_t br,
GetTile16ForTile32(i, k, (int)Dimension::map32TilesBR));
// Add the generated 16-bit tiles to the tiles32 vector.
tiles32_unique_.emplace_back(gfx::Tile32(tl, tr, bl, br));
@@ -192,11 +199,13 @@ void Overworld::AssembleMap32Tiles() {
map_tiles_.dark_world[i].resize(0x200);
map_tiles_.special_world[i].resize(0x200);
}
return absl::OkStatus();
}
void Overworld::AssembleMap16Tiles() {
int tpos = kMap16Tiles;
for (int i = 0; i < 4096; i += 1) {
for (int i = 0; i < kNumTile16Individual; i += 1) {
gfx::TileInfo t0 = gfx::GetTilesInfo(rom()->toint16(tpos));
tpos += 2;
gfx::TileInfo t1 = gfx::GetTilesInfo(rom()->toint16(tpos));
@@ -1515,7 +1524,7 @@ absl::Status Overworld::LoadPrototype(Rom &rom,
const std::string &tilemap_filename) {
rom_ = rom;
AssembleMap32Tiles();
RETURN_IF_ERROR(AssembleMap32Tiles());
AssembleMap16Tiles();
RETURN_IF_ERROR(DecompressProtoMapTiles(tilemap_filename))

View File

@@ -431,6 +431,7 @@ constexpr int overworldCustomMosaicArray = 0x1301F0;
constexpr int kMap16Tiles = 0x78000;
constexpr int kNumOverworldMaps = 160;
constexpr int kNumTile16Individual = 4096;
constexpr int Map32PerScreen = 256;
constexpr int NumberOfMap16 = 3752; // 4096
constexpr int LimitOfMap32 = 8864;
@@ -550,7 +551,9 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
};
void FetchLargeMaps();
void AssembleMap32Tiles();
absl::StatusOr<uint16_t> GetTile16ForTile32(int index, int quadrant,
int dimension);
absl::Status AssembleMap32Tiles();
void AssembleMap16Tiles();
void AssignWorldTiles(int x, int y, int sx, int sy, int tpos,
OWBlockset &world);