Re: sdl2 and ffi
- Posted by ChrisB (moderator) Dec 05, 2022
- 1223 views
Hi
At the risk of sounding like a troll (seriously not my intention, Andy has always done great work on all the libraries he has created, and wrapping all those functions must be some sort of tedium), but I think you've missed my point Gregg.
Let's also take SDL_GetVersion as an example. This is how Andy originally presented it
--SDL_Version is a type set up in sdl_version.e public constant SDL_version = define_c_type({ C_UINT, --major C_UINT, --minor C_UINT --patch }) --and then in sdl_version.e export constant xSDL_GetVersion = define_c_proc(sdl,"+SDL_GetVersion",{SDL_version}) public procedure SDL_GetVersion(sequence ver) c_proc(xSDL_GetVersion,{ver}) end procedure
As you can see, and is passing a type to the c function, not a pointer, and the eu procedure is expecting a sequence
The correct way is
export constant xSDL_GetVersion = define_c_proc(sdl,"+SDL_GetVersion",{C_POINTER}) public procedure SDL_GetVersion(atom ver_ptr) c_proc(xSDL_GetVersion,{ver_ptr}) end procedure
Now the c function gets a pointer value, and the eu procedure gets an atom, created with
atom DLL_vers DLL_vers = allocate_struct(SDL_version, {0,0,0}) --allocate the memory, and return the pointer to that memory SDL_GetVersion(DLL_vers) --an atom, a pointer, is passed to the eu function printf(1, "DLL version : %d.%d.%d\n)", { peek(DLL_vers), --single byte, 8 bit numbers peek(DLL_vers + 1), peek(DLL_vers + 2) }) --the more ''ffi' way printf(1, "DLL version : %d.%d.%d\n", peek_struct( DLL_vers, SDL_VERSION )) --just tried this - doesn't work, peek_struct is 'out'
My point is that all sdl functions need pointers to structures, as far as I can tell, and passing a type, created using allocate structure, does not contain the pointer to that allocated memory
I don't doubt that ffi will make it easier to manipulate structures, it already has done so for me, and I know I'm a pest, but the SDL library as Andy has presented just doesn't work. Now, correct me if I'm wrong, and I'm misunderstanding something here, but is passing a type the same as passing pointer?
Cheers, Chris
ps - see the code above - peek_struct isn't returning the correct values - is C_UINT in euphoria 1 byte, 8 bits only, as that's what SDL_GetVersion returns, and that's why the peek version returns the correct values. Change the SDL_version type C_UINTs to C_UINT8s, and it works.
Cheers
Chris