Refactor LC_LZ2 and expand GraphicsEditor

Add CompressV2 and DecompressV2
Add PortablePalette to PaletteEditor
This commit is contained in:
scawful
2023-07-22 15:24:34 -04:00
parent 82dd9dde1b
commit 01802d1a73
9 changed files with 564 additions and 27 deletions

View File

@@ -9,6 +9,7 @@
#include "absl/status/statusor.h"
#include "app/editor/palette_editor.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/compression.h"
#include "app/gfx/snes_tile.h"
#include "app/gui/canvas.h"
#include "app/gui/input.h"
@@ -19,9 +20,10 @@ namespace app {
namespace editor {
absl::Status GraphicsEditor::Update() {
BEGIN_TABLE("#gfxEditTable", 2, gfx_edit_flags)
BEGIN_TABLE("#gfxEditTable", 3, gfx_edit_flags)
SETUP_COLUMN("Graphics Manager")
SETUP_COLUMN("Memory Editor")
SETUP_COLUMN("Preview")
TABLE_HEADERS()
NEXT_COLUMN()
@@ -33,13 +35,14 @@ absl::Status GraphicsEditor::Update() {
RETURN_IF_ERROR(DrawClipboardImport())
END_TAB_ITEM()
END_TAB_BAR()
ImGui::Separator();
ImGui::Text("Graphics");
ImGui::Separator();
RETURN_IF_ERROR(DrawDecompressedData())
RETURN_IF_ERROR(DrawPaletteControls())
NEXT_COLUMN()
RETURN_IF_ERROR(DrawMemoryEditor())
NEXT_COLUMN()
RETURN_IF_ERROR(DrawDecompressedData())
END_TABLE()
return absl::OkStatus();
@@ -95,6 +98,45 @@ absl::Status GraphicsEditor::DrawFileImport() {
return absl::OkStatus();
}
absl::Status GraphicsEditor::DrawPaletteControls() {
ImGui::Separator();
ImGui::Text("Palette");
ImGui::Separator();
ImGui::Combo("Palette", &current_palette_, kPaletteGroupAddressesKeys,
IM_ARRAYSIZE(kPaletteGroupAddressesKeys));
ImGui::InputText("COL File", col_file_path_, sizeof(col_file_path_));
ImGui::SameLine();
// Open the file dialog when the user clicks the "Browse" button
if (ImGui::Button("Browse")) {
ImGuiFileDialog::Instance()->OpenDialog("ImportColKey", "Choose File",
".col\0", ".");
}
if (ImGuiFileDialog::Instance()->Display("ImportColKey")) {
if (ImGuiFileDialog::Instance()->IsOk()) {
strncpy(col_file_path_,
ImGuiFileDialog::Instance()->GetFilePathName().c_str(),
sizeof(col_file_path_));
RETURN_IF_ERROR(temp_rom_.LoadFromFile(col_file_path_, /*z3_load=*/false))
auto col_data_ = gfx::GetColFileData(temp_rom_.data());
col_file_palette_ = gfx::SNESPalette(col_data_);
col_file_ = true;
is_open_ = true;
}
// Close the modal
ImGuiFileDialog::Instance()->Close();
}
if (col_file_palette_.size() != 0) {
palette_editor_.DrawPortablePalette(col_file_palette_);
}
return absl::OkStatus();
}
absl::Status GraphicsEditor::DrawClipboardImport() {
static Bytes clipboard_data;
@@ -114,7 +156,6 @@ absl::Status GraphicsEditor::DrawMemoryEditor() {
std::string title = "Memory Editor";
if (is_open_) {
static MemoryEditor mem_edit;
// mem_edit.DrawWindow(title.data(), (void *)&temp_rom_, temp_rom_.size());
mem_edit.DrawContents(temp_rom_.data(), temp_rom_.size());
}
return absl::OkStatus();
@@ -136,17 +177,22 @@ absl::Status GraphicsEditor::DrawDecompressedData() {
}
absl::Status GraphicsEditor::DecompressImportData(int size) {
ASSIGN_OR_RETURN(import_data_, temp_rom_.Decompress(current_offset_, size))
ASSIGN_OR_RETURN(import_data_, gfx::lc_lz2::DecompressV2(
temp_rom_.data(), current_offset_, size))
std::cout << "Size of import data " << import_data_.size() << std::endl;
Bytes new_sheet;
auto converted_sheet = gfx::SnesTo8bppSheet(import_data_, 3);
bitmap_.Create(core::kTilesheetWidth, 0x2000, core::kTilesheetDepth,
import_data_.data(), size);
converted_sheet.data(), size);
if (rom_.isLoaded()) {
auto palette_group = rom_.GetPaletteGroup("ow_main");
palette_ = palette_group.palettes[current_palette_];
bitmap_.ApplyPalette(palette_);
if (col_file_) {
bitmap_.ApplyPalette(col_file_palette_);
} else {
bitmap_.ApplyPalette(palette_);
}
}
rom_.RenderBitmap(&bitmap_);
@@ -161,7 +207,6 @@ absl::Status GraphicsEditor::DecompressSuperDonkey() {
std::stoi(offset, nullptr, 16); // convert hex string to int
ASSIGN_OR_RETURN(auto decompressed_data,
temp_rom_.Decompress(offset_value, 0x1000))
}
for (const auto& offset : kSuperDonkeySprites) {

View File

@@ -40,13 +40,21 @@ const std::string kSuperDonkeySprites[] = {
"BE115", "BE5C2", "BEB63", "BF0CB", "BF607", "BFA55", "BFD71", "C017D",
"C0567", "C0981", "C0BA7", "C116D", "C166A", "C1FE0", "C24CE", "C2B19"};
constexpr char* kPaletteGroupAddressesKeys[] = {
"ow_main", "ow_aux", "ow_animated", "hud",
"global_sprites", "armors", "swords", "shields",
"sprites_aux1", "sprites_aux2", "sprites_aux3", "dungeon_main",
"grass", "3d_object", "ow_mini_map",
};
class GraphicsEditor {
public:
absl::Status Update();
void SetupROM(ROM &rom) { rom_ = rom; }
void SetupROM(ROM& rom) { rom_ = rom; }
private:
absl::Status DrawFileImport();
absl::Status DrawPaletteControls();
absl::Status DrawClipboardImport();
absl::Status DrawMemoryEditor();
absl::Status DrawDecompressedData();
@@ -61,7 +69,9 @@ class GraphicsEditor {
bool gfx_loaded_ = false;
bool is_open_ = false;
bool super_donkey_ = false;
bool col_file_ = false;
char file_path_[256];
char col_file_path_[256];
ROM rom_;
ROM temp_rom_;
@@ -76,6 +86,7 @@ class GraphicsEditor {
MemoryEditor memory_editor_;
gfx::SNESPalette palette_;
gfx::SNESPalette col_file_palette_;
ImGuiTableFlags gfx_edit_flags = ImGuiTableFlags_Reorderable |
ImGuiTableFlags_Resizable |

View File

@@ -190,6 +190,49 @@ void PaletteEditor::DisplayPalette(gfx::SNESPalette& palette, bool loaded) {
}
}
void PaletteEditor::DrawPortablePalette(gfx::SNESPalette& palette) {
static bool init = false;
if (!init) {
for (int n = 0; n < palette.size_; n++) {
saved_palette_[n].x = palette.GetColor(n).rgb.x / 255;
saved_palette_[n].y = palette.GetColor(n).rgb.y / 255;
saved_palette_[n].z = palette.GetColor(n).rgb.z / 255;
saved_palette_[n].w = 255; // Alpha
}
init = true;
}
if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)3);
ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
ImGui::BeginGroup(); // Lock X position
ImGui::Text("Palette");
for (int n = 0; n < IM_ARRAYSIZE(saved_palette_); n++) {
ImGui::PushID(n);
if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
if (ImGui::ColorButton("##palette", saved_palette_[n],
palette_button_flags_2, ImVec2(20, 20)))
ImVec4(saved_palette_[n].x, saved_palette_[n].y, saved_palette_[n].z,
1.0f); // Preserve alpha!
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload =
ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F))
memcpy((float*)&saved_palette_[n], payload->Data, sizeof(float) * 3);
if (const ImGuiPayload* payload =
ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F))
memcpy((float*)&saved_palette_[n], payload->Data, sizeof(float) * 4);
ImGui::EndDragDropTarget();
}
ImGui::PopID();
}
ImGui::EndGroup();
}
ImGui::EndChild();
}
} // namespace editor
} // namespace app
} // namespace yaze

View File

@@ -31,6 +31,8 @@ class PaletteEditor {
absl::Status Update();
void DisplayPalette(gfx::SNESPalette& palette, bool loaded);
void DrawPortablePalette(gfx::SNESPalette& palette);
auto SetupROM(ROM& rom) { rom_ = rom; }
private: