Rectangle with round corners in SDL

Hi,

I want to make a shape with round corners.

Is there any simple way of doing it with SDL?

Cheers,

Nuno

Hi,

I suggest using SDL_draw. If it’s a rectangle you need, SDL_draw has a
pre-made function for it. If not, you can probably combine the functions it
provides to achieve what you need. Check out what it’s capable of:
http://sdl-draw.sourceforge.net/screenshots.html

  • Janis> ----- Original Message -----

From: developer@imaginando.net (Nuno Santos)
To: "A list for developers using the SDL library. ((includes SDL-announce))"

Sent: Friday, December 08, 2006 11:07 PM
Subject: [SDL] Rectangle with round corners in SDL

Hi,

I want to make a shape with round corners.

Is there any simple way of doing it with SDL?

Cheers,

Nuno


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Dunno if there are easier ways, but the way i’d do it would be to draw
circles & drawing the shape over those circles, filling or deleting as
needed.

Say you want a quare with 4 rounded corners, i’d draw 4 circles, and
overlay the square on them.

CheersOn Fri, 2006-12-08 at 21:07 +0000, Nuno Santos wrote:

Hi,

I want to make a shape with round corners.

Is there any simple way of doing it with SDL?

Hello Nuno,

Friday, December 8, 2006, 9:07:27 PM, you wrote:

Hi,

I want to make a shape with round corners.

Is there any simple way of doing it with SDL?

Directly, no. You have to write your own routine.

To keep it simple, you want quarter-circle corners. You could get this
effect with the Bresenham circle routine quickly. You could adapt it
so as it draws each circle quadrant, it moves the points into each
corner. Then you only have to add 4 straight lines to complete it.

If you wanted any fancier, you would have to look into splines.–
Best regards,
Peter mailto:@Peter_Mulholland

Here is an implementation that can be easily changed to something more
generic.

Only problems, that I know of are these:

no clipping performed.
bytes per pixel is assumed to be 4.
this really is not very optimized.

but it works.

// draws a rounded box with…
// corner radius of ‘r’
// width of ‘w’
// and height of ‘h’
// draws the box right and down of…
// x-offset xo
// y-offset yo

// returns 0 if 2*r is bigger than w or h
// and draws nothing
// returns 1 on success

int
fill_rounded_box_b( SDL_Surface* dst, int xo, int yo,
int w, int h, int r, Uint32 color ) {
int yd = dst->pitch / dst->format->BytesPerPixel;
Uint32* pixels = NULL;

int x,y,i,j;
int rpsqrt2 = (int) (r / sqrt( 2 ) );
double r2 = r*r;

w /= 2;
h /= 2;

xo += w;
yo += h;

w -= r;
h -= r;

if( w < 0 || h < 0 )
return 0;

SDL_LockSurface( dst );
pixels = (Uint32*)( dst->pixels );

int sy = (yo-h)*yd;
int ey = (yo+h)*yd;
int sx = (xo-w);
int ex = (xo+w);
for( i = sy; i<=ey; i+=yd )
for( j = sx-r; j<=ex+r; j++ )
pixels[i+j] = color;

int d = -r;
int x2m1 = -1;
y = r;
for( x=0; x <= rpsqrt2; x++ ) {
x2m1 += 2;
d+= x2m1;
if( d >= 0 ) {
y–;
d -= (y*2);
}

for( i=sx-x; i<=ex+x; i++ )
  pixels[sy-y*yd + i] = color;

for( i=sx-y; i<=ex+y; i++ )
  pixels[sy-x*yd + i] = color;

for( i=sx-y; i<=ex+y; i++ )
  pixels[ey+x*yd + i] = color;

for( i=sx-x; i<=ex+x; i++ )
  pixels[ey+y*yd + i] = color;

}
SDL_UnlockSurface( dst );
return 1;
}On Sunday 10 December 2006 02:01, Peter Mulholland wrote:

Hello Nuno,

Friday, December 8, 2006, 9:07:27 PM, you wrote:

Hi,

I want to make a shape with round corners.

Is there any simple way of doing it with SDL?

Directly, no. You have to write your own routine.

To keep it simple, you want quarter-circle corners. You could get
this effect with the Bresenham circle routine quickly. You could
adapt it so as it draws each circle quadrant, it moves the points
into each corner. Then you only have to add 4 straight lines to
complete it.

If you wanted any fancier, you would have to look into splines.