/* 
 *  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;

        Data_Get_Struct(self, SDL_Surface, surf);

        if(argc < 1)
        {
                rb_raise(rb_eArgError,"wrong number of arguments (%d for 1 or 2)",argc);
        }

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

        switch(argc)
        {
                case 1: /* fill whole thing */
                        SDL_FillRect(surf,NULL,color);
                        break;
                case 2: /* fill a given rect */
                        rect = make_rect(\
                                         NUM2INT(rb_ary_entry(argv[1],0)),\
                                         NUM2INT(rb_ary_entry(argv[1],1)),\
                                         NUM2INT(rb_ary_entry(argv[1],2)),\
                                         NUM2INT(rb_ary_entry(argv[1],3))\
                                         );
                        SDL_FillRect(surf,rect,color);
                        free(rect);
                        break;
                default:
                        rb_raise( rb_eArgError,"Wrong number of arguments to fill (%d for 1 or 2)",NUM2INT(argc));
                        break;
        }
        return self;
}