Drawing rounded rectangles

by Richard Russell, January 2021

Neither BBC BASIC for Windows nor BBC BASIC for SDL 2.0 supports drawing anti-aliased rounded rectangles (that is, boxes with radiused corners). Fortunately it is easy enough to decompose that shape into simpler components (straight lines and 90° arcs) which can be drawn using the supplied anti-aliased graphics libraries (gdiplib.bbc or aagfxlib.bbc respectively).

Here are the two routines. The parameters are the x and y coordinates of the bottom left-hand corner, the width and height, the corner radius (all in BBC BASIC graphics units), the line thickness (pixels) and the colour. Note that the colour value has a different format depending on the library, &AARRGGBB for gdiplib and &AABBGGRR for aagfxlib.

BB4W version:

      DEF PROCroundrect(x, y, w, h, r, t, C%)
      LOCAL pen%
      pen% = FN_gdipcreatepen(C%, 0, t)
      PROC_gdiparc(pen%, x+r,   y+h-r, r, r, 180, 90)
      PROC_gdiparc(pen%, x+w-r, y+h-r, r, r, 270, 90)
      PROC_gdiparc(pen%, x+r,   y+r,   r, r, 90,  90)
      PROC_gdiparc(pen%, x+w-r, y+r,   r, r, 0,   90)
      r -= 0.3 : REM overlap 0.15 pixels
      PROC_gdipline(pen%, x+r, y+h, x+w-r, y+h)
      PROC_gdipline(pen%, x+r, y,   x+w-r, y)
      PROC_gdipline(pen%, x,   y+r, x,   y+h-r)
      PROC_gdipline(pen%, x+w, y+r, x+w, y+h-r)
      PROC_gdipdeletepen(pen%)
      ENDPROC

BBCSDL version:

      DEF PROCroundrect(x, y, w, h, r, t, C%)
      PROC_aaarc(x+r,   y+h-r, r, r, 180, 90, t, C%, 0)
      PROC_aaarc(x+w-r, y+h-r, r, r, 270, 90, t, C%, 0)
      PROC_aaarc(x+r,   y+r,   r, r, 90,  90, t, C%, 0)
      PROC_aaarc(x+w-r, y+r,   r, r, 0,   90, t, C%, 0)
      r -= 0.3 : REM overlap 0.15 pixels
      PROC_aaline(x+r, y+h, x+w-r, y+h, t, C%, 0)
      PROC_aaline(x+r, y,   x+w-r, y,   t, C%, 0)
      PROC_aaline(x,   y+r, x,   y+h-r, t, C%, 0)
      PROC_aaline(x+w, y+r, x+w, y+h-r, t, C%, 0)
      ENDPROC