1. Wrapping from C Struct
- Posted by Lone_EverGreen_Ranger Sep 11, 2013
- 1601 views
Hello,
How do you wrap the data of a struct from a C source file? Such as this
typedef struct { unsigned int width; unsigned int height; unsigned int bitsPerPixel; } sfVideoMode;
Is there a way to do this?
public constant xsfVideoMode = define_c_func(sf_gfx,"sfVideoMode",{C_UINT,C_UINT,C_UINT},C_POINTER) public function sfVideoMode(atom w,atom h,atom bits) return c_func(xsfVideoMode,{w,h,bits}) end function
2. Re: Wrapping from C Struct
- Posted by raseu Sep 12, 2013
- 1580 views
Using Euphoria 4.1 (with memstruct support)
Euphoria Interpreter v4.1.0 development
32-bit Windows, Using System Memory
Revision Date: 2013-04-11 18:02:57, Id: 6103:25f1efbdaa04
include std/machine.e memstruct struct_sfVideoMode unsigned int width unsigned int height unsigned int bitsPerPixel end memstruct --// allocate atom sfv = allocate(sizeof(struct_sfVideoMode)) sfv.struct_sfVideoMode = {0} --// fill (call your function) --// i assume your function returns a pointer to --// the appropriate struct sfv = sfVideoMode(w, h, bits) --// access struct members ? sfv.struct_sfVideoMode.width ? sfv.struct_sfVideoMode.height ? sfv.struct_sfVideoMode.bitsPerPixel --// cleanup free(sfv)
3. Re: Wrapping from C Struct
- Posted by Lone_EverGreen_Ranger Sep 12, 2013
- 1506 views
Using Euphoria 4.1 (with memstruct support)
Euphoria Interpreter v4.1.0 development
32-bit Windows, Using System Memory
Revision Date: 2013-04-11 18:02:57, Id: 6103:25f1efbdaa04
include std/machine.e memstruct struct_sfVideoMode unsigned int width unsigned int height unsigned int bitsPerPixel end memstruct --// allocate atom sfv = allocate(sizeof(struct_sfVideoMode)) sfv.struct_sfVideoMode = {0} --// fill (call your function) --// i assume your function returns a pointer to --// the appropriate struct sfv = sfVideoMode(w, h, bits) --// access struct members ? sfv.struct_sfVideoMode.width ? sfv.struct_sfVideoMode.height ? sfv.struct_sfVideoMode.bitsPerPixel --// cleanup free(sfv)
Thanks, however Eu 4.1 isn't out yet. Though I could use the bleeding edge version. Any news on when 4.1 will be released?
4. Re: Wrapping from C Struct
- Posted by jaygade Sep 12, 2013
- 1501 views
Even so, someone correct me if I am wrong but I don't think the mem_struct branch will be in 4.1. It's a separate feature branch.
5. Re: Wrapping from C Struct
- Posted by mattlewis (admin) Sep 12, 2013
- 1466 views
Hello,
How do you wrap the data of a struct from a C source file? Such as this
typedef struct { unsigned int width; unsigned int height; unsigned int bitsPerPixel; } sfVideoMode;
Is there a way to do this?
A C struct defines the way C code lays out data in memory. Basically, you have to peek and poke using offsets. So...
constant SFVM_WIDTH = 0, SFVM_HEIGHT = 4, SFVM_BITSPERPIXEL = 8, SFVM_SIZEOF = 12 atom sfvm = allocate( SFVM_SIZEOF ) poke4( sfvm + SFVM_WIDTH, 100 ) poke4( sfvm + SFVM_HEIGHT, 200 ) poke4( sfvm + SFVM_BITSPERPIXEL, 8 ) ? peek4u( sfvm + SFVM_BITSPERPIXEL ) -- get the bits per pixel -- ...etc...
You can, of course, wrap those as functions / procedures for convenience.
Matt
6. Re: Wrapping from C Struct
- Posted by mattlewis (admin) Sep 12, 2013
- 1503 views
Even so, someone correct me if I am wrong but I don't think the mem_struct branch will be in 4.1. It's a separate feature branch.
This is currently true. It's not feature complete yet, and I think there are probably still some bugs, though it is quite useful.
Matt
7. Re: Wrapping from C Struct
- Posted by petelomax Sep 13, 2013
- 1436 views
Hello,
How do you wrap the data of a struct from a C source file? Such as this
typedef struct { unsigned int width; unsigned int height; unsigned int bitsPerPixel; } sfVideoMode;
Is there a way to do this?
public constant xsfVideoMode = define_c_func(sf_gfx,"sfVideoMode",{C_UINT,C_UINT,C_UINT},C_POINTER) public function sfVideoMode(atom w,atom h,atom bits) return c_func(xsfVideoMode,{w,h,bits}) end function
I see somewhat of a complete misunderstanding here: a typedef is not entirely dissimilar to a Euphoria type, in that just as:
constant DHOUR=1, DMIN=2, DSEC=3 [global] type DATE(object t) if sequence(t) and length(t)=3 and integer(t[DHOUR]) and t[DHOUR]>=0 and t[DHOUR]<=23 and integer(t[DMIN]) and t[DMIN]>=0 and t[DMIN]<=59 and integer(t[DSEC]) and t[DSEC]>=0 and t[DSEC]<=59 then return TRUE end if return FALSE end type
(sorry that turned out a bit more verbose than planned when I started typing) does not define anywhere to store eg 2:29:26am, for which you need say:
DATE mydate
then neither does that "typedef sfVideoMode" define any data (that you might better be able to locate using define_c_var), nor (unlike Eu types) does it define a function.
Not that I am faulting what others have said in this thread. In C and C++ you need to digest the headers, macros, typedefs, etc before getting to the code that matters, and I just felt I should point out that a typedef struct is actually neither code nor data, and "sfVideoMode" is almost guaranteed to be the wrong identifier to use.
Pete
8. Re: Wrapping from C Struct
- Posted by Lone_EverGreen_Ranger Sep 14, 2013
- 1340 views
Thanks for the tips. I'm sure sfVideoMode is needed, as you need sfVideoMode in order to create Windows and Graphics within SFML. It seems SFML is a bit more complicated to wrap than some other libraries, but it is still possible. I will continue on improving the wrapper.