/*
* call-seq:
* new(size, depth=0, flags=0) -> Surface
*
* Create and initialize a new Surface object.
*
* A Surface is a grid of image data which you blit (i.e. copy) onto other
* Surfaces. Since the Rubygame display is also a Surface (see the Screen
* class), Surfaces can be blit to the screen; this is the most common way
* to display images on the screen.
*
* This method may raise SDLError if the SDL video subsystem could
* not be initialized for some reason.
*
* This function takes these arguments:
* size:: requested surface size; an array of the form [width, height].
* depth:: requested color depth (in bits per pixel). If depth is 0 (default),
* automatically choose a color depth: either the depth of the Screen
* mode (if one has been set), or the greatest color depth available
* on the system.
* flags:: an Array or Bitwise-OR'd list of zero or more of the following
* flags (located in the Rubygame module, e.g. Rubygame::SWSURFACE).
* This argument may be omitted, in which case the Surface
* will be a normal software surface (this is not necessarily a bad
* thing).
* SWSURFACE:: (default) request a software surface.
* HWSURFACE:: request a hardware-accelerated surface (using a
* graphics card), if available. Creates a software
* surface if hardware surfaces are not available.
* SRCCOLORKEY:: request a colorkeyed surface. #set_colorkey will
* also enable colorkey as needed. For a description
* of colorkeys, see #set_colorkey.
* SRCALPHA:: request an alpha channel. #set_alpha will
* also enable alpha. as needed. For a description
* of alpha, see #alpha.
*/
VALUE rbgm_surface_new(int argc, VALUE *argv, VALUE class)
{
VALUE self;
SDL_Surface *self_surf;
SDL_PixelFormat* pixformat;
Uint32 flags, Rmask, Gmask, Bmask, Amask;
int w, h, depth;
VALUE vsize, vdepth, vflags;
rb_scan_args(argc, argv, "12", &vsize, &vdepth, &vflags);
if( SDL_GetVideoSurface() )
{
/* Pixel format is retrieved from the video surface. */
pixformat = (SDL_GetVideoSurface())->format;
}
else
{
/* We can only get the system color depth when the video system
* has been initialized. */
if( init_video_system() == 0 )
{
pixformat = SDL_GetVideoInfo()->vfmt;
}
else
{
rb_raise(eSDLError,"Could not initialize SDL video subsystem.");
return Qnil;
}
}
Rmask = pixformat->Rmask;
Gmask = pixformat->Gmask;
Bmask = pixformat->Bmask;
Amask = pixformat->Amask;
if( !NIL_P(vdepth) && NUM2INT(vdepth) > 0 )
{
/* TODO: We might want to check that the requested depth makes sense. */
depth = NUM2INT(vdepth);
}
else
{
depth = pixformat->BitsPerPixel;
}
/* Get width and height for new surface from vsize */
vsize = convert_to_array(vsize);
if(RARRAY_LEN(vsize) >= 2)
{
w = NUM2INT(rb_ary_entry(vsize,0));
h = NUM2INT(rb_ary_entry(vsize,1));
}
else
rb_raise(rb_eArgError,"Array is too short for Surface size (%d for 2)",\
RARRAY_LEN(vsize));
flags = collapse_flags(vflags); /* in rubygame_shared */
/* Finally, we can create the new Surface! Or try, anyway... */
self_surf = SDL_CreateRGBSurface(flags,w,h,depth,Rmask,Gmask,Bmask,Amask);
if( self_surf == NULL )
rb_raise(eSDLError,"Could not create new surface: %s",SDL_GetError());
/* Wrap the new surface in a crunchy candy VALUE shell. */
self = Data_Wrap_Struct( cSurface,0,SDL_FreeSurface,self_surf );
/* The default initialize() does nothing, but may be overridden. */
rb_obj_call_init(self,argc,argv);
return self;
}