refactor Tile16Editor: improve LoadTile8 method with asynchronous tile data loading

This commit is contained in:
scawful
2025-01-01 15:53:42 -05:00
parent d711a84ed3
commit 4a250a35cb
2 changed files with 46 additions and 44 deletions

View File

@@ -1,19 +1,13 @@
#include "tile16_editor.h" #include "tile16_editor.h"
#include <cmath> #include <future>
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "app/core/platform/file_dialog.h" #include "app/core/platform/file_dialog.h"
#include "app/core/platform/renderer.h" #include "app/core/platform/renderer.h"
#include "app/editor/editor.h"
#include "app/editor/graphics/palette_editor.h"
#include "app/gfx/bitmap.h" #include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h" #include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"
#include "app/gfx/tilesheet.h"
#include "app/gui/canvas.h" #include "app/gui/canvas.h"
#include "app/gui/icons.h"
#include "app/gui/input.h" #include "app/gui/input.h"
#include "app/gui/style.h" #include "app/gui/style.h"
#include "app/rom.h" #include "app/rom.h"
@@ -33,7 +27,6 @@ using ImGui::BeginTabItem;
using ImGui::BeginTable; using ImGui::BeginTable;
using ImGui::Button; using ImGui::Button;
using ImGui::Checkbox; using ImGui::Checkbox;
using ImGui::Combo;
using ImGui::EndChild; using ImGui::EndChild;
using ImGui::EndMenu; using ImGui::EndMenu;
using ImGui::EndMenuBar; using ImGui::EndMenuBar;
@@ -280,16 +273,18 @@ absl::Status Tile16Editor::DrawTileEditControls() {
absl::Status Tile16Editor::LoadTile8() { absl::Status Tile16Editor::LoadTile8() {
auto ow_main_pal_group = rom()->palette_group().overworld_main; auto ow_main_pal_group = rom()->palette_group().overworld_main;
current_gfx_individual_.reserve(1024); current_gfx_individual_.reserve(1024);
for (int index = 0; index < 1024; index++) { std::vector<std::future<std::array<uint8_t, 0x40>>> futures;
std::vector<uint8_t> tile_data(0x40, 0x00);
for (int index = 0; index < 1024; index++) {
auto task_function = [&]() {
std::array<uint8_t, 0x40> tile_data;
// Copy the pixel data for the current tile into the vector // Copy the pixel data for the current tile into the vector
for (int ty = 0; ty < 8; ty++) { for (int ty = 0; ty < 8; ty++) {
for (int tx = 0; tx < 8; tx++) { for (int tx = 0; tx < 8; tx++) {
// Current Gfx Data is 16 sheets of 8x8 tiles ordered 16 wide by 4 tall // Current Gfx Data is 16 sheets of 8x8 tiles ordered 16 wide by 4
// tall
// Calculate the position in the tile data vector // Calculate the position in the tile data vector
int position = tx + (ty * 0x08); int position = tx + (ty * 0x08);
@@ -310,12 +305,20 @@ absl::Status Tile16Editor::LoadTile8() {
tile_data[position] = value; tile_data[position] = value;
} }
} }
return tile_data;
};
futures.emplace_back(std::async(std::launch::async, task_function));
}
for (auto &future : futures) {
future.wait();
auto tile_data = future.get();
current_gfx_individual_.emplace_back(); current_gfx_individual_.emplace_back();
current_gfx_individual_[index].Create(0x08, 0x08, 0x08, tile_data); auto &tile_bitmap = current_gfx_individual_.back();
RETURN_IF_ERROR(current_gfx_individual_[index].ApplyPaletteWithTransparent( tile_bitmap.Create(0x08, 0x08, 0x08, tile_data);
RETURN_IF_ERROR(tile_bitmap.ApplyPaletteWithTransparent(
ow_main_pal_group[0], current_palette_)); ow_main_pal_group[0], current_palette_));
Renderer::GetInstance().RenderBitmap(&current_gfx_individual_[index]); Renderer::GetInstance().RenderBitmap(&tile_bitmap);
} }
map_blockset_loaded_ = true; map_blockset_loaded_ = true;

View File

@@ -48,7 +48,11 @@ class Tile16Editor : public gfx::GfxContext, public SharedRom {
bool map_blockset_loaded_ = false; bool map_blockset_loaded_ = false;
bool transfer_started_ = false; bool transfer_started_ = false;
bool transfer_blockset_loaded_ = false; bool transfer_blockset_loaded_ = false;
bool x_flip = false;
bool y_flip = false;
bool priority_tile = false;
int tile_size;
int current_tile16_ = 0; int current_tile16_ = 0;
int current_tile8_ = 0; int current_tile8_ = 0;
uint8_t current_palette_ = 0; uint8_t current_palette_ = 0;
@@ -56,12 +60,6 @@ class Tile16Editor : public gfx::GfxContext, public SharedRom {
core::NotifyValue<uint32_t> notify_tile16; core::NotifyValue<uint32_t> notify_tile16;
core::NotifyValue<uint8_t> notify_palette; core::NotifyValue<uint8_t> notify_palette;
// Various options for the Tile16 Editor
bool x_flip;
bool y_flip;
bool priority_tile;
int tile_size;
std::array<uint8_t, 0x200> all_tiles_types_; std::array<uint8_t, 0x200> all_tiles_types_;
// Tile16 blockset for selecting the tile to edit // Tile16 blockset for selecting the tile to edit
@@ -100,4 +98,5 @@ class Tile16Editor : public gfx::GfxContext, public SharedRom {
} // namespace editor } // namespace editor
} // namespace yaze } // namespace yaze
#endif // YAZE_APP_EDITOR_TILE16EDITOR_H #endif // YAZE_APP_EDITOR_TILE16EDITOR_H