Subject:
[ruby-ffi] Functions returning structures
From:
galdor
Date:
12/14/09 3:29 PM
To:
ruby-ffi

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