Subject:
Re: [ruby-ffi] Functions returning structures
From:
Wayne Meissner
Date:
12/14/09 3:33 PM
To:
ruby-ffi@googlegroups.com

You need to specify the return type as ALLEGRO_COLOR.by_value
(same for any struct-by-value parameters).

2009/12/15 galdor <khaelin@gmail.com>:
> Hi,
>
> Let's define a C function which returns a plain structure:
>
>    typedef struct {
>        int r, g, b, a;
>    } ALLEGRO_COLOR;
>
>    ALLEGRO_COLOR al_map_rgb(unsigned char r, unsigned char g,
> unsigned char b);
>
> Now we bind it:
>
>    class ALLEGRO_COLOR < FFI::Struct
>        layout :r, :float,
>               :g, :float,
>               :b, :float,
>               :a, :float
>    end
>
>    attach_function :al_map_rgb, [:uchar, :uchar, :uchar],
> ALLEGRO_COLOR
>
> But the following ruby code doesn't work:
>
>    bgcolor = al_map_rgb(20, 20, 20)
>    fgcolor = al_map_rgb(20, 40, 80)
>    al_clear_to_color(bgcolor)
>
> The screen is cleared with rgb(20, 40, 80), instead of rgb(20, 20,
> 20).
> I don't know how stack allocation is performed, I must be doing
> something wrong.
>
> How should I work with stack-allocated structures ?
>
> Regards,
>
> Nicolas Martyanoff
>