1. SFML Wrapper
- Posted by Lone_EverGreen_Ranger Jan 09, 2012
- 1979 views
Hello,
Am I going about this the right way?
include std/dll.e include std/machine.e include std/wildcard.e atom System, Audio, Graphics, Window, Network System = open_dll("sfml-system.dll") Audio = open_dll("sfml-audio.dll") Graphics = open_dll("sfml-graphics.dll") Window = open_dll("sfml-window.dll") Network = open_dll("sfml-network.dll") if System = -1 then puts(1,"Failed to open sfml-system!") abort(1) end if if Audio = -1 then puts(1,"Failed to open sfml-audio!") abort(1) end if if Graphics = -1 then puts(1,"Failed to open sfml-graphics!") abort(1) end if if Window = -1 then puts(1,"Failed to open sfml-window!") abort(1) end if if Network = -1 then puts(1,"Failed to open sfml-network!") abort(1) end if constant xClock = define_c_func(System,"sf::Clock",{C_FLOAT,C_POINTER},C_POINTER), xClock_GetElapsedTime = define_c_func(System,"sf::Clock.GetElapsedTime",{C_FLOAT},C_FLOAT), xClock_Reset = define_c_func(System,"sf::Clock.Reset",{C_POINTER},C_POINTER) function GetElapsedTime() return c_func(xClock_GetElapsedTime,{}) end function
As you can tell, I am attempting to write an SFML wrapper for euphoria. Wanna make sure I am heading in the right direction.
2. Re: SFML Wrapper
- Posted by mattlewis (admin) Jan 09, 2012
- 1922 views
As you can tell, I am attempting to write an SFML wrapper for euphoria. Wanna make sure I am heading in the right direction.
Looking at the functions you are attempting to define, it appears that you're trying to wrap the C++ version of SFML. There's no easy way to do that with euphoria directly. You'll have better luck using the C version.
Matt
3. Re: SFML Wrapper
- Posted by Lone_EverGreen_Ranger Jan 09, 2012
- 1939 views
As you can tell, I am attempting to write an SFML wrapper for euphoria. Wanna make sure I am heading in the right direction.
Looking at the functions you are attempting to define, it appears that you're trying to wrap the C++ version of SFML. There's no easy way to do that with euphoria directly. You'll have better luck using the C version.
Matt
I was kinda confused on how to do it. I didn't even think of the C version. I'll take a look and see what can be done.
4. Re: SFML Wrapper
- Posted by Lone_EverGreen_Ranger Jan 09, 2012
- 1959 views
How's this looking?
include std/dll.e include std/machine.e include std/wildcard.e atom System, Audio, Graphics, Window, Network System = open_dll("csfml-system.dll") Audio = open_dll("csfml-audio.dll") Graphics = open_dll("csfml-graphics.dll") Window = open_dll("csfml-window.dll") Network = open_dll("csfml-network.dll") if System = -1 then puts(1,"Failed to open sfml-system!") abort(1) end if if Audio = -1 then puts(1,"Failed to open sfml-audio!") abort(1) end if if Graphics = -1 then puts(1,"Failed to open sfml-graphics!") abort(1) end if if Window = -1 then puts(1,"Failed to open sfml-window!") abort(1) end if if Network = -1 then puts(1,"Failed to open sfml-network!") abort(1) end if constant EXIT_SUCCESS = 0 constant EXIT_FAILURE = 1 constant xClock_Create = define_c_func(System,"sfClock_Create",{C_POINTER},C_POINTER), xClock_Destroy = define_c_func(System,"sfClock_Destroy",{C_POINTER},C_POINTER), xClock_GetTime = define_c_func(System,"sfClock_GetTime",{C_FLOAT},C_FLOAT), xClock_Reset = define_c_func(System,"sfClock_Reset",{C_POINTER},C_POINTER) function Clock_Create() return c_func(xClock_Create,{}) end function
5. Re: SFML Wrapper
- Posted by mattlewis (admin) Jan 09, 2012
- 1907 views
How's this looking?
Looks good, although I think that your GetTime wrapper is incorrect:
xClock_GetTime = define_c_func(System,"sfClock_GetTime",{C_FLOAT},C_FLOAT),
The parameter is a pointer to the sfClock:
xClock_GetTime = define_c_func(System,"sfClock_GetTime",{C_POINTER},C_FLOAT),
If you're not familiar with C (and even if you are), parsing the function prototypes and translating that into a wrapper can be very frustrating. If something isn't working the way you think that it should, I find that reviewing your wrappers and the way that you're handling the data is probably the place to look first.
Matt
6. Re: SFML Wrapper
- Posted by ghaberek (admin) Jan 09, 2012
- 1907 views
That's looking better. Although one thing I like to do when wrapping libraries is to make little helper functions to streamline the process. You'll find functions like this in most of my code. Although I take no credit for the idea, it's just something I do.
procedure die(integer exit_code, sequence message, object params = {}) if length( message ) > 0 and message[$] != '\n' then message &= "\n" end if printf( 2, message, params ) abort( exit_code ) end procedure function link_dll(sequence file_name) atom dll = open_dll(file_name) if dll = 0 then die( 1, "Unable to link dll: %s\n", {file_name} ) end if return dll end function function link_c_func(object lib, object routine_name, sequence arg_types, atom return_type) atom func = define_c_func( lib, routine_name, arg_types, return_type ) if func = -1 then die( 1, "Unable to link c func: %s\n", {routine_name} ) end if return func end function function link_c_proc(object lib, object routine_name, sequence arg_types) atom proc = define_c_proc( lib, routine_name, arg_types ) if proc = -1 then die( 1, "Unable to link c proc: %s\n", {routine_name} ) end if return proc end function
And so then your code becomes something more like this... which is much easier to read.
include std/dll.e include std/machine.e include std/wildcard.e public enum EXIT_SUCCESS = 0, EXIT_FAILURE = 1, $ public constant System = open_dll( "csfml-system.dll" ), Audio = open_dll( "csfml-audio.dll" ), Graphics = open_dll( "csfml-graphics.dll" ), Window = open_dll( "csfml-window.dll" ), Network = open_dll( "csfml-network.dll" ), $ public constant xClock_Create = link_c_func( System, "sfClock_Create", {C_POINTER}, C_POINTER ), xClock_Destroy = link_c_func( System, "sfClock_Destroy", {C_POINTER}, C_POINTER ), xClock_GetTime = link_c_func( System, "sfClock_GetTime", {C_FLOAT}, C_FLOAT ), xClock_Reset = link_c_func( System, "sfClock_Reset", {C_POINTER}, C_POINTER ), $ function Clock_Create() return c_func( xClock_Create, {} ) end function
-Greg
7. Re: SFML Wrapper
- Posted by Lone_EverGreen_Ranger Jan 09, 2012
- 2002 views
That's looking better. Although one thing I like to do when wrapping libraries is to make little helper functions to streamline the process. You'll find functions like this in most of my code. Although I take no credit for the idea, it's just something I do.
procedure die(integer exit_code, sequence message, object params = {}) if length( message ) > 0 and message[$] != '\n' then message &= "\n" end if printf( 2, message, params ) abort( exit_code ) end procedure function link_dll(sequence file_name) atom dll = open_dll(file_name) if dll = 0 then die( 1, "Unable to link dll: %s\n", {file_name} ) end if return dll end function function link_c_func(object lib, object routine_name, sequence arg_types, atom return_type) atom func = define_c_func( lib, routine_name, arg_types, return_type ) if func = -1 then die( 1, "Unable to link c func: %s\n", {routine_name} ) end if return func end function function link_c_proc(object lib, object routine_name, sequence arg_types) atom proc = define_c_proc( lib, routine_name, arg_types ) if proc = -1 then die( 1, "Unable to link c proc: %s\n", {routine_name} ) end if return proc end function
And so then your code becomes something more like this... which is much easier to read.
include std/dll.e include std/machine.e include std/wildcard.e public enum EXIT_SUCCESS = 0, EXIT_FAILURE = 1, $ public constant System = open_dll( "csfml-system.dll" ), Audio = open_dll( "csfml-audio.dll" ), Graphics = open_dll( "csfml-graphics.dll" ), Window = open_dll( "csfml-window.dll" ), Network = open_dll( "csfml-network.dll" ), $ public constant xClock_Create = link_c_func( System, "sfClock_Create", {C_POINTER}, C_POINTER ), xClock_Destroy = link_c_func( System, "sfClock_Destroy", {C_POINTER}, C_POINTER ), xClock_GetTime = link_c_func( System, "sfClock_GetTime", {C_FLOAT}, C_FLOAT ), xClock_Reset = link_c_func( System, "sfClock_Reset", {C_POINTER}, C_POINTER ), $ function Clock_Create() return c_func( xClock_Create, {} ) end function
-Greg
Thanks for all the feedback, guys. Hopefully I'll have something you can use within a little while. I'm not talking hours, but perhaps weeks.
8. Re: SFML Wrapper
- Posted by Lone_EverGreen_Ranger Jan 09, 2012
- 2024 views
Doing a double check to see if I've got it right so far.
enum --window types SFNONE, SFTITLEBAR, SFRESIZE, SFCLOSE, SFFULLSCREEN constant xWindow_Close = define_c_func(Window,"sfWindow_Close",{C_POINTER},C_POINTER), xWindow_Create = define_c_func(Window,"sfWindow_Create",{C_POINTER,C_UCHAR,C_ULONG,C_INT},C_POINTER), xWindow_CreateFromHandle = define_c_func(Window,"sfWindow_CreateFromHandle",{C_POINTER,C_POINTER},C_POINTER), xWindow_Destroy = define_c_func(Window,"sfWindow_Destroy",{C_POINTER},C_POINTER), xWindow_Display = define_c_func(Window,"sfWindow_Display",{C_POINTER},C_POINTER), xWindow_EnableKeyRepeat = define_c_func(Window,"sfWindow_EnableKeyRepeat",{C_POINTER,C_INT},C_POINTER), xWindow_GetEvent = define_c_func(Window,"sfWindow_GetEvent",{C_POINTER,C_POINTER},C_INT), xWindow_GetFrameTime = define_c_func(Window,"sfWindow_GetFrameTime",{C_POINTER},C_FLOAT), xWindow_GetHeight = define_c_func(Window,"sfWindow_GetHeight",{C_POINTER},C_UINT), xWindow_GetInput = define_c_func(Window,"sfWindow_GetInput",{C_POINTER},C_POINTER), xWindow_GetSettings = define_c_func(Window,"sfWindow_GetSettings",{C_POINTER},C_POINTER), xWindow_GetWidth = define_c_func(Window,"sfWindow_GetWidth",{C_POINTER},C_UINT), xWindow_IsOpened = define_c_func(Window,"sfWindow_IsOpened",{C_POINTER},C_INT), xWindow_SetActive = define_c_func(Window,"sfWindow_SetActive",{C_POINTER},C_INT), xWindow_SetCursorPosition = define_c_func(Window,"sfWindow_SetCursorPosition",{C_POINTER,C_UINT},C_UINT), xWindow_SetFramerateLimit = define_c_func(Window,"sfWindow_SetFramerateLimit",{C_POINTER},C_UINT), xWindow_SetIcon = define_c_func(Window,"sfWindow_SetIcon",{C_POINTER,C_UINT,C_UINT},C_UINT), xWindow_SetJoystickThreshold = define_c_func(Window,"sfWindow_SetJoystickThreshold",{C_POINTER,C_FLOAT},C_POINTER), xWindow_SetPosition = define_c_func(Window,"sfWindow_SetPosition",{C_POINTER,C_INT,C_INT},C_POINTER), xWindow_SetSize = define_c_func(Window,"sfWindow_SetSize",{C_POINTER,C_UINT,C_UINT},C_POINTER), xWindow_Show = define_c_func(Window,"sfWindow_Show",{C_POINTER,C_INT},C_POINTER), xWindow_ShowMouseCursor = define_c_func(Window,"sfWindow_ShowMouseCursor",{C_POINTER,C_INT},C_POINTER), xWindow_UseVerticalSync = define_c_func(Window,"sfWindow_UseVerticalSync",{C_POINTER,C_INT},C_POINTER) function Window_Close() return c_func(xWindow_Close,{}) end function function Window_Create(atom mode,sequence title,atom style, atom parms) return c_func(xWindow_Create,{mode,title,style,parms}) end function function Window_CreateFromHandle(atom handle, atom parms) return c_func(xWindow_CreateFromHandle,{handle,parms}) end function function Window_Destroy() return c_func(xWindow_Destroy,{}) end function function Window_Display() return c_func(xWindow_Display,{}) end function function Window_EnableKeyRepeat(integer enable) return c_func(xWindow_EnableKeyRepeat,{enable}) end function
Noticed there was some errors. Fixed, now how does it look?
9. Re: SFML Wrapper
- Posted by ghaberek (admin) Jan 10, 2012
- 1838 views
function Window_Create(atom mode,sequence title,atom style, atom parms) return c_func(xWindow_Create,{mode,title,style,parms}) end function
Noticed there was some errors. Fixed, now how does it look?
You won't be able to pass sequences directly as strings to your C functions. Instead, you have to allocate the string into memory, pass the pointer, and then free the memory. Like this:
function Window_Create(atom mode,sequence title,atom style, atom parms) atom ptitle, retval -- allocate the string ptitle = allocate_string( title ) -- call the function (with string pointer) retval = c_func(xWindow_Create,{mode,ptitle,style,parms}) -- free the memory free( ptitle ) -- return the value return retval end function
I have no idea what the SFML headers look like, so all I can say is that everything else looks correct from here.
-Greg
10. Re: SFML Wrapper
- Posted by mattlewis (admin) Jan 10, 2012
- 1841 views
You won't be able to pass sequences directly as strings to your C functions. Instead, you have to allocate the string into memory, pass the pointer, and then free the memory. Like this:
I'd just like to make another plug for one of my favorite features of Euphoria 4.0: You can tell Euphoria to free the pointer when you're done with it by adding another parameter to the allocation function call:
function Window_Create(atom mode,sequence title,atom style, atom parms) -- allocate the string (the 1 tells euphoria to free the memory when we're done with it -- call the function (with string pointer) return c_func(xWindow_Create,{mode, allocate_string( title, 1 ), style, parms} ) -- the memory is automatically freed after the c_func call! end function
Matt
11. Re: SFML Wrapper
- Posted by Lone_EverGreen_Ranger Jan 10, 2012
- 1811 views
You won't be able to pass sequences directly as strings to your C functions. Instead, you have to allocate the string into memory, pass the pointer, and then free the memory. Like this:
I'd just like to make another plug for one of my favorite features of Euphoria 4.0: You can tell Euphoria to free the pointer when you're done with it by adding another parameter to the allocation function call:
function Window_Create(atom mode,sequence title,atom style, atom parms) -- allocate the string (the 1 tells euphoria to free the memory when we're done with it -- call the function (with string pointer) return c_func(xWindow_Create,{mode, allocate_string( title, 1 ), style, parms} ) -- the memory is automatically freed after the c_func call! end function
Matt
Thanks for the help guys. Everything seems to be doing great now. Just gotta double check and make sure it is all done up to par.
12. Re: SFML Wrapper
- Posted by local2 Mar 02, 2012
- 1664 views
I think this wrapper is another very good thing for euphoria. I looked in events.e and around the SFML for C documentation but I don't know what to make of fixing events.e at this point. Thanks for submitting it.
13. Re: SFML Wrapper
- Posted by Lone_EverGreen_Ranger Mar 03, 2012
- 1569 views
I think this wrapper is another very good thing for euphoria. I looked in events.e and around the SFML for C documentation but I don't know what to make of fixing events.e at this point. Thanks for submitting it.
Yeah I have most if not everything wrapped. Though there may need to be some fixing up on some things.