Refactor overworld constructors to accept Rom pointers

This commit is contained in:
scawful
2025-04-12 11:27:23 -04:00
parent cb86c7b2ec
commit 42cfb3bcb2
10 changed files with 280 additions and 235 deletions

View File

@@ -78,6 +78,8 @@ class Editor {
virtual absl::Status Clear() { return absl::OkStatus(); }
virtual void CleanupUnusedTextures(uint64_t current_time, uint64_t timeout) { }
EditorType type() const { return type_; }
void set_context(EditorContext* context) { context_ = context; }

View File

@@ -300,6 +300,15 @@ absl::Status EditorManager::Update() {
ImGui::End();
}
}
static uint64_t last_cleanup_time = 0;
uint64_t current_time = SDL_GetTicks64();
// Clean up unused textures every 5 seconds
if (current_time - last_cleanup_time > 5000) {
current_editor_set_->CleanupUnusedTextures(current_time, 5000);
last_cleanup_time = current_time;
}
}
if (show_homepage_) {
@@ -481,6 +490,7 @@ absl::Status EditorManager::LoadAssets() {
return absl::FailedPreconditionError("No ROM or editor set loaded");
}
current_editor_set_->overworld_editor_.Initialize();
current_editor_set_->message_editor_.Initialize();
auto &sheet_manager = GraphicsSheetManager::GetInstance();
ASSIGN_OR_RETURN(*sheet_manager.mutable_gfx_sheets(),

View File

@@ -51,17 +51,17 @@ class EditorManager {
context_.popup_manager = popup_manager_.get();
}
void Initialize(const std::string &filename = "");
void Initialize(const std::string& filename = "");
absl::Status Update();
void DrawMenuBar();
auto emulator() -> emu::Emulator & { return emulator_; }
auto emulator() -> emu::Emulator& { return emulator_; }
auto quit() const { return quit_; }
auto version() const { return version_; }
absl::Status SetCurrentRom(Rom *rom);
auto GetRoms() -> std::vector<std::unique_ptr<Rom>> & { return roms_; }
auto GetCurrentRom() -> Rom * { return current_rom_; }
absl::Status SetCurrentRom(Rom* rom);
auto GetRoms() -> std::vector<std::unique_ptr<Rom>>& { return roms_; }
auto GetCurrentRom() -> Rom* { return current_rom_; }
auto GetCurrentEditorSet() -> EditorSet* { return current_editor_set_; }
private:
@@ -71,7 +71,7 @@ class EditorManager {
absl::Status LoadRom();
absl::Status LoadAssets();
absl::Status SaveRom();
absl::Status OpenRomOrProject(const std::string &filename);
absl::Status OpenRomOrProject(const std::string& filename);
absl::Status OpenProject();
absl::Status SaveProject();
@@ -110,38 +110,46 @@ class EditorManager {
* @brief Contains a complete set of editors for a single ROM instance
*/
class EditorSet {
public:
explicit EditorSet(Rom* rom)
: assembly_editor_(rom),
dungeon_editor_(rom),
graphics_editor_(rom),
music_editor_(rom),
overworld_editor_(*rom),
palette_editor_(rom),
screen_editor_(rom),
sprite_editor_(rom),
settings_editor_(rom),
message_editor_(rom),
memory_editor_(rom) {
active_editors_ = {&overworld_editor_, &dungeon_editor_, &graphics_editor_,
&palette_editor_, &sprite_editor_, &message_editor_,
&music_editor_, &screen_editor_, &settings_editor_,
&assembly_editor_};
}
public:
explicit EditorSet(Rom* rom = nullptr)
: assembly_editor_(rom),
dungeon_editor_(rom),
graphics_editor_(rom),
music_editor_(rom),
overworld_editor_(rom),
palette_editor_(rom),
screen_editor_(rom),
sprite_editor_(rom),
settings_editor_(rom),
message_editor_(rom),
memory_editor_(rom) {
active_editors_ = {&overworld_editor_, &dungeon_editor_, &graphics_editor_,
&palette_editor_, &sprite_editor_, &message_editor_,
&music_editor_, &screen_editor_, &settings_editor_,
&assembly_editor_};
}
AssemblyEditor assembly_editor_;
DungeonEditor dungeon_editor_;
GraphicsEditor graphics_editor_;
MusicEditor music_editor_;
OverworldEditor overworld_editor_;
PaletteEditor palette_editor_;
ScreenEditor screen_editor_;
SpriteEditor sprite_editor_;
SettingsEditor settings_editor_;
MessageEditor message_editor_;
MemoryEditorWithDiffChecker memory_editor_;
std::vector<Editor*> active_editors_;
AssemblyEditor assembly_editor_;
DungeonEditor dungeon_editor_;
GraphicsEditor graphics_editor_;
MusicEditor music_editor_;
OverworldEditor overworld_editor_;
PaletteEditor palette_editor_;
ScreenEditor screen_editor_;
SpriteEditor sprite_editor_;
SettingsEditor settings_editor_;
MessageEditor message_editor_;
MemoryEditorWithDiffChecker memory_editor_;
std::vector<Editor*> active_editors_;
void CleanupUnusedTextures(uint64_t current_time, uint64_t timeout) {
if (active_editors_.size() > 0) {
for (auto editor : active_editors_) {
editor->CleanupUnusedTextures(current_time, timeout);
}
}
}
};
} // namespace editor

View File

@@ -32,30 +32,7 @@ namespace yaze {
namespace editor {
using core::Renderer;
using ImGui::BeginChild;
using ImGui::BeginTabBar;
using ImGui::BeginTabItem;
using ImGui::BeginTable;
using ImGui::BeginTooltip;
using ImGui::Button;
using ImGui::Checkbox;
using ImGui::EndChild;
using ImGui::EndTabBar;
using ImGui::EndTabItem;
using ImGui::EndTable;
using ImGui::EndTooltip;
using ImGui::IsItemHovered;
using ImGui::NewLine;
using ImGui::PopStyleColor;
using ImGui::PushStyleColor;
using ImGui::SameLine;
using ImGui::Selectable;
using ImGui::Separator;
using ImGui::TableHeadersRow;
using ImGui::TableNextColumn;
using ImGui::TableNextRow;
using ImGui::TableSetupColumn;
using ImGui::Text;
using namespace ImGui;
constexpr int kTile16Size = 0x10;
@@ -70,19 +47,19 @@ void OverworldEditor::Initialize() {
gui::zeml::Bind(&*layout_node_.GetNode("OverworldTileSelector"),
[this]() { status_ = DrawTileSelector(); });
gui::zeml::Bind(&*layout_node_.GetNode("OwUsageStats"), [this]() {
if (rom_.is_loaded()) {
if (rom_->is_loaded()) {
status_ = UpdateUsageStats();
}
});
gui::zeml::Bind(&*layout_node_.GetNode("owToolset"),
[this]() { DrawToolset(); });
gui::zeml::Bind(&*layout_node_.GetNode("OwTile16Editor"), [this]() {
if (rom_.is_loaded()) {
if (rom_->is_loaded()) {
status_ = tile16_editor_.Update();
}
});
gui::zeml::Bind(&*layout_node_.GetNode("OwGfxGroupEditor"), [this]() {
if (rom_.is_loaded()) {
if (rom_->is_loaded()) {
status_ = gfx_group_editor_.Update();
}
});
@@ -1072,7 +1049,7 @@ absl::Status OverworldEditor::LoadGraphics() {
map_blockset_loaded_ = true;
// Copy the tile16 data into individual tiles.
auto tile16_data = overworld_.tile16_blockset_data();
auto tile16_blockset_data = overworld_.tile16_blockset_data();
util::logf("Loading overworld tile16 graphics.");
// Loop through the tiles and copy their pixel data into separate vectors
@@ -1084,9 +1061,9 @@ absl::Status OverworldEditor::LoadGraphics() {
for (int ty = 0; ty < kTile16Size; ty++) {
for (int tx = 0; tx < kTile16Size; tx++) {
int position = tx + (ty * kTile16Size);
uint8_t value =
tile16_data[(i % 8 * kTile16Size) + (i / 8 * kTile16Size * 0x80) +
(ty * 0x80) + tx];
uint8_t value = tile16_blockset_data[(i % 8 * kTile16Size) +
(i / 8 * kTile16Size * 0x80) +
(ty * 0x80) + tx];
tile16_individual_[i].mutable_data()[position] = value;
}
}
@@ -1400,7 +1377,7 @@ absl::Status OverworldEditor::UpdateUsageStats() {
if (BeginChild("UnusedSpritesetScroll", ImVec2(0, 0), true,
ImGuiWindowFlags_HorizontalScrollbar)) {
for (int i = 0; i < 0x81; i++) {
auto entrance_name = rom_.resource_label()->CreateOrGetLabel(
auto entrance_name = rom_->resource_label()->CreateOrGetLabel(
"Dungeon Entrance Names", util::HexByte(i),
zelda3::kEntranceNames[i].data());
std::string str = absl::StrFormat("%#x - %s", i, entrance_name);
@@ -1516,5 +1493,34 @@ void OverworldEditor::DrawDebugWindow() {
}
}
absl::Status OverworldEditor::Clear() {
overworld_.Destroy();
current_graphics_set_.clear();
for (auto &bmp : tile16_individual_) {
bmp.Clear();
}
for (auto &bmp : maps_bmp_) {
bmp.Clear();
}
for (auto &bmp : sprite_previews_) {
bmp.Clear();
}
tile16_blockset_bmp_.Clear();
current_gfx_bmp_.Clear();
all_gfx_loaded_ = false;
map_blockset_loaded_ = false;
return absl::OkStatus();
}
void OverworldEditor::CleanupUnusedTextures(uint64_t current_time,
uint64_t timeout) {
for (auto &bmp : tile16_individual_) {
bmp.CleanupUnusedTexture(current_time, timeout);
}
for (auto &bmp : maps_bmp_) {
bmp.CleanupUnusedTexture(current_time, timeout);
}
}
} // namespace editor
} // namespace yaze

View File

@@ -74,7 +74,7 @@ constexpr absl::string_view kOWMapTable = "#MapSettingsTable";
*/
class OverworldEditor : public Editor, public gfx::GfxContext {
public:
OverworldEditor(Rom& rom) : rom_(rom) { type_ = EditorType::kOverworld; }
explicit OverworldEditor(Rom* rom) : rom_(rom) { type_ = EditorType::kOverworld; }
void Initialize() override;
absl::Status Load() override;
@@ -215,7 +215,7 @@ class OverworldEditor : public Editor, public gfx::GfxContext {
std::vector<std::vector<uint8_t>> tile8_individual_data_;
std::vector<gfx::Bitmap> tile8_individual_;
Rom& rom_;
Rom* rom_;
Tile16Editor tile16_editor_{tile16_individual_};
GfxGroupEditor gfx_group_editor_;