Refactor OverworldEditor and Overworld class

This commit is contained in:
scawful
2024-08-29 19:52:38 -04:00
parent 9a293e0ad6
commit 698055d165
6 changed files with 31 additions and 176 deletions

View File

@@ -383,7 +383,7 @@ absl::Status Tile16Editor::UpdateTransferTileCanvas() {
// Load the Link to the Past overworld.
PRINT_IF_ERROR(transfer_overworld_.Load(transfer_rom_))
transfer_overworld_.set_current_map(0);
palette_ = transfer_overworld_.AreaPalette();
palette_ = transfer_overworld_.current_area_palette();
// Create the tile16 blockset image
RETURN_IF_ERROR(Renderer::GetInstance().CreateAndRenderBitmap(

View File

@@ -749,7 +749,7 @@ absl::Status OverworldEditor::DrawAreaGraphics() {
if (overworld_.is_loaded() &&
current_graphics_set_.count(current_map_) == 0) {
overworld_.set_current_map(current_map_);
palette_ = overworld_.AreaPalette();
palette_ = overworld_.current_area_palette();
gfx::Bitmap bmp;
RETURN_IF_ERROR(Renderer::GetInstance().CreateAndRenderBitmap(
0x80, kOverworldMapSize, 0x08, overworld_.current_graphics(), bmp,
@@ -1048,7 +1048,7 @@ absl::Status OverworldEditor::Save() {
absl::Status OverworldEditor::LoadGraphics() {
// Load the Link to the Past overworld.
RETURN_IF_ERROR(overworld_.Load(*rom()))
palette_ = overworld_.AreaPalette();
palette_ = overworld_.current_area_palette();
// Create the area graphics image
RETURN_IF_ERROR(Renderer::GetInstance().CreateAndRenderBitmap(
@@ -1091,9 +1091,9 @@ absl::Status OverworldEditor::LoadGraphics() {
// Render the overworld maps loaded from the ROM.
for (int i = 0; i < zelda3::overworld::kNumOverworldMaps; ++i) {
overworld_.set_current_map(i);
auto palette = overworld_.AreaPalette();
auto palette = overworld_.current_area_palette();
RETURN_IF_ERROR(Renderer::GetInstance().CreateAndRenderBitmap(
kOverworldMapSize, kOverworldMapSize, 0x200, overworld_.BitmapData(),
kOverworldMapSize, kOverworldMapSize, 0x200, overworld_.current_map_bitmap_data(),
maps_bmp_[i], palette));
}
@@ -1159,21 +1159,21 @@ void OverworldEditor::DrawOverworldProperties() {
}
Text("Area Gfx LW/DW");
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 1.0f, 32, 0);
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 2.0f, 32, 0);
SameLine();
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 1.0f, 32, 1);
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 2.0f, 32, 1);
ImGui::Separator();
Text("Sprite Gfx LW/DW");
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 1.0f, 32, 6);
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 2.0f, 32, 6);
SameLine();
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 1.0f, 32, 7);
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 2.0f, 32, 7);
ImGui::Separator();
Text("Area Pal LW/DW");
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 1.0f, 32, 2);
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 2.0f, 32, 2);
SameLine();
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 1.0f, 32, 3);
properties_canvas_.UpdateInfoGrid(ImVec2(256, 256), 8, 2.0f, 32, 3);
static bool show_gfx_group = false;
Checkbox("Show Gfx Group Editor", &show_gfx_group);

View File

@@ -293,8 +293,7 @@ class OverworldEditor : public Editor,
gui::CanvasGridSize::k32x32};
gui::Canvas graphics_bin_canvas_{"GraphicsBin", kGraphicsBinCanvasSize,
gui::CanvasGridSize::k16x16};
gui::Canvas properties_canvas_{"Properties", ImVec2(0x100, 0x100),
gui::CanvasGridSize::k8x8};
gui::Canvas properties_canvas_;
gui::zeml::Node layout_node_;
absl::Status status_;
};

View File

@@ -60,7 +60,7 @@ void OverworldEditor::RefreshOverworldMap() {
absl::Status OverworldEditor::RefreshMapPalette() {
RETURN_IF_ERROR(
overworld_.mutable_overworld_map(current_map_)->LoadPalette());
const auto current_map_palette = overworld_.AreaPalette();
const auto current_map_palette = overworld_.current_area_palette();
if (overworld_.overworld_map(current_map_)->is_large_map()) {
// We need to update the map and its siblings if it's a large map
@@ -107,7 +107,7 @@ absl::Status OverworldEditor::RefreshTile16Blockset() {
current_blockset_ = overworld_.overworld_map(current_map_)->area_graphics();
overworld_.set_current_map(current_map_);
palette_ = overworld_.AreaPalette();
palette_ = overworld_.current_area_palette();
// Create the tile16 blockset image
Renderer::GetInstance().UpdateBitmap(&tile16_blockset_bmp_);
RETURN_IF_ERROR(tile16_blockset_bmp_.ApplyPalette(palette_));

View File

@@ -7,7 +7,6 @@
#include <unordered_map>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "app/core/constants.h"
#include "app/gfx/bitmap.h"
@@ -22,61 +21,6 @@ namespace app {
namespace zelda3 {
namespace overworld {
namespace {
absl::flat_hash_map<int, MapData> parseFile(const std::string &filename) {
absl::flat_hash_map<int, MapData> resultMap;
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open file: " << filename << std::endl;
return resultMap;
}
std::string line;
int currentKey;
bool isHigh = true;
while (getline(file, line)) {
// Skip empty or whitespace-only lines
if (line.find_first_not_of(" \t\r\n") == std::string::npos) {
continue;
}
// If the line starts with "MAPDTH" or "MAPDTL", extract the ID.
if (line.find("MAPDTH") == 0) {
auto num_str = line.substr(6); // Extract ID after "MAPDTH"
currentKey = std::stoi(num_str);
isHigh = true;
} else if (line.find("MAPDTL") == 0) {
auto num_str = line.substr(6); // Extract ID after "MAPDTH"
currentKey = std::stoi(num_str);
isHigh = false;
} else {
// Check if the currentKey is already in the map. If not, initialize it.
if (resultMap.find(currentKey) == resultMap.end()) {
resultMap[currentKey] = MapData();
}
// Split the line by commas and convert to uint8_t.
std::stringstream ss(line);
std::string valueStr;
while (getline(ss, valueStr, ',')) {
uint8_t value = std::stoi(valueStr, nullptr, 16);
if (isHigh) {
resultMap[currentKey].highData.emplace_back(value);
} else {
resultMap[currentKey].lowData.emplace_back(value);
}
}
}
}
return resultMap;
}
} // namespace
absl::Status Overworld::Load(Rom &rom) {
rom_ = rom;
@@ -1487,96 +1431,6 @@ absl::Status Overworld::SaveMapProperties() {
return absl::OkStatus();
}
absl::Status Overworld::DecompressProtoMapTiles(const std::string &filename) {
proto_map_data_ = parseFile(filename);
int sx = 0;
int sy = 0;
int c = 0;
for (size_t i = 0; i < proto_map_data_.size(); i++) {
int ttpos = 0;
ASSIGN_OR_RETURN(auto bytes, gfx::lc_lz2::DecompressOverworld(
proto_map_data_[i].lowData, 0,
proto_map_data_[i].lowData.size()))
ASSIGN_OR_RETURN(auto bytes2, gfx::lc_lz2::DecompressOverworld(
proto_map_data_[i].highData, 0,
proto_map_data_[i].highData.size()))
OrganizeMapTiles(bytes, bytes2, i, sx, sy, ttpos);
sx++;
if (sx >= 8) {
sy++;
sx = 0;
}
c++;
if (c >= 64) {
sx = 0;
sy = 0;
c = 0;
}
}
return absl::OkStatus();
}
absl::Status Overworld::LoadPrototype(Rom &rom,
const std::string &tilemap_filename) {
rom_ = rom;
RETURN_IF_ERROR(AssembleMap32Tiles());
AssembleMap16Tiles();
RETURN_IF_ERROR(DecompressProtoMapTiles(tilemap_filename))
const bool load_custom_overworld = flags()->overworld.kLoadCustomOverworld;
for (int map_index = 0; map_index < kNumOverworldMaps; ++map_index)
overworld_maps_.emplace_back(map_index, rom_, load_custom_overworld);
FetchLargeMaps();
LoadEntrances();
auto size = tiles16_.size();
std::vector<std::future<absl::Status>> futures;
// for (int i = 0; i < kNumOverworldMaps; ++i) {
// futures.emplace_back(std::async(std::launch::async, [this, i, size]() {
// if (i < 64) {
// return overworld_maps_[i].BuildMap(size, game_state_, 0,
// map_tiles_.light_world);
// } else if (i < 0x80 && i >= 0x40) {
// return overworld_maps_[i].BuildMap(size, game_state_, 1,
// map_tiles_.dark_world);
// } else {
// return overworld_maps_[i].BuildMap(size, game_state_, 2,
// map_tiles_.special_world);
// }
// }));
// }
// Wait for all tasks to complete and check their results
for (auto &future : futures) {
absl::Status status = future.get();
if (!status.ok()) {
return status;
}
}
is_loaded_ = true;
return absl::OkStatus();
}
OWBlockset &Overworld::GetMapTiles(int world_type) {
switch (world_type) {
case 0:
return map_tiles_.light_world;
case 1:
return map_tiles_.dark_world;
case 2:
return map_tiles_.special_world;
default:
return map_tiles_.light_world;
}
}
} // namespace overworld
} // namespace zelda3
} // namespace app

View File

@@ -451,7 +451,6 @@ struct MapData {
*/
class Overworld : public SharedRom, public core::ExperimentFlags {
public:
OWBlockset &GetMapTiles(int world_type);
absl::Status Load(Rom &rom);
absl::Status LoadOverworldMaps();
void LoadTileTypes();
@@ -475,7 +474,6 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
absl::Status SaveMap32Tiles();
absl::Status SaveMapProperties();
absl::Status LoadPrototype(Rom &rom_, const std::string &tilemap_filename);
void Destroy() {
for (auto &map : overworld_maps_) {
@@ -489,7 +487,6 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
is_loaded_ = false;
}
int current_world_ = 0;
int GetTileFromPosition(ImVec2 position) const {
if (current_world_ == 0) {
return map_tiles_.light_world[position.x][position.y];
@@ -500,6 +497,19 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
}
}
OWBlockset &GetMapTiles(int world_type) {
switch (world_type) {
case 0:
return map_tiles_.light_world;
case 1:
return map_tiles_.dark_world;
case 2:
return map_tiles_.special_world;
default:
return map_tiles_.light_world;
}
}
auto overworld_maps() const { return overworld_maps_; }
auto overworld_map(int i) const { return &overworld_maps_[i]; }
auto mutable_overworld_map(int i) { return &overworld_maps_[i]; }
@@ -507,8 +517,7 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
auto mutable_exits() { return &all_exits_; }
std::vector<gfx::Tile16> tiles16() const { return tiles16_; }
auto mutable_tiles16() { return &tiles16_; }
auto Sprites(int state) const { return all_sprites_[state]; }
auto sprites(int state) const { return all_sprites_[state]; }
auto mutable_sprites(int state) { return &all_sprites_[state]; }
auto current_graphics() const {
return overworld_maps_[current_map_].current_graphics();
@@ -519,13 +528,10 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
auto mutable_holes() { return &all_holes_; }
auto deleted_entrances() const { return deleted_entrances_; }
auto mutable_deleted_entrances() { return &deleted_entrances_; }
auto AreaPalette() const {
auto current_area_palette() const {
return overworld_maps_[current_map_].current_palette();
}
auto AreaPaletteById(int id) const {
return overworld_maps_[id].current_palette();
}
auto BitmapData() const {
auto current_map_bitmap_data() const {
return overworld_maps_[current_map_].bitmap_data();
}
auto tile16_blockset_data() const {
@@ -533,12 +539,10 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
}
auto is_loaded() const { return is_loaded_; }
void set_current_map(int i) { current_map_ = i; }
auto map_tiles() const { return map_tiles_; }
auto mutable_map_tiles() { return &map_tiles_; }
auto all_items() const { return all_items_; }
auto mutable_all_items() { return &all_items_; }
auto &ref_all_items() { return all_items_; }
auto all_tiles_types() const { return all_tiles_types_; }
auto mutable_all_tiles_types() { return &all_tiles_types_; }
@@ -562,12 +566,11 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
int &ttpos);
absl::Status DecompressAllMapTiles();
absl::Status DecompressProtoMapTiles(const std::string &filename);
bool is_loaded_ = false;
int game_state_ = 0;
int current_map_ = 0;
int current_world_ = 0;
uchar map_parent_[160];
Rom rom_;
@@ -600,7 +603,6 @@ class Overworld : public SharedRom, public core::ExperimentFlags {
std::vector<int> map_pointers2 = std::vector<int>(kNumOverworldMaps);
std::vector<absl::flat_hash_map<uint16_t, int>> usage_stats_;
absl::flat_hash_map<int, MapData> proto_map_data_;
};
} // namespace overworld