You are not logged in.

#1 2009-03-29 20:49:53

Morukutsu
Member

GRRLIB_GradientRectangle()

Just a small func to draw Gradient rectangles smile

Code:

extern void GRRLIB_GradientRectangle(f32 x, f32 y, f32 width, f32 height, u32 color1, u32 color2, u32 color3, u32 color4, u8 filled);

Code:

inline void GRRLIB_GradientRectangle(f32 x, f32 y, f32 width, f32 height, u32 color1, u32 color2, u32 color3, u32 color4, u8 filled) {
    f32 x2 = x+width;
    f32 y2 = y+height;
    Vector v[] = {{x,y,0.0f}, {x2,y,0.0f}, {x2,y2,0.0f}, {x,y2,0.0f}, {x,y,0.0f}};
    u32 ncolor[]= {color1,color2,color3,color4,color1};

    if (!filled) {
        GRRLIB_NGone(v, ncolor, 5);
    }
    else {
        GRRLIB_NGoneFilled(v, ncolor, 4);
    }
}

Offline

 

#2 2009-07-20 16:15:37

BlueChip
Moderator

Re: GRRLIB_GradientRectangle()

Need to pass an array of the colours - that should allow a gradient of !=4 colours
for the colour count, you can either pass in the cnt, or define a colour (such as 0xABCDEF00 <- alpha 0) to mean "end of list"


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

Offline

 

#3 2009-09-02 18:24:44

elisherer
Member

Re: GRRLIB_GradientRectangle()

How about that one?

Code:

#define GRRLIB_GRAD_DIR_TOPBOTTOM 1 //default
#define GRRLIB_GRAD_DIR_BOTTOMTOP 2
#define GRRLIB_GRAD_DIR_LEFTRIGHT 3
#define GRRLIB_GRAD_DIR_RIGHTLEFT 4

void GRRLIB_RectangleGradient(f32 x, f32 y, f32 width, f32 height, u32 color1, u32 color2, u8 direction)
{
    guVector poly[4]={{x,y,0},{x+width,y,0},{x+width,y+height,0},{x,y+height,0}};
    u32 cpoly[4]={color1,color1,color2,color2}; //deafult GRRLIB_GRAD_DIR_TOPBOTTOM
    
    switch (direction)
    {
        case GRRLIB_GRAD_DIR_BOTTOMTOP:
            cpoly[0]=color2; cpoly[1]=color2;
            cpoly[3]=color1; cpoly[2]=color1;
            break;
        case GRRLIB_GRAD_DIR_LEFTRIGHT:
            cpoly[0]=color1; cpoly[2]=color2;
            cpoly[3]=color1; cpoly[2]=color1;
            break;
        case GRRLIB_GRAD_DIR_RIGHTLEFT:
            cpoly[2]=color2; cpoly[1]=color2;
            cpoly[2]=color1; cpoly[1]=color1;
            break;
        default: break;
    }
    GRRLIB_NGoneFilled(poly,cpoly,4);
}

and more modes can be added...

Offline

 

#4 2009-09-02 18:39:30

Crayon
Bad Mother Fucker

Re: GRRLIB_GradientRectangle()

Thanks for the code, but I have a question.
In some places you affect two times some variables:

Code:

            cpoly[2]=color2; cpoly[1]=color2;
            cpoly[2]=color1; cpoly[1]=color1;

The first line is useless because it's getting overwritten by the second one?

Offline

 

#5 2009-09-02 18:44:30

elisherer
Member

Re: GRRLIB_GradientRectangle()

Crayon wrote:

Thanks for the code, but I have a question.
In some places you affect two times some variables:

Code:

            cpoly[2]=color2; cpoly[1]=color2;
            cpoly[2]=color1; cpoly[1]=color1;

The first line is useless because it's getting overwritten by the second one?

you are right, that can be avoided, and by being so, optimized.

Offline

 

#6 2009-09-03 17:04:19

BlueChip
Moderator

Re: GRRLIB_GradientRectangle()

Aside of the (cut'n'paste?) errors the switch statement is lacking a case and a default and the type really should be enumerated to allow the compiler to check things are OK.  Any chance of uploading an image with an example of all 4 shades?


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

Offline

 

#7 2009-09-03 18:12:34

Crayon
Bad Mother Fucker

Re: GRRLIB_GradientRectangle()

BlueChip, the default values are set before entering the switch with:

Code:

u32 cpoly[4]={color1,color1,color2,color2}; //deafult GRRLIB_GRAD_DIR_TOPBOTTOM

It's an unusual way to proceed, but it works...
But I agree with you, it would be easier to understand with this:

Code:

        case default: // GRRLIB_GRAD_DIR_TOPBOTTOM
            cpoly[0]=color1; cpoly[1]=color1;
            cpoly[2]=color2; cpoly[3]=color2;

Offline

 

#8 2009-09-03 21:41:38

BlueChip
Moderator

Re: GRRLIB_GradientRectangle()

Aside of making the code easier to read, it will also a) remove four indexed memory writes and b) stop the compiler warning that the switch may fail.


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

Offline

 

#9 2009-09-03 21:53:33

BlueChip
Moderator

Re: GRRLIB_GradientRectangle()

...and in case you'd like to have some fun ...you may find that

Code:

register ui32 *dp = cpoly;
*dp++ = colour1; *dp++ = colour2; *dp++ = colour3;  *dp = colour4;

produces code which is both smaller AND faster and at no memory cost.


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

Offline

 

Board footer

Powered by FluxBB