/*  call-seq:
 *    render_utf8(string, aa, fg, bg)  ->  Surface
 *
 *  Renders a string to a Surface with the font's style and the given color(s).
 *
 *  This method takes these arguments:
 *  string:: the text string to render
 *  aa::     Use anti-aliasing if true. Enabling this makes the text
 *           look much nicer (smooth curves), but is much slower.
 *  fg::     the color to render the text, in the form [r,g,b]
 *  bg::     the color to use as a background for the text. This option can
 *           be omitted to have a transparent background.
 */
VALUE rbgm_ttf_render_utf8(int argc, VALUE *argv, VALUE self)
{
        SDL_Surface *surf;
        TTF_Font *font;
        SDL_Color fore, back; /* foreground and background colors */
        VALUE vstring, vaa, vfg, vbg;
 
        rb_scan_args(argc, argv, "31", &vstring, &vaa, &vfg, &vbg);
 
        Data_Get_Struct(self,TTF_Font,font);
 
        RBGM_array_to_color(&fore, vfg);
        RBGM_array_to_color(&back, vbg);

        if( RTEST(vaa) ) /* anti-aliasing enabled */
        {
                if( RTEST(vbg) ) /* background color provided */
                        surf = TTF_RenderUTF8_Shaded(font,StringValuePtr(vstring),fore,back);
                else /* no background color */
                        surf = TTF_RenderUTF8_Blended(font,StringValuePtr(vstring),fore);
        }
        else /* anti-aliasing not enabled */
        {
                if( RTEST(vbg) ) /* background color provided */     
                {
                        /* remove colorkey, set color index 0 to background color */
                        surf = TTF_RenderUTF8_Solid(font,StringValuePtr(vstring),fore);
                        SDL_SetColors(surf,&back,0,1);
                        SDL_SetColorKey(surf,0,0);
                }
                else /* no background color */
                {
                        surf = TTF_RenderUTF8_Solid(font,StringValuePtr(vstring),fore);
                }
        }
 
        if(surf==NULL)
                rb_raise(eSDLError,"could not render font object: %s",TTF_GetError());
        return Data_Wrap_Struct(cSurface,0,SDL_FreeSurface,surf);
}