SVN Unstable

The SVN version is very unstable now. The code may not even compile!

Please stick with 1.4.3 until 2.0 is released. This will make it easier for me to move between computers.
I've been browsing through the changes, and now I'm really looking forwards to 2.0... 😎

The callback mechanism would work better using the C++ Fast Delegate template instead of an abstract interface class. I switched over to it from abstract interfaces for collision and damage events and it drastically simplified things and eliminated a lot of multiple inheritance. The delegate object is essentially a function pointer and "this" pointer, and can handle free functions (including static members), regular member functions, virtual member functions, and even gnarly things like multiple inheritance and virtual base classes. Once you have a delegate, you can then use it like a function pointer (using operator()) without caring where it came from. I discovered the power of delegates from ActionScript mx.utils.Delegate, of all places, but it's just as useful (if not more so) in C++.

The CodeProject site has been a bit slow today, but it seems to be working now.
Thanks for the tip. I won't have time to look into this for 2.0, but perhaps for a future release.

One issue that concerns me is compiler support. Box2D is often compiled on some limited platforms, like the DS and GP32.
It doesn't take long to add once you figure out how to actually include it and call it. It's in a namespace (fastdelegate), which tripped me up for a while. Aside from that, it acts just like a function pointer except that it can point to member functions (even virtual ones).

For example, using FastDelegate in b2World for joint-destroyed events (patterned after the "Passing a FastDelegate as a function parameter" section of the article) would look something like this:

Code: Select all

public:
typedef fastdelegate::FastDelegate<void (b2Joint *)> b2WorldListenerJointDestroyed;
void SetWorldListenerJointDestroyed(b2WorldListenerJointDestroyed listener) { m_jointdestroyed = listener; }

private:
b2WorldListenerJointDestroyed m_jointdestroyed;


To bind the delegate to a non-member function JointDestroyedCallback:

Code: Select all

SetWorldListenerJointDestroyed(JointDestroyedCallback);


To bind the delegate to an instance of a b2WorldListener named worldListener:

Code: Select all

SetWorldListenerJointDestroyed(MakeDelegate(worldListener, &b2WorldListener::NotifyJointDestroyed));


Alternatively, if the listener is public:

Code: Select all

m_jointdestroyed.bind(JointDestroyedCallback);

and

Code: Select all

m_jointdestroyed.bind(worldListener, &b2WorldListener::NotifyJointDestroyed);


Then, b2World can use the delegate to notify the listener when a joint gets destroyed:

Code: Select all

if (m_jointdestroyed)
   m_jointdestroyed(jn0->joint);


The FastDelegate header is incredibly convoluted because of the variety of ways compilers implement pointer-to-member, but this towering edifice of template code compiles down to an amazingly small number of machine instructions.
Yup, that sure is broken 😛

Feature list in TODO looks good though 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=206