Skip to content

Commit 394abc1

Browse files
authored
gg: optimise the draw_rounded_rect_filled function (#26390)
1 parent 8745ab1 commit 394abc1

1 file changed

Lines changed: 23 additions & 59 deletions

File tree

‎vlib/gg/draw.c.v‎

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ pub fn (ctx &Context) draw_rounded_rect_empty(x f32, y f32, w f32, h f32, radius
435435
// `w` is the width, `h` is the height .
436436
// `radius` is the radius of the corner-rounding in pixels.
437437
// `c` is the color of the filled.
438+
// it divides the rounded rectangle into 3 shapes, the top part, the midle and the bottom part.
438439
pub fn (ctx &Context) draw_rounded_rect_filled(x f32, y f32, w f32, h f32, radius f32, c Color) {
439440
if w <= 0 || h <= 0 || radius < 0 {
440441
return
@@ -456,87 +457,50 @@ pub fn (ctx &Context) draw_rounded_rect_filled(x f32, y f32, w f32, h f32, radiu
456457
sy := y * ctx.scale
457458
width := w * ctx.scale
458459
height := h * ctx.scale
459-
// circle center coordinates
460-
ltx := sx + r
461-
lty := sy + r
462-
rtx := sx + width - r
463-
rty := lty
464-
rbx := rtx
465-
rby := sy + height - r
466-
lbx := ltx
467-
lby := rby
468460

469-
mut rad := f32(0)
470-
mut dx := f32(0)
471-
mut dy := f32(0)
461+
// left x coordinate
462+
lx := sx + r
463+
// right x coordinate
464+
rx := sx + width - r
465+
// top y coordinate
466+
ty := sy + r
467+
// bottom y coordinate
468+
by := sy + height - r
472469

473470
if r != 0 {
474-
// left top quarter
475-
sgl.begin_triangle_strip()
476-
for i in 0 .. 31 {
477-
rad = f32(math.radians(i * 3))
478-
dx = r * math.cosf(rad)
479-
dy = r * math.sinf(rad)
480-
sgl.v2f(ltx - dx, lty - dy)
481-
sgl.v2f(ltx, lty)
482-
}
483-
sgl.end()
471+
mut rad := f32(0)
472+
mut dx := f32(0)
473+
mut dy := f32(0)
484474

485-
// right top quarter
475+
// top part
486476
sgl.begin_triangle_strip()
487477
for i in 0 .. 31 {
488478
rad = f32(math.radians(i * 3))
489479
dx = r * math.cosf(rad)
490480
dy = r * math.sinf(rad)
491-
sgl.v2f(rtx + dx, rty - dy)
492-
sgl.v2f(rtx, rty)
481+
sgl.v2f(rx + dx, ty - dy)
482+
sgl.v2f(lx - dx, ty - dy)
493483
}
494484
sgl.end()
495485

496-
// right bottom quarter
486+
// bottom part
497487
sgl.begin_triangle_strip()
498488
for i in 0 .. 31 {
499489
rad = f32(math.radians(i * 3))
500490
dx = r * math.cosf(rad)
501491
dy = r * math.sinf(rad)
502-
sgl.v2f(rbx + dx, rby + dy)
503-
sgl.v2f(rbx, rby)
504-
}
505-
sgl.end()
506-
507-
// left bottom quarter
508-
sgl.begin_triangle_strip()
509-
for i in 0 .. 31 {
510-
rad = f32(math.radians(i * 3))
511-
dx = r * math.cosf(rad)
512-
dy = r * math.sinf(rad)
513-
sgl.v2f(lbx - dx, lby + dy)
514-
sgl.v2f(lbx, lby)
492+
sgl.v2f(rx + dx, by + dy)
493+
sgl.v2f(lx - dx, by + dy)
515494
}
516495
sgl.end()
517496
}
518497

519-
// Separate drawing is to prevent transparent color overlap
520-
// top rectangle
521-
sgl.begin_quads()
522-
sgl.v2f(ltx, sy)
523-
sgl.v2f(rtx, sy)
524-
sgl.v2f(rtx, rty)
525-
sgl.v2f(ltx, lty)
526-
sgl.end()
527-
// middle rectangle
528-
sgl.begin_quads()
529-
sgl.v2f(sx, lty)
530-
sgl.v2f(rtx + r, rty)
531-
sgl.v2f(rbx + r, rby)
532-
sgl.v2f(sx, lby)
533-
sgl.end()
534-
// bottom rectangle
498+
// Draw the iner rectangle
535499
sgl.begin_quads()
536-
sgl.v2f(lbx, lby)
537-
sgl.v2f(rbx, rby)
538-
sgl.v2f(rbx, rby + r)
539-
sgl.v2f(lbx, rby + r)
500+
sgl.v2f(sx, ty)
501+
sgl.v2f(rx + r, ty)
502+
sgl.v2f(rx + r, by)
503+
sgl.v2f(sx, by)
540504
sgl.end()
541505
}
542506

0 commit comments

Comments
 (0)