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
9#include "imgui.h"
10#include "util/log.h"
11#include "zelda3/game_data.h"
12
13namespace yaze::editor {
14
24
26 std::vector<gfx::SnesColor> colors;
27 colors.reserve(16);
28 for (int i = 0; i < 16; ++i) {
29 const float value = static_cast<float>(i) / 15.0f;
30 colors.emplace_back(ImVec4(value, value, value, 1.0f));
31 }
32
33 if (!colors.empty()) {
34 colors[0].set_transparent(true);
35 }
36
37 return gfx::SnesPalette(colors);
38}
39
42 if (!rom() || !rom()->is_loaded()) {
43 LOG_WARN("MessageEditor", "ROM not loaded - skipping font graphics load");
44 return;
45 }
46
47 std::fill(raw_font_gfx_data_.begin(), raw_font_gfx_data_.end(), 0);
48
49 const size_t rom_size = rom()->size();
50 if (rom_size > static_cast<size_t>(kGfxFont)) {
51 const size_t available = std::min(raw_font_gfx_data_.size(),
52 rom_size - static_cast<size_t>(kGfxFont));
53 std::copy_n(rom()->data() + kGfxFont, available,
55 if (available < raw_font_gfx_data_.size()) {
56 LOG_WARN("MessageEditor",
57 "Font graphics truncated (ROM size %zu, read %zu bytes)",
58 rom_size, available);
59 }
60 } else {
61 LOG_WARN("MessageEditor",
62 "ROM size %zu too small for font graphics offset 0x%X", rom_size,
63 kGfxFont);
64 }
65
67 gfx::SnesTo8bppSheet(raw_font_gfx_data_, /*bpp=*/2, /*num_sheets=*/2);
68
69 auto load_font = zelda3::LoadFontGraphics(*rom());
70 if (load_font.ok()) {
71 message_preview_.font_gfx16_data_2_ = load_font.value().vector();
72 } else {
73 const std::string error_message(load_font.status().message());
74 LOG_WARN("MessageEditor", "LoadFontGraphics failed: %s",
75 error_message.c_str());
76 }
77
78 const auto& font_data = !message_preview_.font_gfx16_data_.empty()
81 RefreshFontAtlasBitmap(font_data);
83}
84
86 const std::vector<uint8_t>& font_data) {
87 if (font_data.empty()) {
88 LOG_WARN("MessageEditor", "Font graphics data missing - atlas stays empty");
89 return;
90 }
91
92 const int atlas_width = kFontGfxMessageSize;
93 const size_t row_count = (font_data.size() + atlas_width - 1) / atlas_width;
94 const int atlas_height = static_cast<int>(std::max<size_t>(1, row_count));
95
96 const size_t expected_size = static_cast<size_t>(atlas_width) * atlas_height;
97 std::vector<uint8_t> padded(font_data.begin(), font_data.end());
98 if (padded.size() < expected_size) {
99 padded.resize(expected_size, 0);
100 } else if (padded.size() > expected_size) {
101 padded.resize(expected_size);
102 }
103
104 font_gfx_bitmap_.Create(atlas_width, atlas_height, kFontGfxMessageDepth,
105 padded);
108 }
111}
112
115
117 return;
118 }
119
120 auto queue_refresh = [](gfx::Bitmap& bitmap) {
121 if (!bitmap.is_active()) {
122 return;
123 }
124 const auto command = bitmap.texture()
127 gfx::Arena::Get().QueueTextureCommand(command, &bitmap);
128 };
129
131 queue_refresh(font_gfx_bitmap_);
132
135 queue_refresh(current_font_gfx16_bitmap_);
136 }
137}
138
157
158} // namespace yaze::editor
auto begin()
Definition rom.h:203
auto size() const
Definition rom.h:200
zelda3::GameData * game_data() const
Definition editor.h:357
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
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
#define LOG_WARN(category, format,...)
Definition log.h:138
Editors are the view controllers for the application.
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
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