yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
sprite_oam_tables.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cctype>
5#include <filesystem>
6#include <fstream>
7#include <string>
8#include <unordered_map>
9
10namespace yaze {
11namespace zelda3 {
12
13namespace {
14
15// Oracle Sprites/Enemies/puffstool.asm, Sprite_Puffstool_Draw frame 0:
16// CHR C0/D0, properties 33 (page 1, OBJ palette 1), 16x16 at (0,-8)/(0,0).
17// Core/sprite_macros.asm DrawSprite emits the source entries in reverse order.
18// This is a source-backed static preview, not an emulator-verified animation.
20 .sprite_id = 0xB1,
21 .name = "Puffstool",
22 .tiles = {{0, 0, 0x1D0, 1, true, false, false},
23 {0, -8, 0x1C0, 1, true, false, false}},
24 .required_sheets = {0, 0, 0, 0}};
25
26// Oracle Sprites/Bosses/manhandla.asm, Sprite_Manhandla_Draw frame 0. Its
27// descending loop emits CHR20 at (0,8) before CHR00 at (0,-8); properties $33
28// select OBJ page 1 and palette 1. This is the static front head, not the spawned
29// heads, BG body, or Big Chuchu phase. ApplyManhandlaGraphics loads an external
30// $2000-byte page at VRAM $5000; ordinary base-ROM room sheets are not this art.
32 .sprite_id = 0x88,
33 .name = "Manhandla (static head)",
34 .tiles = {{0, 8, 0x120, 1, true, false, false},
35 {0, -8, 0x100, 1, true, false, false}},
36 .required_sheets = {0, 0, 0, 0},
37 .graphics_resource = "Bosses/manhandla.bin"};
38
39// Static registry of all sprite layouts
40const std::unordered_map<uint8_t, const SpriteOamLayout*>& GetLayoutMap() {
41 static const std::unordered_map<uint8_t, const SpriteOamLayout*> layouts = {
42 {0x01, &kVultureLayout}, {0x08, &kOctorokLayout},
43 {0x0B, &kChickenLayout}, {0x0E, &kGreenSoldierLayout},
44 {0x0F, &kBlueSoldierLayout}, {0x10, &kRedSoldierLayout},
45 {0x29, &kBlueGuardLayout}, {0x41, &kGreenPatrolLayout},
46 {0x44, &kArmosKnightLayout}, {0x4B, &kOctoballoonLayout},
47 {0x53, &kRedEyegoreLayout}, {0x54, &kGreenEyegoreLayout},
48 {0x64, &kMoblinLayout}, {0x81, &kHinoxLayout},
49 {0x88, &kUncleLayout}, {0xD8, &kHeartContainerLayout},
50 {0xDA, &kGreenRupeeLayout}, {0xDB, &kBlueRupeeLayout},
51 {0xDC, &kRedRupeeLayout}, {0xDE, &kSmallHeartLayout},
52 {0xDF, &kKeyLayout}, {0xE1, &kSmallMagicLayout},
53 {0xE2, &kLargeMagicLayout},
54 };
55 return layouts;
56}
57
58} // namespace
59
61 const auto& layouts = GetLayoutMap();
62 auto it = layouts.find(sprite_id);
63 if (it != layouts.end()) {
64 return it->second;
65 }
66 return nullptr;
67}
68
70 uint8_t sprite_id, std::string_view hack_name) {
71 if ((sprite_id != 0xB1 && sprite_id != 0x88) || hack_name.empty()) {
72 return nullptr;
73 }
74 // Match the existing Oracle hack-workflow name fallback. This is per-call,
75 // never global state, so switching projects cannot retain an old override.
76 std::string normalized(hack_name);
77 std::transform(normalized.begin(), normalized.end(), normalized.begin(),
78 [](unsigned char c) { return std::tolower(c); });
79 if (normalized.find("oracle") == std::string::npos) {
80 return nullptr;
81 }
82 return sprite_id == 0x88 ? &kOracleManhandlaPreview
83 : &kOraclePuffstoolPreview;
84}
85
86bool SpritePreviewResourceCache::SetContext(std::string_view project_path,
87 std::string_view assets_path,
88 std::string_view hack_name) {
89 if (project_path_ == project_path && assets_path_ == assets_path &&
90 hack_name_ == hack_name) {
91 return false;
92 }
93 project_path_ = project_path;
94 assets_path_ = assets_path;
95 hack_name_ = hack_name;
96 resource_.clear();
97 graphics_.clear();
98 return true;
99}
100
102 const SpriteOamLayout* layout) {
103 if (layout == nullptr || layout->graphics_resource == nullptr ||
104 project_path_.empty() || assets_path_.empty() ||
106 nullptr) {
107 return {};
108 }
109 if (resource_ == layout->graphics_resource) {
110 return graphics_;
111 }
113 graphics_.clear();
114 // Match ApplyManhandlaGraphics' whole OBJ page transfer, not a partial file.
115 // Read at most this small fixed payload; reject truncated or oversized files.
116 constexpr size_t kObjPageBytes = 0x2000;
117 const auto path = std::filesystem::path(assets_path_) / resource_;
118 std::ifstream file(path, std::ios::binary | std::ios::ate);
119 if (!file || file.tellg() != static_cast<std::streamoff>(kObjPageBytes)) {
120 return {};
121 }
122 file.seekg(0);
123 graphics_.resize(kObjPageBytes);
124 if (!file.read(reinterpret_cast<char*>(graphics_.data()), graphics_.size()) ||
125 file.peek() != std::char_traits<char>::eof()) {
126 graphics_.clear();
127 }
128 return graphics_;
129}
130
131std::optional<std::array<uint8_t, 4>> SpriteOamRegistry::GetRequiredSheets(
132 uint8_t sprite_id) {
133 const auto* layout = GetLayout(sprite_id);
134 if (layout) {
135 return layout->required_sheets;
136 }
137 return std::nullopt;
138}
139
140bool SpriteOamRegistry::HasLayout(uint8_t sprite_id) {
141 const auto& layouts = GetLayoutMap();
142 return layouts.find(sprite_id) != layouts.end();
143}
144
145std::vector<const SpriteOamLayout*> SpriteOamRegistry::GetAllLayouts() {
146 std::vector<const SpriteOamLayout*> result;
147 const auto& layouts = GetLayoutMap();
148 result.reserve(layouts.size());
149 for (const auto& [id, layout] : layouts) {
150 result.push_back(layout);
151 }
152 return result;
153}
154
155} // namespace zelda3
156} // namespace yaze
static const SpriteOamLayout * GetLayout(uint8_t sprite_id)
Get the OAM layout for a sprite ID.
static std::vector< const SpriteOamLayout * > GetAllLayouts()
Get all defined layouts.
static const SpriteOamLayout * GetPreviewOverride(uint8_t sprite_id, std::string_view hack_name)
static std::optional< std::array< uint8_t, 4 > > GetRequiredSheets(uint8_t sprite_id)
Get required graphics sheets for a sprite.
static bool HasLayout(uint8_t sprite_id)
Check if a sprite has a defined layout.
std::span< const uint8_t > GetGraphics(const SpriteOamLayout *layout)
bool SetContext(std::string_view project_path, std::string_view assets_path, std::string_view hack_name)
const std::unordered_map< uint8_t, const SpriteOamLayout * > & GetLayoutMap()
const SpriteOamLayout kRedEyegoreLayout
const SpriteOamLayout kOctorokLayout
const SpriteOamLayout kBlueGuardLayout
const SpriteOamLayout kLargeMagicLayout
const SpriteOamLayout kBlueSoldierLayout
const SpriteOamLayout kBlueRupeeLayout
const SpriteOamLayout kRedSoldierLayout
const SpriteOamLayout kGreenSoldierLayout
const SpriteOamLayout kSmallMagicLayout
const SpriteOamLayout kGreenRupeeLayout
const SpriteOamLayout kArmosKnightLayout
const SpriteOamLayout kGreenPatrolLayout
const SpriteOamLayout kOctoballoonLayout
const SpriteOamLayout kHinoxLayout
const SpriteOamLayout kHeartContainerLayout
const SpriteOamLayout kMoblinLayout
const SpriteOamLayout kChickenLayout
const SpriteOamLayout kUncleLayout
const SpriteOamLayout kKeyLayout
const SpriteOamLayout kGreenEyegoreLayout
const SpriteOamLayout kSmallHeartLayout
const SpriteOamLayout kVultureLayout
const SpriteOamLayout kRedRupeeLayout
Complete OAM layout for a vanilla sprite.