Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Chipmunk #122

Merged
merged 3 commits into from

2 participants

@StephaneDelcroix

Allow subclasses of chipmunk objects. Also return the right subclass of Constraint and Shape from the callback.

StephaneDelcroix added some commits
@StephaneDelcroix StephaneDelcroix [cocos2d] re-enable chipmunk integration for ios 81cae74
@StephaneDelcroix StephaneDelcroix [chipmunk] expose cpArbiter's UserData 8e52586
@StephaneDelcroix StephaneDelcroix [chipmunk] unseal Body, Constraint, Shape, ...
Keep a reference of the managed object in the native UserData. Allows
subclasses of Shape and Constraint returned by callbacks to be the right
one instead of parent's.

Allow also user to subclass Bodies, Constraints, Shapes. Arbiters are
still sealed: we don't control their livetimes, so if we keep a
referenced to the managed object, we don't know at which point in time
to clear it.
f4144b4
@migueldeicaza migueldeicaza merged commit ce95e6f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 18, 2013
  1. @StephaneDelcroix
  2. @StephaneDelcroix
  3. @StephaneDelcroix

    [chipmunk] unseal Body, Constraint, Shape, ...

    StephaneDelcroix authored
    Keep a reference of the managed object in the native UserData. Allows
    subclasses of Shape and Constraint returned by callbacks to be the right
    one instead of parent's.
    
    Allow also user to subclass Bodies, Constraints, Shapes. Arbiters are
    still sealed: we don't control their livetimes, so if we keep a
    referenced to the managed object, we don't know at which point in time
    to clear it.
This page is out of date. Refresh to see the latest.
View
11 chipmunk/binding/chipmunk-diff
@@ -1,7 +1,7 @@
-diff -urN ./include/chipmunk/chipmunk_bind.h ../Chipmunk/include/chipmunk/chipmunk_bind.h
+diff -ruN ./include/chipmunk/chipmunk_bind.h ../Chipmunk/include/chipmunk/chipmunk_bind.h
--- ./include/chipmunk/chipmunk_bind.h 1970-01-01 01:00:00.000000000 +0100
-+++ ../Chipmunk/include/chipmunk/chipmunk_bind.h 2013-02-12 14:00:21.000000000 +0100
-@@ -0,0 +1,155 @@
++++ ../Chipmunk/include/chipmunk/chipmunk_bind.h 2013-03-18 13:03:43.000000000 +0100
+@@ -0,0 +1,156 @@
+//
+// chipmunk_bind.h
+//
@@ -152,14 +152,15 @@ diff -urN ./include/chipmunk/chipmunk_bind.h ../Chipmunk/include/chipmunk/chipmu
+MAKE_PROPERTY_FOR_BINDING(cpFloat, cpArbiter, Elasticity);
+MAKE_PROPERTY_FOR_BINDING(cpFloat, cpArbiter, Friction);
+MAKE_PROPERTY_FOR_BINDING(cpVect, cpArbiter, SurfaceVelocity);
++MAKE_PROPERTY_FOR_BINDING(cpDataPointer, cpArbiter, UserData);
+
+void __cpArbiterGetShapes(const cpArbiter *arb, cpShape **a, cpShape **b) { cpArbiterGetShapes (arb, a, b); }
+void __cpArbiterGetBodies(const cpArbiter *arb, cpBody **a, cpBody **b) { cpArbiterGetBodies (arb, a, b); }
+
+#endif
-diff -urN ./src/chipmunk.c ../Chipmunk/src/chipmunk.c
+diff -ruN ./src/chipmunk.c ../Chipmunk/src/chipmunk.c
--- ./src/chipmunk.c 2012-05-25 15:20:06.000000000 +0200
-+++ ../Chipmunk/src/chipmunk.c 2013-02-05 16:01:20.000000000 +0100
++++ ../Chipmunk/src/chipmunk.c 2013-03-18 13:00:58.000000000 +0100
@@ -320,3 +320,5 @@
#endif
View
20 chipmunk/binding/src/Arbiter.cs
@@ -99,8 +99,8 @@ public void GetShapes (out Shape shapeA, out Shape shapeB)
{
IntPtr a, b;
__cpArbiterGetShapes (Handle.Handle, out a, out b);
- shapeA = new Shape (a);
- shapeB = new Shape (b);
+ shapeA = Shape.FromIntPtr (a);
+ shapeB = Shape.FromIntPtr (b);
}
[DllImport ("__Internal")]
@@ -110,8 +110,8 @@ public void GetBodies (out Body bodyA, out Body bodyB)
{
IntPtr a, b;
__cpArbiterGetBodies (Handle.Handle, out a, out b);
- bodyA = new Body (a);
- bodyB = new Body (b);
+ bodyA = Body.FromIntPtr (a);
+ bodyB = Body.FromIntPtr (b);
}
//TODO contact point set
@@ -137,7 +137,15 @@ public void GetBodies (out Body bodyA, out Body bodyB)
get { return cpArbiterTotalKE (Handle.Handle); }
}
-
-
+ [DllImport("__Internal")]
+ extern static IntPtr __cpArbiterGetUserData (IntPtr body);
+
+ [DllImport("__Internal")]
+ extern static void __cpArbiterSetUserData (IntPtr body, IntPtr userData);
+
+ internal override IntPtr UserData {
+ get { return __cpArbiterGetUserData (Handle.Handle); }
+ set { __cpArbiterSetUserData (Handle.Handle, value); }
+ }
}
}
View
22 chipmunk/binding/src/Body.cs
@@ -20,7 +20,7 @@
namespace Chipmunk
{
- public sealed partial class Body : ChipmunkObject
+ public partial class Body : ChipmunkObject
{
public Body (IntPtr ptr) : base (ptr)
{
@@ -41,6 +41,15 @@ public static Body CreateStatic ()
return new Body (cpBodyNewStatic ());
}
+ internal static Body FromIntPtr (IntPtr ptr)
+ {
+ var userdata = __cpBodyGetUserData (ptr);
+ if (userdata == IntPtr.Zero)
+ return new Body (ptr);
+ var gchandle = GCHandle.FromIntPtr (__cpBodyGetUserData (ptr));
+ return (Body)gchandle.Target;
+ }
+
[DllImport("__Internal")]
extern static void cpBodyFree (IntPtr body);
@@ -182,7 +191,7 @@ protected override void Free ()
[DllImport("__Internal")]
extern static void __cpBodySetUserData (IntPtr body, IntPtr userData);
- public IntPtr UserData {
+ internal override IntPtr UserData {
get { return __cpBodyGetUserData (Handle.Handle); }
set { __cpBodySetUserData (Handle.Handle, value); }
}
@@ -278,7 +287,8 @@ public void SleepWithGroup (Body group)
public void EachShape (Action<Body, Shape> action)
{
ShapeIterator iterator = (body, shape, data) => {
- action (new Body(body), new Shape(shape));
+ action (Body.FromIntPtr (body),
+ Shape.FromIntPtr (shape));
};
cpBodyEachShape (Handle.Handle, iterator, IntPtr.Zero);
}
@@ -291,7 +301,8 @@ public void EachShape (Action<Body, Shape> action)
public void EachConstraint (Action<Body, Constraint> action)
{
ConstraintIterator iterator = (body, constraint, data) => {
- action (new Body(body), new Constraint(constraint));
+ action (Body.FromIntPtr (body),
+ Constraint.FromIntPtr (constraint));
};
cpBodyEachConstraint (Handle.Handle, iterator, IntPtr.Zero);
}
@@ -304,7 +315,8 @@ public void EachConstraint (Action<Body, Constraint> action)
public void EachArbiter (Action<Body, Arbiter> action)
{
ArbiterIterator iterator = (body, arbiter, data) => {
- action (new Body(body), new Arbiter(arbiter));
+ action (Body.FromIntPtr (body),
+ new Arbiter(arbiter));
};
cpBodyEachArbiter (Handle.Handle, iterator, IntPtr.Zero);
}
View
18 chipmunk/binding/src/ChipmunkObject.cs
@@ -17,7 +17,7 @@
namespace Chipmunk
{
- public class ChipmunkObject : IDisposable
+ public abstract class ChipmunkObject : IDisposable
{
static Dictionary<IntPtr, List<ChipmunkObject>> references = new Dictionary<IntPtr, List<ChipmunkObject>> ();
@@ -31,14 +31,17 @@ public void Dispose ()
}
bool refcount;
- protected ChipmunkObject (IntPtr ptr, bool refcount = true)
+ internal protected ChipmunkObject (IntPtr ptr, bool refcount = true)
{
Handle = new HandleRef (this, ptr);
this.refcount = refcount;
- if (refcount)
+ if (refcount)
AddRef (this, ptr);
+
+ if (refcount) //Don't set it for Arbiters, as we have no way to free it
+ UserData = GCHandle.ToIntPtr (GCHandle.Alloc (this));
}
-
+
public static bool operator ==(ChipmunkObject a, ChipmunkObject b)
{
if (System.Object.ReferenceEquals (a, b))
@@ -76,6 +79,11 @@ public override int GetHashCode ()
void Cleanup ()
{
+ if (refcount && UserData!= IntPtr.Zero) {
+ var gchandle = GCHandle.FromIntPtr (UserData);
+ gchandle.Free ();
+ }
+
if (refcount && RemoveRef (this, Handle.Handle))
Free ();
Handle = new HandleRef (this, IntPtr.Zero);
@@ -111,5 +119,7 @@ static bool RemoveRef (ChipmunkObject obj, IntPtr ptr)
#endif
return false;
}
+
+ internal abstract IntPtr UserData { get; set; }
}
}
View
53 chipmunk/binding/src/Constraint.cs
@@ -18,6 +18,15 @@ internal Constraint (IntPtr ptr) : base (ptr)
{
}
+ internal static Constraint FromIntPtr (IntPtr ptr)
+ {
+ var userdata = __cpConstraintGetUserData (ptr);
+ if (userdata == IntPtr.Zero)
+ return new Constraint (ptr);
+ var gchandle = GCHandle.FromIntPtr (__cpConstraintGetUserData (ptr));
+ return (Constraint)gchandle.Target;
+ }
+
[DllImport ("__Internal")]
extern static void cpConstraintFree (IntPtr constraint);
@@ -30,14 +39,14 @@ protected override void Free ()
extern static IntPtr __cpConstraintGetA (IntPtr constraint);
public Body A {
- get { return new Body (__cpConstraintGetA (Handle.Handle)); }
+ get { return Body.FromIntPtr (__cpConstraintGetA (Handle.Handle)); }
}
[DllImport("__Internal")]
extern static IntPtr __cpConstraintGetB (IntPtr constraint);
public Body B {
- get { return new Body (__cpConstraintGetA (Handle.Handle)); }
+ get { return Body.FromIntPtr (__cpConstraintGetA (Handle.Handle)); }
}
[DllImport("__Internal")]
@@ -73,12 +82,12 @@ protected override void Free ()
set { __cpConstraintSetMaxBias (Handle.Handle, value); }
}
- [DllImport ("__Internal")]
- extern static IntPtr __cpConstraintGetSpace (IntPtr constraint);
+ //[DllImport ("__Internal")]
+ //extern static IntPtr __cpConstraintGetSpace (IntPtr constraint);
- public Space Space {
- get { return new Space (__cpConstraintGetSpace (Handle.Handle)); }
- }
+ //public Space Space {
+ // get { return new Space (__cpConstraintGetSpace (Handle.Handle)); }
+ //}
[DllImport("__Internal")]
extern static float __cpConstraintGetImpulse (IntPtr constraint);
@@ -86,10 +95,20 @@ protected override void Free ()
public float Impulse {
get { return __cpConstraintGetImpulse (Handle.Handle); }
}
+
+ [DllImport("__Internal")]
+ extern static IntPtr __cpConstraintGetUserData (IntPtr body);
+ [DllImport("__Internal")]
+ extern static void __cpConstraintSetUserData (IntPtr body, IntPtr userData);
+
+ internal override IntPtr UserData {
+ get { return __cpConstraintGetUserData (Handle.Handle); }
+ set { __cpConstraintSetUserData (Handle.Handle, value); }
+ }
}
- public sealed partial class PinJoint : Constraint
+ public partial class PinJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpPinJointNew (IntPtr bodyA, IntPtr bodyB, PointF anchr1, PointF anchr2);
@@ -132,7 +151,7 @@ public PinJoint (Body bodyA, Body bodyB, PointF anchr1, PointF anchr2) : base (c
}
}
- public sealed partial class SlideJoint : Constraint
+ public partial class SlideJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpSlideJointNew (IntPtr bodyA, IntPtr bodyB, PointF anchr1, PointF anchr2, float min, float max);
@@ -186,7 +205,7 @@ public SlideJoint (Body bodyA, Body bodyB, PointF anchr1, PointF anchr2, float m
}
}
- public sealed partial class PivotJoint : Constraint
+ public partial class PivotJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpPivotJointNew (IntPtr bodyA, IntPtr bodyB, PointF pivot);
@@ -225,7 +244,7 @@ public PivotJoint (Body bodyA, Body bodyB, PointF anchr1, PointF anchr2) : base
}
}
- public sealed partial class GrooveJoint : Constraint
+ public partial class GrooveJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpGrooveJointNew (IntPtr bodyA, IntPtr bodyB, PointF grooveA, PointF grooveB, PointF anchr2);
@@ -268,7 +287,7 @@ public GrooveJoint (Body bodyA, Body bodyB, PointF grooveA, PointF grooveB, Poin
}
}
- public sealed partial class DampedSpring : Constraint
+ public partial class DampedSpring : Constraint
{
[DllImport("__Internal")]
extern static IntPtr cpDampedSpringNew (IntPtr bodyA, IntPtr bodyB, PointF anchr1, PointF anchr2, float restLength, float stiffness, float damping);
@@ -333,7 +352,7 @@ public DampedSpring (Body bodyA, Body bodyB, PointF anchr1, PointF anchr2, float
}
}
- public sealed partial class DampedRotarySpring : Constraint
+ public partial class DampedRotarySpring : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpDampedRotarySpringNew (IntPtr bodyA, IntPtr bodyB, float restAngle, float stiffness, float damping);
@@ -376,7 +395,7 @@ public DampedRotarySpring (Body bodyA, Body bodyB, float restAngle, float stiffn
}
}
- public sealed partial class RotaryLimitJoint : Constraint
+ public partial class RotaryLimitJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpRotaryLimitJointNew (IntPtr bodyA, IntPtr bodyB, float min, float max);
@@ -408,7 +427,7 @@ public RotaryLimitJoint (Body bodyA, Body bodyB, float min, float max) : base (c
}
}
- public sealed partial class RatchetJoint : Constraint
+ public partial class RatchetJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpRatchetJointNew (IntPtr bodyA, IntPtr bodyB, float phase, float ratchet);
@@ -451,7 +470,7 @@ public RatchetJoint (Body bodyA, Body bodyB, float phase, float ratchet) : base
}
}
- public sealed partial class GearJoint : Constraint
+ public partial class GearJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpGearJointNew (IntPtr bodyA, IntPtr bodyB, float pahse, float ratio);
@@ -483,7 +502,7 @@ public GearJoint (Body bodyA, Body bodyB, float phase, float ratio) : base (cpGe
}
}
- public sealed partial class SimpleMotor : Constraint
+ public partial class SimpleMotor : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpSimpleMotorNew (IntPtr bodyA, IntPtr bodyB, float rate);
View
30 chipmunk/binding/src/Shape.cs
@@ -21,7 +21,16 @@ public partial class Shape : ChipmunkObject
public Shape (IntPtr handle) : base (handle)
{
}
-
+
+ internal static Shape FromIntPtr (IntPtr ptr)
+ {
+ var userdata = __cpShapeGetUserData (ptr);
+ if (userdata == IntPtr.Zero)
+ return new Shape (ptr);
+ var gchandle = GCHandle.FromIntPtr (__cpShapeGetUserData (ptr));
+ return (Shape)gchandle.Target;
+ }
+
[DllImport ("__Internal")]
extern static void cpShapeFree (IntPtr shape);
@@ -38,7 +47,7 @@ protected override void Free ()
extern static void cpShapeSetBody (IntPtr shape, IntPtr body);
public Body Body {
- get { return new Body (__cpShapeGetBody (Handle.Handle)); }
+ get { return Body.FromIntPtr (__cpShapeGetBody (Handle.Handle)); }
set { cpShapeSetBody (Handle.Handle, value.Handle.Handle); }
}
@@ -154,9 +163,20 @@ public RectangleF Update (PointF position, PointF rotation)
[DllImport ("__Internal", EntryPoint="cpResetShapeIdCounter")]
public extern static void ResetShapeIdCounter ();
+
+ [DllImport("__Internal")]
+ extern static IntPtr __cpShapeGetUserData (IntPtr body);
+
+ [DllImport("__Internal")]
+ extern static void __cpShapeSetUserData (IntPtr body, IntPtr userData);
+
+ internal override IntPtr UserData {
+ get { return __cpShapeGetUserData (Handle.Handle); }
+ set { __cpShapeSetUserData (Handle.Handle, value); }
+ }
}
- public sealed partial class CircleShape : Shape
+ public partial class CircleShape : Shape
{
[DllImport ("__Internal")]
extern static IntPtr cpCircleShapeNew (IntPtr body, float radius, PointF offset);
@@ -180,7 +200,7 @@ public CircleShape (Body body, float radius, PointF offset) : base (cpCircleShap
}
}
- public sealed partial class SegmentShape:Shape
+ public partial class SegmentShape:Shape
{
[DllImport("__Internal")]
extern static IntPtr cpSegmentShapeNew (IntPtr body, PointF a, PointF b, float radius);
@@ -218,7 +238,7 @@ public SegmentShape (Body body, PointF a, PointF b, float radius) : base (cpSegm
}
}
- public sealed partial class PolygonShape : Shape
+ public partial class PolygonShape : Shape
{
[DllImport("__Internal")]
extern static IntPtr cpPolyShapeNew (IntPtr body, int numVerts, PointF[] verts, PointF offset);
View
27 chipmunk/binding/src/Space.cs
@@ -155,7 +155,7 @@ protected override void Free ()
extern static IntPtr __cpSpaceGetStaticBody(IntPtr space);
public Body StaticBody {
- get { return new Body (__cpSpaceGetStaticBody(Handle.Handle)); }
+ get { return Body.FromIntPtr (__cpSpaceGetStaticBody(Handle.Handle)); }
}
//operations on the space
@@ -286,7 +286,7 @@ static void BodyIterator (IntPtr body, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Body>)handle.Target;
- action (new Body(body));
+ action (Body.FromIntPtr (body));
}
[DllImport ("__Internal")]
@@ -307,7 +307,7 @@ static void ShapeIterator (IntPtr shape, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Shape>)handle.Target;
- action (new Shape(shape));
+ action (Shape.FromIntPtr (shape));
}
[DllImport ("__Internal")]
@@ -328,7 +328,7 @@ static void ConstraintIterator (IntPtr constraint, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Constraint>)handle.Target;
- action (new Constraint (constraint));
+ action (Constraint.FromIntPtr (constraint));
}
[DllImport ("__Internal")]
@@ -360,7 +360,7 @@ static void PostStepForBody (IntPtr space, IntPtr obj, IntPtr data)
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Body>)handle.Target;
handle.Free ();
- action (obj == IntPtr.Zero ? null : new Body (obj));
+ action (obj == IntPtr.Zero ? null : Body.FromIntPtr (obj));
}
[MonoTouch.MonoPInvokeCallback (typeof (PostStepFunc))]
@@ -369,7 +369,7 @@ static void PostStepForShape (IntPtr space, IntPtr obj, IntPtr data)
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Shape>)handle.Target;
handle.Free ();
- action (obj == IntPtr.Zero ? null : new Shape (obj));
+ action (obj == IntPtr.Zero ? null : Shape.FromIntPtr (obj));
}
[MonoTouch.MonoPInvokeCallback (typeof (PostStepFunc))]
@@ -378,7 +378,7 @@ static void PostStepForConstraint (IntPtr space, IntPtr obj, IntPtr data)
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Constraint>)handle.Target;
handle.Free ();
- action (obj == IntPtr.Zero ? null : new Constraint (obj));
+ action (obj == IntPtr.Zero ? null : Constraint.FromIntPtr (obj));
}
[DllImport ("__Internal")]
@@ -535,5 +535,18 @@ void SetDefaultCollisionHandler (Func<Arbiter,Space,bool> beginFunc, Func<Arbite
};
cpSpaceSetDefaultCollisionHandler (Handle.Handle, begin, presolve, postsolve, separate, IntPtr.Zero);
}
+
+ [DllImport("__Internal")]
+ extern static IntPtr __cpSpaceGetUserData (IntPtr body);
+
+ [DllImport("__Internal")]
+ extern static void __cpSpaceSetUserData (IntPtr body, IntPtr userData);
+
+ internal override IntPtr UserData {
+ get { return __cpSpaceGetUserData (Handle.Handle); }
+ set { __cpSpaceSetUserData (Handle.Handle, value); }
+ }
+
+
}
}
View
2  cocos2d/binding/Extra.cs
@@ -236,7 +236,7 @@ public CCLabelTTF (string label, SizeF dimensions, UITextAlignment alignment, st
#if ENABLE_CHIPMUNK_INTEGRATION
public partial class CCPhysicsSprite {
public Chipmunk.Body Body {
- get { return new Chipmunk.Body (BodyPtr); }
+ get { return Chipmunk.Body.FromIntPtr (BodyPtr); }
set { BodyPtr = value.Handle.Handle; }
}
View
3  cocos2d/binding/Makefile
@@ -1,4 +1,4 @@
-ENABLE_CHIPMUNK_INTEGRATION=0
+ENABLE_CHIPMUNK_INTEGRATION=1
VERSION=2.1-rc0a
XBUILD=/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild
@@ -37,6 +37,7 @@ all: ios
ios: Cocos2D.dll
+osx: ENABLE_CHIPMUNK_INTEGRATION=0
osx: Cocos2DMacOS.dll
debug: CONFIGURATION = Debug
Something went wrong with that request. Please try again.