/* 
 *  call-seq:
 *     fill(color,rect=nil)
 *
 *  Fill all or part of a Surface with a color.
 *
 *  This method takes these arguments:
 *  color:: color to fill with, in the form +[r,g,b]+ or +[r,g,b,a]+ (for
 *          partially transparent fills).
 *  rect::  a Rubygame::Rect representing the area of the surface to fill
 *          with color. Omit to fill the entire surface.
 */
VALUE rbgm_surface_fill( int argc, VALUE *argv, VALUE self )
{
        SDL_Surface *surf;
        SDL_Rect *rect;
        Uint32 color;
        Uint8 r,g,b,a;
        VALUE vcolor, vrect;

        Data_Get_Struct(self, SDL_Surface, surf);

        rb_scan_args(argc, argv, "11", &vcolor, &vrect);

        vcolor = convert_to_array(vcolor);
        r = NUM2UINT(rb_ary_entry(vcolor,0));
        g = NUM2UINT(rb_ary_entry(vcolor,1));
        b = NUM2UINT(rb_ary_entry(vcolor,2));
        /* if the array is larger than [R,G,B], it should be [R,G,B,A] */
        if(RARRAY(vcolor)->len > 3)
        {
                a = NUM2UINT(rb_ary_entry(vcolor,3));
                color = SDL_MapRGBA(surf->format, r,g,b,a);
        }
        else
        {
                color = SDL_MapRGB(surf->format, r,g,b);
        }

        if( NIL_P(vrect) )
        {
                SDL_FillRect(surf,NULL,color);
        }
        else
        {
                vrect = convert_to_array(vrect);

                rect = make_rect(\
                                                                                 NUM2INT(rb_ary_entry(vrect,0)),\
                                                                                 NUM2INT(rb_ary_entry(vrect,1)),\
                                                                                 NUM2INT(rb_ary_entry(vrect,2)),\
                                                                                 NUM2INT(rb_ary_entry(vrect,3))\
                                                                                 );
                SDL_FillRect(surf,rect,color);
                free(rect);
        }

        return self;
}