yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
render_service.h
Go to the documentation of this file.
1#ifndef YAZE_APP_SERVICE_RENDER_SERVICE_H_
2#define YAZE_APP_SERVICE_RENDER_SERVICE_H_
3
4#include <cstdint>
5#include <map>
6#include <mutex>
7#include <string>
8#include <vector>
9
10#include "absl/status/statusor.h"
11#include "absl/strings/string_view.h"
12#include "app/gfx/core/bitmap.h"
13#include "rom/rom.h"
14#include "zelda3/dungeon/room.h"
15#include "zelda3/game_data.h"
17
18namespace yaze {
19namespace app {
20namespace service {
21
22// Overlay bitmask — callers OR these together in RenderRequest::overlay_flags.
23// Adding new overlays costs nothing for existing callers.
24namespace RenderOverlay {
25constexpr uint32_t kNone = 0;
26constexpr uint32_t kCollision = 1 << 0; // custom collision tile IDs
27constexpr uint32_t kSprites = 1 << 1; // sprite positions
28constexpr uint32_t kObjects = 1 << 2; // tile object extents
29constexpr uint32_t kTrack = 1 << 3; // minecart track tiles
30constexpr uint32_t kCameraQuads = 1 << 4; // camera quadrant boundaries
31constexpr uint32_t kGrid = 1 << 5; // 8×8 tile grid
32constexpr uint32_t kTileTypes = 1 << 6; // vanilla pits ($7F2000/$7F3000)
33constexpr uint32_t kAll = ~0u;
34} // namespace RenderOverlay
35
36// Overworld overlay bitmask for OverworldRenderRequest::overlay_flags.
37namespace OverworldOverlay {
38constexpr uint32_t kNone = 0;
39constexpr uint32_t kSprites = 1 << 0; // sprite list entries (id labels)
40constexpr uint32_t kEntrances = 1 << 1; // entrance tiles (E + entrance id)
41constexpr uint32_t kExits = 1 << 2; // exit player spawns (X + room id)
42constexpr uint32_t kHoles = 1 << 3; // hole entrances (H + entrance id)
43constexpr uint32_t kItems = 1 << 4; // hidden items (I + item id)
44constexpr uint32_t kGrid = 1 << 5; // 16px grid + screen boundaries
45constexpr uint32_t kAll = ~0u;
46} // namespace OverworldOverlay
47
49 int screen_id = 0; // any screen in the area; children resolve to parent
51 int phase = -1; // sprite game state 0..2; -1 draws every phase
52 float scale = 1.0f; // Finite [0.25, 8.0]; 1.0 = 512px per screen.
53 // Composite the area's subscreen overlay (sky, fog, lava, canopy, ...) the
54 // way the game layers it: background overlays show through backdrop pixels,
55 // foreground overlays blend on top.
56 bool area_overlay = true;
57 // Raw runtime graphics (a project's [files] overworld_graphics_preview),
58 // keyed by parent map. Applied to the area before drawing; empty = none.
60};
61
62// One overlay marker drawn on an overworld render, in area-local pixels.
64 std::string kind; // sprite, entrance, exit, hole, item
65 int id = 0; // sprite id, entrance id, exit room id, item id
66 int phase = -1; // sprites only
67 int list_index = -1; // sprites: index within the phase list
68 int x = 0;
69 int y = 0;
70};
71
73 std::vector<uint8_t> png_data;
74 int width = 0;
75 int height = 0;
78 std::string area_size; // small, large, wide, tall
79 int subscreen_overlay = -1; // overlay screen composited, -1 = none
80 bool graphics_preview = false; // request's preview covered this area
81 std::vector<int> screens;
82 std::vector<OverworldRenderMarker> markers;
83};
84
86 int room_id = 0;
88 float scale = 1.0f; // Finite [0.25, 8.0]; 1.0 = 512×512 native.
89 // Unsaved sheet pixels (8bpp, 4096 bytes per sheet) to render instead of
90 // GameData::graphics_buffer. See Room::SetGraphicsSheetOverrides().
91 std::map<uint16_t, std::vector<uint8_t>> sheet_overrides;
92};
93
94// Shared strict parser for CLI/API scale input. Rejects malformed, non-finite,
95// and out-of-range values instead of silently clamping or using a default.
96absl::StatusOr<float> ParseRenderScale(absl::string_view value);
97
99 std::vector<uint8_t> png_data;
100 int width = 0;
101 int height = 0;
102};
103
104// Metadata about a dungeon room — no rendering required.
106 int room_id = 0;
107 uint8_t blockset = 0;
108 uint8_t spriteset = 0;
109 uint8_t palette = 0;
110 int layout_id = 0;
111 int effect = 0;
112 int collision = 0;
113 int tag1 = 0;
114 int tag2 = 0;
115 uint16_t message_id = 0;
119};
120
121// Renders dungeon rooms to PNG images headlessly (no ImGui or GPU required).
122// rom and game_data are non-owning; caller must keep them alive.
123// Thread-safe: each RenderDungeonRoom call is protected by an internal mutex.
125 public:
126 RenderService(Rom* rom, zelda3::GameData* game_data);
127
128 // Render room_id to PNG with the requested overlays at the given scale.
129 absl::StatusOr<RenderResult> RenderDungeonRoom(const RenderRequest& req);
130
131 // Render the whole overworld area containing screen_id (ZSCustomOverworld
132 // large/wide/tall aware) from current ROM data, with optional overlays.
133 absl::StatusOr<OverworldRenderResult> RenderOverworldArea(
134 const OverworldRenderRequest& req);
135
136 // Return metadata for a room without rendering.
137 absl::StatusOr<RoomMetadata> GetDungeonRoomMetadata(int room_id);
138
139 private:
140 // Convert the composite Bitmap's indexed+palette surface to an RGBA buffer.
141 absl::StatusOr<std::vector<uint8_t>> BitmapToRgba(const gfx::Bitmap& bitmap,
142 int width, int height);
143
144 // Paint requested overlays into an RGBA buffer (CPU rasterizer).
145 void ApplyOverlays(std::vector<uint8_t>& rgba, int width, int height,
146 const zelda3::Room& room, uint32_t flags, float scale);
147
148 // Encode an RGBA buffer to PNG bytes.
149 absl::StatusOr<std::vector<uint8_t>> EncodePng(
150 const std::vector<uint8_t>& rgba, int width, int height);
151
152 Rom* rom_; // non-owning
154 mutable std::mutex mu_;
155};
156
157} // namespace service
158} // namespace app
159} // namespace yaze
160
161#endif // YAZE_APP_SERVICE_RENDER_SERVICE_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
absl::StatusOr< std::vector< uint8_t > > EncodePng(const std::vector< uint8_t > &rgba, int width, int height)
RenderService(Rom *rom, zelda3::GameData *game_data)
absl::StatusOr< RoomMetadata > GetDungeonRoomMetadata(int room_id)
absl::StatusOr< OverworldRenderResult > RenderOverworldArea(const OverworldRenderRequest &req)
void ApplyOverlays(std::vector< uint8_t > &rgba, int width, int height, const zelda3::Room &room, uint32_t flags, float scale)
absl::StatusOr< std::vector< uint8_t > > BitmapToRgba(const gfx::Bitmap &bitmap, int width, int height)
absl::StatusOr< RenderResult > RenderDungeonRoom(const RenderRequest &req)
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:70
absl::StatusOr< float > ParseRenderScale(absl::string_view value)
std::map< int, AreaGraphicsPreview > OverworldGraphicsPreview
zelda3::OverworldGraphicsPreview graphics_preview
std::vector< OverworldRenderMarker > markers
std::map< uint16_t, std::vector< uint8_t > > sheet_overrides
std::vector< uint8_t > png_data