Page 1 of 2

Ruby Gem for Chipmunk

Posted: Wed Sep 30, 2009 5:01 am
by banister
Heya,

great work on Chipmunk :) and thanks for the ruby bindings, however it's slightly difficult to get chipmunk going for most ruby users. Would it be okay if i released a cross-platform ruby gem for chipmunk?

you can contact me at jrmair@gmail.com

thanks,

banister

Re: Ruby Gem for Chipmunk

Posted: Wed Sep 30, 2009 8:51 am
by banister
...I just went ahead and released the gem, hope you dont mind :)

I set as the homepage a link to the official chipmunk site, you can check out the github project here: http://github.com/banister/chipmunk

To install the gem just go: gem install chipmunk

If there's anything at all you want me to change about the gem or the README or anything at all, just let me know,

EDIT: just letting you know i had to modify the README file as alot of the content there was not relevant to the ruby release

thanks

banister

Re: Ruby Gem for Chipmunk

Posted: Wed Sep 30, 2009 11:45 am
by slembcke
That's great. People have been asking about making a gem for a while, but I never looked into what it would take to make one. Maybe you could give me some tips on how to build an extension properly for the trunk version? For our last project, I wanted to try SCons and hacked something together that could build the extension on Windows, Linux, and OS X. I wasn't 100% satisfied with it though.

Re: Ruby Gem for Chipmunk

Posted: Thu Oct 01, 2009 12:57 am
by banister
well the best solution by far for building extensions (and gems based on extensions) is to use rake-compiler (http://github.com/luislavena/rake-compiler)
it even cross-compiles to windows ;) (so you can save a tedious reboot and eternal frustration with the microsoft C compiler...which hates c99 btw :/)

If you're interested how to use it, just take a peek at the Rakefile I use in my project (http://github.com/banister/chipmunk)

cheers :)

Re: Ruby Gem for Chipmunk

Posted: Thu Oct 01, 2009 8:20 am
by slembcke
banister wrote:.which hates c99 btw :/
It certainly does :(. The solution to compiling Chipmunk under MSVC up to now has been to compile as C++ which has enough of C99's features to make it non-tedious. Dynamically sized arrays on the stack are still out, but it's not to bad to use alloca instead though.

Re: Ruby Gem for Chipmunk

Posted: Fri Nov 20, 2009 11:20 am
by shawn42
You should look at using FFI to make this gem more portable.

Re: Ruby Gem for Chipmunk

Posted: Tue Dec 01, 2009 11:09 am
by shawn42
I have started to play with FFI and chipmunk bindings..

http://github.com/shawn42/chipmunk-ffi

I have noticed that most of your cpVect class is static inlines, this makes them unavailable for calling via FFI. There are two ways around this:
One, I write all the Vec stuff in Ruby, not a fan of this option as it will be very slow.
Two, I'd like it if you could create a wrapper of all these inline functions that external libs could call. The benefits here would be that all the C chipmunk code could still get the speed of inline calls, and external libs could still call them without having to rewrite them.

Re: Ruby Gem for Chipmunk

Posted: Tue Dec 01, 2009 11:42 am
by slembcke
Assuming that you are using GCC, you could compile an empty C file that imports the chipmunk header and use the -fkeep-inline-functions flag. I think that should give you a library that you can link to that has a concrete version of all of the inline functions and nothing else. As there are a lot more inline functions than just the cpVect ops, this would probably be the easiest solution all around.

http://gcc.gnu.org/onlinedocs/gcc/Inline.html

Defining them as extern inline also looks like an option, but it requires you to copy/paste the function and maintain a copy of it in the header and in a module. That sounds rather annoying and I'd probably forget to update both copies. If the above solution works, that would be best.

Re: Ruby Gem for Chipmunk

Posted: Tue Dec 01, 2009 12:19 pm
by slembcke
Hmm. Just tried that, and it doesn't seem to work the way I expected it would.

Making a reference to each function you need does work however. A slight pain, though at least it can be templated so it's as simple as maintaining a list of function names.

Code: Select all

#include <stdlib.h>
#include "src/chipmunk.h"

static void *functions[] = {
	cpvadd,
	cpvsub,
};
Couldn't figure out how to make it work without compiling as -O0, but that's not really going to be a big deal when calling out to the functions from slow Ruby code. Probably not much slower than my hand crafted extension code I have now.

Compile and check with:
gcc -O0 -ffast-math -std=c99 test.c -c && nm test.o
Compile that as a dynamic library and you should be good to go I think.

Re: Ruby Gem for Chipmunk

Posted: Tue Dec 01, 2009 1:41 pm
by shawn42
I guess I'm looking for more of a global solution that would be in chipmunk itself. The idea of writing FFI bindings is that the users wouldn't need to custom compile anything.

It'd be great if we could somehow just make those available in the default build of chipmunk. Does that make sense?

PS feel free to shoot me an email or IM at shawn42 __A_T__ gmail