Functions returning structures
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
galdor <khae... @gmail.com>
Date: Mon, 14 Dec 2009 13:29:55 -0800 (PST)
Local: Mon, Dec 14 2009 4:29 pm
Subject: Functions returning structures
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Wayne Meissner <wmeiss... @gmail.com>
Date: Tue, 15 Dec 2009 07:33:22 +1000
Local: Mon, Dec 14 2009 4:33 pm
Subject: Re: [ruby-ffi] Functions returning structures
You need to specify the return type as ALLEGRO_COLOR.by_value
(same for any struct-by-value parameters).
2009/12/15 galdor <khae... @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
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
galdor <khae... @gmail.com>
Date: Mon, 14 Dec 2009 13:42:47 -0800 (PST)
Local: Mon, Dec 14 2009 4:42 pm
Subject: Re: Functions returning structures
Thank you, it just works!
I also had to use .by_value for parameters, of course.
I'll notify the author of ffi-swig-generator about this, its tool
doesn't produce these annotations.
Regards,
Nicolas Martyanoff
On Dec 14, 10:33 pm, Wayne Meissner <wmeiss... @gmail.com> wrote:
> You need to specify the return type as ALLEGRO_COLOR.by_value
> (same for any struct-by-value parameters).
> 2009/12/15 galdor <khae... @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
You must
Sign in before you can post messages.
You do not have the permission required to post.