yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
game_data.h
Go to the documentation of this file.
1#ifndef YAZE_ZELDA3_GAME_DATA_H
2#define YAZE_ZELDA3_GAME_DATA_H
3
4#include <array>
5#include <cstdint>
6#include <map>
7#include <vector>
8
9#include "absl/status/status.h"
10#include "absl/status/statusor.h"
11#include "app/gfx/core/bitmap.h"
13#include "rom/rom.h"
14#include "rom/rom_diagnostics.h"
15#include "zelda.h"
16
17namespace yaze {
18namespace zelda3 {
19
20// ============================================================================
21// Graphics Constants
22// ============================================================================
23
24// Sheet counts
25constexpr uint32_t kNumGfxSheets = 223;
26constexpr uint32_t kNumLinkSheets = 14;
27
28// Blockset/Group counts
29constexpr uint32_t kNumMainBlocksets = 37;
30constexpr uint32_t kNumRoomBlocksets = 82;
31constexpr uint32_t kNumSpritesets = 144;
32constexpr uint32_t kNumPalettesets = 72;
33
34// ROM pointer locations
35constexpr uint32_t kEntranceGfxGroup = 0x5D97;
36// Note: kGfxGroupsPointer defined in zelda3/dungeon/dungeon_rom_addresses.h
37constexpr uint32_t kMaxGraphics = 0x0C3FFF;
38
39// Dungeon palette lookup tables
40// Room headers store a "palette set ID" (0-71), NOT a direct palette index!
41// Two-level lookup: paletteset_ids[room.palette][0] gives a byte offset into
42// kDungeonPalettePointerTable. The word there, divided by 180, is the palette.
43constexpr uint32_t kPalettesetIdsAddress = 0x75460;
44constexpr uint32_t kDungeonPalettePointerTable = 0xDEC4B;
45constexpr int kDungeonPaletteBytes = 180;
46
47// Link graphics location ($10:8000)
48constexpr uint32_t kLinkGfxOffset = 0x80000;
49constexpr uint16_t kLinkGfxLength = 0x800;
50
51// Font graphics location
52constexpr uint32_t kFontSpriteLocation = 0x70000;
53
54// Sheet sizes
55constexpr uint32_t kUncompressedSheetSize = 0x0800; // 2048 bytes
56
57// Tile16 pointer location
58constexpr uint32_t kTile16Ptr = 0x78000;
59
60// Version constants map
61static const std::map<zelda3_version, zelda3_version_pointers>
62 kVersionConstantsMap = {
63 {zelda3_version::US, zelda3_us_pointers},
64 {zelda3_version::JP, zelda3_jp_pointers},
65 {zelda3_version::SD, {}},
66 {zelda3_version::RANDO, {}},
67};
68
69struct GameData {
70 // Constructors
71 GameData() = default;
72 explicit GameData(Rom* rom) : rom_(rom) {}
73
74 // ROM reference (non-owning)
75 Rom* rom() const { return rom_; }
76 void set_rom(Rom* rom) { rom_ = rom; }
77
78 // Version info
79 zelda3_version version = zelda3_version::US;
80 std::string title;
81
82 // Graphics Resources
83 std::vector<uint8_t> graphics_buffer; // Legacy contiguous buffer
84 std::array<std::vector<uint8_t>, kNumGfxSheets>
85 raw_gfx_sheets; // 8BPP indexed
86 std::array<gfx::Bitmap, kNumGfxSheets> gfx_bitmaps; // Renderable bitmaps
87 std::array<gfx::Bitmap, kNumLinkSheets> link_graphics;
89
90 // Game Data Structures
92
93 std::array<std::array<uint8_t, 8>, kNumMainBlocksets> main_blockset_ids;
94 std::array<std::array<uint8_t, 4>, kNumRoomBlocksets> room_blockset_ids;
95 std::array<std::array<uint8_t, 4>, kNumSpritesets> spriteset_ids;
96
97 // Palette set lookup table (72 entries × 4 bytes each)
98 // Entry format: [bg_palette_offset, aux1, aux2, aux3]
99 // NOTE: paletteset_ids[n][0] is a BYTE OFFSET into kDungeonPalettePointerTable,
100 // NOT a direct palette index! See room.cc for correct lookup algorithm.
101 std::array<std::array<uint8_t, 4>, kNumPalettesets> paletteset_ids;
102
103 // Diagnostics
105
106 void Clear() {
107 graphics_buffer.clear();
108 for (auto& sheet : raw_gfx_sheets)
109 sheet.clear();
110 // gfx_bitmaps don't need explicit clearing if reloaded
112 }
113
114 private:
115 Rom* rom_ = nullptr;
116};
117
119 bool load_graphics = true;
120 bool load_palettes = true;
121 bool load_gfx_groups = true;
122 bool expand_rom = true;
123 bool populate_metadata = true;
124};
125
132absl::Status LoadGameData(Rom& rom, GameData& data,
133 const LoadOptions& options = {});
134
140absl::Status SaveGameData(Rom& rom, GameData& data);
141
142// Individual loaders (internal use or fine-grained control)
143absl::Status LoadMetadata(const Rom& rom, GameData& data);
144absl::Status LoadPalettes(const Rom& rom, GameData& data);
145absl::Status LoadGfxGroups(Rom& rom, GameData& data);
146absl::Status LoadGraphics(Rom& rom, GameData& data);
147absl::Status SaveGfxGroups(Rom& rom, const GameData& data);
148
154absl::StatusOr<std::array<gfx::Bitmap, kNumLinkSheets>> LoadLinkGraphics(
155 const Rom& rom);
156
162absl::StatusOr<std::vector<uint8_t>> Load2BppGraphics(const Rom& rom);
163
169absl::StatusOr<gfx::Bitmap> LoadFontGraphics(const Rom& rom);
170
177absl::Status SaveAllGraphicsData(
178 Rom& rom, const std::array<gfx::Bitmap, kNumGfxSheets>& sheets);
179
190uint32_t GetGraphicsAddress(const uint8_t* data, uint8_t addr, uint32_t ptr1,
191 uint32_t ptr2, uint32_t ptr3, size_t rom_size);
192
193} // namespace zelda3
194} // namespace yaze
195
196#endif // YAZE_ZELDA3_GAME_DATA_H
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:28
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
zelda3_version
Different versions of the game supported by YAZE.
Definition zelda.h:33
absl::Status SaveGfxGroups(Rom &rom, const GameData &data)
Definition game_data.cc:264
absl::StatusOr< std::vector< uint8_t > > Load2BppGraphics(const Rom &rom)
Loads 2BPP graphics sheets from ROM.
Definition game_data.cc:561
constexpr uint32_t kPalettesetIdsAddress
Definition game_data.h:43
constexpr uint16_t kLinkGfxLength
Definition game_data.h:49
constexpr uint32_t kFontSpriteLocation
Definition game_data.h:52
absl::StatusOr< std::array< gfx::Bitmap, kNumLinkSheets > > LoadLinkGraphics(const Rom &rom)
Loads Link's graphics sheets from ROM.
Definition game_data.cc:535
absl::StatusOr< gfx::Bitmap > LoadFontGraphics(const Rom &rom)
Loads font graphics from ROM.
Definition game_data.cc:605
constexpr uint32_t kNumRoomBlocksets
Definition game_data.h:30
constexpr uint32_t kUncompressedSheetSize
Definition game_data.h:55
absl::Status LoadGameData(Rom &rom, GameData &data, const LoadOptions &options)
Loads all Zelda3-specific game data from a generic ROM.
Definition game_data.cc:123
absl::Status LoadMetadata(const Rom &rom, GameData &data)
Definition game_data.cc:182
constexpr uint32_t kMaxGraphics
Definition game_data.h:37
constexpr uint32_t kTile16Ptr
Definition game_data.h:58
constexpr uint32_t kNumPalettesets
Definition game_data.h:32
absl::Status SaveAllGraphicsData(Rom &rom, const std::array< gfx::Bitmap, kNumGfxSheets > &sheets)
Saves all graphics sheets back to ROM.
Definition game_data.cc:629
constexpr uint32_t kLinkGfxOffset
Definition game_data.h:48
constexpr int kDungeonPaletteBytes
Definition game_data.h:45
constexpr uint32_t kNumMainBlocksets
Definition game_data.h:29
constexpr uint32_t kNumGfxSheets
Definition game_data.h:25
constexpr uint32_t kEntranceGfxGroup
Definition game_data.h:35
constexpr uint32_t kNumSpritesets
Definition game_data.h:31
constexpr uint32_t kNumLinkSheets
Definition game_data.h:26
absl::Status LoadPalettes(const Rom &rom, GameData &data)
Definition game_data.cc:206
absl::Status SaveGameData(Rom &rom, GameData &data)
Saves modified game data back to the ROM.
Definition game_data.cc:153
absl::Status LoadGraphics(Rom &rom, GameData &data)
Definition game_data.cc:456
constexpr uint32_t kDungeonPalettePointerTable
Definition game_data.h:44
uint32_t GetGraphicsAddress(const uint8_t *data, uint8_t addr, uint32_t ptr1, uint32_t ptr2, uint32_t ptr3, size_t rom_size)
Gets the graphics address for a sheet index.
Definition game_data.cc:112
absl::Status LoadGfxGroups(Rom &rom, GameData &data)
Definition game_data.cc:212
Represents a mapping of palette groups.
std::array< std::array< uint8_t, 4 >, kNumSpritesets > spriteset_ids
Definition game_data.h:95
void set_rom(Rom *rom)
Definition game_data.h:76
std::array< std::array< uint8_t, 4 >, kNumRoomBlocksets > room_blockset_ids
Definition game_data.h:94
std::array< std::array< uint8_t, 4 >, kNumPalettesets > paletteset_ids
Definition game_data.h:101
gfx::Bitmap font_graphics
Definition game_data.h:88
gfx::PaletteGroupMap palette_groups
Definition game_data.h:91
GraphicsLoadDiagnostics diagnostics
Definition game_data.h:104
zelda3_version version
Definition game_data.h:79
std::array< gfx::Bitmap, kNumGfxSheets > gfx_bitmaps
Definition game_data.h:86
std::array< gfx::Bitmap, kNumLinkSheets > link_graphics
Definition game_data.h:87
Rom * rom() const
Definition game_data.h:75
std::array< std::array< uint8_t, 8 >, kNumMainBlocksets > main_blockset_ids
Definition game_data.h:93
std::array< std::vector< uint8_t >, kNumGfxSheets > raw_gfx_sheets
Definition game_data.h:85
std::vector< uint8_t > graphics_buffer
Definition game_data.h:83
The Legend of Zelda: A Link to the Past - Data Structures and Constants.