/* call-seq:
 *   window.setIcon( width, height, pixels )
 *
 * Change the window's icon.
 *
 * pixels must be an array of width x height pixels in 32-bits RGBA format. In the ruby binding the array will be
 * flattened so you can have array's up to 3 dimensions(or more) to represent each pixel component. The size of the
 * array will be assumed to be width * height * 4.
 *
 * The OS default icon is used by default.
 * 
 * Usage example:
 *   pixels = [
 *     [[255, 0, 0, 255], [0, 0, 255, 255]],
 *     [[0, 255, 0, 255], [0, 0, 0, 255]]
 *   ]
 *   
 *   window.setIcon( 2, 2, pixels )
 */
static VALUE Window_SetIcon( VALUE self, VALUE aWidth, VALUE aHeight, VALUE somePixels )
{
        const unsigned int rawWidth = FIX2UINT( aWidth );
        const unsigned int rawHeight = FIX2UINT( aHeight );
        VALIDATE_CLASS( somePixels, rb_cArray, "third" );
        const unsigned long dataSize = rawWidth * rawHeight * 4;
        sf::Uint8 * const tempData = new sf::Uint8[dataSize];
        VALUE pixels = rb_funcall( somePixels, rb_intern("flatten"), 0 );
        for(unsigned long index = 0; index < dataSize; index++)
        {
                sf::Uint8 val = NUM2CHR( rb_ary_entry( pixels, index ) );
                tempData[index] = val;
        }
        
        sf::Window *object = NULL;
        Data_Get_Struct( self, sf::Window, object );
        object->SetIcon( rawWidth, rawHeight, tempData );
        delete[] tempData;
        return Qnil;
}