Refactor graphics handling to utilize Arena for graphics sheets management

Update various editors to replace instances of GraphicsSheetManager with gfx::Arena for accessing graphics sheets. This change enhances memory management and performance by centralizing graphics data handling within the Arena class. Clean up related code for improved clarity and maintainability.
This commit is contained in:
scawful
2025-05-02 12:06:46 -04:00
parent 4f8d765c48
commit d6551f84d2
7 changed files with 35 additions and 58 deletions

View File

@@ -15,6 +15,7 @@
#include "app/editor/overworld/overworld_editor.h"
#include "app/editor/sprite/sprite_editor.h"
#include "app/emu/emulator.h"
#include "app/gfx/arena.h"
#include "app/gui/icons.h"
#include "app/gui/input.h"
#include "app/gui/style.h"
@@ -548,7 +549,7 @@ absl::Status EditorManager::LoadAssets() {
current_editor_set_->overworld_editor_.Initialize();
current_editor_set_->message_editor_.Initialize();
auto &sheet_manager = GraphicsSheetManager::GetInstance();
auto &sheet_manager = gfx::Arena::Get();
ASSIGN_OR_RETURN(*sheet_manager.mutable_gfx_sheets(),
LoadAllGraphicsData(*current_rom_));
for (auto &editor : current_editor_set_->active_editors_) {
@@ -569,8 +570,8 @@ absl::Status EditorManager::SaveRom() {
RETURN_IF_ERROR(current_editor_set_->overworld_editor_.Save());
if (core::FeatureFlags::get().kSaveGraphicsSheet)
RETURN_IF_ERROR(SaveAllGraphicsData(
*current_rom_, GraphicsSheetManager::GetInstance().gfx_sheets()));
RETURN_IF_ERROR(
SaveAllGraphicsData(*current_rom_, gfx::Arena::Get().gfx_sheets()));
return current_rom_->SaveToFile(backup_rom_, save_new_auto_);
}

View File

@@ -2,6 +2,7 @@
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "app/gfx/arena.h"
#include "app/gfx/snes_palette.h"
#include "app/gui/canvas.h"
#include "app/gui/color.h"
@@ -111,9 +112,7 @@ void GfxGroupEditor::DrawBlocksetViewer(bool sheet_only) {
BeginGroup();
for (int i = 0; i < 8; i++) {
int sheet_id = rom()->main_blockset_ids[selected_blockset_][i];
auto &sheet =
GraphicsSheetManager::GetInstance().mutable_gfx_sheets()->at(
sheet_id);
auto &sheet = gfx::Arena::Get().mutable_gfx_sheets()->at(sheet_id);
gui::BitmapCanvasPipeline(blockset_canvas_, sheet, 256, 0x10 * 0x04,
0x20, true, false, 22);
}
@@ -166,9 +165,7 @@ void GfxGroupEditor::DrawRoomsetViewer() {
BeginGroup();
for (int i = 0; i < 4; i++) {
int sheet_id = rom()->room_blockset_ids[selected_roomset_][i];
auto &sheet =
GraphicsSheetManager::GetInstance().mutable_gfx_sheets()->at(
sheet_id);
auto &sheet = gfx::Arena::Get().mutable_gfx_sheets()->at(sheet_id);
gui::BitmapCanvasPipeline(roomset_canvas_, sheet, 256, 0x10 * 0x04,
0x20, true, false, 23);
}
@@ -207,8 +204,7 @@ void GfxGroupEditor::DrawSpritesetViewer(bool sheet_only) {
for (int i = 0; i < 4; i++) {
int sheet_id = rom()->spriteset_ids[selected_spriteset_][i];
auto &sheet =
GraphicsSheetManager::GetInstance().mutable_gfx_sheets()->at(
115 + sheet_id);
gfx::Arena::Get().mutable_gfx_sheets()->at(115 + sheet_id);
gui::BitmapCanvasPipeline(spriteset_canvas_, sheet, 256, 0x10 * 0x04,
0x20, true, false, 24);
}

View File

@@ -8,6 +8,7 @@
#include "app/core/platform/clipboard.h"
#include "app/core/platform/file_dialog.h"
#include "app/core/platform/renderer.h"
#include "app/gfx/arena.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/compression.h"
#include "app/gfx/scad_format.h"
@@ -50,10 +51,9 @@ absl::Status GraphicsEditor::Update() {
status_ = UpdateGfxEdit();
TAB_ITEM("Sheet Browser")
if (asset_browser_.Initialized == false) {
asset_browser_.Initialize(
GraphicsSheetManager::GetInstance().gfx_sheets());
asset_browser_.Initialize(gfx::Arena::Get().gfx_sheets());
}
asset_browser_.Draw(GraphicsSheetManager::GetInstance().gfx_sheets());
asset_browser_.Draw(gfx::Arena::Get().gfx_sheets());
END_TAB_ITEM()
status_ = UpdateScadView();
status_ = UpdateLinkGfxView();
@@ -121,10 +121,8 @@ void GraphicsEditor::DrawGfxEditToolset() {
TableNextColumn();
if (Button(ICON_MD_CONTENT_COPY)) {
std::vector<uint8_t> png_data = GraphicsSheetManager::GetInstance()
.gfx_sheets()
.at(current_sheet_)
.GetPngData();
std::vector<uint8_t> png_data =
gfx::Arena::Get().gfx_sheets().at(current_sheet_).GetPngData();
core::CopyImageToClipboard(png_data);
}
HOVER_HINT("Copy to Clipboard");
@@ -135,13 +133,12 @@ void GraphicsEditor::DrawGfxEditToolset() {
int width, height;
core::GetImageFromClipboard(png_data, width, height);
if (png_data.size() > 0) {
GraphicsSheetManager::GetInstance()
gfx::Arena::Get()
.mutable_gfx_sheets()
->at(current_sheet_)
.Create(width, height, 8, png_data);
Renderer::GetInstance().UpdateBitmap(
&GraphicsSheetManager::GetInstance().mutable_gfx_sheets()->at(
current_sheet_));
&gfx::Arena::Get().mutable_gfx_sheets()->at(current_sheet_));
}
}
HOVER_HINT("Paste from Clipboard");
@@ -161,8 +158,7 @@ void GraphicsEditor::DrawGfxEditToolset() {
}
TableNextColumn();
auto bitmap =
GraphicsSheetManager::GetInstance().gfx_sheets()[current_sheet_];
auto bitmap = gfx::Arena::Get().gfx_sheets()[current_sheet_];
auto palette = bitmap.palette();
for (int i = 0; i < palette.size(); i++) {
ImGui::SameLine();
@@ -201,7 +197,7 @@ absl::Status GraphicsEditor::UpdateGfxSheetList() {
(int)ms_io->RangeSrcItem); // Ensure RangeSrc item is not clipped.
int key = 0;
for (auto& value : GraphicsSheetManager::GetInstance().gfx_sheets()) {
for (auto& value : gfx::Arena::Get().gfx_sheets()) {
ImGui::BeginChild(absl::StrFormat("##GfxSheet%02X", key).c_str(),
ImVec2(0x100 + 1, 0x40 + 1), true,
ImGuiWindowFlags_NoDecoration);
@@ -291,8 +287,7 @@ absl::Status GraphicsEditor::UpdateGfxTabView() {
ImGuiWindowFlags_AlwaysHorizontalScrollbar);
gfx::Bitmap& current_bitmap =
GraphicsSheetManager::GetInstance().mutable_gfx_sheets()->at(
sheet_id);
gfx::Arena::Get().mutable_gfx_sheets()->at(sheet_id);
auto draw_tile_event = [&]() {
current_sheet_canvas_.DrawTileOnBitmap(tile_size_, &current_bitmap,
@@ -301,8 +296,7 @@ absl::Status GraphicsEditor::UpdateGfxTabView() {
};
current_sheet_canvas_.UpdateColorPainter(
GraphicsSheetManager::GetInstance().mutable_gfx_sheets()->at(
sheet_id),
gfx::Arena::Get().mutable_gfx_sheets()->at(sheet_id),
current_color_, draw_tile_event, tile_size_, current_scale_);
ImGui::EndChild();
@@ -335,8 +329,7 @@ absl::Status GraphicsEditor::UpdateGfxTabView() {
current_sheet_ = id;
// ImVec2(0x100, 0x40),
current_sheet_canvas_.UpdateColorPainter(
GraphicsSheetManager::GetInstance().mutable_gfx_sheets()->at(id),
current_color_,
gfx::Arena::Get().mutable_gfx_sheets()->at(id), current_color_,
[&]() {
},
@@ -372,13 +365,12 @@ absl::Status GraphicsEditor::UpdatePaletteColumn() {
palette);
if (refresh_graphics_ && !open_sheets_.empty()) {
GraphicsSheetManager::GetInstance()
gfx::Arena::Get()
.mutable_gfx_sheets()
->data()[current_sheet_]
.SetPaletteWithTransparent(palette, edit_palette_sub_index_);
Renderer::GetInstance().UpdateBitmap(&GraphicsSheetManager::GetInstance()
.mutable_gfx_sheets()
->data()[current_sheet_]);
Renderer::GetInstance().UpdateBitmap(
&gfx::Arena::Get().mutable_gfx_sheets()->data()[current_sheet_]);
refresh_graphics_ = false;
}
}

View File

@@ -12,6 +12,7 @@
#include "app/core/platform/renderer.h"
#include "app/editor/graphics/palette_editor.h"
#include "app/editor/overworld/entity.h"
#include "app/gfx/arena.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/tilemap.h"
@@ -717,7 +718,7 @@ void OverworldEditor::DrawTile8Selector() {
graphics_bin_canvas_.DrawContextMenu();
if (all_gfx_loaded_) {
int key = 0;
for (auto &value : GraphicsSheetManager::GetInstance().gfx_sheets()) {
for (auto &value : gfx::Arena::Get().gfx_sheets()) {
int offset = 0x40 * (key + 1);
int top_left_y = graphics_bin_canvas_.zero_point().y + 2;
if (key >= 1) {

View File

@@ -2,6 +2,7 @@
#include "app/core/platform/file_dialog.h"
#include "app/editor/sprite/zsprite.h"
#include "app/gfx/arena.h"
#include "app/gui/icons.h"
#include "app/gui/input.h"
#include "app/zelda3/sprite/sprite.h"
@@ -180,9 +181,8 @@ void SpriteEditor::DrawCurrentSheets() {
graphics_sheet_canvas_.DrawTileSelector(32);
for (int i = 0; i < 8; i++) {
graphics_sheet_canvas_.DrawBitmap(
GraphicsSheetManager::GetInstance().gfx_sheets().at(
current_sheets_[i]),
1, (i * 0x40) + 1, 2);
gfx::Arena::Get().gfx_sheets().at(current_sheets_[i]), 1,
(i * 0x40) + 1, 2);
}
graphics_sheet_canvas_.DrawGrid();
graphics_sheet_canvas_.DrawOverlay();

View File

@@ -26,6 +26,11 @@ class Arena {
SDL_Surface* AllocateSurface(int width, int height, int depth, int format);
void FreeSurface(SDL_Surface* surface);
std::array<gfx::Bitmap, 223>& gfx_sheets() { return gfx_sheets_; }
auto gfx_sheet(int i) { return gfx_sheets_[i]; }
auto mutable_gfx_sheet(int i) { return &gfx_sheets_[i]; }
auto mutable_gfx_sheets() { return &gfx_sheets_; }
auto& bg1() { return bg1_; }
auto& bg2() { return bg2_; }
@@ -42,6 +47,8 @@ class Arena {
std::array<uint16_t, kTotalTiles> layer1_buffer_;
std::array<uint16_t, kTotalTiles> layer2_buffer_;
std::array<gfx::Bitmap, 223> gfx_sheets_;
std::unordered_map<SDL_Texture*,
std::unique_ptr<SDL_Texture, core::SDL_Texture_Deleter>>
textures_;

View File

@@ -1,30 +1,10 @@
#ifndef YAZE_SNES_H_
#define YAZE_SNES_H_
#include <array>
#include <cstdint>
#include "app/gfx/bitmap.h"
namespace yaze {
class GraphicsSheetManager {
public:
static GraphicsSheetManager& GetInstance() {
static GraphicsSheetManager instance;
return instance;
}
GraphicsSheetManager() = default;
virtual ~GraphicsSheetManager() = default;
std::array<gfx::Bitmap, 223>& gfx_sheets() { return gfx_sheets_; }
auto gfx_sheet(int i) { return gfx_sheets_[i]; }
auto mutable_gfx_sheet(int i) { return &gfx_sheets_[i]; }
auto mutable_gfx_sheets() { return &gfx_sheets_; }
private:
std::array<gfx::Bitmap, 223> gfx_sheets_;
};
inline uint32_t SnesToPc(uint32_t addr) noexcept {
constexpr uint32_t kFastRomRegion = 0x808000;
if (addr >= kFastRomRegion) {