yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
message_editor_font.cc
Go to the documentation of this file.
1#include "message_editor.h"
2
3#include <algorithm>
4#include <string>
5#include <vector>
6
10#include "app/gui/core/icons.h"
12#include "imgui.h"
13#include "util/i18n/tr.h"
14#include "util/log.h"
15#include "zelda3/game_data.h"
16
17namespace yaze::editor {
18
28
30 std::vector<gfx::SnesColor> colors;
31 colors.reserve(32);
32 for (int i = 0; i < 32; ++i) {
33 const float value = static_cast<float>(i % 16) / 15.0f;
34 colors.emplace_back(ImVec4(value, value, value, 1.0f));
35 }
36
37 colors[25] = gfx::SnesColor(ImVec4(0.0f, 0.0f, 0.44f, 1.0f));
38 colors[26] = gfx::SnesColor(ImVec4(0.97f, 0.97f, 0.97f, 1.0f));
39 colors[27] = gfx::SnesColor(ImVec4(0.75f, 0.0f, 0.0f, 1.0f));
40
41 if (!colors.empty()) {
42 colors[0].set_transparent(true);
43 }
44
45 return gfx::SnesPalette(colors);
46}
47
50 if (!rom() || !rom()->is_loaded()) {
51 LOG_WARN("MessageEditor", "ROM not loaded - skipping font graphics load");
52 return;
53 }
54
55 std::fill(raw_font_gfx_data_.begin(), raw_font_gfx_data_.end(), 0);
56
57 const size_t rom_size = rom()->size();
58 if (rom_size > static_cast<size_t>(kGfxFont)) {
59 const size_t available = std::min(raw_font_gfx_data_.size(),
60 rom_size - static_cast<size_t>(kGfxFont));
61 std::copy_n(rom()->data() + kGfxFont, available,
63 if (available < raw_font_gfx_data_.size()) {
64 LOG_WARN("MessageEditor",
65 "Font graphics truncated (ROM size %zu, read %zu bytes)",
66 rom_size, available);
67 }
68 } else {
69 LOG_WARN("MessageEditor",
70 "ROM size %zu too small for font graphics offset 0x%X", rom_size,
71 kGfxFont);
72 }
73
75 gfx::SnesTo8bppSheet(raw_font_gfx_data_, /*bpp=*/2, /*num_sheets=*/2);
76
77 auto load_font = zelda3::LoadFontGraphics(*rom());
78 if (load_font.ok()) {
79 message_preview_.font_gfx16_data_2_ = load_font.value().vector();
80 } else {
81 const std::string error_message(load_font.status().message());
82 LOG_WARN("MessageEditor", "LoadFontGraphics failed: %s",
83 error_message.c_str());
84 }
85
86 const auto& font_data = !message_preview_.font_gfx16_data_.empty()
89 RefreshFontAtlasBitmap(font_data);
91}
92
94 const std::vector<uint8_t>& font_data) {
95 if (font_data.empty()) {
96 LOG_WARN("MessageEditor", "Font graphics data missing - atlas stays empty");
97 return;
98 }
99
100 const int atlas_width = kFontGfxMessageSize;
101 const size_t row_count = (font_data.size() + atlas_width - 1) / atlas_width;
102 const int atlas_height = static_cast<int>(std::max<size_t>(1, row_count));
103
104 const size_t expected_size = static_cast<size_t>(atlas_width) * atlas_height;
105 std::vector<uint8_t> padded(font_data.begin(), font_data.end());
106 if (padded.size() < expected_size) {
107 padded.resize(expected_size, 0);
108 } else if (padded.size() > expected_size) {
109 padded.resize(expected_size);
110 }
111
112 font_gfx_bitmap_.Create(atlas_width, atlas_height, kFontGfxMessageDepth,
113 padded);
116 }
119}
120
123
125 return;
126 }
127
128 auto queue_refresh = [](gfx::Bitmap& bitmap) {
129 if (!bitmap.is_active()) {
130 return;
131 }
132 const auto command = bitmap.texture()
135 gfx::Arena::Get().QueueTextureCommand(command, &bitmap);
136 };
137
139 queue_refresh(font_gfx_bitmap_);
140
143 queue_refresh(current_font_gfx16_bitmap_);
144 }
145}
146
165
171 gui::BeginCanvas(font_gfx_canvas_, ImVec2(256, 256));
175 ImGui::TextDisabled(
176 tr("2bpp dialogue font at $070000. Click a tile to inspect it."));
177}
178
179} // namespace yaze::editor
auto begin()
Definition rom.h:215
auto size() const
Definition rom.h:212
zelda3::GameData * game_data() const
Definition editor.h:369
void RefreshFontAtlasBitmap(const std::vector< uint8_t > &font_data)
std::array< uint8_t, 0x4000 > raw_font_gfx_data_
gfx::SnesPalette BuildFallbackFontPalette() const
gfx::SnesPalette font_preview_colors_
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:39
static Arena & Get()
Definition arena.cc:24
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:70
void Create(int width, int height, int depth, std::span< uint8_t > data)
Create a bitmap with the given dimensions and data.
Definition bitmap.cc:198
TextureHandle texture() const
Definition bitmap.h:410
bool is_active() const
Definition bitmap.h:414
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
Definition bitmap.cc:452
SNES Color container.
Definition snes_color.h:110
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
void DrawBitmap(Bitmap &bitmap, int border_offset, float scale)
Definition canvas.cc:1295
bool DrawTileSelector(int size, int size_y=0)
Definition canvas.cc:1231
CanvasConfig & GetConfig()
Definition canvas.h:243
#define ICON_MD_FONT_DOWNLOAD
Definition icons.h:818
#define LOG_WARN(category, format,...)
Definition log.h:138
Editors are the view controllers for the application.
constexpr int kDialogueFontPalette
constexpr int kGfxFont
constexpr int kFontGfxMessageSize
constexpr int kFontGfxMessageDepth
std::vector< uint8_t > SnesTo8bppSheet(std::span< const uint8_t > sheet, int bpp, int num_sheets)
Definition snes_tile.cc:133
void EndCanvas(Canvas &canvas)
void BeginCanvas(Canvas &canvas, ImVec2 child_size)
void SectionHeader(const char *icon, const char *label, const ImVec4 &color)
ImVec4 GetInfoColor()
Definition ui_helpers.cc:64
absl::StatusOr< gfx::Bitmap > LoadFontGraphics(const Rom &rom)
Loads font graphics from ROM.
Definition game_data.cc:682
std::vector< uint8_t > Data
std::vector< uint8_t > font_gfx16_data_2_
std::vector< uint8_t > font_gfx16_data_
auto palette(int i) const
gfx::PaletteGroupMap palette_groups
Definition game_data.h:95