/*  call-seq:
 *     update_rects(rects)
 *
 *  Updates (as Screen#update does) several areas of the screen.
 *
 *  This method takes these arguments:
 *  rects:: an Array containing any number of Rect objects, each
 *          rect representing a portion of the screen to update.
 */
VALUE rbgm_screen_updaterects(VALUE self, VALUE array_rects)
{
  int i, num_rects;
  VALUE each_rect;
  SDL_Surface *screen;
  SDL_Rect *rects;

  /* unwrap the Screen instance from self (VALUE) */
  Data_Get_Struct(self,SDL_Surface,screen);

  /* prepare an (uninitialized) array of Rects */
  array_rects = convert_to_array(array_rects);
  num_rects = RARRAY(array_rects)->len;
  rects = ALLOCA_N(SDL_Rect, num_rects);

  /* initialize the array of Rects from array_rects */
  for( i=0; i < num_rects; i++ )
  {
    each_rect = convert_to_array(rb_ary_entry(array_rects,i));

    Sint16 x,y,left,top,right,bottom;
    Uint16 w,h;

    x = NUM2INT(rb_ary_entry(each_rect,0));
    y = NUM2INT(rb_ary_entry(each_rect,1));
    w = NUM2INT(rb_ary_entry(each_rect,2));
    h = NUM2INT(rb_ary_entry(each_rect,3));

                left   = min( max( 0,    x    ), screen->w );
                top    = min( max( 0,    y    ), screen->h );
                right  = min( max( left, x + w), screen->w );
                bottom = min( max( top,  y + h), screen->h );

    rects[i].x = left;
    rects[i].y = top;
    rects[i].w = right - left;
    rects[i].h = bottom - top;
  }

  /* call the SDL method to update from all these rects */
  SDL_UpdateRects( screen, num_rects, rects );

  return self;
}