summaryrefslogtreecommitdiff
path: root/scene/main
diff options
context:
space:
mode:
authorDanil Alexeev <danil@alexeev.xyz>2023-02-08 10:44:12 +0300
committerDanil Alexeev <danil@alexeev.xyz>2023-02-08 10:44:12 +0300
commitb8eda519d0701f2700976ab71d58cd96f35b0181 (patch)
treeeaf4e82aedda21265f6aa1a750e7685224b9dc76 /scene/main
parenta05670c617abc1df93ebac5afacf3e6c48008a99 (diff)
Optimize `draw_dashed_line()` and `draw_rect()`
Diffstat (limited to 'scene/main')
-rw-r--r--scene/main/canvas_item.cpp53
1 files changed, 20 insertions, 33 deletions
diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp
index 906b478eb3..e5dcdd2afd 100644
--- a/scene/main/canvas_item.cpp
+++ b/scene/main/canvas_item.cpp
@@ -530,10 +530,18 @@ void CanvasItem::draw_dashed_line(const Point2 &p_from, const Point2 &p_to, cons
if (p_aligned) {
off += (p_to - p_from).normalized() * (length - steps * p_dash) / 2.0;
}
+
+ Vector<Vector2> points;
+ points.resize(steps + 1);
for (int i = 0; i < steps; i += 2) {
- RenderingServer::get_singleton()->canvas_item_add_line(canvas_item, (i == 0) ? p_from : off, (p_aligned && i == steps - 1) ? p_to : (off + step), p_color, p_width);
+ points.write[i] = (i == 0) ? p_from : off;
+ points.write[i + 1] = (p_aligned && i == steps - 1) ? p_to : (off + step);
off += step * 2;
}
+
+ Vector<Color> colors = { p_color };
+
+ RenderingServer::get_singleton()->canvas_item_add_multiline(canvas_item, points, colors, p_width);
}
void CanvasItem::draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, real_t p_width, bool p_antialiased) {
@@ -599,38 +607,17 @@ void CanvasItem::draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_fil
} else if (p_width >= rect.size.width || p_width >= rect.size.height) {
RenderingServer::get_singleton()->canvas_item_add_rect(canvas_item, rect.grow(0.5f * p_width), p_color);
} else {
- // Thick lines are offset depending on their width to avoid partial overlapping.
- // Thin lines are drawn without offset. The result may not be perfect.
- real_t offset = (p_width >= 0) ? 0.5f * p_width : 0.0f;
-
- // Top line.
- RenderingServer::get_singleton()->canvas_item_add_line(
- canvas_item,
- rect.position + Size2(-offset, 0),
- rect.position + Size2(-offset + rect.size.width, 0),
- p_color,
- p_width);
- // Right line.
- RenderingServer::get_singleton()->canvas_item_add_line(
- canvas_item,
- rect.position + Size2(rect.size.width, -offset),
- rect.position + Size2(rect.size.width, -offset + rect.size.height),
- p_color,
- p_width);
- // Bottom line.
- RenderingServer::get_singleton()->canvas_item_add_line(
- canvas_item,
- rect.position + Size2(offset + rect.size.width, rect.size.height),
- rect.position + Size2(offset, rect.size.height),
- p_color,
- p_width);
- // Left line.
- RenderingServer::get_singleton()->canvas_item_add_line(
- canvas_item,
- rect.position + Size2(0, offset + rect.size.height),
- rect.position + Size2(0, offset),
- p_color,
- p_width);
+ Vector<Vector2> points;
+ points.resize(5);
+ points.write[0] = rect.position;
+ points.write[1] = rect.position + Vector2(rect.size.x, 0);
+ points.write[2] = rect.position + rect.size;
+ points.write[3] = rect.position + Vector2(0, rect.size.y);
+ points.write[4] = rect.position;
+
+ Vector<Color> colors = { p_color };
+
+ RenderingServer::get_singleton()->canvas_item_add_polyline(canvas_item, points, colors, p_width);
}
}