/*
* call-seq:
* blit(target,dest,source=nil) -> Rect
*
* Blit (copy) all or part of the surface's image to another surface,
* at a given position. Returns a Rect representing the area of
* +target+ which was affected by the blit.
*
* This method takes these arguments:
* target:: the target Surface on which to paste the image.
* dest:: the coordinates of the top-left corner of the blit. Affects the
* area of +other+ the image data is /pasted/ over.
* Can also be a Rect or an Array larger than 2, but
* width and height will be ignored.
* source:: a Rect representing the area of the source surface to get data
* from. Affects where the image data is /copied/ from.
* Can also be an Array of no less than 4 values.
*/
VALUE rbgm_surface_blit(int argc, VALUE *argv, VALUE self)
{
int left, top, right, bottom;
int blit_x,blit_y,blit_w,blit_h;
//int dest_x,dest_y,dest_w,dest_h;
int src_x,src_y,src_w,src_h;
VALUE returnrect;
SDL_Surface *src, *dest;
SDL_Rect *src_rect, *blit_rect;
VALUE vtarget, vdest, vsource;
rb_scan_args( argc, argv, "21", &vtarget, &vdest, &vsource );
Data_Get_Struct(self, SDL_Surface, src);
Data_Get_Struct(vtarget, SDL_Surface, dest);
vdest = convert_to_array(vdest);
blit_x = NUM2INT(rb_ary_entry(vdest,0));
blit_y = NUM2INT(rb_ary_entry(vdest,1));
/* did we get a src_rect argument or not? */
if( !NIL_P(vsource) )
{
/* it might be good to check that it's actually a rect */
vsource = convert_to_array(vsource);
src_x = NUM2INT( rb_ary_entry(vsource,0) );
src_y = NUM2INT( rb_ary_entry(vsource,1) );
src_w = NUM2INT( rb_ary_entry(vsource,2) );
src_h = NUM2INT( rb_ary_entry(vsource,3) );
}
else
{
src_x = 0;
src_y = 0;
src_w = src->w;
src_h = src->h;
}
src_rect = make_rect( src_x, src_y, src_w, src_h );
/* experimental (broken) rectangle cropping code */
/* crop if it went off left/top/right/bottom */
//left = max(blit_x,0);
//top = max(blit_y,0);
//right = min(blit_x+src_w,dest->w);
//bottom = min(blit_y+src_h,dest->h);
left = blit_x;
top = blit_y;
right = blit_x+src_w;
bottom = blit_y+src_h;
//blit_w = min(blit_x+blit_w,dest->w) - max(blit_x,0);
//blit_h = min(blit_y+blit_h,dest->h) - max(blit_y,0);
blit_w = right - left;
blit_h = bottom - top;
blit_rect = make_rect( left, top, blit_w, blit_h );
SDL_BlitSurface(src,src_rect,dest,blit_rect);
returnrect = rb_funcall(cRect,rb_intern("new"),4,
INT2NUM(left),INT2NUM(top),\
INT2NUM(blit_w),INT2NUM(blit_h));
free(blit_rect);
free(src_rect);
return returnrect;
}