yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas_rendering.cc
Go to the documentation of this file.
1#include "canvas_rendering.h"
2
3#include <algorithm>
4#include <cmath>
6
7namespace yaze {
8namespace gui {
9
10namespace {
11// Constants extracted from canvas.cc
12constexpr uint32_t kRectangleColor = IM_COL32(32, 32, 32, 255);
13constexpr uint32_t kWhiteColor = IM_COL32(255, 255, 255, 255);
14} // namespace
15
17 ImDrawList* draw_list,
18 const CanvasGeometry& geometry) {
19
20 // Draw border and background color (extracted from Canvas::DrawBackground)
21 draw_list->AddRectFilled(geometry.canvas_p0, geometry.canvas_p1, kRectangleColor);
22 draw_list->AddRect(geometry.canvas_p0, geometry.canvas_p1, kWhiteColor);
23}
24
26 ImDrawList* draw_list,
27 const CanvasGeometry& geometry,
28 const CanvasConfig& config,
29 int highlight_tile_id) {
30
31 if (!config.enable_grid) {
32 return;
33 }
34
35 // Create render context for utility functions (extracted from Canvas::DrawGrid)
37 .draw_list = draw_list,
38 .canvas_p0 = geometry.canvas_p0,
39 .canvas_p1 = geometry.canvas_p1,
40 .scrolling = geometry.scrolling,
41 .global_scale = config.global_scale,
42 .enable_grid = config.enable_grid,
43 .enable_hex_labels = config.enable_hex_labels,
44 .grid_step = config.grid_step};
45
46 // Use high-level utility function
47 CanvasUtils::DrawCanvasGrid(ctx, highlight_tile_id);
48}
49
51 ImDrawList* draw_list,
52 const CanvasGeometry& geometry,
53 const CanvasConfig& config,
54 const ImVector<ImVec2>& points,
55 const ImVector<ImVec2>& selected_points) {
56
57 // Create render context for utility functions (extracted from Canvas::DrawOverlay)
59 .draw_list = draw_list,
60 .canvas_p0 = geometry.canvas_p0,
61 .canvas_p1 = geometry.canvas_p1,
62 .scrolling = geometry.scrolling,
63 .global_scale = config.global_scale,
64 .enable_grid = config.enable_grid,
65 .enable_hex_labels = config.enable_hex_labels,
66 .grid_step = config.grid_step};
67
68 // Use high-level utility function
69 CanvasUtils::DrawCanvasOverlay(ctx, points, selected_points);
70}
71
73 ImDrawList* draw_list,
74 const CanvasGeometry& geometry,
75 const CanvasConfig& config,
76 const ImVector<ImVector<std::string>>& labels,
77 int current_labels,
78 int tile_id_offset) {
79
80 if (!config.enable_custom_labels || current_labels >= labels.size()) {
81 return;
82 }
83
84 // Push clip rect to prevent drawing outside canvas
85 draw_list->PushClipRect(geometry.canvas_p0, geometry.canvas_p1, true);
86
87 // Create render context for utility functions
89 .draw_list = draw_list,
90 .canvas_p0 = geometry.canvas_p0,
91 .canvas_p1 = geometry.canvas_p1,
92 .scrolling = geometry.scrolling,
93 .global_scale = config.global_scale,
94 .enable_grid = config.enable_grid,
95 .enable_hex_labels = config.enable_hex_labels,
96 .grid_step = config.grid_step};
97
98 // Use high-level utility function (extracted from Canvas::DrawInfoGrid)
99 CanvasUtils::DrawCanvasLabels(ctx, labels, current_labels, tile_id_offset);
100
101 draw_list->PopClipRect();
102}
103
105 ImDrawList* draw_list,
106 const CanvasGeometry& geometry,
107 gfx::Bitmap& bitmap,
108 int /*border_offset*/,
109 float scale) {
110
111 if (!bitmap.is_active()) {
112 return;
113 }
114
115 // Extracted from Canvas::DrawBitmap (border offset variant)
116 draw_list->AddImage(
117 (ImTextureID)(intptr_t)bitmap.texture(),
118 ImVec2(geometry.canvas_p0.x, geometry.canvas_p0.y),
119 ImVec2(geometry.canvas_p0.x + (bitmap.width() * scale),
120 geometry.canvas_p0.y + (bitmap.height() * scale)));
121 draw_list->AddRect(geometry.canvas_p0, geometry.canvas_p1, kWhiteColor);
122}
123
125 ImDrawList* draw_list,
126 const CanvasGeometry& geometry,
127 gfx::Bitmap& bitmap,
128 int x_offset,
129 int y_offset,
130 float scale,
131 int alpha) {
132
133 if (!bitmap.is_active()) {
134 return;
135 }
136
137 // Calculate the actual rendered size including scale and offsets
138 // CRITICAL: Use scale parameter (NOT global_scale_) for per-bitmap scaling
139 // Extracted from Canvas::DrawBitmap (x/y offset variant)
140 ImVec2 rendered_size(bitmap.width() * scale, bitmap.height() * scale);
141
142 // CRITICAL FIX: Draw bitmap WITHOUT additional global_scale multiplication
143 // The scale parameter already contains the correct scale factor
144 // The scrolling should NOT be scaled - it's already in screen space
145 draw_list->AddImage(
146 (ImTextureID)(intptr_t)bitmap.texture(),
147 ImVec2(geometry.canvas_p0.x + x_offset + geometry.scrolling.x,
148 geometry.canvas_p0.y + y_offset + geometry.scrolling.y),
149 ImVec2(geometry.canvas_p0.x + x_offset + geometry.scrolling.x + rendered_size.x,
150 geometry.canvas_p0.y + y_offset + geometry.scrolling.y + rendered_size.y),
151 ImVec2(0, 0), ImVec2(1, 1), IM_COL32(255, 255, 255, alpha));
152}
153
155 ImDrawList* draw_list,
156 const CanvasGeometry& geometry,
157 gfx::Bitmap& bitmap,
158 ImVec2 dest_pos,
159 ImVec2 dest_size,
160 ImVec2 src_pos,
161 ImVec2 src_size) {
162
163 if (!bitmap.is_active()) {
164 return;
165 }
166
167 // Extracted from Canvas::DrawBitmap (custom source/dest regions variant)
168 draw_list->AddImage(
169 (ImTextureID)(intptr_t)bitmap.texture(),
170 ImVec2(geometry.canvas_p0.x + dest_pos.x, geometry.canvas_p0.y + dest_pos.y),
171 ImVec2(geometry.canvas_p0.x + dest_pos.x + dest_size.x,
172 geometry.canvas_p0.y + dest_pos.y + dest_size.y),
173 ImVec2(src_pos.x / bitmap.width(), src_pos.y / bitmap.height()),
174 ImVec2((src_pos.x + src_size.x) / bitmap.width(),
175 (src_pos.y + src_size.y) / bitmap.height()));
176}
177
179 ImDrawList* draw_list,
180 const CanvasGeometry& geometry,
181 std::vector<int>& group,
182 gfx::Tilemap& tilemap,
183 int tile_size,
184 float scale,
185 int local_map_size,
186 ImVec2 total_map_size) {
187
188 // Extracted from Canvas::DrawBitmapGroup (lines 1148-1264)
189 // This is used for multi-tile selection preview in overworld editor
190
191 if (group.empty()) {
192 return;
193 }
194
195 // OPTIMIZATION: Use optimized rendering for large groups to improve performance
196 bool use_optimized_rendering = group.size() > 128;
197
198 // Pre-calculate common values to avoid repeated computation
199 const float tile_scale = tile_size * scale;
200 const int atlas_tiles_per_row = tilemap.atlas.width() / tilemap.tile_size.x;
201
202 // Get selected points (note: this assumes selected_points are available in context)
203 // For now, we'll just render tiles at their grid positions
204 // The full implementation would need the selected_points passed in
205
206 int i = 0;
207 for (const auto tile_id : group) {
208 // Calculate grid position for this tile
209 int tiles_per_row = 32; // Default for standard maps
210 int x = i % tiles_per_row;
211 int y = i / tiles_per_row;
212
213 int tile_pos_x = x * tile_size * scale;
214 int tile_pos_y = y * tile_size * scale;
215
216 // Check if tile_id is within the range
217 auto tilemap_size = tilemap.map_size.x;
218 if (tile_id >= 0 && tile_id < tilemap_size) {
219 if (tilemap.atlas.is_active() && tilemap.atlas.texture() &&
220 atlas_tiles_per_row > 0) {
221 int atlas_tile_x = (tile_id % atlas_tiles_per_row) * tilemap.tile_size.x;
222 int atlas_tile_y = (tile_id / atlas_tiles_per_row) * tilemap.tile_size.y;
223
224 // Simple bounds check
225 if (atlas_tile_x >= 0 && atlas_tile_x < tilemap.atlas.width() &&
226 atlas_tile_y >= 0 && atlas_tile_y < tilemap.atlas.height()) {
227
228 // Calculate UV coordinates once for efficiency
229 const float atlas_width = static_cast<float>(tilemap.atlas.width());
230 const float atlas_height = static_cast<float>(tilemap.atlas.height());
231 ImVec2 uv0 = ImVec2(atlas_tile_x / atlas_width, atlas_tile_y / atlas_height);
232 ImVec2 uv1 = ImVec2((atlas_tile_x + tilemap.tile_size.x) / atlas_width,
233 (atlas_tile_y + tilemap.tile_size.y) / atlas_height);
234
235 // Calculate screen positions
236 float screen_x = geometry.canvas_p0.x + geometry.scrolling.x + tile_pos_x;
237 float screen_y = geometry.canvas_p0.y + geometry.scrolling.y + tile_pos_y;
238 float screen_w = tilemap.tile_size.x * scale;
239 float screen_h = tilemap.tile_size.y * scale;
240
241 // Use higher alpha for large selections to make them more visible
242 uint32_t alpha_color = use_optimized_rendering
243 ? IM_COL32(255, 255, 255, 200)
244 : IM_COL32(255, 255, 255, 150);
245
246 // Draw from atlas texture with optimized parameters
247 draw_list->AddImage(
248 (ImTextureID)(intptr_t)tilemap.atlas.texture(),
249 ImVec2(screen_x, screen_y),
250 ImVec2(screen_x + screen_w, screen_y + screen_h),
251 uv0, uv1, alpha_color);
252 }
253 }
254 }
255 i++;
256 }
257}
258
259} // namespace gui
260} // namespace yaze
261
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:66
TextureHandle texture() const
Definition bitmap.h:289
bool is_active() const
Definition bitmap.h:293
int height() const
Definition bitmap.h:283
int width() const
Definition bitmap.h:282
void DrawCanvasLabels(const CanvasRenderContext &ctx, const ImVector< ImVector< std::string > > &labels, int current_labels, int tile_id_offset)
void DrawCanvasOverlay(const CanvasRenderContext &ctx, const ImVector< ImVec2 > &points, const ImVector< ImVec2 > &selected_points)
void DrawCanvasGrid(const CanvasRenderContext &ctx, int highlight_tile_id)
constexpr uint32_t kWhiteColor
Definition canvas.cc:96
constexpr uint32_t kRectangleColor
Definition canvas.cc:95
void RenderBitmapGroup(ImDrawList *draw_list, const CanvasGeometry &geometry, std::vector< int > &group, gfx::Tilemap &tilemap, int tile_size, float scale, int local_map_size, ImVec2 total_map_size)
Render group of bitmaps from tilemap.
void RenderCanvasLabels(ImDrawList *draw_list, const CanvasGeometry &geometry, const CanvasConfig &config, const ImVector< ImVector< std::string > > &labels, int current_labels, int tile_id_offset)
Render canvas labels on grid.
void RenderCanvasBackground(ImDrawList *draw_list, const CanvasGeometry &geometry)
Render canvas background and border.
void RenderCanvasGrid(ImDrawList *draw_list, const CanvasGeometry &geometry, const CanvasConfig &config, int highlight_tile_id)
Render canvas grid with optional highlighting.
void RenderBitmapOnCanvas(ImDrawList *draw_list, const CanvasGeometry &geometry, gfx::Bitmap &bitmap, int, float scale)
Render bitmap on canvas (border offset variant)
void RenderCanvasOverlay(ImDrawList *draw_list, const CanvasGeometry &geometry, const CanvasConfig &config, const ImVector< ImVec2 > &points, const ImVector< ImVec2 > &selected_points)
Render canvas overlay (hover and selection points)
Main namespace for the application.
Definition controller.cc:20
int y
Y coordinate or height.
Definition tilemap.h:20
int x
X coordinate or width.
Definition tilemap.h:19
Tilemap structure for SNES tile-based graphics management.
Definition tilemap.h:109
Pair tile_size
Size of individual tiles (8x8 or 16x16)
Definition tilemap.h:113
Pair map_size
Size of tilemap in tiles.
Definition tilemap.h:114
Bitmap atlas
Master bitmap containing all tiles.
Definition tilemap.h:110
Unified configuration for canvas display and interaction.
Canvas geometry calculated per-frame.