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