Use of STL in 2.1 (building for Android)

Hi all,

I have a successful build of 2.0.1 for Android (after some hacking) but I got stuck needing to move "static" bodies about. This is solved in 2.1 with the introduction of kinematic bodies (beautiful!) however I am now stuck building 2.1 for Android due to the use of the std::sort function in b2BroadPhase.h:
Code:
   // Sort the pair buffer to expose duplicates.
   std::sort(m_pairBuffer, m_pairBuffer + m_pairCount, b2PairLessThan);


Unfortunately Android does not have an implementation of STL. This is the only bit of code causing a problem for me, so if I can alter this to use some other custom sort function, or somehow remove the need to sort all my [box2d] problems would be solved.

Anyone got any ideas on the best approach to get this puppy building for Android?

Thanks,

Jason.
Use C's quicksort?
Yep.. done just that.

Changed:
Code:
std::sort(m_pairBuffer, m_pairBuffer + m_pairCount, b2PairLessThan);


To
Code:
qsort(m_pairBuffer, sizeof(m_pairBuffer) / sizeof(struct b2Pair) , sizeof(struct b2Pair), b2PairCompareQSort);


And added:
Code:
//The return value of this function should represent whether elem1 is considered less than,
//equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.
inline int b2PairCompareQSort(const void * elem1, const void * elem2)
{
   b2Pair* pair1 = (b2Pair*) elem1;
   b2Pair* pair2 = (b2Pair*) elem2;

   if (pair1->proxyIdA < pair2->proxyIdA)
   {
      return -1;
   }

   if (pair1->proxyIdA == pair2->proxyIdA)
   {
      if( pair1->proxyIdB < pair2->proxyIdB ) {
         return -1;
      }
      else if(pair1->proxyIdB > pair2->proxyIdB) {
         return 1;
      }
      else {
         return 0;
      }
   }
   else {
      return 1;
   }

}


I don't have the test environment running, so I have no idea if this will work as expected however. I'd appreciate any feedback/alternatives.

Cheers.
P.S. to anyone stumbling in here....

The above code change seems to work for me.. so far.
FYI: I've had success in using the custom NDK from CRYSTAX .NET (available here: http://www.crystax.net/android/ndk-r3.php) to build box2d version 2.1.2 without modification. The custom NDK includes at least some of the C++ standard library/stl (not sure if it's the full library or a partial one) and is forwards-compatible with the official ABI.

Here is my Android.mk and Application.mk files for reference. I haven't set any box2d specific optimization/compiler flags or anything. I'm working on that next. Most notably, this doesn't build it with any soft floating point compiler flags.

Update: fixed Android.mk to properly build the shared library version of box2d.

Application.mk:
Code:
APP_MODULES      := box2d box2d-shared
APP_PROJECT_PATH := /cygdrive/c/AndroidWorkspace/Box2DTest
APP_OPTIM := release


Android.mk:
Note: I generate a static library libbox2d.a and a shared library libbox2d-shared.so, but no JNI bindings as of yet.
Code:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := box2d
LOCAL_C_INCLUDES := $(LOCAL_PATH)/Box2D
LOCAL_SRC_FILES :=    Box2D/Collision/b2BroadPhase.cpp \
            Box2D/Collision/b2CollideCircle.cpp \
            Box2D/Collision/b2CollidePolygon.cpp \
            Box2D/Collision/b2Collision.cpp \
            Box2D/Collision/b2Distance.cpp \
            Box2D/Collision/b2DynamicTree.cpp \
            Box2D/Collision/b2TimeOfImpact.cpp \
            Box2D/Collision/Shapes/b2CircleShape.cpp \
            Box2D/Collision/Shapes/b2PolygonShape.cpp \
            Box2D/Common/b2BlockAllocator.cpp \
            Box2D/Common/b2Math.cpp \
            Box2D/Common/b2Settings.cpp \
            Box2D/Common/b2StackAllocator.cpp \
            Box2D/Dynamics/b2Body.cpp \
            Box2D/Dynamics/b2ContactManager.cpp \
            Box2D/Dynamics/b2Fixture.cpp \
            Box2D/Dynamics/b2Island.cpp \
            Box2D/Dynamics/b2World.cpp \
            Box2D/Dynamics/b2WorldCallbacks.cpp \
            Box2D/Dynamics/Contacts/b2CircleContact.cpp \
            Box2D/Dynamics/Contacts/b2Contact.cpp \
            Box2D/Dynamics/Contacts/b2ContactSolver.cpp \
            Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp \
            Box2D/Dynamics/Contacts/b2PolygonContact.cpp \
            Box2D/Dynamics/Contacts/b2TOISolver.cpp \
            Box2D/Dynamics/Joints/b2DistanceJoint.cpp \
            Box2D/Dynamics/Joints/b2FrictionJoint.cpp \
            Box2D/Dynamics/Joints/b2GearJoint.cpp \
            Box2D/Dynamics/Joints/b2Joint.cpp \
            Box2D/Dynamics/Joints/b2LineJoint.cpp \
            Box2D/Dynamics/Joints/b2MouseJoint.cpp \
            Box2D/Dynamics/Joints/b2PrismaticJoint.cpp \
            Box2D/Dynamics/Joints/b2PulleyJoint.cpp \
            Box2D/Dynamics/Joints/b2RevoluteJoint.cpp \
            Box2D/Dynamics/Joints/b2WeldJoint.cpp

include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := box2d-shared
LOCAL_C_INCLUDES := $(LOCAL_PATH)/Box2D
LOCAL_SRC_FILES :=    Box2D/Collision/b2BroadPhase.cpp \
            Box2D/Collision/b2CollideCircle.cpp \
            Box2D/Collision/b2CollidePolygon.cpp \
            Box2D/Collision/b2Collision.cpp \
            Box2D/Collision/b2Distance.cpp \
            Box2D/Collision/b2DynamicTree.cpp \
            Box2D/Collision/b2TimeOfImpact.cpp \
            Box2D/Collision/Shapes/b2CircleShape.cpp \
            Box2D/Collision/Shapes/b2PolygonShape.cpp \
            Box2D/Common/b2BlockAllocator.cpp \
            Box2D/Common/b2Math.cpp \
            Box2D/Common/b2Settings.cpp \
            Box2D/Common/b2StackAllocator.cpp \
            Box2D/Dynamics/b2Body.cpp \
            Box2D/Dynamics/b2ContactManager.cpp \
            Box2D/Dynamics/b2Fixture.cpp \
            Box2D/Dynamics/b2Island.cpp \
            Box2D/Dynamics/b2World.cpp \
            Box2D/Dynamics/b2WorldCallbacks.cpp \
            Box2D/Dynamics/Contacts/b2CircleContact.cpp \
            Box2D/Dynamics/Contacts/b2Contact.cpp \
            Box2D/Dynamics/Contacts/b2ContactSolver.cpp \
            Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp \
            Box2D/Dynamics/Contacts/b2PolygonContact.cpp \
            Box2D/Dynamics/Contacts/b2TOISolver.cpp \
            Box2D/Dynamics/Joints/b2DistanceJoint.cpp \
            Box2D/Dynamics/Joints/b2FrictionJoint.cpp \
            Box2D/Dynamics/Joints/b2GearJoint.cpp \
            Box2D/Dynamics/Joints/b2Joint.cpp \
            Box2D/Dynamics/Joints/b2LineJoint.cpp \
            Box2D/Dynamics/Joints/b2MouseJoint.cpp \
            Box2D/Dynamics/Joints/b2PrismaticJoint.cpp \
            Box2D/Dynamics/Joints/b2PulleyJoint.cpp \
            Box2D/Dynamics/Joints/b2RevoluteJoint.cpp \
            Box2D/Dynamics/Joints/b2WeldJoint.cpp

include $(BUILD_SHARED_LIBRARY)
Update: I compiled the "Hello World" box2d example under both the official ndk (using doug's port of 2.0.1) and the unofficial ndk (doug's port with the redirect headers taken out).

Results:
Official NDK: final libHelloWorld.so size: 240,567 bytes, or about 235kb
NDK w/full c++ standard library: final libHelloWorld.so size: 314,635 bytes, or about 307kb

So, the full standard C++ library added only 74,068 bytes, or about 72kb, to the final linked binary.

I also tried compiling box2d as a shared library under both environments and compared the results:
Official NDK: final libbox2d.so size: 258,806 bytes, or about 253kb
NDK w/full c++ standard library: 337,931 bytes, or about 330kb

So, the full standard C++ library only adds 79,125 bytes, or about 77kb.
Great info. Thanks.

I will try your approach then hopefully I can just grab updates from box2d trunk without having to manually merge into my "Android version"

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