Skip to content

Commit 6592686

Browse files
committed
#17 avoid dynamic allocation
1 parent ca0b529 commit 6592686

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

Sources/vger/vger.mm

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ void vgerStrokeBezier(vgerContext vg, vgerBezierSegment s, float width, vgerPain
302302
}
303303

304304
// Generate a single closed path from all segments
305-
std::vector<float2> top_points, bottom_points;
305+
vg->top_points.clear();
306+
vg->bottom_points.clear();
306307

307308
for (const auto& seg : vg->segments) {
308309
// Calculate tangent vectors for this segment
@@ -323,42 +324,42 @@ void vgerStrokeBezier(vgerContext vg, vgerBezierSegment s, float width, vgerPain
323324
}
324325

325326
// Add points for top edge of stroke (left side as we trace the curve)
326-
if (top_points.empty()) {
327-
top_points.push_back(seg.a - d0); // Start point
327+
if (vg->top_points.empty()) {
328+
vg->top_points.push_back(seg.a - d0); // Start point
328329
}
329-
top_points.push_back(seg.b - d1); // Control point
330-
top_points.push_back(seg.c - d2); // End point
330+
vg->top_points.push_back(seg.b - d1); // Control point
331+
vg->top_points.push_back(seg.c - d2); // End point
331332

332333
// Add points for bottom edge of stroke (right side, will be traced in reverse)
333-
if (bottom_points.empty()) {
334-
bottom_points.push_back(seg.a + d0); // Start point
334+
if (vg->bottom_points.empty()) {
335+
vg->bottom_points.push_back(seg.a + d0); // Start point
335336
}
336-
bottom_points.push_back(seg.b + d1); // Control point
337-
bottom_points.push_back(seg.c + d2); // End point
337+
vg->bottom_points.push_back(seg.b + d1); // Control point
338+
vg->bottom_points.push_back(seg.c + d2); // End point
338339
}
339340

340341
// Create single closed path by tracing top edge forward, then bottom edge backward
341-
vgerMoveTo(vg, top_points[0]);
342+
vgerMoveTo(vg, vg->top_points[0]);
342343

343344
// Trace top edge with quadratic bezier segments
344-
for (size_t i = 1; i < top_points.size(); i += 2) {
345-
if (i + 1 < top_points.size()) {
346-
vgerQuadTo(vg, top_points[i], top_points[i + 1]);
345+
for (size_t i = 1; i < vg->top_points.size(); i += 2) {
346+
if (i + 1 < vg->top_points.size()) {
347+
vgerQuadTo(vg, vg->top_points[i], vg->top_points[i + 1]);
347348
}
348349
}
349350

350351
// Connect to bottom edge
351-
vgerLineTo(vg, bottom_points.back());
352+
vgerLineTo(vg, vg->bottom_points.back());
352353

353354
// Trace bottom edge in reverse with quadratic bezier segments
354-
for (int i = (int)bottom_points.size() - 2; i >= 1; i -= 2) {
355+
for (int i = (int)vg->bottom_points.size() - 2; i >= 1; i -= 2) {
355356
if (i - 1 >= 0) {
356-
vgerQuadTo(vg, bottom_points[i], bottom_points[i - 1]);
357+
vgerQuadTo(vg, vg->bottom_points[i], vg->bottom_points[i - 1]);
357358
}
358359
}
359360

360361
// Close the path
361-
vgerLineTo(vg, top_points[0]);
362+
vgerLineTo(vg, vg->top_points[0]);
362363
vgerFill(vg, paint);
363364

364365
#else

Sources/vger/vger_private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ struct vger {
129129
/// Used in vgerStrokeBezier.
130130
std::vector<vgerBezierSegment> segments;
131131

132+
/// Used in vgerStrokeBezier.
133+
std::vector<float2> top_points, bottom_points;
134+
132135
vger(uint32_t flags, MTLPixelFormat pixelFormat);
133136

134137
void addPrim(const vgerPrim& prim) {

0 commit comments

Comments
 (0)