You are not logged in.

#1 2009-06-30 12:14:51

suloku
Member

GRRLIB_RoundRectangle

I've coded this little function based on GRRLIB_Circle().

It is really versatile, because you can actually not only draw rectangles with rounded corners, but also normal rectangles, circles and elipses.

The radius is limited by the rectangle's total width and height.

Now I only need some help on making this function able to fill it with a gradient instead of a plain color.

Rounded rectangle example:

GRRLIB_RoundRectangle(100, 100, 20, 20, 168, 48, 0xFFFFFFFF, 1);

Normal rectangle example:
GRRLIB_RoundRectangle(100, 100, 0, 0, 168, 48, 0xFFFFFFFF, 1);

Circle example:
GRRLIB_RoundRectangle(100, 100, 50, 50, 0, 0, 0xFFFFFFFF, 1);

Elipse example:
GRRLIB_RoundRectangle(100, 100, 25, 50, 0, 0, 0xFFFFFFFF, 1);

Code:

/**
 * Draw a round-cornered rectangle.
 * @author suloku
 * @param x Specifies the x-coordinate of the center of the rectangle.
 * @param y Specifies the y-coordinate of the center of the rectangle.
 * @param radius_x The horizontal radius of the corners.
 * @param radius_y The vertical radius of the corners.
 * @param w Specifies the total width
 * @param w Specifies the total height
 * @param color The color of the circle in RGBA format.
 * @param filled Set to true to fill the circle.
 */
inline void GRRLIB_RoundRectangle(f32 x, f32 y, f32 radius_x, f32 radius_y, u32 w, u32 h, u32 color, u8 filled) {
    Vector v[40];
    u32 ncolor[40];
    u32 a;
    f32 ra;
    f32 G_DTOR = M_DTOR * 10;

    if(w!=0){
        while(1){
            if (w-(radius_x*2)>=0){
                break;
            }
                radius_x--;
        }
        w = w-(radius_x*2);
        x+= w/2;
    }
    
    if(w!=0){
        while(1){
            if (h-(radius_y*2)>=0){
                break;
            }
                radius_y--;
        }
        h = h-(radius_y*2);
        y+= h/2;
    }
    

    for (a = 0; a < 9; a++) {
        ra = (a) * G_DTOR;

        v[a].x = cos(ra) * radius_x + x;
        v[a].y = sin(ra) * radius_y + y;
        v[a].z = 0.0f;
        ncolor[a]  = color;
    }
    
    //horizontal bottom line
        v[9].x = x-w;
        v[9].y = v[8].y;
        v[9].z = 0.0f;
        ncolor[9]  = color;
        
    for (a = 10; a < 19; a++) {
        ra = (a-1) * G_DTOR;

        v[a].x = cos(ra) * radius_x + x-w;
        v[a].y = sin(ra) * radius_y + y;
        v[a].z = 0.0f;
        ncolor[a]  = color;
    }

        //vertical left line
        v[19].x = v[18].x;
        v[19].y = y-h;
        v[19].z = 0.0f;
        ncolor[19]  = color;
        
    for (a = 20; a < 29; a++) {
        ra = (a-2) * G_DTOR;

        v[a].x = cos(ra) * radius_x + x-w;
        v[a].y = sin(ra) * radius_y + y-h;
        v[a].z = 0.0f;
        ncolor[a]  = color;
    }
    
        //horizontal top line
        v[29].x = v[8].x;
        v[29].y = v[28].y;
        v[29].z = 0.0f;
        ncolor[29]  = color;
        
    for (a = 30; a < 39; a++) {
        ra = (a-3) * G_DTOR;

        v[a].x = cos(ra) * radius_x + x;
        v[a].y = sin(ra) * radius_y + y-h;
        v[a].z = 0.0f;
        ncolor[a]  = color;
    }
    
        //vertical right line
        v[39].x = v[0].x;
        v[39].y = v[0].y;
        v[39].z = 0.0f;
        ncolor[39]  = color;
    
    if (!filled) {
        GRRLIB_GXEngine(v, ncolor, 40, GX_LINESTRIP);
    }
    else {
        GRRLIB_GXEngine(v, ncolor, 40, GX_TRIANGLEFAN);
    }
}

Offline

 

#2 2009-07-20 16:10:07

BlueChip
Moderator

Re: GRRLIB_RoundRectangle

Vote: yes


I can be found on efnet, freenode, msn, gtalk, aim, ychat & icq ...PM me for details

Offline

 

Board footer

Powered by FluxBB