1. Going About This Right

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.

new topic     » topic index » view message » categorize

2. Re: Going About This Right

Icy_Viking said...

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

Icy_Viking said...
 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}) 
Icy_Viking said...
 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.

Icy_Viking said...
 		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

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

3. Re: Going About This Right

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

-Greg

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

4. Re: Going About This Right

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.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu