yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
usage_preview.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_GRAPHICS_USAGE_PREVIEW_H
2#define YAZE_APP_EDITOR_GRAPHICS_USAGE_PREVIEW_H
3
4#include <array>
5#include <cstdint>
6#include <functional>
7#include <map>
8#include <memory>
9#include <optional>
10#include <set>
11#include <string>
12#include <vector>
13
14#include "absl/status/status.h"
18#include "app/gfx/core/bitmap.h"
20#include "rom/rom.h"
21#include "zelda3/dungeon/room.h"
22#include "zelda3/game_data.h"
25
26namespace yaze::zelda3 {
27class Overworld;
28}
29
31
47constexpr size_t kSheetBytes = 4096; // 128x32 8bpp sheet
48
50using SheetPixels = std::map<uint16_t, std::vector<uint8_t>>;
51
56[[deprecated("Use SnapshotSessionSheets; Arena is a display cache")]]
57SheetPixels SnapshotArenaSheets(const std::set<uint16_t>& ids,
58 const zelda3::GameData* game_data);
59
60// Preferred preview source: this ROM session's authoritative raw pixels.
61// Independent of whichever session currently occupies the global Arena.
62SheetPixels SnapshotSessionSheets(const std::set<uint16_t>& ids,
63 const zelda3::GameData* game_data);
64
67
68// ---------------------------------------------------------------------------
69// Room context
70// ---------------------------------------------------------------------------
71
75std::map<int, std::array<uint8_t, 16>> CollectRoomSheetBlocks(
76 Rom* rom, zelda3::GameData* game_data,
77 const std::map<int, zelda3::RoomGfxInfo>& rooms);
78
81std::vector<int> RoomsUsingSheet(
82 const std::map<int, std::array<uint8_t, 16>>& room_blocks, uint16_t sheet,
83 bool include_sprites);
84
85// Ephemeral pointers, resolved by the owning session before each view update.
87 zelda3::Room* room = nullptr;
88 std::string provenance;
89 bool current = false;
90 int entrance_slot = -1;
91};
92using LiveRoomContexts = std::map<int, LiveRoomContext>;
93using LiveRoomProvider = std::function<LiveRoomContexts()>;
94using LiveOverworldProvider = std::function<zelda3::Overworld*()>;
95std::string RoomContextLabel(const LiveRoomContext* live);
96
98 size_t object_index = 0;
99 uint16_t object_id = 0;
100 uint16_t tile_word = 0;
101 uint16_t sheet = 0;
102 int slot = 0;
103 // Loader-owned source addresses, not displayed palette-row identity.
104 std::array<std::optional<zelda3::PaletteColorAddress>, 8> palette_colors{};
105};
106
108 size_t object_index = 0;
109 uint16_t object_id = 0;
110 bool shares_pixels = false;
111 bool shares_palette = false;
112 std::vector<RoomTileReference> references;
113};
114
115// Potential consumers of the selected source. Pixel identity is sheet + local
116// tile; palette identity is the loader-owned group/index/color address. A match
117// does not promise a visible change (raw indices and occlusion also matter).
118std::vector<SourceObjectUse> FindSourceObjectUses(
119 const std::vector<RoomTileReference>& references,
120 const std::vector<GraphicsEditTarget>& targets,
121 const zelda3::GameData* owner);
122
124 public:
127 absl::Status Render(Rom* rom, zelda3::GameData* game_data, int room_id,
128 const SheetPixels& sheets, bool objects_only,
129 zelda3::Room* live_room = nullptr);
130
131 const std::vector<RoomTileReference>& tile_references() const {
132 return tile_references_;
133 }
134 size_t unknown_objects() const { return unknown_objects_; }
135
136 int room_id() const { return room_id_; }
137 const gfx::Bitmap& bitmap() const { return composite_; }
140
142 std::array<uint8_t, 16> blocks() const;
145 std::vector<SDL_Rect> ObjectRectsUsingSheet(uint16_t sheet) const;
146 std::vector<SDL_Rect> ObjectRectsForObject(size_t index) const;
147 // Explicit loaded-room scan; never loads another room from ROM or creates a
148 // composite image. Rendering caches may refresh, authoring state is retained.
149 absl::Status TraceLoadedRoom(Rom* rom, zelda3::GameData* game_data,
150 zelda3::Room* room);
152 void Reset();
153
154 private:
155 void CaptureUsage(const zelda3::Room& room);
156 std::unique_ptr<zelda3::Room> room_;
157 std::array<uint8_t, 16> blocks_{};
158 std::vector<RoomTileReference> tile_references_;
159 std::map<size_t, SDL_Rect> object_rects_;
161 int room_id_ = -1;
163};
164
166 int room_id = -1;
167 std::vector<SourceObjectUse> objects;
168 size_t unknown_objects = 0;
169 std::string error;
170};
171
172std::vector<LoadedRoomSourceUses> FindLoadedRoomSourceUses(
173 Rom* rom, zelda3::GameData* game_data, const LiveRoomContexts& rooms,
174 const std::vector<GraphicsEditTarget>& targets);
175
176// ---------------------------------------------------------------------------
177// Sprite context
178// ---------------------------------------------------------------------------
179
181 public:
186 absl::Status Configure(Rom* rom, zelda3::GameData* game_data, int spriteset,
187 int palette_room, std::string hack_name = {});
188
190 void SetVanillaSprite(uint8_t sprite_id);
192 void SetZsm(const zsprite::ZSprite& zsm, int animation);
193 bool has_zsm() const { return zsm_.has_value(); }
194
196 int frame_count() const;
197 int frame() const { return frame_; }
198 void SetFrame(int frame);
201 bool Advance(float seconds);
202
204 PreviewImage RenderFrame(const SheetPixels& sheets) const;
205
206 const std::array<uint8_t, 8>& slots() const { return slots_; }
207 bool UsesSheet(uint16_t sheet) const;
208
209 private:
210 std::vector<uint8_t> SheetOrRom(const SheetPixels& sheets,
211 uint16_t sheet) const;
212
213 Rom* rom_ = nullptr;
215 std::array<uint8_t, 8> slots_{};
216 std::array<SDL_Color, 256> colors_{};
217 std::string hack_name_;
218 uint8_t sprite_id_ = 0;
219 std::optional<zsprite::ZSprite> zsm_;
220 int animation_ = 0;
221 int frame_ = 0;
222 float remainder_ = 0.0f;
223};
224
225// ---------------------------------------------------------------------------
226// Tile16 context
227// ---------------------------------------------------------------------------
228
230 public:
232 absl::Status Configure(Rom* rom, zelda3::GameData* game_data, int area);
233
234 // Copies the selected live map and unsaved Tile16 definitions. The renderer
235 // owns its copies, so preview overrides never mutate the source editor.
236 absl::Status ConfigureFromLive(const zelda3::OverworldMap& map,
237 const std::vector<gfx::Tile16>& definitions,
238 const zelda3::GameData* owner);
239
242 std::vector<int> Tile16sUsingTile(uint16_t sheet, int tile_index) const;
243
247 PreviewImage Render(const SheetPixels& sheets,
248 const std::vector<int>& tile16s, uint16_t sheet,
249 int tile_index,
250 std::vector<SDL_Rect>* highlights = nullptr);
251
252 int area() const { return area_; }
253
254 private:
255 std::vector<int> TileIdsFor(uint16_t sheet, int tile_index) const;
256
257 Rom* rom_ = nullptr;
259 int area_ = -1;
260 std::unique_ptr<zelda3::OverworldMap> map_;
261 std::vector<gfx::Tile16> tiles16_;
262};
263
264} // namespace yaze::editor::usage_preview
265
266#endif // YAZE_APP_EDITOR_GRAPHICS_USAGE_PREVIEW_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:52
std::map< size_t, SDL_Rect > object_rects_
std::vector< RoomTileReference > tile_references_
absl::Status TraceLoadedRoom(Rom *rom, zelda3::GameData *game_data, zelda3::Room *room)
std::unique_ptr< zelda3::Room > room_
absl::Status Render(Rom *rom, zelda3::GameData *game_data, int room_id, const SheetPixels &sheets, bool objects_only, zelda3::Room *live_room=nullptr)
void CaptureUsage(const zelda3::Room &room)
std::array< uint8_t, 16 > blocks() const
Room graphics blocks (0-7 background, 8-15 sprites) of the loaded room.
std::vector< SDL_Rect > ObjectRectsUsingSheet(uint16_t sheet) const
const std::vector< RoomTileReference > & tile_references() const
void Reset()
Clears the loaded room so the next Render() reloads it from the ROM.
std::vector< SDL_Rect > ObjectRectsForObject(size_t index) const
absl::Status Configure(Rom *rom, zelda3::GameData *game_data, int spriteset, int palette_room, std::string hack_name={})
std::optional< zsprite::ZSprite > zsm_
void SetVanillaSprite(uint8_t sprite_id)
Plays a vanilla sprite (single pose from the sprite catalog).
PreviewImage RenderFrame(const SheetPixels &sheets) const
Draws the current frame with sheets (edited pixels) over the ROM's.
std::vector< uint8_t > SheetOrRom(const SheetPixels &sheets, uint16_t sheet) const
void SetZsm(const zsprite::ZSprite &zsm, int animation)
Plays animation animation of a .zsm (the preview keeps a copy).
int frame_count() const
Frames in the current animation (1 for vanilla sprites).
const std::array< uint8_t, 8 > & slots() const
PreviewImage Render(const SheetPixels &sheets, const std::vector< int > &tile16s, uint16_t sheet, int tile_index, std::vector< SDL_Rect > *highlights=nullptr)
std::vector< int > TileIdsFor(uint16_t sheet, int tile_index) const
absl::Status Configure(Rom *rom, zelda3::GameData *game_data, int area)
Loads overworld area area (graphics and palette) and the tile16 table.
std::unique_ptr< zelda3::OverworldMap > map_
absl::Status ConfigureFromLive(const zelda3::OverworldMap &map, const std::vector< gfx::Tile16 > &definitions, const zelda3::GameData *owner)
std::vector< int > Tile16sUsingTile(uint16_t sheet, int tile_index) const
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:70
Represents a single Overworld map screen.
Which graphics sheets exist, which tables use them, and which 16x16 blocks are free.
SheetPixels SnapshotSessionSheets(const std::set< uint16_t > &ids, const zelda3::GameData *game_data)
std::vector< LoadedRoomSourceUses > FindLoadedRoomSourceUses(Rom *rom, zelda3::GameData *game_data, const LiveRoomContexts &rooms, const std::vector< GraphicsEditTarget > &targets)
std::string RoomContextLabel(const LiveRoomContext *live)
SheetPixels SnapshotArenaSheets(const std::set< uint16_t > &ids, const zelda3::GameData *game_data)
std::map< uint16_t, std::vector< uint8_t > > SheetPixels
Sheet id -> 4096 8bpp pixels (the GameData::graphics_buffer layout).
constexpr size_t kSheetBytes
std::function< LiveRoomContexts()> LiveRoomProvider
std::map< int, LiveRoomContext > LiveRoomContexts
PreviewImage SnapshotBitmap(const gfx::Bitmap &bitmap)
Copies a palettized Bitmap (for example a room composite).
std::vector< int > RoomsUsingSheet(const std::map< int, std::array< uint8_t, 16 > > &room_blocks, uint16_t sheet, bool include_sprites)
std::function< zelda3::Overworld *()> LiveOverworldProvider
std::map< int, std::array< uint8_t, 16 > > CollectRoomSheetBlocks(Rom *rom, zelda3::GameData *game_data, const std::map< int, zelda3::RoomGfxInfo > &rooms)
std::vector< SourceObjectUse > FindSourceObjectUses(const std::vector< RoomTileReference > &references, const std::vector< GraphicsEditTarget > &targets, const zelda3::GameData *owner)
Zelda 3 specific classes and functions.
std::array< std::optional< zelda3::PaletteColorAddress >, 8 > palette_colors
std::vector< RoomTileReference > references