1. FFI question
- Posted by ChrisB (moderator) Dec 28, 2022
- 565 views
Hi
In Andy's SDL, events, the event union is a bunch of stuctures within a structure - a union. I understand that ffi cannot access members of a particular union, but I think we can 'flatten' these, as each member of the union is a structure with only one level deep.
So, very simply, Andy has defined each of the members of the union as a structure already. Simplified, the event union looks like this.
public constant SDL_Event = define_c_type({ C_UINT32, --type [1] SDL_CommonEvent, --[2] SDL_DisplayEvent, --[3] SDL_WindowEvent, --[4] SDL_KeyboardEvent, --[5] --and so on {)
and exh of these members of the union has been defined like this (for instance)
public constant SDL_CommonEvent = define_c_type({ C_UINT32, --type C_UINT32 --timestamp }) public constant SDL_DisplayEvent = define_c_type({ C_UINT32, --type C_UINT32, --timestamp C_UINT32, --display C_UINT8, --event C_UINT8, --padding1 C_UINT8, --padding2 C_UINT8, --padding3 C_INT32 --data1 --and so on })
Now, from what I can tell, FFI allocates a block of memory for the structure depending on the size of the elements, and returns a pointer to the start of the structure. Easy enough with C_UINT32 and C_UINT8 etc, but in the event union does FFI allocate a block of memory depending on the size of the structure elements? Ie does it know the size of a SDL_CommonEvent and the size of an SDL_DisplayEvent and so on, so that it allocates the correct amount / block of memory for the entire union?
Would make life easier, instead of 'flattening' it.
Cheers
Chris