yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dungeon_torch_codec.h
Go to the documentation of this file.
1#ifndef YAZE_ZELDA3_DUNGEON_DUNGEON_TORCH_CODEC_H_
2#define YAZE_ZELDA3_DUNGEON_DUNGEON_TORCH_CODEC_H_
3
4#include <cstdint>
5
6namespace yaze::zelda3 {
7
8// Pure encode/decode helpers for entries in the lightable-torch table at
9// `kTorchData`. `RoomDraw_LightableTorch` ($01:B509) tests bit 15 to select
10// the lit tile data, then masks the word with `AND.w #$3FFF` before drawing.
11// The retained bit 13 adds $2000 to the upper-layer $7E2000 tilemap base and
12// therefore selects the lower/BG2 tilemap at $7E4000. Bit 14 is reserved by
13// the torch table and must be preserved independently of the draw target.
14//
15// Word layout (high << 8 | low):
16// bit 0 : unused (always 0; LSL-by-1 padding)
17// bits 1..6 : px (6 bits, range 0..63)
18// bits 7..12 : py (6 bits, range 0..63)
19// bit 13 : draw target (0 = upper/BG1, 1 = lower/BG2)
20// bit 14 : reserved (preserved on load/save)
21// bit 15 : initially lit
22//
23// The codec represents the complete 6-bit coordinate fields. SaveAllTorches
24// separately restricts editable 2x2 torch anchors to 0..62 so their art stays
25// within the 64x64 room tilemap.
26
28 uint8_t px = 0;
29 uint8_t py = 0;
30 uint8_t draw_layer = 0;
31 uint8_t reserved = 0;
32 bool lit = false;
33};
34
36 uint8_t low = 0;
37 uint8_t high = 0;
38};
39
41 const LightableTorchBytes& bytes) {
42 const uint16_t word = static_cast<uint16_t>(bytes.low | (bytes.high << 8));
44 .px = static_cast<uint8_t>((word >> 1) & 0x3F),
45 .py = static_cast<uint8_t>((word >> 7) & 0x3F),
46 .draw_layer = static_cast<uint8_t>((word >> 13) & 0x01),
47 .reserved = static_cast<uint8_t>((word >> 14) & 0x01),
48 .lit = (word & 0x8000) != 0,
49 };
50}
51
53 const LightableTorchEntry& entry) {
54 const uint16_t word = static_cast<uint16_t>(
55 ((entry.px & 0x3F) << 1) | ((entry.py & 0x3F) << 7) |
56 ((entry.draw_layer & 0x01) << 13) | ((entry.reserved & 0x01) << 14) |
57 (entry.lit ? 0x8000 : 0));
59 .low = static_cast<uint8_t>(word & 0xFF),
60 .high = static_cast<uint8_t>((word >> 8) & 0xFF),
61 };
62}
63
64} // namespace yaze::zelda3
65
66#endif // YAZE_ZELDA3_DUNGEON_DUNGEON_TORCH_CODEC_H_
Zelda 3 specific classes and functions.
LightableTorchEntry DecodeLightableTorchEntry(const LightableTorchBytes &bytes)
LightableTorchBytes EncodeLightableTorchEntry(const LightableTorchEntry &entry)