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
27
43constexpr size_t kSheetBytes = 4096; // 128x32 8bpp sheet
44
46using SheetPixels = std::map<uint16_t, std::vector<uint8_t>>;
47
52[[deprecated("Use SnapshotSessionSheets; Arena is a display cache")]]
53SheetPixels SnapshotArenaSheets(const std::set<uint16_t>& ids,
54 const zelda3::GameData* game_data);
55
56// Preferred preview source: this ROM session's authoritative raw pixels.
57// Independent of whichever session currently occupies the global Arena.
58SheetPixels SnapshotSessionSheets(const std::set<uint16_t>& ids,
59 const zelda3::GameData* game_data);
60
63
64// ---------------------------------------------------------------------------
65// Room context
66// ---------------------------------------------------------------------------
67
71std::map<int, std::array<uint8_t, 16>> CollectRoomSheetBlocks(
72 Rom* rom, zelda3::GameData* game_data,
73 const std::map<int, zelda3::RoomGfxInfo>& rooms);
74
77std::vector<int> RoomsUsingSheet(
78 const std::map<int, std::array<uint8_t, 16>>& room_blocks, uint16_t sheet,
79 bool include_sprites);
80
81// Ephemeral pointers, resolved by the owning session before each view update.
83 zelda3::Room* room = nullptr;
84 std::string provenance;
85 bool current = false;
86 int entrance_slot = -1;
87};
88using LiveRoomContexts = std::map<int, LiveRoomContext>;
89using LiveRoomProvider = std::function<LiveRoomContexts()>;
90std::string RoomContextLabel(const LiveRoomContext* live);
91
93 size_t object_index = 0;
94 uint16_t object_id = 0;
95 uint16_t tile_word = 0;
96 uint16_t sheet = 0;
97 int slot = 0;
98 // Loader-owned source addresses, not displayed palette-row identity.
99 std::array<std::optional<zelda3::PaletteColorAddress>, 8> palette_colors{};
100};
101
103 size_t object_index = 0;
104 uint16_t object_id = 0;
105 bool shares_pixels = false;
106 bool shares_palette = false;
107 std::vector<RoomTileReference> references;
108};
109
110// Potential consumers of the selected source. Pixel identity is sheet + local
111// tile; palette identity is the loader-owned group/index/color address. A match
112// does not promise a visible change (raw indices and occlusion also matter).
113std::vector<SourceObjectUse> FindSourceObjectUses(
114 const std::vector<RoomTileReference>& references,
115 const std::vector<GraphicsEditTarget>& targets,
116 const zelda3::GameData* owner);
117
119 public:
122 absl::Status Render(Rom* rom, zelda3::GameData* game_data, int room_id,
123 const SheetPixels& sheets, bool objects_only,
124 zelda3::Room* live_room = nullptr);
125
126 const std::vector<RoomTileReference>& tile_references() const {
127 return tile_references_;
128 }
129 size_t unknown_objects() const { return unknown_objects_; }
130
131 int room_id() const { return room_id_; }
132 const gfx::Bitmap& bitmap() const { return composite_; }
135
137 std::array<uint8_t, 16> blocks() const;
140 std::vector<SDL_Rect> ObjectRectsUsingSheet(uint16_t sheet) const;
141 std::vector<SDL_Rect> ObjectRectsForObject(size_t index) const;
142 // Explicit loaded-room scan; never loads another room from ROM or creates a
143 // composite image. Rendering caches may refresh, authoring state is retained.
144 absl::Status TraceLoadedRoom(Rom* rom, zelda3::GameData* game_data,
145 zelda3::Room* room);
147 void Reset();
148
149 private:
150 void CaptureUsage(const zelda3::Room& room);
151 std::unique_ptr<zelda3::Room> room_;
152 std::array<uint8_t, 16> blocks_{};
153 std::vector<RoomTileReference> tile_references_;
154 std::map<size_t, SDL_Rect> object_rects_;
156 int room_id_ = -1;
158};
159
161 int room_id = -1;
162 std::vector<SourceObjectUse> objects;
163 size_t unknown_objects = 0;
164 std::string error;
165};
166
167std::vector<LoadedRoomSourceUses> FindLoadedRoomSourceUses(
168 Rom* rom, zelda3::GameData* game_data, const LiveRoomContexts& rooms,
169 const std::vector<GraphicsEditTarget>& targets);
170
171// ---------------------------------------------------------------------------
172// Sprite context
173// ---------------------------------------------------------------------------
174
176 public:
181 absl::Status Configure(Rom* rom, zelda3::GameData* game_data, int spriteset,
182 int palette_room, std::string hack_name = {});
183
185 void SetVanillaSprite(uint8_t sprite_id);
187 void SetZsm(const zsprite::ZSprite& zsm, int animation);
188 bool has_zsm() const { return zsm_.has_value(); }
189
191 int frame_count() const;
192 int frame() const { return frame_; }
193 void SetFrame(int frame);
196 bool Advance(float seconds);
197
199 PreviewImage RenderFrame(const SheetPixels& sheets) const;
200
201 const std::array<uint8_t, 8>& slots() const { return slots_; }
202 bool UsesSheet(uint16_t sheet) const;
203
204 private:
205 std::vector<uint8_t> SheetOrRom(const SheetPixels& sheets,
206 uint16_t sheet) const;
207
208 Rom* rom_ = nullptr;
210 std::array<uint8_t, 8> slots_{};
211 std::array<SDL_Color, 256> colors_{};
212 std::string hack_name_;
213 uint8_t sprite_id_ = 0;
214 std::optional<zsprite::ZSprite> zsm_;
215 int animation_ = 0;
216 int frame_ = 0;
217 float remainder_ = 0.0f;
218};
219
220// ---------------------------------------------------------------------------
221// Tile16 context
222// ---------------------------------------------------------------------------
223
225 public:
227 absl::Status Configure(Rom* rom, zelda3::GameData* game_data, int area);
228
231 std::vector<int> Tile16sUsingTile(uint16_t sheet, int tile_index) const;
232
236 PreviewImage Render(const SheetPixels& sheets,
237 const std::vector<int>& tile16s, uint16_t sheet,
238 int tile_index,
239 std::vector<SDL_Rect>* highlights = nullptr);
240
241 int area() const { return area_; }
242
243 private:
244 std::vector<int> TileIdsFor(uint16_t sheet, int tile_index) const;
245
246 Rom* rom_ = nullptr;
248 int area_ = -1;
249 std::unique_ptr<zelda3::OverworldMap> map_;
250 std::vector<gfx::Tile16> tiles16_;
251};
252
253} // namespace yaze::editor::usage_preview
254
255#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:49
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_
std::vector< int > Tile16sUsingTile(uint16_t sheet, int tile_index) const
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:70
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::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)
std::array< std::optional< zelda3::PaletteColorAddress >, 8 > palette_colors
std::vector< RoomTileReference > references