1. Euphoria Chipmunk Physics

Hello,

I have managed to wrap the chipmunk physics library for Euphoria. http://www.rapideuphoria.com/uploads/euchipmunkphy.zip You can get it from the link. I was hoping to make a demo to go with it, however, I could not figure out how to Euphorialize the hello world chipmunk example that is written in C. The wrapper itself seems to work fine. I did test it and check it against some functions and procedures and it seems to work. No errors came up. Any comments or suggestions are welcomed.

new topic     » topic index » view message » categorize

2. Re: Euphoria Chipmunk Physics

Icy_Viking said...

Hello,

I have managed to wrap the chipmunk physics library for Euphoria. http://www.rapideuphoria.com/uploads/euchipmunkphy.zip You can get it from the link. I was hoping to make a demo to go with it, however, I could not figure out how to Euphorialize the hello world chipmunk example that is written in C. The wrapper itself seems to work fine. I did test it and check it against some functions and procedures and it seems to work. No errors came up. Any comments or suggestions are welcomed.

I had a look at your code and I'm not sure if you're going to be able to do this the way you're intending. The problem is that the Chipmunk library passes structures by value. Every time you see the code passing cpVect without an asterisk *, that means it's passing two literal C_DOUBLE values on the stack. This is the same problem you'd have on Allegro 5. Euphoria's C interop routines are simply not designed to pass structures by value at this time. I assume this is why I saw all of the vector parameters declared as sequence in your wrapper code. Unfortunately that will not work either. I was able to work around this in Allegro 5 by using a C shim library that manually peeked and poked the values as pointers instead.

Take the following C definitions:

typedef struct cpVect{ 
    cpFloat x, y; 
} cpVect 
 
cpVect cpBodyGetPosition(const cpBody *body) 
void cpBodySetPosition(cpBody *body, cpVect pos) 

And here is the non-functional wrapper code:

-- structure offets 
constant cpVect_x = 0 -- cpFloat (C_DOUBLE) 
constant cpVext_y = 8 -- cpFloat (C_DOUBLE) 
constant SIZEOF_CPVECT = 16 
 
constant x_cpBodyGetPosition = define_c_func( chipmunk, "+cpBodyGetPosition", {C_POINTER}, {C_DOUBLE,C_DOUBLE} /* return value must be an atom. this will not work! */ ) 
constant x_cpBodySetPosition = define_c_proc( chipmunk, "+cpBodySetPosition", {C_POINTER,C_DOUBLE,C_DOUBLE} /* this is 'cheating' but should technically work */ ) 

Another issue is that some C functions, like cpv() are declared as static inline, which prevents them from being exported by the library.

-Greg

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

3. Re: Euphoria Chipmunk Physics

ghaberek said...
Icy_Viking said...

Hello,

I have managed to wrap the chipmunk physics library for Euphoria. http://www.rapideuphoria.com/uploads/euchipmunkphy.zip You can get it from the link. I was hoping to make a demo to go with it, however, I could not figure out how to Euphorialize the hello world chipmunk example that is written in C. The wrapper itself seems to work fine. I did test it and check it against some functions and procedures and it seems to work. No errors came up. Any comments or suggestions are welcomed.

I had a look at your code and I'm not sure if you're going to be able to do this the way you're intending. The problem is that the Chipmunk library passes structures by value. Every time you see the code passing cpVect without an asterisk *, that means it's passing two literal C_DOUBLE values on the stack. This is the same problem you'd have on Allegro 5. Euphoria's C interop routines are simply not designed to pass structures by value at this time. I assume this is why I saw all of the vector parameters declared as sequence in your wrapper code. Unfortunately that will not work either. I was able to work around this in Allegro 5 by using a C shim library that manually peeked and poked the values as pointers instead.

Take the following C definitions:

typedef struct cpVect{ 
    cpFloat x, y; 
} cpVect 
 
cpVect cpBodyGetPosition(const cpBody *body) 
void cpBodySetPosition(cpBody *body, cpVect pos) 

And here is the non-functional wrapper code:

-- structure offets 
constant cpVect_x = 0 -- cpFloat (C_DOUBLE) 
constant cpVext_y = 8 -- cpFloat (C_DOUBLE) 
constant SIZEOF_CPVECT = 16 
 
constant x_cpBodyGetPosition = define_c_func( chipmunk, "+cpBodyGetPosition", {C_POINTER}, {C_DOUBLE,C_DOUBLE} /* return value must be an atom. this will not work! */ ) 
constant x_cpBodySetPosition = define_c_proc( chipmunk, "+cpBodySetPosition", {C_POINTER,C_DOUBLE,C_DOUBLE} /* this is 'cheating' but should technically work */ ) 

Another issue is that some C functions, like cpv() are declared as static inline, which prevents them from being exported by the library.

-Greg

Ah. I did not think of that. So it seems some more tricks and hacks sorta to speak would be needed in order to make this fully work. Even though Chipmunk is written in C as is Allegro, it seems some libraries are harder to wrap than others. Still a worthy shot, if nothing less. I wonder if Phix might be better at wrapping some of these libraries, it is very similar to Euphoria?

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

4. Re: Euphoria Chipmunk Physics

Icy_Viking said...

Ah. I did not think of that. So it seems some more tricks and hacks sorta to speak would be needed in order to make this fully work. Even though Chipmunk is written in C as is Allegro, it seems some libraries are harder to wrap than others. Still a worthy shot, if nothing less. I wonder if Phix might be better at wrapping some of these libraries, it is very similar to Euphoria?

As far as I can tell, Phix currently only supports a single return value as well. See builtins/VM/pcfunc.e. I'm not too sure how much work would be involved in extended Euphoria or Phix to handle this. Perhaps Pete or Jim could chime in on this.

-Greg

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

5. Re: Euphoria Chipmunk Physics

Icy_Viking said...

Ah. I did not think of that. So it seems some more tricks and hacks sorta to speak would be needed in order to make this fully work. Even though Chipmunk is written in C as is Allegro, it seems some libraries are harder to wrap than others. Still a worthy shot, if nothing less. I wonder if Phix might be better at wrapping some of these libraries, it is very similar to Euphoria?

If you can stretch to a teensie weensie bit of inline assembly then yes, Phix would indeed be better.

As well as (and completely independently of) c_func and define_c_func, in Phix you can call directly from inline assembly, with no other declaration required, eg:

function GetLastError() 
integer res 
#ilASM{ 
        call "kernel32.dll","GetLastError" 
        mov [res],eax 
      } 
    return res 
end function 

Unfortunately I have no direct experience with those rarer kinds of C functions, but if you can compile a bit of C, you can disassemble it with demo\pGUI\filedump.exw to see what a call translates to.

Pete

PS For some reason, I also wrote "For cross-platform use, #ilASM also has the guards [32], [64], [PE32], [PE64], ELF32], and [ELF64]" and I refuse to delete it.
Documentation of #ilASM is sketchy (but see phix.chm/internals, especially pLoadFlt..pStoreMint) however there are plenty of examples in builtins\VM
Also I would recommend something like tinycc over something that translates everything to incomprehensible SSE2 instructions

PPS another resource I often use is https://board.flatassembler.net/search.php to see if there is an example for a specific call

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

Search



Quick Links

User menu

Not signed in.

Misc Menu