Page 1 of 2

Purebasic interface to Box2D / Chipmunk2D [solved. again]

Posted: Sat Nov 21, 2015 1:20 pm
by Lunasole
Currently I'm messing around Box2D, nice physics engine for 2D games.

The problem is that it is written totally in C++ classes and it static lib cannot be directly included to PB program.

But there is dll wrapper that as it's author said, allows using Box2D from pure C.
Well, I have generated PB interface to that wrapper using it sources, but some functions still requiring classes as arguments and this makes me frustrating for 2 recent days yet.

Seems I need someone much more experienced in PureBasic/C++ and it classes and wrapping them to procedures, to explain how I can deal with such wrapper functions requiring objects as arguments:

Code: Select all

; cb2vec2 here represents CLASS, not even a pointer to class stored inside wrapper
Prototype     b2body_getlocalpoint (*cb2body_body, localPoint.cb2vec2, *cb2vec2_outPos)
Prototype     b2body_getlocalvector (*cb2body_body, localPoint.cb2vec2, *cb2vec2_outPos)
The archive here contains Box2C sources (wrapper for C), Box2CS (wrapper for C#, using that dll for C) + BOX2D static lib used by wrapper.
Also _out.pb is my file with declarations of wrapper functions (it is raw and auto-generated, currently just test version which may be very incorrect)
http://rghost.net/79svZvBpd

Re: Purebasic interface to Box2D

Posted: Sat Nov 21, 2015 4:06 pm
by Lunasole
I'll try to concretize the problem using single example (the other problematic functions are the same).


There is exported function in wrapper library (BOX2C), written in C++:

Code: Select all

cb2world *b2world_constructor (cb2vec2 gravity, bool doSleep)
{
	return new b2World(gravity, doSleep);
}
This function creates new instance of B2World class (from BOX2D static library), stores it inside wrapper library, and the function returns pointer to it. The pointer can be used later as argument to other wrapper functions, so that's OK and clear with that.

But It also takes argument "gravity" of type "cb2vec2". Which is defined inside wrapper header files as

Code: Select all

typedef b2Vec2 cb2vec2;
// b2Vec2 here is class from BOX2D, cb2vec2 is it's name inside wrapper library (BOX2C)

I didn't get what this declaration does exactly. Does it exports that type or something like that?
How it is possible to declare/access/send to function that "cb2vec2" from PureBasic code?

Re: Purebasic interface to Box2D

Posted: Sat Nov 21, 2015 10:19 pm
by Demivec
I have little experience doing this but I do have some observations from discussions in the forum.

If type b2vec2 is just two numbers, no bigger than longs, it could be coded this way?

Code: Select all

Structure b2vec2
  x.l
  y.l
EndStructure
This means if you were passing the structure (and not a pointer to a structure) as a parameter you can use a quad to hold both elements.

Code: Select all

Prototype     b2body_getlocalpoint (*cb2body_body, localPoint.q, *cb2vec2_outPos) ;quad instead of cb2vec2 type
Prototype     b2body_getlocalvector (*cb2body_body, localPoint.q, *cb2vec2_outPos) ;quad instead of cb2vec2 type

Re: Purebasic interface to Box2D

Posted: Mon Nov 23, 2015 6:38 pm
by Lunasole
Well I had few possible solutions:
- try send structure instead of class (similar to @Demivec variant)
- edit wrapper sources and add several function to it which would handle those classes too, that would be easiest way
- maybe something like "Interface" might work? not sure about how it works, but maybe can add to wrapper CreateObj() function which will give pointer to bind interface
- emulate classes in C-style, using structures with pointers. so much work

And I didn't tried none of them 8)
Just found some Chipmunk2D phys engine (https://chipmunk-physics.net/) - it is written in pure C99, is faster than BOX2D and gives the same abilities and quality, and it surely doesn't uses classes everywhere. It is possible to use it static lib directly, currently I'm working on bindings for it latest version (PureBasic bindings to outdated engine version 5 can be found, but they are not complete and having some other minuses)

Re: Purebasic interface to Box2D

Posted: Tue Nov 24, 2015 2:40 pm
by IceSoft
Lunasole wrote: Just found some Chipmunk2D phys engine (https://chipmunk-physics.net/) - it is written in pure C99, is faster than BOX2D and gives the same abilities and quality, and it surely doesn't uses classes everywhere. It is possible to use it static lib directly, currently I'm working on bindings for it latest version (PureBasic bindings to outdated engine version 5 can be found, but they are not complete and having some other minuses)
Really?
I am working also on Chipmunk2D 7.0.1 wrapper. How far are you?

Re: Purebasic interface to Box2D

Posted: Tue Nov 24, 2015 3:02 pm
by Lunasole
IceSoft wrote: I am working also on Chipmunk2D 7.0.1 wrapper. How far are you?
Thats great. What I have done for now:
- compiled Chipmunk2D 7.0.1 static lib for Win32 [using Visual Studio 2010 Express)
- generated purebasic-formatted list of imported functions using dumpbin.exe + custom parsers
- manually ported all structures/types required for imported functions

// thanks to author of previous bindings, cause without using some his tricks me with my one-month purebasic experience would have much more annoying problems doing such stuff

And got stuck since yesterday, when tried to test it all at last.
Getting such error from PB linker:
Image

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Tue Nov 24, 2015 3:20 pm
by Lunasole
@IceSoft
Also, here are all my current resulting files, so take a look if something might be interesting for you: http://rghost.net/8RtCXkxZC

Currently I absolutely have no idea how to solve that "another annoying crap" with linker, so resurrecting this thread and maybe someone might help

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Tue Nov 24, 2015 6:30 pm
by Lunasole
Thanks to Chi post here [http://www.purebasic.fr/english/viewtop ... 13&t=63370]

After doing this
chi wrote:Go to Project\Properties\Configuration Properties\C/C++\All Options

Security Check -> Disable Security Check (/GS-)
Whole Program Optimization -> No
... and rebuilding library, I've got static lib with only 900kb+ in size, which is very different from 2.6mb of previous.
And polink.exe message changed, now it talks about unrecognized commands and 2 unresolved symbols.
Well, this looks like some progress :lol:

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Tue Nov 24, 2015 6:34 pm
by IceSoft
Change some other properties and is it working

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Tue Nov 24, 2015 7:03 pm
by Lunasole
IceSoft wrote:Change some other properties and is it working
I just took msvcrt.lib from Visual Studio and replaced one that PureBasic uses, as it was in your topic
http://www.purebasic.fr/english/viewtop ... =5&t=41247

After that only one external symbol remains unresolved: "?cpCheckSignedArea@@YAEUcpVect@@00@Z".
This is a name of one of imported functions

Code: Select all

cpCheckSignedArea.cpBool  (*a.cpVect, *b.cpVect, *c.cpVect) As "?cpCheckSignedArea@@YAEUcpVect@@00@Z"
Another little but extremely annoying shit, hah. I'd rather start testing what I ported than messing with all of this don't exactly knowing such problems and their solutions :shock: But nothing to do

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Tue Nov 24, 2015 7:17 pm
by IceSoft
for your first test comment it out

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Tue Nov 24, 2015 7:50 pm
by Lunasole
IceSoft wrote:for your first test comment it out
Even if commented linker still tells that it is unresolved. Or wait, if you mean comment in lib code, then OK, should work :)


Also solving CRT problem by replacing default lib of PB compiler is not good, so the problem with unresolved "__imp____iob_func" symbol remains too.
I tried preprocessor definition "/D__imp____iob_func=" but it has no effect.

Maybe polink has option to skip unresolved symbols or similar "easy way"?
Anyway I'm tired of those funny things for today so will try something a bit later.

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Tue Nov 24, 2015 7:58 pm
by IceSoft
remove all fprint/sprint with print and it will work

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Tue Nov 24, 2015 10:51 pm
by Lunasole
IceSoft wrote:remove all fprint/sprint with print and it will work
Thanks a lot man, it worked.

The problem with cpCheckSignedArea symbol just resolved too -- it was because cpRobust.c [where that funct is implemented] for some reason was not initially included to project.

At last now It all works as should and I got my first handle calling cpSpaceInit () from PB 8)

Now I'll leave The First Working Version here [http://rghost.net/8jQrdlVRj] (nice archive size, btw) for those who might be interested, and going learn deeper the engine itself & fix/improve bindings at the same time.
Thanks to all who helped one way or another.

@IceSoft: - in your version of Chipmunk v5 binds functions params were declared as par1, par2, that was a "minus", I tried to make my parser keep it all more usable and close to original. You can use anything you find useful in my result.

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Wed Nov 25, 2015 7:42 am
by IceSoft
Lunasole wrote: @IceSoft: - in your version of Chipmunk v5 binds functions params were declared as par1, par2, that was a "minus", I tried to make my parser keep it all more usable and close to original. You can use anything you find useful in my result.
Of course I will do. Thanks for your works.