1. SFML Wrapper

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.

new topic     » topic index » view message » categorize

2. Re: SFML Wrapper

Lone_EverGreen_Ranger said...

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: SFML Wrapper

mattlewis said...
Lone_EverGreen_Ranger said...

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.

new topic     » goto parent     » topic index » view message » categorize

4. Re: SFML Wrapper

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 
 
new topic     » goto parent     » topic index » view message » categorize

5. Re: SFML Wrapper

Lone_EverGreen_Ranger said...

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: SFML Wrapper

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. smile

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

new topic     » goto parent     » topic index » view message » categorize

7. Re: SFML Wrapper

ghaberek said...

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. smile

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.

new topic     » goto parent     » topic index » view message » categorize

8. Re: SFML Wrapper

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?

new topic     » goto parent     » topic index » view message » categorize

9. Re: SFML Wrapper

Lone_EverGreen_Ranger said...
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. smile

-Greg

new topic     » goto parent     » topic index » view message » categorize

10. Re: SFML Wrapper

ghaberek said...

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

new topic     » goto parent     » topic index » view message » categorize

11. Re: SFML Wrapper

mattlewis said...
ghaberek said...

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.

new topic     » goto parent     » topic index » view message » categorize

12. Re: SFML Wrapper

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.

new topic     » goto parent     » topic index » view message » categorize

13. Re: SFML Wrapper

local2 said...

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.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu