1. Going About This Right
- Posted by Icy_Viking May 06, 2015
- 1583 views
Hello,
I am wondering if I am going about this right, wrapping the SFML 2 library correctly.
public constant xsfTexture_createFromFile = define_c_func(gfx,"sfTexture_createFromFile",{C_POINTER,C_POINTER},C_POINTER) public function sfTexture_createFromFile(sequence file,atom area) atom str,handle,ret,dat str = allocate_string(file) ret = c_func(xsfTexture_createFromFile,{handle = open(str,"rb"), area}) if handle = -1 then puts(1,"Could not load file!\n") abort(0) else handle = gets(dat) end if close(handle) return ret end function
I just want to make sure I am doing this correctly when loading a file in Eu. Also, keep in mind this is just a snippet of code I'm working on, I have many other functions already written. I can post more if needed.
2. Re: Going About This Right
- Posted by petelomax May 06, 2015
- 1557 views
Hello,
I am wondering if I am going about this right, wrapping the SFML 2 library correctly.
public constant xsfTexture_createFromFile = define_c_func(gfx,"sfTexture_createFromFile",{C_POINTER,C_POINTER},C_POINTER) public function sfTexture_createFromFile(sequence file,atom area) atom str,handle,ret,dat str = allocate_string(file)
You need to free(str) before returning
ret = c_func(xsfTexture_createFromFile,{handle = open(str,"rb"), area})
You cannot assign mid-statement like that, but instead must split the line into two:
handle = open(str,"rb") ret = c_func(xsfTexture_createFromFile,{handle, area})
if handle = -1 then
and a) it would be open(file) not open(str), b) you would normally want to check handle before passing it to c_func, and c) a quick google suggests it should be
ret = c_func(xsfTexture_createFromFile,{str, area})
with your function not needing the open/close anyway.
handle = gets(dat)
that would spanner the (un-needed) close(handle) next, plus you are not doing anything with the data just read, and you'd get a "dat has not been assigned a value" error. Try this:
public constant xsfTexture_createFromFile = define_c_func(gfx,"sfTexture_createFromFile",{C_POINTER,C_POINTER},C_POINTER) public function sfTexture_createFromFile(sequence file,atom area) atom str,ret str = allocate_string(file) ret = c_func(xsfTexture_createFromFile,{str,area}) free(str) return ret end function
possibly with the addition of
if ret=?? then message/abort end if
Pete
3. Re: Going About This Right
- Posted by ghaberek (admin) May 06, 2015
- 1548 views
public constant xsfTexture_createFromFile = define_c_func(gfx,"sfTexture_createFromFile",{C_POINTER,C_POINTER},C_POINTER) public function sfTexture_createFromFile(sequence file,atom area) atom str,ret str = allocate_string(file) ret = c_func(xsfTexture_createFromFile,{str,area}) free(str) return ret end function
We can clean that up a bit by in-lining the call to allocate_string() with cleanup = 1. Without the call to free(), we can in-line the return value as well.
public constant xsfTexture_createFromFile = define_c_func(gfx,"sfTexture_createFromFile",{C_POINTER,C_POINTER},C_POINTER) public function sfTexture_createFromFile(sequence file,atom area = NULL) return c_func(xsfTexture_createFromFile,{allocate_string(file,1),area}) end function
Edit: apparently area can be NULL so let's add that as a default parameter.
-Greg
4. Re: Going About This Right
- Posted by Icy_Viking May 06, 2015
- 1503 views
thank you both for the help. Glad I double checked on this. The code works. Now I will just need to finish up wrapper the rest of the library.