From 09d76f5f5d23ac26ed1c66942dda94ae675656d1 Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Tue, 1 Aug 2023 22:02:58 -0400 Subject: [PATCH] Cgx to gfx::Bitmap pipeline in GraphicsEditor --- src/app/editor/graphics_editor.cc | 119 +++++++++++++++++++++++++----- src/app/editor/graphics_editor.h | 16 ++++ src/app/rom.h | 3 +- src/app/viewer/cgx_viewer.cc | 13 +--- src/app/viewer/cgx_viewer.h | 3 +- 5 files changed, 126 insertions(+), 28 deletions(-) diff --git a/src/app/editor/graphics_editor.cc b/src/app/editor/graphics_editor.cc index 095ad307..a1f9b541 100644 --- a/src/app/editor/graphics_editor.cc +++ b/src/app/editor/graphics_editor.cc @@ -21,23 +21,33 @@ namespace app { namespace editor { absl::Status GraphicsEditor::Update() { + RETURN_IF_ERROR(DrawToolset()) + + if (open_memory_editor_) { + ImGui::Begin("Memory Editor", &open_memory_editor_); + RETURN_IF_ERROR(DrawMemoryEditor()) + ImGui::End(); + } + BEGIN_TABLE("#gfxEditTable", 3, kGfxEditFlags) - SETUP_COLUMN("Graphics Manager") - SETUP_COLUMN("Memory Editor") + SETUP_COLUMN("Graphics (BIN, CGX, SCR)") + SETUP_COLUMN("Palette (COL)") SETUP_COLUMN("Preview") TABLE_HEADERS() NEXT_COLUMN() + status_ = DrawCgxImport(); status_ = DrawFileImport(); status_ = DrawExperimentalFeatures(); - status_ = DrawPaletteControls(); NEXT_COLUMN() - RETURN_IF_ERROR(DrawMemoryEditor()) + status_ = DrawPaletteControls(); NEXT_COLUMN() if (super_donkey_) { status_ = DrawGraphicsBin(); + } else if (cgx_loaded_ && col_file_) { + status_ = DrawCgxViewer(); } else { status_ = DrawDecompressedData(); } @@ -47,17 +57,72 @@ absl::Status GraphicsEditor::Update() { return absl::OkStatus(); } +absl::Status GraphicsEditor::DrawToolset() { + if (ImGui::BeginTable("GraphicsToolset", 2, ImGuiTableFlags_SizingFixedFit, + ImVec2(0, 0))) { + for (const auto& name : kGfxToolsetColumnNames) + ImGui::TableSetupColumn(name.data()); + + ImGui::TableNextColumn(); + if (ImGui::Button(ICON_MD_MEMORY)) { + open_memory_editor_ = true; + } + + TEXT_COLUMN("Open Memory Editor") // Separator + + ImGui::EndTable(); + } + return absl::OkStatus(); +} + +absl::Status GraphicsEditor::DrawCgxImport() { + gui::TextWithSeparators("Cgx Import"); + + ImGui::InputText("##CGXFile", cgx_file_name_, sizeof(cgx_file_name_)); + ImGui::SameLine(); + // Open the file dialog when the user clicks the "Browse" button + if (ImGui::Button("Open CGX")) { + ImGuiFileDialog::Instance()->OpenDialog("ImportCgxKey", "Choose File", + ".CGX,.cgx\0", "."); + } + + if (ImGuiFileDialog::Instance()->Display("ImportCgxKey")) { + if (ImGuiFileDialog::Instance()->IsOk()) { + strncpy(cgx_file_path_, + ImGuiFileDialog::Instance()->GetFilePathName().c_str(), + sizeof(cgx_file_path_)); + strncpy(cgx_file_name_, + ImGuiFileDialog::Instance()->GetCurrentFileName().c_str(), + sizeof(cgx_file_name_)); + RETURN_IF_ERROR(temp_rom_.LoadFromFile(cgx_file_path_, /*z3_load=*/false)) + cgx_viewer_.LoadCgx(temp_rom_); + auto all_tiles_data = cgx_viewer_.GetCgxData(); + cgx_bitmap_.Create(core::kTilesheetWidth, 8192, core::kTilesheetDepth, + all_tiles_data.data(), all_tiles_data.size()); + rom_.RenderBitmap(&cgx_bitmap_); + is_open_ = true; + cgx_loaded_ = true; + } + ImGuiFileDialog::Instance()->Close(); + } + + return absl::OkStatus(); +} + absl::Status GraphicsEditor::DrawFileImport() { static int size = 0; - gui::TextWithSeparators("File Import"); + gui::TextWithSeparators("Clipboard Import"); + RETURN_IF_ERROR(DrawClipboardImport()) + + gui::TextWithSeparators("BIN Import"); ImGui::InputText("##ROMFile", file_path_, sizeof(file_path_)); ImGui::SameLine(); // Open the file dialog when the user clicks the "Browse" button if (ImGui::Button("Open BIN")) { ImGuiFileDialog::Instance()->OpenDialog("ImportDlgKey", "Choose File", - ".bin\0.hex\0", "."); + ".bin,.hex\0", "."); } // Draw the file dialog @@ -78,7 +143,7 @@ absl::Status GraphicsEditor::DrawFileImport() { gui::InputHex("Offset", ¤t_offset_); gui::InputHex("Size ", &size); - if (ImGui::Button("Import")) { + if (ImGui::Button("BIN Import")) { if (strlen(file_path_) > 0) { RETURN_IF_ERROR(DecompressImportData(size)) } else { @@ -87,25 +152,17 @@ absl::Status GraphicsEditor::DrawFileImport() { } } - gui::TextWithSeparators("Clipboard Import"); - RETURN_IF_ERROR(DrawClipboardImport()) - return absl::OkStatus(); } absl::Status GraphicsEditor::DrawPaletteControls() { - gui::TextWithSeparators("Palette"); - gui::InputHex("Palette Index", ¤t_palette_index_); - - ImGui::Combo("Palette", ¤t_palette_, kPaletteGroupAddressesKeys, - IM_ARRAYSIZE(kPaletteGroupAddressesKeys)); - + gui::TextWithSeparators("COL Import"); ImGui::InputText("##ColFile", col_file_name_, sizeof(col_file_name_)); ImGui::SameLine(); if (ImGui::Button("Open COL")) { ImGuiFileDialog::Instance()->OpenDialog("ImportColKey", "Choose File", - ".col", "."); + ".COL,.col", "."); } if (ImGuiFileDialog::Instance()->Display("ImportColKey")) { @@ -121,10 +178,22 @@ absl::Status GraphicsEditor::DrawPaletteControls() { col_file_palette_ = gfx::SNESPalette(col_data_); col_file_ = true; is_open_ = true; + + if (cgx_loaded_) { + cgx_bitmap_.ApplyPalette(col_file_palette_); + rom_.RenderBitmap(&cgx_bitmap_); + } } ImGuiFileDialog::Instance()->Close(); } + if (rom_.isLoaded()) { + gui::TextWithSeparators("ROM Palette"); + gui::InputHex("Palette Index", ¤t_palette_index_); + ImGui::Combo("Palette", ¤t_palette_, kPaletteGroupAddressesKeys, + IM_ARRAYSIZE(kPaletteGroupAddressesKeys)); + } + if (col_file_palette_.size() != 0) { ImGuiFileDialog::Instance()->prDrawFileListView(ImVec2(0, 200)); palette_editor_.DrawPortablePalette(col_file_palette_); @@ -143,6 +212,7 @@ absl::Status GraphicsEditor::DrawClipboardImport() { ImGui::MemFree((void*)text); RETURN_IF_ERROR(temp_rom_.LoadFromBytes(clipboard_data)) is_open_ = true; + open_memory_editor_ = true; } } @@ -171,6 +241,21 @@ absl::Status GraphicsEditor::DrawMemoryEditor() { return absl::OkStatus(); } +absl::Status GraphicsEditor::DrawCgxViewer() { + if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)5); + ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true, + ImGuiWindowFlags_AlwaysVerticalScrollbar)) { + import_canvas_.DrawBackground(ImVec2(0x100 + 1, (8192 * 2) + 1)); + import_canvas_.DrawContextMenu(); + import_canvas_.DrawBitmap(cgx_bitmap_, 2, cgx_loaded_); + import_canvas_.DrawTileSelector(32); + import_canvas_.DrawGrid(32.0f); + import_canvas_.DrawOverlay(); + } + ImGui::EndChild(); + return absl::OkStatus(); +} + absl::Status GraphicsEditor::DrawDecompressedData() { if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)2); ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true, diff --git a/src/app/editor/graphics_editor.h b/src/app/editor/graphics_editor.h index 3e61a1c4..b7b2a63a 100644 --- a/src/app/editor/graphics_editor.h +++ b/src/app/editor/graphics_editor.h @@ -48,6 +48,11 @@ constexpr char* kPaletteGroupAddressesKeys[] = { "grass", "3d_object", "ow_mini_map", }; +static constexpr absl::string_view kGfxToolsetColumnNames[] = { + "#memoryEditor", + "##separator_gfx1", +}; + constexpr ImGuiTableFlags kGfxEditFlags = ImGuiTableFlags_Reorderable | ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingStretchSame; @@ -58,11 +63,14 @@ class GraphicsEditor { void SetupROM(ROM& rom) { rom_ = rom; } private: + absl::Status DrawToolset(); + absl::Status DrawCgxImport(); absl::Status DrawFileImport(); absl::Status DrawPaletteControls(); absl::Status DrawClipboardImport(); absl::Status DrawExperimentalFeatures(); absl::Status DrawMemoryEditor(); + absl::Status DrawCgxViewer(); absl::Status DrawDecompressedData(); absl::Status DrawGraphicsBin(); @@ -75,14 +83,20 @@ class GraphicsEditor { int current_palette_ = 0; int current_palette_index_ = 0; int num_sheets_to_load_ = 1; + bool open_memory_editor_ = false; bool gfx_loaded_ = false; bool is_open_ = false; bool super_donkey_ = false; bool col_file_ = false; + bool cgx_loaded_ = false; + char file_path_[256] = ""; char col_file_path_[256] = ""; char col_file_name_[256] = ""; + char cgx_file_path_[256] = ""; + char cgx_file_name_[256] = ""; + ROM rom_; ROM temp_rom_; Bytes import_data_; @@ -91,6 +105,8 @@ class GraphicsEditor { MemoryEditor memory_editor_; PaletteEditor palette_editor_; + gfx::Bitmap cgx_bitmap_; + gfx::Bitmap bitmap_; gui::Canvas import_canvas_; diff --git a/src/app/rom.h b/src/app/rom.h index 2018124a..96022139 100644 --- a/src/app/rom.h +++ b/src/app/rom.h @@ -131,8 +131,9 @@ class ROM { rom_data_.reserve(n_bytes); rom_data_.resize(n_bytes); for (int i = 0; i < n_bytes; i++) { - rom_data_[i] = 0x00; + rom_data_.push_back(0x00); } + size_ = n_bytes; } uchar& operator[](int i) { diff --git a/src/app/viewer/cgx_viewer.cc b/src/app/viewer/cgx_viewer.cc index d8722c31..506ba856 100644 --- a/src/app/viewer/cgx_viewer.cc +++ b/src/app/viewer/cgx_viewer.cc @@ -19,26 +19,21 @@ void CgxViewer::Update() { LoadScr(); } -void CgxViewer::LoadCgx(std::string pathfile) { +void CgxViewer::LoadCgx(ROM& cgx_rom) { raw_data_.malloc(0x40000); all_tiles_data_.malloc(0x40000); - std::ifstream fs(pathfile, std::ios::binary); - std::vector data((std::istreambuf_iterator(fs)), - std::istreambuf_iterator()); - fs.close(); - std::vector matched_bytes; int matching_position = -1; bool matched = false; - for (int i = 0; i < data.size(); i++) { + for (int i = 0; i < cgx_rom.size(); i++) { if (matched) { break; } - raw_data_[i] = data[i]; + raw_data_[i] = cgx_rom[i]; for (int j = 0; j < matched_bytes.size(); j++) { - if (data[i + j] == matched_bytes[j]) { + if (cgx_rom[i + j] == matched_bytes[j]) { if (j == matched_bytes.size() - 1) { matching_position = i; matched = true; diff --git a/src/app/viewer/cgx_viewer.h b/src/app/viewer/cgx_viewer.h index 912f8f91..7fd83f76 100644 --- a/src/app/viewer/cgx_viewer.h +++ b/src/app/viewer/cgx_viewer.h @@ -16,9 +16,10 @@ namespace viewer { class CgxViewer { public: void Update(); + auto GetCgxData() const { return all_tiles_data_; } + void LoadCgx(ROM&); private: - void LoadCgx(std::string pathfile); void LoadGfx(int comboBpp); void LoadScr();